123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(* a 'window' is in fine just a layout *)(* Do not mistake it with a hardware (SDL) window *)openTsdlopenB_utilsmoduleLayout=B_layoutmoduleDraw=B_drawmoduleChain=B_chaintypet={layout:Layout.t;mutableis_fresh:bool;mutablebogue:bool;(* if bogue = false this means that bogue didn't create the corresponding
SDL window, and we should neither clear it before rendering, nor
RenderPresent it after rendering *)mutableon_close:(t->unit)option(* None means destroy window *)}letcreate?on_closelayout=ifLayout.get_houselayout<>Nonethenbeginprintd(debug_error+debug_user)"Cannot construct a Window from room %s because it is already contained in \
a house."(Layout.sprint_idlayout);raise(Invalid_argument"[Window.create] Cannot create a Window from a Layout that belongs \
to a house.")end;letg=layout.Layout.current_geominLayout.(layout.current_geom<-{gwithx=not_specified;y=not_specified});{layout;is_fresh=false;bogue=true;on_close}letget_layoutw=w.layoutletis_freshw=w.is_freshletset_freshw=w.is_fresh<-trueletto_refreshw=w.is_fresh<-falseleton_closewf=w.on_close<-f(* This is not very efficient because it will search for the Window.t from the
Layout.id... while we already have the Window.t at hand! Well, this function
doesn't have to be efficient anyways. *)letdestroyw=Layout.destroy_windoww.layoutletsdl_windoww=Layout.windoww.layoutletis_shownw=w.layout.Layout.showletshow_maybew=matchLayout.window_optw.layoutwith|Somewin->ifis_shownwthenSdl.show_windowwinelseSdl.hide_windowwin|None->printd(debug_error+debug_board)"[Window.show_maybe] the SDL window does not exist for layout %s"(Layout.sprint_idw.layout)(* physical size *)letsizew=Draw.get_window_size(sdl_windoww)letset_size~w~hwin=do_option(Layout.window_optwin.layout)(Draw.set_window_size~w~h)letmaximize_widthwin=do_option(Layout.window_optwin.layout)(funsdl_win->letid=go(Sdl.get_window_display_indexsdl_win)inletrect=go(Sdl.get_display_boundsid)inletw=Sdl.Rect.wrectinprintddebug_graphics"[maximize_width] Detected display size for layout %s: (%i,%i)."(Layout.sprint_idwin.layout)w(Sdl.Rect.hrect);let_w,h=Draw.get_window_sizesdl_wininDraw.set_window_sizesdl_win~w~h)letget_canvasw=Layout.get_canvasw.layout(** get SDL windows id, in case the canvas was created *)letidw=Sdl.get_window_id(Layout.windoww.layout)letequalw1w2=Layout.equalw1.layoutw2.layoutletrenderw=Layout.renderw.layoutletflip?clearw=ifnot(is_freshw)thenbeginrenderw;letclear=defaultclearw.bogueinprintddebug_graphics"clear=%b"clear;letpresent=w.bogueinLayout.flip~clear~presentw.layout;set_freshwendelseDraw.clear_layers(Layout.get_layerw.layout)(* Span an SDL window controlled by Bogue *)letmake_sdl_windoww=printddebug_board"Make window for layout %s (stack %d)."(Layout.sprint_idw.layout)(Chain.get_stack_id(Layout.get_layerw.layout));Layout.make_windoww.layout;w.bogue<-trueletuse_sdl_windowsdl_winw=printddebug_board"Use existing SDL Window for layout %s."(Layout.sprint_idw.layout);Layout.make_window~window:sdl_winw.layout;w.bogue<-false