render texture properly

This commit is contained in:
Srayan Jana
2025-09-23 22:08:54 -07:00
parent c369f53f7c
commit 7df80f6d9e
6 changed files with 15 additions and 10 deletions
+4
View File
@@ -17,3 +17,7 @@ lean_exe «lean-sdl-test» where
If you want to see an example project that uses these bindings, check this out:
https://github.com/ValorZard/lean-sdl-test
## License & Attribution
MIT
+2 -2
View File
@@ -59,8 +59,8 @@ opaque loadTexture : String → IO UInt32
@[extern "sdl_load_font"]
opaque loadFont : String → UInt32 → IO Bool
@[extern "sdl_render_texture_column"]
opaque renderTextureColumn : Int32 → Int32 → Int32 → Int32 → Int32 → Int32 → IO Int32
@[extern "sdl_render_texture"]
opaque renderTexture (x : Int32) (y : Int32) (w : Int32) (h : Int32) : IO Int32
@[extern "sdl_render_text"]
opaque renderText (message : String) (x : Int32) (y : Int32) (red : UInt8) (green : UInt8) (blue : UInt8) (alpha : UInt8) : IO Int32
+5
View File
@@ -38,6 +38,8 @@ def renderScene (state : EngineState) : IO Unit := do
setColor { r := 255, g := 0, b := 0 }
fillRect 100 100 200 200
let _ ← SDL.renderTexture 500 150 64 64
let message := "Hello, Lean SDL!"
let _ ← SDL.renderText message 50 50 255 255 255 255
pure ()
@@ -76,6 +78,9 @@ partial def run : IO Unit := do
SDL.quit
return
unless (← SDL.loadTexture "assets/wall.png") != 0 do
IO.println "Failed to load texture, using solid colors"
unless (← SDL.ttfInit) do
IO.println "Failed to initialize SDL_ttf"
SDL.quit
+1
View File
@@ -0,0 +1 @@
wall.png texture by [FacadeGaikan](https://opengameart.org/node/31075), licensed under CC0.
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 7.4 KiB

+3 -8
View File
@@ -163,15 +163,10 @@ lean_obj_res sdl_render_text(lean_obj_arg text, uint32_t dst_x, uint32_t dst_y,
}
lean_obj_res sdl_render_texture_column(uint32_t dst_x, uint32_t dst_y, uint32_t dst_height, uint32_t src_x, uint32_t src_y_start, uint32_t src_y_end, lean_obj_arg w) {
lean_obj_res sdl_render_texture(uint32_t dst_x, uint32_t dst_y, uint32_t dst_height, uint32_t dst_width, lean_obj_arg w) {
if (!g_renderer || !g_texture) return lean_io_result_mk_ok(lean_box_uint32(-1));
uint32_t tex_y_start = src_y_start >= 64 ? 0 : src_y_start;
uint32_t tex_y_end = src_y_end > 64 ? 64 : src_y_end;
if (tex_y_end <= tex_y_start) tex_y_end = tex_y_start + 1;
SDL_FRect dst_rect = { (float)dst_x, (float)dst_y, (float)dst_width, (float)dst_height };
SDL_FRect src_rect = {(float)(src_x % 64), (float)tex_y_start, 1.0f, (float)(tex_y_end - tex_y_start)};
SDL_FRect dst_rect = {(float)dst_x, (float)dst_y, 1.0f, (float)dst_height};
return lean_io_result_mk_ok(lean_box_uint32(SDL_RenderTexture(g_renderer, g_texture, &src_rect, &dst_rect)));
return lean_io_result_mk_ok(lean_box_uint32(SDL_RenderTexture(g_renderer, g_texture, NULL, &dst_rect)));
}