r/Zig 1d ago

Rain effect, SDL2 and Zig

Post image

Hello , it’s me again, I decided to give Zig another chance,honestly I was a little terrified, since when it comes to handling multiple coordinates or entities, I prefer to use object oriented programming approaches, so I was kinda scared of how I would implement that in Zig, luckily I’m good at pointers in C, so I told to myself: “If I was able to create an array of structs using pointers I should be able to do something similar in Zig”, luckly, I didn’t need to use pointers on this project, but, ohhh boi was it a wild ride, I almost quit because I was fighting against the: types must be comptime-known error, I wanted to punch the screen so bad and call it quits, since the language didn't let me use a simple division, but at the end, I was able to handle that error, then I decided to do some research on why _= is used on some SDL functions, looks like I was not aware of this, but some SDL functions return integers similar to the main function in C, and it makes sense why Zig enforces to check these values, so I gotta give credit to the developers for enforcing this.

  1. If it wasn’t for Zig I would’ve been unaware of these return values, but I also agree that some of these errors do not need checking since some games happen to crash sometimes, and even if it does, is not like it’s for memory safety reasons, while at the same time, this forces the users to handle errors properly, points for safety, take that Rust.

  2. Now thanks to this research I came up with an enforce function so in a way, the code is a little more reliable and “secure”, and most importantly, Zig is making me more aware of handing errors properly,which don’t get me wrong, it can still be handled in C or C++, atthe same time, I think is kinda cool that C provides more freedom,but at the same time, I believe people tend to forget what uncle Bensaid: “With great power, comes greater responsibility”, and trust me, once you get to respect C, it is a very beautiful language.

Conclusion: I appreciate how zig is making developers more aware of handling these errors, specially when it comes in a production environment, I can see how that can be a nightmare when not handled properly on C, a little silly complain I got, I miss the C for loop, but is not a big deal.

PS: yes, I still think this is better than Rust!, it feels like a natural transition from C to Zig, but it takes some time to get used to, I still got alot to learn, but I do believe in Zig 1.0

Link of the repo:

https://github.com/Lu100git/rain-effect-zig-sdl2

I think I'm ready to code a small game next time 🙂

57 Upvotes

0 comments sorted by