49 Commits
Author SHA1 Message Date
Greg Shuflin 21c9cad34b C refactor: separate out SDL_Rect integer extraction code 2026-07-01 16:58:02 -07:00
Greg ShuflinandClaude Opus 4.6 1830995cc5 Remove opaque Pixels type; add safe updateTexture and zero-copy updateTextureFromSurface
The opaque Pixels type wrapped a void* pointing into an SDL_Surface's
pixel buffer, but nothing tied the Pixels lifetime to the surface. If
the GC collected the surface first, the Pixels pointer would dangle,
causing use-after-free.

Replace with two safe alternatives:

- updateTexture now takes a ByteArray (Lean-owned, GC-managed buffer)
  plus pitch, matching how Haskell's sdl2 uses ByteString and Rust's
  rust-sdl2 uses &[u8]. This is faithful to SDL_UpdateTexture's general
  void* interface but requires copying pixels into a ByteArray first.

- updateTextureFromSurface is a new zero-copy convenience that takes an
  SDLSurface directly. The C side extracts both pixels and pitch from
  the surface internally. Because the surface is borrowed (@&) for the
  call's duration, the GC cannot collect it mid-use. This is not
  strictly faithful to SDL_UpdateTexture's C API, but is the common
  case and avoids an unnecessary memcpy.

Also removes: Pixels type, Pixels.nonemptyType, sdl_pixels_external_class,
sdl_Surface_get_pixels, and the pixels_external_class registration in
sdl_init.

WebcamApp.lean is updated to use updateTextureFromSurface.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:41:24 -07:00
Greg ShuflinandClaude Opus 4.6 3ec33e9a27 Fix mouse state packing bit-field overlap
The old packing was:
  X << 32 | Y << 16 | buttons
where X and Y were full 32-bit values. Y's upper 16 bits (bits 32-47)
overlapped with X (bits 32-63), and buttons (bits 0-31) overlapped with
Y (bits 16-47). This corrupted X whenever Y >= 65536, and corrupted Y
whenever buttons had any bits set above bit 15.

The Lean unpacking side already assumed a non-overlapping layout:
  X = bits 63:32, Y = bits 31:16 (masked to 16), buttons = bits 15:0

Now the C side explicitly masks Y and buttons to 16 bits before packing,
matching the Lean expectations. This means Y is limited to [-32768,
32767] which is adequate for screen coordinates and typical relative
mode deltas.

Also fix the SDLIO error type in sdl_set_relative_mouse_mode (same
lean_mk_io_user_error issue fixed in the earlier commit).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 e3dcc8da14 Fix camera and camera spec finalizers to free resources
sdl_camera_finalizer was a no-op, so SDL cameras opened with
SDL_OpenCamera were never closed. Now calls SDL_CloseCamera.

sdl_camera_spec_finalizer was a no-op, so the SDL_CameraSpec structs
allocated with malloc in sdl_get_camera_format were never freed. Now
calls free().

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 9c1c9e84db Fix error type mismatch in SDLIO-returning functions
SDLIO is defined as EIO SDLError, where SDLError is a single-field
structure wrapping String. At runtime, SDLError is represented as just
a String (newtype erasure). Therefore, error returns from C functions
with SDLIO return types should use lean_mk_string(...) directly.

Several functions incorrectly wrapped the error string in
lean_mk_io_user_error(), which creates an IO.Error constructor. When
Lean tried to pattern-match this as SDLError (a String), it would read
the wrong memory layout, causing potential crashes on any error path.

Affected functions: sdl_create_window, sdl_create_window_and_renderer,
sdl_set_render_draw_color, sdl_set_render_draw_color_float,
sdl_render_clear, sdl_render_fill_rect, sdl_create_texture_from_surface
(one of two error paths), sdl_set_track_audio, sdl_play_track.

sdl_render_present is left unchanged because it returns IO Unit (not
SDLIO), so lean_mk_io_user_error is correct there.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg ShuflinandClaude Opus 4.6 35fde25748 Fix SDLFRect field offsets and float/double ABI mismatch
Two related issues with Lean's Float type being 8 bytes (double), not 4:

1. sdl_render_texture_rect read SDLFRect scalar fields at byte offsets
   0,4,8,12 but lean_ctor_get_float reads 8-byte doubles, so the correct
   offsets are 0,8,16,24. The old offsets caused each read to straddle
   two adjacent fields, producing garbage values for y/w/h.

2. sdl_set_render_draw_color_float declared its parameters as float (4
   bytes) but Lean passes Float as double (8 bytes) at the ABI level.
   On x86-64, Lean puts doubles in XMM registers and the C function
   interpreted the wrong bits—e.g. double 1.0 (0x3FF0000000000000) has
   lower 32 bits 0x00000000, so all colors read as 0.

Both functions now use double to match Lean's Float ABI, with explicit
(float) casts where SDL3 APIs expect float.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07:00
Greg Shuflin cb9e7e8839 Fix signatures in C 2025-12-26 23:30:25 -08:00
Greg Shuflin 10fe99aa80 Remove printing 2025-12-26 23:17:16 -08:00
Greg Shuflin 62c72f978a Threw claude at the problem - webcam works now 2025-12-26 23:15:55 -08:00
Greg Shuflin 7c94e99f58 Float rect 2025-12-26 22:56:19 -08:00
Greg Shuflin 52b882daaf render texture rect partially done 2025-12-25 19:44:20 -08:00
Greg Shuflin 71ddbdeb5a texture updating + pixels 2025-12-25 18:44:18 -08:00
Greg Shuflin 37fbf85118 SDLRect as lean structure 2025-12-25 04:23:29 -08:00
Greg Shuflin 38aa7e316b more webcam stuff 2025-12-24 02:53:47 -08:00
Greg Shuflin 5b96cc352f createTexture pixelformat 2025-12-24 02:42:04 -08:00
Greg Shuflin e21b84039b get format from sdl_surface 2025-12-24 02:40:26 -08:00
Greg Shuflin 23295a116f fix webcam frame leaking issue 2025-12-24 02:35:27 -08:00
Greg Shuflin a06b234ac0 more sdl texture 2025-12-24 02:25:54 -08:00
Greg Shuflin d1479f9db4 SDL_CreateTexture 2025-12-24 01:13:19 -08:00
Greg Shuflin e33e3ec144 use b_lean_obj_arg 2025-12-24 00:57:19 -08:00
Greg Shuflin 3880a650f4 sdl release camera frame 2025-12-24 00:53:17 -08:00
Greg Shuflin 9900a844ff Stub for timestamp to fill in later 2025-12-24 00:04:50 -08:00
Greg Shuflin ff5eea30d4 SDL_AcquireCameraFrame 2025-12-24 00:00:53 -08:00
Greg Shuflin 6d7a079f1e more camera type stuff 2025-12-23 23:37:22 -08:00
Greg Shuflin fbab89c3ad working cameraSpec 2025-12-18 03:15:45 -08:00
Greg Shuflin 760d050391 opencamera 2025-12-17 16:37:21 -08:00
Greg Shuflin 65a5f3c066 Working on camera code 2025-12-17 03:10:58 -08:00
Greg Shuflin 6418561029 sdl_create_window_and_renderer 2025-12-16 00:28:37 -08:00
Greg Shuflin 39dbe70f22 Remove lean_obj_arg world
cf. https://github.com/leanprover/lean4/pull/10625
2025-12-15 20:30:51 -08:00
Greg Shuflin 5b915f5e10 sdl createwindowandrenderer 2025-12-15 20:11:42 -08:00
Srayan Jana 3582357b4c add two different render texture functions 2025-09-27 19:29:22 -07:00
Srayan Jana bb9e94f540 update return type of get_ticks to correct 2025-09-26 19:35:19 -07:00
Srayan Jana 21d4ccc4e5 add comment about signed integers in lean 2025-09-25 13:29:38 -07:00
Srayan Jana 11c89d21b0 split font/text functions 2025-09-25 13:26:43 -07:00
Srayan Jana 9cec57c1ef refactored audio and split it up 2025-09-25 13:13:20 -07:00
Srayan Jana 47ef2dcd95 wrap SDL Errors in a more lean way 2025-09-25 12:29:40 -07:00
Srayan Jana 36c032a33d Split stuff out of sdl.c into lean itself (part 1) 2025-09-25 12:23:53 -07:00
Srayan Jana b30856e04b add minor documentation 2025-09-25 11:19:39 -07:00
Srayan Jana d56efc38bc turn texture to lean object 2025-09-25 11:10:17 -07:00
Srayan Jana 9b4b7602d1 Merge pull request #2 from oOo0oOo/master
[feature] Add mouse support (normal and relative mode)
2025-09-24 17:45:00 -07:00
Srayan Jana cc28dc00f4 we have audio 2025-09-24 15:21:48 -07:00
Srayan Jana 8c83559d85 init mixer 2025-09-24 15:11:41 -07:00
Oliver Dressler 497501d5bd [feature] Add mouse support (normal and relative mode) 2025-09-24 11:58:35 +02:00
Srayan Jana 00970d2bd6 fix font rendering 2025-09-23 22:16:03 -07:00
Srayan Jana 7df80f6d9e render texture properly 2025-09-23 22:08:54 -07:00
Srayan Jana c369f53f7c got text working, but its jank 2025-09-23 21:57:36 -07:00
Srayan Jana c48b9f5b37 load in font 2025-09-23 21:34:45 -07:00
Srayan Jana 9b58e5e1b4 ttf builds properly 2025-09-23 21:21:42 -07:00
Srayan Jana 6fdaf64761 got bindings building, but not right 2025-09-20 16:11:27 -07:00