123 lines
2.5 KiB
Odin
123 lines
2.5 KiB
Odin
package main
|
|
|
|
import "core:fmt"
|
|
import "core:os"
|
|
import "core:strings"
|
|
import SDL "vendor:sdl2"
|
|
|
|
WINDOW_TITLE :: "PPM Viewer"
|
|
WINDOW_X := i32(400)
|
|
WINDOW_Y := i32(400)
|
|
WINDOW_WIDTH := i32(1024)
|
|
WINDOW_HEIGHT := i32(920)
|
|
WINDOW_FLAGS :: SDL.WindowFlags{.SHOWN, .RESIZABLE, .ALWAYS_ON_TOP}
|
|
|
|
print_usage :: proc() {
|
|
fmt.printfln("Usage:\n\t%s [ppm file]", os.args[0])
|
|
os.exit(1)
|
|
}
|
|
|
|
|
|
load_ppm :: proc() -> (data: []byte) {
|
|
if len(os.args) > 3 || len(os.args) < 2 {
|
|
print_usage()
|
|
}
|
|
filename := os.args[1]
|
|
//fmt.printfln("filename: %s", filename)
|
|
|
|
if !strings.has_suffix(filename, ".ppm") {
|
|
fmt.eprintfln("ERROR: File %s does not have a valid ppm extention (.ppm)", filename)
|
|
os.exit(1)
|
|
}
|
|
|
|
//now read file
|
|
succ: bool
|
|
data, succ = os.read_entire_file_from_filename(filename, context.temp_allocator)
|
|
if !succ {
|
|
fmt.eprintfln("ERROR: Failed to load file: %s ", filename)
|
|
os.exit(1)
|
|
}
|
|
return data //don't like naked returns
|
|
}
|
|
|
|
load_ppm_to_rect :: proc() -> (rect: SDL.Rect) {
|
|
if len(os.args) > 3 || len(os.args) < 2 {
|
|
print_usage()
|
|
}
|
|
filename := os.args[1]
|
|
//fmt.printfln("filename: %s", filename)
|
|
|
|
if !strings.has_suffix(filename, ".ppm") {
|
|
fmt.eprintfln("ERROR: File %s does not have a valid ppm extention (.ppm)", filename)
|
|
os.exit(1)
|
|
}
|
|
|
|
//now read file
|
|
data, succ := os.read_entire_file_from_filename(filename, context.temp_allocator)
|
|
if !succ {
|
|
fmt.eprintfln("ERROR: Failed to load file: %s ", filename)
|
|
os.exit(1)
|
|
}
|
|
|
|
it := string(data)
|
|
for line in strings.split_lines_iterator(&it) {
|
|
switch line {
|
|
case "#":
|
|
continue
|
|
//ignore comments
|
|
case "P3":
|
|
fmt.println("has P3")
|
|
case "P6":
|
|
fmt.println("has P6")
|
|
|
|
}
|
|
}
|
|
|
|
return {0, 0, 0, 0}
|
|
}
|
|
main :: proc() {
|
|
//first check if the user has provided a ppm file
|
|
load_ppm_to_rect()
|
|
|
|
sdl_init_error := SDL.Init(SDL.INIT_VIDEO)
|
|
assert(sdl_init_error == 0, SDL.GetErrorString())
|
|
defer SDL.Quit()
|
|
|
|
|
|
window := SDL.CreateWindow(
|
|
WINDOW_TITLE,
|
|
SDL.WINDOWPOS_CENTERED,
|
|
SDL.WINDOWPOS_CENTERED,
|
|
WINDOW_WIDTH,
|
|
WINDOW_HEIGHT,
|
|
WINDOW_FLAGS,
|
|
)
|
|
|
|
assert(window != nil, SDL.GetErrorString())
|
|
defer SDL.DestroyWindow(window)
|
|
|
|
surface := SDL.GetWindowSurface(window)
|
|
|
|
colour_skyblue := SDL.MapRGB(surface.format, 42, 42, 42)
|
|
SDL.FillRect(surface, nil, colour_skyblue)
|
|
|
|
SDL.UpdateWindowSurface(window)
|
|
|
|
//Loop!
|
|
quit := false
|
|
e: SDL.Event
|
|
for (!quit) {
|
|
for SDL.PollEvent(&e) {
|
|
#partial switch (e.type) {
|
|
case .QUIT:
|
|
quit = true
|
|
case .KEYDOWN:
|
|
#partial switch (e.key.keysym.sym) {
|
|
case .ESCAPE:
|
|
quit = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|