it works
This commit is contained in:
parent
69b44998f1
commit
95b0a90499
11
gen.py
11
gen.py
@ -1,11 +0,0 @@
|
||||
# Create a 400x400 PPM image filled with red color
|
||||
width = 400
|
||||
height = 400
|
||||
max_color = 255
|
||||
|
||||
with open("red_image.ppm", "w") as f:
|
||||
f.write(f"P3\n{width} {height}\n{max_color}\n")
|
||||
for i in range(height):
|
||||
for j in range(width):
|
||||
f.write(f"{max_color} 0 0 ") # Red color
|
||||
f.write("\n")
|
||||
38
main.odin
38
main.odin
@ -32,19 +32,17 @@ main :: proc() {
|
||||
}
|
||||
ppm_parse(&file)
|
||||
|
||||
window_width: i32 = 0; if i32(file.width) < WINDOW_WIDTH_MIN {window_width = WINDOW_WIDTH_MIN}
|
||||
window_height: i32 = 0; if i32(file.height) < WINDOW_WIDTH_MIN {window_height = WINDOW_HEIGHT_MIN}
|
||||
|
||||
fmt.println(file.magic_num)
|
||||
fmt.printfln("width: %d", file.width)
|
||||
fmt.printfln("height: %d", file.height)
|
||||
fmt.printfln("max_col_val: %d", file.max_col_val)
|
||||
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,
|
||||
@ -59,9 +57,27 @@ main :: proc() {
|
||||
|
||||
surface := SDL.GetWindowSurface(window)
|
||||
|
||||
colour_skyblue := SDL.MapRGB(surface.format, 42, 42, 42)
|
||||
SDL.FillRect(surface, nil, colour_skyblue)
|
||||
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!
|
||||
|
||||
BIN
ppm-viewer
BIN
ppm-viewer
Binary file not shown.
74
ppm.odin
74
ppm.odin
@ -3,6 +3,7 @@ package main
|
||||
import "core:c"
|
||||
import "core:fmt"
|
||||
import "core:os"
|
||||
import "core:os/os2"
|
||||
import "core:strconv"
|
||||
import "core:strings"
|
||||
|
||||
@ -11,11 +12,20 @@ PPM_MAGIC_NUMBER :: enum {
|
||||
P3,
|
||||
}
|
||||
|
||||
Ppm_Pixel :: struct {
|
||||
red: u8,
|
||||
green: u8,
|
||||
blue: u8,
|
||||
}
|
||||
|
||||
Ppm_file :: struct {
|
||||
magic_num: PPM_MAGIC_NUMBER,
|
||||
width: int,
|
||||
height: int,
|
||||
data: []byte,
|
||||
file: os2.File, //TODO: use this instead perhaps?
|
||||
data: []byte,
|
||||
magic_num: PPM_MAGIC_NUMBER,
|
||||
width: int,
|
||||
height: int,
|
||||
max_col_val: int,
|
||||
pixels: []Ppm_Pixel,
|
||||
}
|
||||
|
||||
load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (success: bool) {
|
||||
@ -33,25 +43,77 @@ load_ppm :: proc(ppm_file: ^Ppm_file, filename: string) -> (success: bool) {
|
||||
return true
|
||||
}
|
||||
|
||||
//TODO: handle errors and err return type (success: bool?, err: ErrorType?)
|
||||
ppm_parse :: proc(ppm_file: ^Ppm_file) {
|
||||
magic_num_found := false
|
||||
w_h_found := false
|
||||
max_col_val_found := false
|
||||
sb := strings.builder_make(allocator = context.temp_allocator)
|
||||
defer strings.builder_destroy(&sb)
|
||||
|
||||
it := string(ppm_file.data)
|
||||
for line in strings.split_lines_iterator(&it) {
|
||||
if strings.starts_with(line, "#") {continue} //ignore comments
|
||||
if strings.starts_with(line, "P") {
|
||||
|
||||
if !magic_num_found && strings.starts_with(line, "P") {
|
||||
num, _ := strconv.parse_int(strings.trim(line, "P"))
|
||||
if num == 3 {
|
||||
ppm_file.magic_num = .P3
|
||||
} else if num == 6 {
|
||||
ppm_file.magic_num = .P6
|
||||
}
|
||||
magic_num_found = true
|
||||
continue
|
||||
}
|
||||
|
||||
w_h_parts := strings.split(line, " ")
|
||||
if 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.height, _ = strconv.parse_int(w_h_parts[1])
|
||||
ppm_file.pixels = make(
|
||||
[]Ppm_Pixel,
|
||||
ppm_file.width * ppm_file.height,
|
||||
context.temp_allocator,
|
||||
)
|
||||
w_h_found = true
|
||||
continue
|
||||
}
|
||||
|
||||
max_val_part := strings.split(line, " ")
|
||||
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
|
||||
continue
|
||||
}
|
||||
|
||||
if magic_num_found && w_h_found && max_col_val_found {
|
||||
for value in strings.split(line, " ") {
|
||||
strings.write_string(&sb, value)
|
||||
strings.write_string(&sb, " ")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
all_values := strings.to_string(sb)
|
||||
|
||||
index := 0
|
||||
pixel_values := strings.split(all_values, " ")
|
||||
|
||||
for i := 0; i < len(pixel_values); i += 3 {
|
||||
if index < len(ppm_file.pixels) {
|
||||
if i + 2 < len(pixel_values) {
|
||||
red_val, _ := strconv.parse_int(pixel_values[i])
|
||||
green_val, _ := strconv.parse_int(pixel_values[i + 1])
|
||||
blue_val, _ := strconv.parse_int(pixel_values[i + 2])
|
||||
|
||||
ppm_file.pixels[index].red = u8(red_val)
|
||||
ppm_file.pixels[index].green = u8(green_val)
|
||||
ppm_file.pixels[index].blue = u8(blue_val)
|
||||
|
||||
index += 1
|
||||
}
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
P6
|
||||
4 4
|
||||
255
|
||||
0 0 0 100 0 0 0 0 0 255 0 255
|
||||
0 0 0 0 255 175 0 0 0 0 0 0
|
||||
0 0 0 0 0 0 0 15 175 0 0 0
|
||||
255 0 255 0 0 0 0 0 0 255 255 255
|
||||
0 0 0 100 0 0 0 0 0 255 0 255
|
||||
0 0 0 0 255 175 0 0 0 0 0 0
|
||||
0 0 0 0 0 0 0 15 175 0 0 0
|
||||
255 0 255 0 0 0 0 0 0 255 255 255
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user