Simon Kellet 95b0a90499 it works
2025-12-16 18:14:09 +00:00

100 lines
2.0 KiB
Odin

package main
import "core:fmt"
import "core:os"
import SDL "vendor:sdl2"
WINDOW_TITLE :: "PPM Viewer"
WINDOW_X := i32(400)
WINDOW_Y := i32(400)
WINDOW_WIDTH_MIN := i32(920)
WINDOW_HEIGHT_MIN := 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)
}
main :: proc() {
file := Ppm_file{}
if len(os.args) > 3 || len(os.args) < 2 {
print_usage()
}
filename := os.args[1]
success := load_ppm(&file, filename)
if !success {
fmt.eprintfln("Could not load file %s", filename)
os.exit(1)
}
ppm_parse(&file)
window_width: i32 = i32(
file.width,
); if i32(file.width) < WINDOW_WIDTH_MIN {window_width = WINDOW_WIDTH_MIN}
window_height: i32 = i32(
file.width,
); if i32(file.height) < WINDOW_WIDTH_MIN {window_height = WINDOW_HEIGHT_MIN}
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_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
pixel.x = x
pixel.y = y
SDL.FillRect(surface, &pixel, SDL.MapRGB(surface.format, red, green, blue))
i += 1
}
}
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
}
}
}
}
}