41 Commits
Author SHA1 Message Date
Greg ShuflinandClaude Fable 5.1 8104955c71 Make the webcam demo run on macOS
Lean runs main on a helper thread, which Cocoa rejects for video init;
export LEAN_MAIN_USE_THREAD=0 from the justfile on macOS. Wait for the
camera permission event before reading the camera format, since SDL
grants access asynchronously.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-09-08 15:26:20 -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 95fa063c2b Fix @& ownership mismatches causing refcount leaks
When a Lean extern parameter lacks @&, Lean increments the refcount
before the call and expects the C callee to lean_dec it. If the C side
declares b_lean_obj_arg (borrowed) and never decrements, the refcount
stays elevated and the object is never freed.

Add @& (borrow annotation) to parameters where C only reads the value:

- createWindow/createWindowAndRenderer: title String was owned but C
  only calls lean_string_cstr (borrowed read). Leaked the string.

- renderTextureRect: sourceRect and destRect SDLFRect were owned but C
  only reads scalar fields via lean_ctor_get_float. These are typically
  constructed fresh each frame, so this leaked two rect objects per
  frame (~48 bytes each at 60fps = ~5.6 KB/s).

- setRelativeMouseMode: window SDLWindow was owned but C only reads the
  external data pointer. Leaked the window's refcount.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-23 13:17:24 -07: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 15bde2fad8 move pixelformat 2025-12-24 02:36:38 -08:00
Greg Shuflin f7b5532068 shouldn't be reference 2025-12-24 02:28:49 -08:00
Greg Shuflin d1479f9db4 SDL_CreateTexture 2025-12-24 01:13: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 1b562dfb5b Use camera 2025-12-17 16:49:39 -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 e6bf480252 EIO.adaptExcept -> EIO.adapt 2025-12-15 20:29:48 -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 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 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