ziglibs / zgl

Zig OpenGL Wrapper
MIT License
425 stars 62 forks source link

SIGSEGV when calling any function #78

Closed mondegreengames closed 1 year ago

mondegreengames commented 1 year ago

This is my first time using zgl, and I'm still super new at Zig. But I tried running this super simple example:

const std = @import("std");
const c = @cImport({
    @cInclude("SDL.h");
});
const gl = @import("third-party/zgl/zgl.zig");

pub fn main() !void {

     _ = c.SDL_Init(c.SDL_INIT_VIDEO);
    defer c.SDL_Quit();

    var window = c.SDL_CreateWindow("Hello, world!", c.SDL_WINDOWPOS_CENTERED, c.SDL_WINDOWPOS_CENTERED, 640, 400, c.SDL_WINDOW_OPENGL);
    defer c.SDL_DestroyWindow(window);

    var glContext = c.SDL_GL_CreateContext(window);
    defer c.SDL_GL_DeleteContext(glContext);

    mainloop: while (true) {
        var sdl_event: c.SDL_Event = undefined;
        while (c.SDL_PollEvent(&sdl_event) != 0) {
            switch (sdl_event.type) {
                c.SDL_QUIT => break :mainloop,
                else => {},
            }
        }

        // this line will crash!
        // Output: Stop reason: signal SIGSEGV: invalid address (fault address: 0x0)
        gl.clear(.{.color = true, .depth = true });

        c.SDL_GL_SwapWindow(window);
    }
}

As soon as it executes the gl.clear() line I get Stop reason: signal SIGSEGV: invalid address (fault address: 0x0)

Valgrind reports this:

==121080== Jump to the invalid address stated on the next line
==121080==    at 0x0: ???
==121080==    by 0x20C3D4: main.main (main.zig:30)
==121080==    by 0x20C946: callMain (start.zig:609)
==121080==    by 0x20C946: initEventLoopAndCallMain (start.zig:543)
==121080==    by 0x20C946: callMainWithArgs (start.zig:493)
==121080==    by 0x20C946: main (start.zig:508)
==121080==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
==121080== 

It seems like gl.Clear() itself (and any other functions I've tried) is somehow null, which seems weird because from looking at the zgl source it's a regular function, not a dynamically loaded function pointer or anything like that.

If I were using GLEW I would assume I didn't call glewInit(), but I haven't seen a similar zgl function in any of the examples I've found.

Am I missing something simple?

mondegreengames commented 1 year ago

I was missing something:

gl.loadExtensions({}, myWrapperAroundSDL_GL_GetProcAddress);

I'm not sure why none of the examples I found had that line.