Compare commits
8
Commits
8061e699b9
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c4a4ab46c | ||
|
|
313d00c711 | ||
|
|
9e1721f58b | ||
|
|
62a5cab38e | ||
|
|
117ff1dd96 | ||
|
|
8cec191732 | ||
|
|
b7e7357ef4 | ||
|
|
3252771e56 |
@@ -0,0 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -xe
|
||||
|
||||
odin build . -no-bounds-check -o:speed -thread-count:6
|
||||
+162
@@ -0,0 +1,162 @@
|
||||
#+vet explicit-allocators
|
||||
package fleg
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
|
||||
FORCE_HELP_ON_EMPTY_ARGS := false
|
||||
|
||||
Flag_Value_Ptr :: union {
|
||||
^bool,
|
||||
^int,
|
||||
^string,
|
||||
^f32,
|
||||
^f64,
|
||||
}
|
||||
|
||||
Flag :: struct {
|
||||
name: string,
|
||||
value: Flag_Value_Ptr,
|
||||
usage: string,
|
||||
parsed: bool, // sck: flag to check if it has been parsed
|
||||
}
|
||||
|
||||
all_flags: [dynamic]Flag
|
||||
flag_allocator: runtime.Allocator
|
||||
|
||||
// sck: Used to create a customer allocator
|
||||
init_custom_allocator :: proc(allocator: runtime.Allocator) {
|
||||
destroy() // sck: make sure to destory the old allocations
|
||||
|
||||
flag_allocator = allocator
|
||||
all_flags = make([dynamic]Flag, flag_allocator)
|
||||
}
|
||||
|
||||
destroy :: proc() {
|
||||
delete(all_flags)
|
||||
all_flags = {} // sck: needed?
|
||||
}
|
||||
|
||||
print_flags :: proc() {
|
||||
for f in all_flags {
|
||||
fmt.printfln("%s: %v: %s", f.name, f.value, f.usage)
|
||||
}
|
||||
}
|
||||
|
||||
print_usage :: proc() {
|
||||
fmt.println("\tUsage: ")
|
||||
for f in all_flags {
|
||||
switch v in f.value {
|
||||
case ^bool:
|
||||
fmt.printfln("\t-%s:\n\t\t%s (default: %v)\n", f.name, f.usage, v^)
|
||||
case ^int:
|
||||
fmt.printfln("\t-%s:\n\t\t%s (default: %d)\n", f.name, f.usage, v^)
|
||||
case ^string:
|
||||
fmt.printfln("\t-%s:\n\t\t%s (default: %s)\n", f.name, f.usage, v^)
|
||||
case ^f32:
|
||||
fmt.printfln("\t-%s:\n\t\t%s (default: %f)\n", f.name, f.usage, v^)
|
||||
case ^f64:
|
||||
fmt.printfln("\t-%s:\n\t\t%s (default: %f)\n", f.name, f.usage, v^)
|
||||
}
|
||||
}
|
||||
os.exit(0)
|
||||
}
|
||||
|
||||
BoolVar :: proc(ptr: ^bool, name: string, default: bool, usage: string) {
|
||||
ptr^ = default
|
||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
||||
}
|
||||
|
||||
IntVar :: proc(ptr: ^int, name: string, default: int, usage: string) {
|
||||
ptr^ = default
|
||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
||||
}
|
||||
|
||||
StringVar :: proc(ptr: ^string, name: string, default: string, usage: string) {
|
||||
ptr^ = default
|
||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
||||
}
|
||||
|
||||
Float32Var :: proc(ptr: ^f32, name: string, default: f32, usage: string) {
|
||||
ptr^ = default
|
||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
||||
}
|
||||
|
||||
Float64Var :: proc(ptr: ^f64, name: string, default: f64, usage: string) {
|
||||
ptr^ = default
|
||||
append(&all_flags, Flag{name = name, value = ptr, usage = usage})
|
||||
}
|
||||
|
||||
parse_flags :: proc() {
|
||||
if len(os.args) < 2 && FORCE_HELP_ON_EMPTY_ARGS {print_usage()}
|
||||
|
||||
for &a in os.args {
|
||||
if a == "-help" || a == "--help" {print_usage()}
|
||||
if a == os.args[0] {continue}
|
||||
|
||||
for &f in all_flags {
|
||||
if f.parsed {continue}
|
||||
a = strings.trim_prefix(a, "-")
|
||||
|
||||
name := a
|
||||
value: string
|
||||
|
||||
split := strings.index_any(a, "=")
|
||||
if split >= 0 {
|
||||
name = a[:split]
|
||||
value = a[split + 1:]
|
||||
} else {
|
||||
name = a
|
||||
}
|
||||
|
||||
if name == f.name && !f.parsed {
|
||||
f.parsed = true
|
||||
switch &v in f.value {
|
||||
case ^bool:
|
||||
if value == "" {break}
|
||||
parsed, ok := strconv.parse_bool(value)
|
||||
if !ok {
|
||||
fmt.fprintfln(os.stderr, "[ERROR] Failed to parse flag %s: got %T, wanted bool", f.name, value)
|
||||
os.exit(1)
|
||||
}
|
||||
v^ = parsed
|
||||
|
||||
case ^int:
|
||||
if value == "" {break}
|
||||
parsed, ok := strconv.parse_int(value)
|
||||
if !ok {
|
||||
fmt.fprintfln(os.stderr, "[ERROR] Failed to parse flag %s: got %T, wanted int", f.name, value)
|
||||
os.exit(1)
|
||||
}
|
||||
v^ = parsed
|
||||
|
||||
case ^string:
|
||||
if value == "" {break}
|
||||
// TODO: validate string?
|
||||
v^ = value
|
||||
|
||||
case ^f32:
|
||||
if value == "" {break}
|
||||
parsed, ok := strconv.parse_f32(value)
|
||||
if !ok {
|
||||
fmt.fprintfln(os.stderr, "[ERROR] Failed to parse flag %s: got %T, wanted f32", f.name, value)
|
||||
os.exit(1)
|
||||
}
|
||||
v^ = parsed
|
||||
|
||||
case ^f64:
|
||||
if value == "" {break}
|
||||
parsed, ok := strconv.parse_f64(value)
|
||||
if !ok {
|
||||
fmt.fprintfln(os.stderr, "[ERROR] Failed to parse flag %s: got %T, wanted f64", f.name, value)
|
||||
os.exit(1)
|
||||
}
|
||||
v^ = parsed
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,9 @@
|
||||
#+vet explicit-allocators
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "fleg"
|
||||
import SDL "vendor:sdl2"
|
||||
|
||||
WINDOW_TITLE :: "PPM Viewer"
|
||||
@@ -16,20 +18,31 @@ print_usage :: proc() {
|
||||
os.exit(1)
|
||||
}
|
||||
|
||||
|
||||
main :: proc() {
|
||||
file := Ppm_file{}
|
||||
|
||||
/*
|
||||
if len(os.args) > 3 || len(os.args) < 2 {
|
||||
print_usage()
|
||||
}
|
||||
filename := os.args[1]
|
||||
if ok, err := load_ppm(&file, filename); !ok {
|
||||
fmt.eprintfln("ERROR: Failed to load file '%s': %s ", filename, os.error_string(err))
|
||||
*/
|
||||
|
||||
file := Ppm_file{}
|
||||
|
||||
file_arg: string
|
||||
fleg.StringVar(&file_arg, "f", "in.ppm", "input file [.ppm]")
|
||||
fleg.FORCE_HELP_ON_EMPTY_ARGS = true
|
||||
defer fleg.destroy()
|
||||
|
||||
fleg.parse_flags()
|
||||
|
||||
if ok, err := load_ppm(&file, file_arg, context.temp_allocator); !ok {
|
||||
fmt.eprintfln("ERROR: Failed to load file '%s': %s ", file_arg, os.error_string(err))
|
||||
os.exit(1)
|
||||
}
|
||||
ppm_parse(&file)
|
||||
ppm_parse(&file, context.temp_allocator)
|
||||
defer free_all(context.temp_allocator) //clear the temp
|
||||
|
||||
// Set the window size within the minimum or
|
||||
// if the image is bigger, set the window size to that!
|
||||
window_width: i32 = i32(
|
||||
file.width,
|
||||
); if i32(file.width) < WINDOW_WIDTH_MIN {window_width = WINDOW_WIDTH_MIN}
|
||||
@@ -37,10 +50,11 @@ main :: proc() {
|
||||
file.width,
|
||||
); if i32(file.height) < WINDOW_WIDTH_MIN {window_height = WINDOW_HEIGHT_MIN}
|
||||
|
||||
// SDL Init
|
||||
sdl_init_error := SDL.Init(SDL.INIT_VIDEO)
|
||||
assert(sdl_init_error == 0, SDL.GetErrorString())
|
||||
defer SDL.Quit() // Defer quit at scope end
|
||||
|
||||
defer SDL.Quit()
|
||||
window := SDL.CreateWindow(
|
||||
WINDOW_TITLE,
|
||||
SDL.WINDOWPOS_CENTERED,
|
||||
@@ -49,26 +63,22 @@ main :: proc() {
|
||||
window_height,
|
||||
WINDOW_FLAGS,
|
||||
)
|
||||
|
||||
assert(window != nil, SDL.GetErrorString())
|
||||
defer SDL.DestroyWindow(window)
|
||||
|
||||
surface := SDL.GetWindowSurface(window)
|
||||
defer SDL.FreeSurface(surface)
|
||||
|
||||
colour_bg := SDL.MapRGB(surface.format, 42, 42, 42)
|
||||
SDL.FillRect(surface, nil, colour_bg)
|
||||
|
||||
i := 0
|
||||
pixel := SDL.Rect{0, 0, 1, 1}
|
||||
|
||||
for y: i32 = 0; y < i32(file.height); y += 1 {
|
||||
for x: i32 = 0; x < i32(file.width); x += 1 {
|
||||
red: u8
|
||||
green: u8
|
||||
blue: u8
|
||||
red = file.pixels[i].red
|
||||
green = file.pixels[i].green
|
||||
blue = file.pixels[i].blue
|
||||
red: u8 = file.pixels[i].red
|
||||
green: u8 = file.pixels[i].green
|
||||
blue: u8 = file.pixels[i].blue
|
||||
pixel.x = x
|
||||
pixel.y = y
|
||||
|
||||
|
||||
BIN
Binary file not shown.
@@ -1,6 +1,6 @@
|
||||
#+vet explicit-allocators
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
@@ -17,7 +17,6 @@ Ppm_Pixel :: struct {
|
||||
}
|
||||
|
||||
Ppm_file :: struct {
|
||||
file: os.File, //TODO: use this instead perhaps?
|
||||
data: []u8,
|
||||
magic_num: PPM_MAGIC_NUMBER,
|
||||
width: int,
|
||||
@@ -26,7 +25,14 @@ Ppm_file :: struct {
|
||||
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 {
|
||||
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!
|
||||
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 {
|
||||
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?)
|
||||
ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
||||
ppm_parse :: proc(ppm_file: ^Ppm_file, allocator := context.allocator) {
|
||||
magic_num_found := false
|
||||
w_h_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)
|
||||
|
||||
it := string(ppm_file.data)
|
||||
@@ -66,20 +72,16 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
||||
continue
|
||||
}
|
||||
|
||||
w_h_parts := strings.split(line, " ")
|
||||
w_h_parts := strings.split(line, " ", allocator)
|
||||
if !w_h_found && len(w_h_parts) == 2 {
|
||||
ppm_file.width, _ = strconv.parse_int(w_h_parts[0])
|
||||
ppm_file.height, _ = strconv.parse_int(w_h_parts[1])
|
||||
ppm_file.pixels = make(
|
||||
[]Ppm_Pixel,
|
||||
ppm_file.width * ppm_file.height,
|
||||
context.temp_allocator,
|
||||
)
|
||||
ppm_file.pixels = make([]Ppm_Pixel, ppm_file.width * ppm_file.height, allocator)
|
||||
w_h_found = true
|
||||
continue
|
||||
}
|
||||
|
||||
max_val_part := strings.split(line, " ")
|
||||
max_val_part := strings.split(line, " ", allocator)
|
||||
if !max_col_val_found && len(max_val_part) == 1 {
|
||||
ppm_file.max_col_val, _ = strconv.parse_int(max_val_part[0])
|
||||
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 {
|
||||
for value in strings.split(line, " ") {
|
||||
for value in strings.split(line, " ", allocator) {
|
||||
strings.write_string(&sb, value)
|
||||
strings.write_string(&sb, " ")
|
||||
}
|
||||
@@ -97,7 +99,7 @@ ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
||||
all_values := strings.to_string(sb)
|
||||
|
||||
index := 0
|
||||
pixel_values := strings.split(all_values, " ")
|
||||
pixel_values := strings.split(all_values, " ", allocator)
|
||||
|
||||
for i := 0; i < len(pixel_values); i += 3 {
|
||||
if index < len(ppm_file.pixels) {
|
||||
|
||||
Reference in New Issue
Block a user