package miaou-widgets-display-sdl
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Miaou SDL widget implementations
Install
dune-project
Dependency
Authors
Maintainers
Sources
v0.5.2.tar.gz
md5=60a3b9f181f24572a06a9492532bfdda
sha512=fcc35a275066be2900e6201782faf47503076fa4640f08cf78067835a6f447b74613009e55b2ac799adb7ca46f1bffa261fc5971753f2cc3c6bef327511c7ef6
doc/src/miaou-widgets-display-sdl.widgets/image_widget_sdl.ml.html
Source file image_widget_sdl.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65(** SDL renderer for image widget with texture caching *) module Img = Miaou_widgets_display.Image_widget type cached_texture = { texture : Tsdl.Sdl.texture; width : int; height : int; scale : int; } let texture_cache : (Img.t, cached_texture) Hashtbl.t = Hashtbl.create 8 let create_cached_texture renderer img_widget scale = let open Tsdl.Sdl in let width, height = Img.get_dimensions img_widget in let tex_width = width * scale in let tex_height = height * scale in match create_texture renderer Pixel.format_argb8888 Texture.access_target ~w:tex_width ~h:tex_height with | Error (`Msg e) -> failwith ("Failed to create texture: " ^ e) | Ok texture -> (* Set texture as render target and draw pixels *) let _ = set_render_target renderer (Some texture) in for py = 0 to height - 1 do for px = 0 to width - 1 do let pixel = Img.get_pixel img_widget ~x:px ~y:py in let _ = set_render_draw_color renderer pixel.r pixel.g pixel.b 255 in let rect = Rect.create ~x:(px * scale) ~y:(py * scale) ~w:scale ~h:scale in let _ = render_fill_rect renderer (Some rect) in () done done ; (* Restore default render target *) let _ = set_render_target renderer None in {texture; width = tex_width; height = tex_height; scale} let render renderer img_widget ~x ~y ~scale = let open Tsdl.Sdl in (* Get or create cached texture *) let cached = match Hashtbl.find_opt texture_cache img_widget with | Some c when c.scale = scale -> c | _ -> (* Create new texture and cache it *) let c = create_cached_texture renderer img_widget scale in Hashtbl.replace texture_cache img_widget c ; c in (* Render the cached texture *) let dst_rect = Rect.create ~x ~y ~w:cached.width ~h:cached.height in let _ = render_copy renderer cached.texture ~dst:dst_rect in ()
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>