Hi, I’ve been working on bindings for SDL3: GitHub - SleepingInsomniac/sdl3.cr: SDL3 bindings for crystal.
There’s a type signature that accepts a pointer to a c-struct in the renderer bindings:
# extern SDL_DECLSPEC bool SDLCALL SDL_RenderTexture(SDL_Renderer *renderer, SDL_Texture *texture, const SDL_FRect *srcrect, const SDL_FRect *dstrect);
fun render_texture = SDL_RenderTexture(renderer : Renderer*, texture : Texture*, srcrect : FRect*, dstrect : FRect*) : Bool
SDL3’s documentation states that the rect arguments are: “a pointer to the source rectangle, or NULL for the entire ….”
I can’t use : FRect*? or crystal gives an error about only using primitive types.
I’m calling this lib function from my crystal adapter code:
def render_texture(texture : Texture, source_rect : FRect? = nil, dest_rect : FRect? = nil)
sr = source_rect.try { |sr| pointerof(sr) } || Pointer(FRect).null
dr = dest_rect.try { |dr| pointerof(dr) } || Pointer(FRect).null
LibSdl3.render_texture(@pointer, texture, sr, dr)
end
Which works, but if I pass the rects directly, I get type errors. For objects like my texture object the c-bindings call #to_unsafe. I would have expected something similar for the c-struct that’s being passed in.
Is there a better way to handle a this?