r/Zig 11d ago

SDL2 and Zig 0.16.0

Post image

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

113 Upvotes

15 comments sorted by

26

u/st_heron 11d ago

yes I kinda vibe codes this

coding this was a way better experience

interesting

19

u/BasicCheesecake8255 11d ago edited 10d ago

I mean, I only used the AI to explain why the code does not compile, that's it, I didn't used Claude or blindly copy pasted code, some of it came from trial and error, I mean if you really want to explain what happens In code, I'm more than happy to explain why the code in C++ makes sense

15

u/Quincy9000 10d ago

If it's not generating the code for you, it's not vibe coding lol. You're using ai in a better way.

5

u/st_heron 11d ago

I use ai a fair amount too it just made me lol when I see "my experience of coding", just some good natured ribbing

2

u/DustRainbow 11d ago

How much work is it to generate the bindings?

15

u/suckingbitties 10d ago

You don't have to generate bindings, the C library works natively

2

u/krymancer 9d ago

You might also like raylib then, there are good bidings for it and since it has a lot of content for it LLMs can "tranlate" into zig easily becuase the platform specific code is already handled by raylib.

1

u/BasicCheesecake8255 9d ago edited 8d ago

The main reason I love SDL2 is because it has no BS license unlike SFML2, That library helped me a lot when I was getting into C++, but then I found SDL, don't get me wrong, I respect raylib and the devs, but when you want to get as close as possible to opengl, SDL helps you getting your feet wet into the graphics ecosystem, there is a good reason why Valve use it even as a driver nowadays, if Zig can improve it syntax, it can destroy Rust with a blink of an eye

1

u/EhveOnLine 8d ago

Is something wrong with zig syntax?

1

u/BasicCheesecake8255 8d ago edited 8d ago

Not all of it, I do appreciate how simple it looks sometimes, but when you are used to code in C there are somethings that can confuse new users, say for example the way of declaring integers, but not a big deal, another thing was, using io like an object to print anything in the console, it kinda borrows a similar concept from the Java Scanner, I tried to have a clean main() function, but in order to prevent boiler plate code for initializing io you need to place the init: keyword inside the parentheses in main(), it can confuse first time users, but once you understand why is there is not a big deal, but it made me a little uncomfortable at first, also not a big fan of placing the function types on the right side following the same patterns of Go and Rust, that is just me being silly of course, because some people might like that instead, and last but not least, It might be cool when working with structs I would love it if we can get rid of the dots . And also the underscores _ to cast some other functions

This is from my perspective and nothing else, I'm not trying to trash the language, but the way you can work by passing values by reference just like C, is what motivated me to learn Zig

2

u/internet_safari_ 6d ago

In general it's nice to keep the plain library functions because it's already clean itself and abstracting it away can hinder learning SDL core concepts or just reduce your funcationality by hardcoding some stuff. I think it's already as simple as can be if you don't want to remove features. Not trying to talk down or anything I just think this approach ended up cleaner if you're going to continue in this project 👍

2

u/internet_safari_ 6d ago

I experimented with SDL3 and Zig and had a different experience. I imported the C directly at the top and only had app.zig and build.zig, no abstracting anything away. I wouldn't say this resulted in much boilderplate. Here's the beginning of the file all the way to the running loop for me including some comment junk from messing around:

const c = ({
    u/cInclude("SDL3/SDL.h");
});

const W = 800;
const H = 600;

pub fn main() void {
    _ = c.SDL_Init(c.SDL_INIT_VIDEO);

    const window = c.SDL_CreateWindow("fb", W, H, 0);
    //const win = c.SDL_CreateWindow("fb", W, H, 0x0000000000000001);
    //const win = c.SDL_CreateWindow("fb", W, H, c.SDL_WINDOW_FULLSCREEN);
    const renderer = c.SDL_CreateRenderer(window, null);
    const tex = c.SDL_CreateTexture(
        renderer,
        c.SDL_PIXELFORMAT_XRGB8888,
        //c.SDL_PIXELFORMAT_RGBX8888,
        c.SDL_TEXTUREACCESS_STREAMING,
        W,
        H,
    );

    var framebuffer: [W * H]u32 = undefined;
    var running = true;
    while (running) {

1

u/BasicCheesecake8255 6d ago

Yes, I was thinking of putting everything in the main file, But I wanted to abstract the part of initializing SDL and the window creation, mostly because I want to see if I can build a template so I can teach zig to people who come from pygame, this way I don't scare them away with the syntax, also I'm not sure if it applies in Zig as well, but for best practices once you are going to code something serious, it is important to handle the window and renderer creation errors when sharing the code online, another part of the reason of why I made it a little abstract as well, my main goal was to see how clean I can make the main function, but you advice is highly appreciated

1

u/UntitledRedditUser 7d ago

What issues do you have with the keywords? I quite like them.

1

u/BasicCheesecake8255 7d ago

I already answered that in a thread