actually seperate act from render

This commit is contained in:
cqc
2021-09-01 05:05:48 -05:00
parent 3004a87571
commit d6b16f2a4e

48
main.ml
View File

@ -363,19 +363,20 @@ module Display = struct
(* current window state to be passed to window renderer *) (* current window state to be passed to window renderer *)
type state = type state =
{ box: box2 { box: box2
; (* This is cannonically box within which the next element should draw *) (* This is cannonically box within which the next element should draw *)
time: float ; time: float
; events: Event.events
; wall: Wall.renderer } ; wall: Wall.renderer }
(* the box2 here is cannonically the place the returner drew (* the box2 here is cannonically the place the returner drew
(the Wall.image extents) *) (the Wall.image extents) *)
type image = box2 * Wall.image type image = box2 * Wall.image
let empty : image = (Box2.empty, Image.empty) let image_empty : image = (Box2.empty, Image.empty)
type pane = state -> state * image type pane = state -> state * image
let pane_empty s = (s, image_empty)
type frame = type frame =
{ sdl_win: Sdl.window { sdl_win: Sdl.window
; gl: Sdl.gl_context ; gl: Sdl.gl_context
@ -410,7 +411,7 @@ module Display = struct
Ok {sdl_win; gl; wall; quit= false; fullscreen= false} ) Ok {sdl_win; gl; wall; quit= false; fullscreen= false} )
~cleanup:(fun () -> Sdl.destroy_window sdl_win) ~cleanup:(fun () -> Sdl.destroy_window sdl_win)
let display_frame frame render = let get_events () =
(* create and fill event list *) (* create and fill event list *)
let ev = Sdl.Event.create () in let ev = Sdl.Event.create () in
let el = ref [`None] in let el = ref [`None] in
@ -419,8 +420,13 @@ module Display = struct
if e != `None then el := !el @ [e] if e != `None then el := !el @ [e]
(* HACK? *) (* HACK? *)
done ; done ;
(* Filter the events *) !el
el :=
let last_pane = ref pane_empty
let display_frame frame actor =
let events =
(* Handle relevant events *)
List.filter_map List.filter_map
(function (function
| `Quit -> | `Quit ->
@ -438,19 +444,14 @@ module Display = struct
else Sdl.Window.windowed ) else Sdl.Window.windowed )
: _ result ) ) ; : _ result ) ) ;
None None
| `Key_up a -> Some (`Key_up a)
| `Key_down a -> Some (`Key_down a)
| `Mouse a -> Some (`Mouse a)
| a -> Some a (*| a -> Some a*) ) | a -> Some a (*| a -> Some a*) )
!el ; (get_events ()) in
if List.length !el > 0 then (
let width, height = Sdl.gl_get_drawable_size frame.sdl_win in let width, height = Sdl.gl_get_drawable_size frame.sdl_win in
if List.length events > 0 then last_pane := actor events ;
let _, (_, image) = let _, (_, image) =
render !last_pane
{ box= { box= Box2.v (P2.v 0. 0.) (P2.v (float width) (float height))
Box2.v (P2.v 0. 0.) (P2.v (float width) (float height))
; time= ticks () ; time= ticks ()
; events= !el
; wall= frame.wall } in ; wall= frame.wall } in
Sdl.gl_make_current frame.sdl_win frame.gl Sdl.gl_make_current frame.sdl_win frame.gl
>>= fun () -> >>= fun () ->
@ -459,8 +460,7 @@ module Display = struct
Gl.clear_color 0.0 0.0 0.0 1.0 ; Gl.clear_color 0.0 0.0 0.0 1.0 ;
Gl.( Gl.(
clear clear
( color_buffer_bit lor depth_buffer_bit (color_buffer_bit lor depth_buffer_bit lor stencil_buffer_bit)) ;
lor stencil_buffer_bit )) ;
Gl.enable Gl.blend ; Gl.enable Gl.blend ;
Gl.blend_func_separate Gl.one Gl.src_alpha Gl.one Gl.blend_func_separate Gl.one Gl.src_alpha Gl.one
Gl.one_minus_src_alpha ; Gl.one_minus_src_alpha ;
@ -469,8 +469,7 @@ module Display = struct
let width = float width and height = float height in let width = float width and height = float height in
Wall.Renderer.render frame.wall ~width ~height image ; Wall.Renderer.render frame.wall ~width ~height image ;
Sdl.gl_swap_window frame.sdl_win ; Sdl.gl_swap_window frame.sdl_win ;
Ok () ) Ok ()
else Ok ()
let run frame render () = let run frame render () =
let frame = get_result frame in let frame = get_result frame in
@ -618,7 +617,9 @@ module Panel = struct
type actor = Event.events -> Display.pane type actor = Event.events -> Display.pane
let blank = let blank =
{ act= (fun panel _events -> (panel, fun s -> (s, Display.empty))) { act=
(fun panel _events ->
(panel, fun s -> (s, Display.image_empty)) )
; subpanels= [] ; subpanels= []
; tag= "blank pane" } ; tag= "blank pane" }
@ -1109,10 +1110,7 @@ let () =
[ Panel.draw (fun (s : Display.state) -> [ Panel.draw (fun (s : Display.state) ->
(s, Display.fill_box (Display.gray 0.125) s.box) ) (s, Display.fill_box (Display.gray 0.125) s.box) )
; top_panel top_1 ] ) in ; top_panel top_1 ] ) in
Display.( Display.(run (make_frame ~title:"hi" ~w:1440 ~h:900 ()) actor) ()
run (make_frame ~title:"hi" ~w:1440 ~h:900 ()) (fun s ->
actor s.events s ))
()
(* Implement the "window management" as just toplevel defined functions that manipulate the window tree *) (* Implement the "window management" as just toplevel defined functions that manipulate the window tree *)