Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DrawTextureRec failing silently in Release builds #9

Closed
anaanook opened this issue Dec 22, 2020 · 4 comments
Closed

DrawTextureRec failing silently in Release builds #9

anaanook opened this issue Dec 22, 2020 · 4 comments

Comments

@anaanook
Copy link

anaanook commented Dec 22, 2020

This is likely a zig C ABI problem.. but I'm throwing it here first because I'm worried if I post it to ziglang they will say its the library or wrapper at fault. This had me stumped for several hours! It is critical that you test this on any of the release builds, as it works fine in debug. I am using zig build -Drelease-small=true. I'm also running zig version 0.7.0+58241364c

usingnamespace @import("raylib");

pub const TextureDrawer = struct {
    width:f32,
    height:f32,
    pub fn GetRect(self:*TextureDrawer) Rectangle{
        return Rectangle{
            .x=0,.y=0,
            .width=self.width,
            .height=self.height};
    }
    pub fn Draw( self:*TextureDrawer ) void{
         DrawTextureRec(Tex,self.GetRect(),Vector2{.x=0,.y=0},WHITE);
    }
    pub fn DrawWithRect(self:*TextureDrawer,rect:Rectangle ) void {
        DrawTextureRec(Tex,rect,Vector2{.x=0,.y=0},WHITE);
    }
};

pub var Drawer = TextureDrawer{.width=64,.height=64};
pub var Tex : Texture2D = undefined;

pub fn main() anyerror!void
{
    const screenWidth = 800;
    const screenHeight = 450;
    InitWindow(screenWidth, screenHeight, "raylib-zig [core] example - basic window");
    Tex = LoadTexture("tiles.bmp");

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

        ClearBackground(WHITE);

        Drawer.Draw();  //DOES NOT WORK
        Drawer.DrawWithRect(Drawer.GetRect());  //THIS WORKS

        EndDrawing();
    }
    CloseWindow();  
}

So this is the absolute bare minimum code to show the problem. Basically if you call a function that returns a rectangle from inside a struct and pass it to DrawTextureRec it fails silently. I am way too dumb to try and diagnose what's happening here.

Some additional info that had me really scratching my head - If you do a print command inside the problematic function to check the contents of the rectangle, it works again.

    pub fn DrawWithPrint( self:*TextureDrawer ) void{
        var rect = self.GetRect();
        std.debug.print("Rect: {}",.{rect});
        DrawTextureRec(Tex,rect,Vector2{.x=0,.y=0},WHITE);
    }

Its some schrodinger's cat behavior, works only when observed.

@Not-Nik
Copy link
Owner

Not-Nik commented Dec 22, 2020

You are right, this is a zig C ABI problem. I can't really reproduce your situation as for me the code you sent doesn't draw at all, which is somewhat expected. Zig doesn't yet support passing structures between Zig and C, including both as arguments and in return values. These kinds of issues have partly been addressed in the Readme under Technical Restrictions and the workaround branch. The exact behaviour you want to achieve should be possible on that branch, but any raylib functions returning structs wont work.

Speaking of the workaround branch I'm currently trying to fix the projectSetup.sh over there and add a lib.zig similar to the one on the master branch.

EDIT: My bad, I just realised your source requires a tiles.bmp which I obviously didn't have. However, trying your example with a different Image (a PNG, which shouldn't really matter) works on the workaround branch. I'm going to tidy up my local changes and push them in a bit.

@Not-Nik
Copy link
Owner

Not-Nik commented Dec 22, 2020

Shuould work now since 88c9614. Reopen if your issue persists.

@Not-Nik Not-Nik closed this as completed Dec 22, 2020
@Not-Nik
Copy link
Owner

Not-Nik commented Dec 22, 2020

This issue gets weirder and weirder, the more I investigate. Both the draw functions work for me even on master. I do use -Dsystem-raylib=true because building raylib on macOS is not supported yet, so that might be related. Additionally as described in my first comment, both draw calls shouldn't work because of the lack of C ABI support. Rectangle, Vector2 and Color are all <= 16 which means they should raise a compiler error.

@anaanook
Copy link
Author

I can confirm the workaround fixes the problem which is great! Thanks for investigating this.

Not sure if you saw, but both draw functions work for me but only in debug builds. If I switch over to any of the 3 release modes the one with the embedded function fails. It's definitely weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants