Compare commits

..

No commits in common. "117ff1dd96633192592314a3a1735aae6e108726" and "8061e699b9b9a676ba4eb4e296ac824ddbae15e1" have entirely different histories.

4 changed files with 18 additions and 30 deletions

View File

@ -1,4 +0,0 @@
#!/bin/bash
set -xe
odin build . -no-bounds-check -o:speed

View File

@ -1,7 +1,6 @@
package main package main
import "core:fmt" import "core:fmt"
import vmem "core:mem/virtual"
import "core:os" import "core:os"
import SDL "vendor:sdl2" import SDL "vendor:sdl2"
@ -17,24 +16,19 @@ print_usage :: proc() {
os.exit(1) os.exit(1)
} }
main :: proc() {
arena: vmem.Arena
arena_err := vmem.arena_init_growing(&arena)
ensure(arena_err == nil)
arena_allocator := vmem.arena_allocator(&arena)
defer vmem.arena_destroy(&arena) //clean up everything!
main :: proc() {
file := Ppm_file{} file := Ppm_file{}
if len(os.args) > 3 || len(os.args) < 2 { if len(os.args) > 3 || len(os.args) < 2 {
print_usage() print_usage()
} }
filename := os.args[1] filename := os.args[1]
if ok, err := load_ppm(&file, filename, arena_allocator); !ok { if ok, err := load_ppm(&file, filename); !ok {
fmt.eprintfln("ERROR: Failed to load file '%s': %s ", filename, os.error_string(err)) fmt.eprintfln("ERROR: Failed to load file '%s': %s ", filename, os.error_string(err))
os.exit(1) os.exit(1)
} }
ppm_parse(&file, arena_allocator) ppm_parse(&file)
window_width: i32 = i32( window_width: i32 = i32(
file.width, file.width,

Binary file not shown.

View File

@ -1,6 +1,6 @@
#+vet explicit-allocators
package main package main
import "core:fmt"
import "core:os" import "core:os"
import "core:strconv" import "core:strconv"
import "core:strings" import "core:strings"
@ -17,6 +17,7 @@ Ppm_Pixel :: struct {
} }
Ppm_file :: struct { Ppm_file :: struct {
file: os.File, //TODO: use this instead perhaps?
data: []u8, data: []u8,
magic_num: PPM_MAGIC_NUMBER, magic_num: PPM_MAGIC_NUMBER,
width: int, width: int,
@ -25,14 +26,7 @@ Ppm_file :: struct {
pixels: []Ppm_Pixel, pixels: []Ppm_Pixel,
} }
load_ppm :: proc( load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
ppm_file: ^Ppm_file,
filename: string,
allocator := context.allocator,
) -> (
bool,
os.Error,
) {
if ppm_file == nil || len(filename) == 0 { if ppm_file == nil || len(filename) == 0 {
return false, .Not_Exist return false, .Not_Exist
} }
@ -42,7 +36,7 @@ load_ppm :: proc(
//if file is valid, opemn but check error too! //if file is valid, opemn but check error too!
err: os.Error err: os.Error
ppm_file.data, err = os.read_entire_file_from_path(filename, allocator) ppm_file.data, err = os.read_entire_file_from_path(filename, context.temp_allocator)
if err != nil { if err != nil {
return false, err return false, err
} }
@ -50,11 +44,11 @@ load_ppm :: proc(
} }
//TODO: handle errors and err return type (success: bool?, err: ErrorType?) //TODO: handle errors and err return type (success: bool?, err: ErrorType?)
ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) { ppm_parse :: proc(ppm_file: ^Ppm_file) {
magic_num_found := false magic_num_found := false
w_h_found := false w_h_found := false
max_col_val_found := false max_col_val_found := false
sb := strings.builder_make(allocator) sb := strings.builder_make(allocator = context.temp_allocator)
defer strings.builder_destroy(&sb) defer strings.builder_destroy(&sb)
it := string(ppm_file.data) it := string(ppm_file.data)
@ -72,16 +66,20 @@ ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
continue continue
} }
w_h_parts := strings.split(line, " ", allocator) w_h_parts := strings.split(line, " ")
if !w_h_found && len(w_h_parts) == 2 { if !w_h_found && len(w_h_parts) == 2 {
ppm_file.width, _ = strconv.parse_int(w_h_parts[0]) ppm_file.width, _ = strconv.parse_int(w_h_parts[0])
ppm_file.height, _ = strconv.parse_int(w_h_parts[1]) ppm_file.height, _ = strconv.parse_int(w_h_parts[1])
ppm_file.pixels = make([]Ppm_Pixel, ppm_file.width * ppm_file.height, allocator) ppm_file.pixels = make(
[]Ppm_Pixel,
ppm_file.width * ppm_file.height,
context.temp_allocator,
)
w_h_found = true w_h_found = true
continue continue
} }
max_val_part := strings.split(line, " ", allocator) max_val_part := strings.split(line, " ")
if !max_col_val_found && len(max_val_part) == 1 { if !max_col_val_found && len(max_val_part) == 1 {
ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0]) ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0])
max_col_val_found = true max_col_val_found = true
@ -89,7 +87,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
} }
if magic_num_found && w_h_found && max_col_val_found { if magic_num_found && w_h_found && max_col_val_found {
for value in strings.split(line, " ", allocator) { for value in strings.split(line, " ") {
strings.write_string(&sb, value) strings.write_string(&sb, value)
strings.write_string(&sb, " ") strings.write_string(&sb, " ")
} }
@ -99,7 +97,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
all_values := strings.to_string(sb) all_values := strings.to_string(sb)
index := 0 index := 0
pixel_values := strings.split(all_values, " ", allocator) pixel_values := strings.split(all_values, " ")
for i := 0; i < len(pixel_values); i += 3 { for i := 0; i < len(pixel_values); i += 3 {
if index < len(ppm_file.pixels) { if index < len(ppm_file.pixels) {