Sdl2 Breakout
▲ 69 r/Zig

Sdl2 Breakout

Hello Zig community, once again I bring another project that you might find interesting, Breakout SDL2 and Zig, now, I might not post in a while after this project since I decided to wait for Zig 1.0 before I try move my platformer and my rpg templates into Zig, so, I’m going to try to make this as short as possible so let’s begin:

Link of the repo: https://github.com/Lu100git/sdl2-breakout-zig

Why SDL2 and no SDL3 or Raylib or SFML etc...:

The main reason I decided to use SDL2 it was because I coded a lot of projects in C++ and SDL2, also I’m not sure if this is true but some Linux distros it was still not supported yet so I decided to wait until SDL3 is fully supported on all operating systems, now another specific reason why I decided to use SDL in particular, is because SFML used to be my library of choice on C++, but I wanted to avoid any licensing issues so I found a better alternative, also, SDL2 it’s so good that even Valve uses it, yes I know they are moving to SDL3, but SDL2 it’s still used, last but not least, if you are getting into graphics programming, and you want to learn about openGL, SDL2 will help you to have a smooth transition, no pain no gain.

Why I choose Zig over Rust or Go:

I cannot consider myself an expert, since I do not code in a production environment, I do this as a hobby, since I’m a big nerd who loves computers and software, after I learned Bash and C, I fell in love with Linux even more, at the beginning I didn’t wanted to try to use these 3 languages since I thought they were mostly hype, but then I tried to work with Rust, it was a very annoying experience fighting against the compiler over a couple simple strings, I understand is for safety but sometimes I hate that I have to go trough mental gymnastics in order to print 1 char after a string and a new line that somehow it gets stuck in a buffer so adding a module to fix a bug like that, while in C all you gotta do is remove the special character: \n it doesn’t feel intuitive at all, then they decided to remove the parentheses on the if statements, what is this? Goolang?, either way I don’t want to bore you with the bad experience I had, then I decided to give Goolang a try, and OMG it felt like a relief, the syntax makes sense, the print statements are simple, and the compiler does not yell at me all the time, but the down side was, I’m not a big fan of having a for loop as a while loop, and here we go, INDENTATION, IT’S BACK! 😭, what is this Python!?

After all that, I saw a couple videos on my youtube feed about Zig, and I was like , what the heck?, what is dis!?, I had no clue Zig was a thing, and a lot of people were saying it was a good alternative over Rust, so I got curious… “I wonder if I can use SDL on Zig”, so I found a project, I don’t remember which one, but it made the Syntax look way cleaner that C++ and It hooked me in, so I decided to learn about Zig, and let me tell you, it was not as easy as I thought, and I was wondering why I had to do boilerplate code in order to print a single string, then I found out about the init: argument in the main function which helps to get rid of the boiler plate code, but now I believe I feel confident enough to see if I can come with a solution to make the prints easier, so I didn’t let that demotivated me, since once I learned that you can use memory references and pointers I felt right at home, and the way it handles arrays, it’s beautiful, after I got comfortable using SDL2 on Zig, I was discovering little by little each of the language features, and then I found out about the modules, let me tell you, the way I found out I can used them in this break out project OMG, I was in love, it felt like a beautiful combination of Java, and C, it allows you to code low level, while at the same time you can use the modules as OOP, putting all your configs and public values so you can used them anywhere else, it makes the code clean and consistent, now, a couple things to consider about Zig,

I believe some of the syntax can be improved like some of the dots in structs in order to make the code look a little more simple so it doesn’t scare away new programmers, now, this is only my perspective, I’m ok with this syntax, since I’m used to other programming languages, so it;s not a big deal for me, another thing to consider, even tho the compiler outputs some helpful messages, sometimes it can be a little cryptic, YES I KNOW I CAN USE AI FOR THIS, but I learned how to code before the AI, so little things like this can be very helpful at debugging, now I’m not sure why both Rust and Zig compiles a huge binary file when it performs something simple like print statements, but if Go was able to bring it down to 2mb, I know Zig might be able to pull it off, it just needs time.

Do I recommend Zig as your first programming language?:

I would say, if you really want to go for it, then go ahead, but, based on my painful coding path, I would say probably no, since it involves dealing with pointers and memory references witch it might be a little hard to understand, I would say, learn python as your first programming language, then learn a little bit of C, you don’t have to go all the way down to shifting bytes or dealing with data structures and algorithms, but I belive the best way to understand how programming languages work, C is the best way to go, I would say maybe a little bit of Java just for fun, but Java will make you really good at OOP, wich it can become handy when working with Zig modules

My final advice, programming graphics is one of the best ways to learn about programming, it makes it less painful, and enjoyable, have a good day!

u/BasicCheesecake8255 — 11 days ago
▲ 75 r/Zig

Rain effect, SDL2 and Zig

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 🙂

u/BasicCheesecake8255 — 19 days ago
▲ 103 r/Zig

SDL2 and Zig 0.16.0

Hi everyone, I been experimenting with Zig lately, and I managed to make a bouncing rectangle that mimics the dvd logo using SDL2, yes I kinda vibe codes this, since I have so many C concepts in my head, but rest assure, I didn't copy pasted code, I was just asking the AI why things don't compile, so I'm not 100% sure if I followed good practices, but this language it kinda reminds me of Java a little bit, so I applied this code in a way that makes sense in my head, honestly I'm not a big fan of the keywords I see in the language, but I can definitely confirm, coding this was a way better experience than using Rust, I do believe zig can improve to become a really good language in the future, here is the link of the repoif you are interested:

https://github.com/Lu100git/dvd-clone-in-zig.git

u/BasicCheesecake8255 — 26 days ago

NG+ not sure if this will prove anything

Some one was saying that I was doing too much damage with embrujo, and that I wasn't even on NG+, well here is the game file, and my altar pieces, and rosary beads, also, I'm not sure if some people know this, but once you reach a certain point in the game, you are allowed to use all rosary beads and your health returns back to a bar, so when I beat the game I already unlocked everything, and I got the DLC after, this might help to explain why I was doing so much damage at the boss fight, but you know what, that's it, I will ascend again and beat Crescencia with the health dots so I can prove is doable, it will take me time pain and tears but I will record it for you, because that is the way of the miracle

u/BasicCheesecake8255 — 26 days ago

Blasphemous 2 vs Crescencia, NG+ true torment,

The dots no longer show up, because I already beat the game in true torment before, after a certain amount of gameplay, you can use everything else, regardless this didn't helped with Crescencia, I loved how she kicked my Asz, but I managed in the end, don't know how, but I'm willing to say it was the miracle way

u/BasicCheesecake8255 — 2 months ago

Whoever you are, thank you, I had to borrow your truck for a long drive on chapter 10 on DS2, I left the truck in the garage at F7 North Distribution Center, I think it should still be there, unless some one already took it

u/BasicCheesecake8255 — 4 months ago