Not-Nik / raylib-zig

Manually tweaked, auto-generated raylib bindings for zig. https://github.com/raysan5/raylib
MIT License
517 stars 102 forks source link

DrawTextureEx, DrawTexturePro, ect, not drawing #17

Closed dmbfm closed 1 year ago

dmbfm commented 2 years ago

I was trying to draw scaled textures and I was not able to. While DrawTexture works, any of the other texture drawing methods that allow for scaling seem to be broken.

If I try this zig code:

const std = @import("std");
const rl = @import("raylib");

const window_width = 800;
const window_height = 600;

pub fn main() anyerror!void {
    rl.InitWindow(window_width, window_height, "Nemo Font Converter");
    rl.SetTargetFPS(60);

    var img = rl.GenImageChecked(512, 512, 12, 12, rl.RED, rl.BLUE);
    defer rl.UnloadImage(img);

    var tex = rl.LoadTextureFromImage(img);
    defer rl.UnloadTexture(tex);

    while (!rl.WindowShouldClose()) {
        rl.BeginDrawing();
        rl.ClearBackground(rl.WHITE);
        var pos = rl.Vector2{ .x = 0, .y = 0 };
        rl.DrawTextureEx(
            tex,
            pos,
            @as(f32, 0),
            @as(f32, 2),
            rl.WHITE,
        );

        rl.EndDrawing();
    }

    rl.CloseWindow();
}

I only get a while screen, while the same code in C gives me the correct result:

#include "raylib/src/raylib.h"

int main() {
    InitWindow(800, 600, "Textur test");
    SetTargetFPS(60);

    Image img = GenImageChecked(512, 512, 12, 12, RED, BLUE);

    Texture2D tex = LoadTextureFromImage(img);

    while (!WindowShouldClose()) {
        BeginDrawing();

        Vector2 pos = { .x = 0, .y = 0 };

        DrawTextureEx(tex, pos, 0, 2, WHITE);

        EndDrawing();
    }

    UnloadImage(img);
    UnloadTexture(tex);
}

I have tried with both system and compiled raylib and get the same results. I am using macOS:

INFO: Initializing raylib 4.0
INFO: DISPLAY: Device initialized successfully
INFO:     > Display size: 1920 x 1080
INFO:     > Screen size:  800 x 600
INFO:     > Render size:  800 x 600
INFO:     > Viewport offsets: 0, 0
INFO: GL: Supported extensions count: 43
INFO: GL: OpenGL device information:
INFO:     > Vendor:   Apple
INFO:     > Renderer: Apple M1
INFO:     > Version:  4.1 Metal - 76.3
INFO:     > GLSL:     4.10
❯ zig version
0.10.0-dev.378+40b3c9a59
dmbfm commented 2 years ago

Thiis seems to be the same type of issue: https://github.com/ziglang/zig/issues/10560, and the cause is that the C abi compatibility is not complete yet: https://github.com/ziglang/zig/issues/1481.

The ARM: struct & union parameters is still not implemented, so this should be the issue here.

dmbfm commented 2 years ago

I could verify that indeed this was the issue by creating a wrapper function in c:

#include "../raylib-zig/raylib/src/raylib.h"

void _DrawTextureExWrapper(Texture2D tex, float x, float y, float rotation, float scale, Color color) {
    Vector2 pos = { .x = x, .y = y };
    DrawTextureEx(tex, pos, rotation, scale, color);
}

When I called this function from zig, rendering of the texture worked as expected.

ChaseAColvin commented 2 years ago

Late to the party, but wanted to throw this out there:

https://github.com/pfgithub/mousegame

This project is old, but seems to have an interesting approach to working around this in the aptly named 'workaround' c, h, and zig files. Not sure how well it works in practice, but wanted to put it here for others to find and spur on more creative solutions, if nothing else.

Not-Nik commented 2 years ago

The way mousegame works around the issue indeed seems to be the only way at the moment. For raylib 3.0 there is the workaround branch, but since struct parameters are supported on x86_64 it's no longer maintained. Fixing these C ABI issues is way out of scope for raylib-zig, but feel free to update the branch with a PR

Not-Nik commented 1 year ago

With https://github.com/ziglang/zig/issues/1481 being closed, could you check this again?

AlxHnr commented 1 year ago

Passing structs smaller than 16 bytes is still broken when enabling any of the release optimizations in the latest 0.11.0-dev compiler. This breaks raymath completely, as it relies heavily on Vector2 and Vector3. Only solution is to stick to debug builds, for now.

AlxHnr commented 1 year ago

https://github.com/ziglang/zig/issues/14262

AlxHnr commented 1 year ago

The way mousegame works around the issue indeed seems to be the only way at the moment.

Another workaround is adding unused fields to the C structs. E.g. giving Vector3 a w component and assigning a default 0 in the zig bindings.

Not-Nik commented 1 year ago

https://github.com/ziglang/zig/issues/13830 and by extension https://github.com/ziglang/zig/issues/14262 are closed. Could you check this again?

AlxHnr commented 1 year ago

Didn't retest the changes. But as long as these bindings officially only target zig 0.10.X, we should keep the issue open. The fix for sub-16 byte structs only exists in the 0.11.X branch.

Not-Nik commented 1 year ago

Good note. Once 0.11 is released I'll update the binding accordingly and all this will hopefully work