floooh / sokol-zig

Zig bindings for the sokol headers (https://github.com/floooh/sokol)
zlib License
355 stars 49 forks source link

sg.makeImage does not work outside the callbacks #6

Closed darltrash closed 3 years ago

darltrash commented 3 years ago

Hello! My name is Neil and it's my first time using Sokol, Zig and "low level"-ish stuff in general.

I was working on my game/engine and I realized something, while sg.makeImage() worked inside the sokol callbacks (src/main.zig), it didnt work on other files (src/game.zig) that were accessing functions/methods/whatever (Texture.fromPNGPath) from where the callbacks were stored (src/main.zig).

OS: Void Linux + Musl libc Arch: x86_64 Zig version: 0.7.1

Full error message:

Assertion failed: _sg.valid (/home/darltrash/Projects/zigengine/src/sokol/c/sokol_gfx.h: sg_make_image: 14662)
The following command terminated unexpectedly:
cd /home/darltrash/Projects/zigengine && /home/darltrash/Projects/zigengine/zig-cache/o/c21211b4a21069a353acbf41275cdaba/PROJECT SCRUMPTIOUS 
error: the following build command failed with exit code 1:
/home/darltrash/Projects/zigengine/zig-cache/o/26725e637f9cba583ffd76ff7c7a828b/build /usr/bin/zig /home/darltrash/Projects/zigengine /home/darltrash/Projects/zigengine/zig-cache /home/darltrash/.cache/zig run

If this is my fault, please let me know. I apologize beforehand.

floooh commented 3 years ago

I suspect that the fromPNGPath() function (and thus sg.makeImage()) is called before sokol-gfx has been initialized via sg.setup() (at least that's what I gather from the assert message, and looking at this code: https://github.com/darltrash/scrumptious/blob/ffa1228dd4dd11311a6bce7dc6d9dcef35ecce33/src/game.zig#L10-L26

This call is missing basically:

https://github.com/darltrash/scrumptious/blob/ffa1228dd4dd11311a6bce7dc6d9dcef35ecce33/src/main.zig#L93

floooh commented 3 years ago

No need to apologize btw ;)

floooh commented 3 years ago

...I guess this is the culprit:

https://github.com/darltrash/scrumptious/blob/ffa1228dd4dd11311a6bce7dc6d9dcef35ecce33/src/main.zig#L249

setState() is called before the sokol-app init() callback (which is called from inside sapp.run()), and setState() calls game.init().

If you move that setState() call somewhere around here:

https://github.com/darltrash/scrumptious/blob/ffa1228dd4dd11311a6bce7dc6d9dcef35ecce33/src/main.zig#L94

(after the sg.setup() call), it should work.

darltrash commented 3 years ago

(after the sg.setup() call), it should work.

It works! Thanks for the time you took to inspect the code! I guess this "issue" is now closed, It was a pleasure to work with Sokol and your Sokol-zig bindings, I hope this project grows a lot because it really seems very promising :)