raylib-zig icon indicating copy to clipboard operation
raylib-zig copied to clipboard

DrawTextureEx, DrawTexturePro, ect, not drawing

Open dmbfm opened this issue 3 years ago • 4 comments

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 avatar Jan 26 '22 23:01 dmbfm

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 avatar Jan 26 '22 23:01 dmbfm

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.

dmbfm avatar Jan 26 '22 23:01 dmbfm

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.

ChaseAColvin avatar Jul 21 '22 01:07 ChaseAColvin

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 avatar Jul 22 '22 11:07 Not-Nik

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

Not-Nik avatar Dec 23 '22 19:12 Not-Nik

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 avatar Jan 06 '23 21:01 AlxHnr

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

AlxHnr avatar Jan 10 '23 22:01 AlxHnr

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.

AlxHnr avatar Jan 11 '23 20:01 AlxHnr

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

Not-Nik avatar Feb 07 '23 18:02 Not-Nik

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.

AlxHnr avatar Feb 19 '23 21:02 AlxHnr

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

Not-Nik avatar Feb 19 '23 23:02 Not-Nik