Not-Nik / raylib-zig

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

`DrawTextEx` odd text size behaviour #26

Closed alexjgriffith closed 2 years ago

alexjgriffith commented 2 years ago

Running into an issue when printing text using DrawTextEx. The font size is over ridden by the y member of the Vector2 struct. As you increase position.y the text size increases.

Not sure if this is a raylib issue, a zig issue, or a me issue.

Code Example

const font: raylib.Font = raylib.LoadFont("assets/fonts/Vermin Vibes 1989.ttf"); // or any other font

...

const x: f32 = 10;
const y: f32 = 10;
const position = raylib.Vector2{ .x = x, .y = y };

...

raylib.DrawTextEx(font, "Hello World", position, 8.0, 1.0, raylib.BLUE);

GDB Step Through

When I walk through DrawTextEx in GDB we can see the Vector2 somehow loses its y value and that value ends up shifting the fontSize parameter into the spacing parameter.

Before we enter DrawTextEx we can see that position is still `{x=10,y=10}

(gdb) p position
$1 = {x = 10, y = 10}

The call to enter position is wrong. The fontSize should be 8 and the spacing should be 1.

(gdb) s
DrawTextEx (font=..., text=0x20c702 <game-state-example.gameloop.anon_3209> "Hello World", position=..., fontSize=10, spacing=8, tint=...)
    at raylib-zig/raylib/src/rtext.c:1030

Looking at position from within DrawTextEx we can see that position.y has been set to 0

(gdb) p position
$2 = {x = 10, y = 0}

As we saw from the call to Draw Text Ex fontSize is 10 and spacing is 8. These should be 8 and 1 respectivly

(gdb) p fontSize
$3 = 10
(gdb) p spacing
$4 = 8

I cant tell if there is an issue with tint, the struct signature looks a little off.

(gdb) p tint
$5 = {r = 0 '\000', g = 121 'y', b = 241 '\361', a = 255 '\377'}
(gdb) 

GDB Disassembly

Disassembling the lead into the DrawTextEx function yields:

│  >0x2b5e42 <game-state-example.gameloop+466>      add    $0x8,%rax                                                                                                           │
│   0x2b5e46 <game-state-example.gameloop+470>      vmovss -0x8(%rbp),%xmm0                                                                                                    │
│   0x2b5e4b <game-state-example.gameloop+475>      vmovss -0x4(%rbp),%xmm1                                                                                                    │
│   0x2b5e50 <game-state-example.gameloop+480>      mov    -0xb275a(%rip),%esi        # 0x2036fc                                                                               │
│   0x2b5e56 <game-state-example.gameloop+486>      vmovups (%rax),%ymm2                                                                                                       │
│   0x2b5e5a <game-state-example.gameloop+490>      vmovups 0x10(%rax),%ymm3                                                                                                   │
│   0x2b5e5f <game-state-example.gameloop+495>      mov    %rsp,%rax                                                                                                           │
│   0x2b5e62 <game-state-example.gameloop+498>      vmovups %ymm3,0x10(%rax)                                                                                                   │
│   0x2b5e67 <game-state-example.gameloop+503>      vmovups %ymm2,(%rax)                                                                                                       │
│   0x2b5e6b <game-state-example.gameloop+507>      lea    -0xa9770(%rip),%rdi        # 0x20c702 <game-state-example.gameloop__anon_3209>     
│  >0x2b5e42 <game-state-example.gameloop+466>      add    $0x8,%rax                                                                                                           │
│   0x2b5e46 <game-state-example.gameloop+470>      vmovss -0x8(%rbp),%xmm0                                                                                                    │
│   0x2b5e4b <game-state-example.gameloop+475>      vmovss -0x4(%rbp),%xmm1                                                                                                    │
│   0x2b5e50 <game-state-example.gameloop+480>      mov    -0xb275a(%rip),%esi        # 0x2036fc                                                                               │
│   0x2b5e56 <game-state-example.gameloop+486>      vmovups (%rax),%ymm2                                                                                                       │
│   0x2b5e5a <game-state-example.gameloop+490>      vmovups 0x10(%rax),%ymm3                                                                                                   │
│   0x2b5e5f <game-state-example.gameloop+495>      mov    %rsp,%rax                                                                                                           │
│   0x2b5e62 <game-state-example.gameloop+498>      vmovups %ymm3,0x10(%rax)                                                                                                   │
│   0x2b5e67 <game-state-example.gameloop+503>      vmovups %ymm2,(%rax)                                                                                                       │
│   0x2b5e6b <game-state-example.gameloop+507>      lea    -0xa9770(%rip),%rdi        # 0x20c702 <game-state-example.gameloop__anon_3209> 
│   0x2b5e72 <game-state-example.gameloop+514>      vmovss -0xb278a(%rip),%xmm2        # 0x2036f0                                                                              │
│   0x2b5e7a <game-state-example.gameloop+522>      vmovss -0xb2796(%rip),%xmm3        # 0x2036ec                                                                              │
│   0x2b5e82 <game-state-example.gameloop+530>      vzeroupper                                                                                                                 │
│  >0x2b5e85 <game-state-example.gameloop+533>      callq  0x35cbc0 <DrawTextEx>  
│   0x35cbc1 <DrawTextEx+1>         mov    %rsp,%rbp                                                                                                                           │
│   0x35cbc4 <DrawTextEx+4>         sub    $0xc0,%rsp                                                                                                                          │
│   0x35cbcb <DrawTextEx+11>        lea    0x10(%rbp),%rax                                                                                                                     │
│   0x35cbcf <DrawTextEx+15>        mov    %rax,-0x90(%rbp)                                                                                                                    │
│   0x35cbd6 <DrawTextEx+22>        mov    %fs:0x28,%rcx                                                                                                                       │
│   0x35cbdf <DrawTextEx+31>        mov    %rcx,-0x8(%rbp)                                                                                                                     │
│   0x35cbe3 <DrawTextEx+35>        vmovlpd %xmm0,-0x48(%rbp)                                                                                                                  │
│   0x35cbe8 <DrawTextEx+40>        mov    %esi,-0x50(%rbp)                                                                                                                    │
│   0x35cbeb <DrawTextEx+43>        mov    %rdi,-0x58(%rbp)                                                                                                                    │
│   0x35cbef <DrawTextEx+47>        vmovss %xmm1,-0x5c(%rbp)                                                                                                                   │
│   0x35cbf4 <DrawTextEx+52>        vmovss %xmm2,-0x60(%rbp) 

edit: extracted position to demonstrate issue via GDB

alexjgriffith commented 2 years ago

Closing, this is just a repeat of #17. For those who come across this in the future check https://github.com/ziglang/zig/issues/1481

Note, we seem to be receiving structs fine from C, we just are unable to send some, like Vector2s. Colors seem to pass back and forth ok.

alexjgriffith commented 2 years ago

After additional testing, this appears to be a regression. Using Zig 9.1 avoids this C ABI issue