Compare commits
4
Commits
117ff1dd96
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5c4a4ab46c | ||
|
|
313d00c711 | ||
|
|
9e1721f58b | ||
|
|
62a5cab38e |
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
set -xe
|
||||
|
||||
odin build . -no-bounds-check -o:speed
|
||||
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,8 +1,9 @@
|
||||
#+vet explicit-allocators
|
||||
package main
|
||||
|
||||
import "core:fmt"
|
||||
import vmem "core:mem/virtual"
|
||||
import "core:os"
|
||||
import "fleg"
|
||||
import SDL "vendor:sdl2"
|
||||
|
||||
WINDOW_TITLE :: "PPM Viewer"
|
||||
@@ -18,24 +19,30 @@ print_usage :: 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{}
|
||||
|
||||
/*
|
||||
if len(os.args) > 3 || len(os.args) < 2 {
|
||||
print_usage()
|
||||
}
|
||||
filename := os.args[1]
|
||||
if ok, err := load_ppm(&file, filename, arena_allocator); !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, arena_allocator)
|
||||
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}
|
||||
@@ -43,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,
|
||||
@@ -55,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.
Reference in New Issue
Block a user