First of all, sorry for the excessive verbosity. TL;DR: No work, help plz!
I’m on current Fedora, Odin version is dev-2025-02-nightly
. I meant to download the current stable at the time, maybe I made a mistake.
I’m trying to learn Odin by rewriting Pygame Tutorial #3: Tile-based game by KidsCanCode in Odin with raylib. Are there better ways to learn Odin? Sure, but this is what I chose to do.
Since I know don’t know Odin, my idea is to stick as close to the original as possible in the first iteration (bearing in mind that Python and Odin are rather different), and make it more “Odinesque” (?) later.
The first part went fine. I found some tutorials on Odin and raylib to help me out. Now I’m on part 2 and am a bit stumped on something. I know I’m doing something wrong and it sure looks like a memory-related issue, but I’m can’t figure it out, so I’m hoping to get an explanation as to what I’m doing wrong.
I created the following procedure. It reads in a file that consists of two characters (runes): ‘1’ and ‘.’, where ‘1’ indicates a “wall” and ‘.’ nothing.
load_data :: proc(md: ^[dynamic]string) {
data, ok := os.read_entire_file("./map.txt", context.allocator)
if !ok {
return
}
defer delete(data, context.allocator)
it := string(data)
for line in strings.split_lines_iterator(&it) {
append(md, line)
}
}
In the main proc, I call this to load in the map:
map_data: [dynamic]string
defer delete(map_data)
load_data(&map_data)
for row, y in map_data {
for col, x in row {
if col == '1' {
append(&wall_group, Wall{{f32(x), f32(y)}, rl.GREEN})
}
}
}
Wall
is just a struct:
Wall :: struct {
pos: rl.Vector2,
fill: rl.Color,
}
Now this works, EXCEPT for the first 8 characters/runes/cells of the first row. They get filled with some random characters. Every time I run, it’s the first 8 cells of the first row, which is why it smells like some memory allocation thing that I am unaware of.
If I print out each row inside the load_data
proc, then the first row is correct, it’s all 1s. However, after returning to main
, those first 8 runes are damaged.
Another odd thing: if I move the body of load_data
into main
(renaming variables as needed), then it works fine, no runes get corrupted.
I’d be very grateful if somebody could explain what I’m doing wrong here, because I can’t figure it out. If somebody wants to also point out a better way to do this, I’ll of course be happy about that as well, but I would still want to understand what I’ve done wrong and how to fix it.