Compare commits
4 Commits
8061e699b9
...
117ff1dd96
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
117ff1dd96 | ||
|
|
8cec191732 | ||
|
|
b7e7357ef4 | ||
|
|
3252771e56 |
4
build.sh
Executable file
4
build.sh
Executable file
@ -0,0 +1,4 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
set -xe
|
||||||
|
|
||||||
|
odin build . -no-bounds-check -o:speed
|
||||||
12
main.odin
12
main.odin
@ -1,6 +1,7 @@
|
|||||||
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"
|
||||||
|
|
||||||
@ -16,19 +17,24 @@ print_usage :: proc() {
|
|||||||
os.exit(1)
|
os.exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
main :: proc() {
|
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!
|
||||||
|
|
||||||
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); !ok {
|
if ok, err := load_ppm(&file, filename, arena_allocator); !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)
|
ppm_parse(&file, arena_allocator)
|
||||||
|
|
||||||
window_width: i32 = i32(
|
window_width: i32 = i32(
|
||||||
file.width,
|
file.width,
|
||||||
|
|||||||
BIN
ppm-viewer
BIN
ppm-viewer
Binary file not shown.
32
ppm.odin
32
ppm.odin
@ -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,7 +17,6 @@ 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,
|
||||||
@ -26,7 +25,14 @@ Ppm_file :: struct {
|
|||||||
pixels: []Ppm_Pixel,
|
pixels: []Ppm_Pixel,
|
||||||
}
|
}
|
||||||
|
|
||||||
load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
|
load_ppm :: proc(
|
||||||
|
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
|
||||||
}
|
}
|
||||||
@ -36,7 +42,7 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
|
|||||||
|
|
||||||
//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, context.temp_allocator)
|
ppm_file.data, err = os.read_entire_file_from_path(filename, allocator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
@ -44,11 +50,11 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (bool, os.Error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//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) {
|
ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
|
||||||
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 = context.temp_allocator)
|
sb := strings.builder_make(allocator)
|
||||||
defer strings.builder_destroy(&sb)
|
defer strings.builder_destroy(&sb)
|
||||||
|
|
||||||
it := string(ppm_file.data)
|
it := string(ppm_file.data)
|
||||||
@ -66,20 +72,16 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
w_h_parts := strings.split(line, " ")
|
w_h_parts := strings.split(line, " ", allocator)
|
||||||
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_file.pixels = make([]Ppm_Pixel, ppm_file.width * ppm_file.height, allocator)
|
||||||
[]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, " ")
|
max_val_part := strings.split(line, " ", allocator)
|
||||||
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
|
||||||
@ -87,7 +89,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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, " ") {
|
for value in strings.split(line, " ", allocator) {
|
||||||
strings.write_string(&sb, value)
|
strings.write_string(&sb, value)
|
||||||
strings.write_string(&sb, " ")
|
strings.write_string(&sb, " ")
|
||||||
}
|
}
|
||||||
@ -97,7 +99,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
|||||||
all_values := strings.to_string(sb)
|
all_values := strings.to_string(sb)
|
||||||
|
|
||||||
index := 0
|
index := 0
|
||||||
pixel_values := strings.split(all_values, " ")
|
pixel_values := strings.split(all_values, " ", allocator)
|
||||||
|
|
||||||
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) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user