12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061(* We create a Box widget whose image will be a snapshot of a given room. *)(* The texture is initialized at the startup event, or upon user call. *)(* In order to achieve this, we hijack the Layout.display function, which uses
blits in layers, and render the layers to a target texture. Hence the
assumption is that the layers blits are empty. This means that creating
snapshots should NOT be done in a separate thread: the risk is to corrupt
layers in case it is called at the same time as the Bogue.render function. *)(* Just in case, we have added a mutex to the current_layer. but still... *)(* When we create the widget, the size of the box may not be the same as the
size of the widget at the time it receives the startup event... *)(* TODO: we have trouble rendering correctly when the room background has alpha
channel... ? use the new https://wiki.libsdl.org/SDL_ComposeCustomBlendMode??
*)(* Warning: a new texture is created at every update, because size might
change. TODO add an option in case we want to be fast and reuse the same
texture? *)openB_utils;;moduleLayout=B_layoutmoduleWidget=B_widgetmoduleVar=B_varmoduleTrigger=B_triggermoduleDraw=B_drawmoduleBox=B_boxmoduleStyle=B_styleletupdatewidgetroom=letrenderer=Layout.rendererroominletw,h=Layout.get_physical_sizeroominletx,y=Layout.(getxroom,getyroom)inlettarget=Draw.create_targetrendererwhinprintddebug_graphics"Rendering snapshot...";Var.protectDraw.current_layer;(* Now we assume that the blits are empty... TODO check this and issue an
error otherwise *)Layout.display~pos0:(-x,-y)room;letsave=Draw.push_targetrenderertargetinDraw.render_all_layers(Layout.get_layerroom);Var.releaseDraw.current_layer;Draw.pop_targetrenderersave;printddebug_graphics"...rendering snapshot done.";(* essai blur *)(* let target = Draw.blur_texture renderer target 24 in *)letbox=Widget.get_boxwidgetindo_option(Var.getbox.Box.render)Draw.forget_texture;Var.setbox.Box.render(Sometarget);;letcreate?borderroom=letw,h=Layout.get_sizeroominletstyle=Style.create?border()inletbox=Widget.box~w~h~style()inletc=Widget.connect_mainboxbox(funw__->updatewroom)[Trigger.startup]inWidget.add_connectionboxc;box;;