Evolution of main_loop().
This commit is contained in:
parent
2eaecd6bd6
commit
9a5ad6c8e8
4 changed files with 48 additions and 3 deletions
35
Game.ml
Normal file
35
Game.ml
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
|
||||||
|
let rec refresh_input () =
|
||||||
|
(* poll events *)
|
||||||
|
let get_handler_fun ev =
|
||||||
|
match ev with
|
||||||
|
| Sdlevent.KEYDOWN _ -> Keyboard.handle_event
|
||||||
|
| Sdlevent.KEYUP _ -> Keyboard.handle_event
|
||||||
|
|
||||||
|
| Sdlevent.MOUSEMOTION _ -> Mouse.handle_event
|
||||||
|
| Sdlevent.MOUSEBUTTONDOWN _ -> Mouse.handle_event
|
||||||
|
| Sdlevent.MOUSEBUTTONUP _ -> Mouse.handle_event
|
||||||
|
|
||||||
|
(*
|
||||||
|
| Sdlevent.JOYAXISMOTION _ -> Joystick.handle_event
|
||||||
|
| Sdlevent.JOYBALLMOTION _ -> Joystick.handle_event
|
||||||
|
| Sdlevent.JOYHATMOTION _ -> Joystick.handle_event
|
||||||
|
| Sdlevent.JOYBUTTONDOWN _ -> Joystick.handle_event
|
||||||
|
| Sdlevent.JOYBUTTONUP _ -> Joystick.handle_event
|
||||||
|
*)
|
||||||
|
|
||||||
|
| _ -> ignore
|
||||||
|
in
|
||||||
|
|
||||||
|
let some_event = Sdlevent.poll ()
|
||||||
|
in
|
||||||
|
|
||||||
|
match some_event with
|
||||||
|
| Some Sdlevent.QUIT -> ()
|
||||||
|
| Some ev ->
|
||||||
|
let handler_fun = get_handler_fun ev
|
||||||
|
in
|
||||||
|
handler_fun ev ;
|
||||||
|
refresh_input ()
|
||||||
|
| None -> ()
|
||||||
|
;;
|
4
Keyboard.ml
Normal file
4
Keyboard.ml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
let handle_event ev =
|
||||||
|
ignore ev
|
||||||
|
;;
|
4
Mouse.ml
Normal file
4
Mouse.ml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
|
||||||
|
let handle_event ev =
|
||||||
|
ignore ev
|
||||||
|
;;
|
8
main.ml
8
main.ml
|
@ -38,14 +38,13 @@ let string_of_keyboard_event event =
|
||||||
let rec event_loop () =
|
let rec event_loop () =
|
||||||
print_endline "Event_loop...";
|
print_endline "Event_loop...";
|
||||||
Sdltimer.delay 20;
|
Sdltimer.delay 20;
|
||||||
let event_opt = Sdlevent.poll ()
|
|
||||||
in
|
|
||||||
let match_event event = (
|
let match_event event = (
|
||||||
match event with
|
match event with
|
||||||
| Sdlevent.KEYDOWN {Sdlevent.keysym=Sdlkey.KEY_ESCAPE} ->
|
| Sdlevent.KEYDOWN {Sdlevent.keysym=Sdlkey.KEY_ESCAPE} ->
|
||||||
print_endline "You pressed escape! The fun is over now."
|
print_endline "You pressed escape! The fun is over now."
|
||||||
|
|
||||||
| Sdlevent.KEYDOWN event ->
|
| Sdlevent.KEYDOWN event ->
|
||||||
|
Keyboard.handle_event event
|
||||||
let keystr = string_of_keyboard_event event
|
let keystr = string_of_keyboard_event event
|
||||||
in
|
in
|
||||||
print_endline ("You pressed " ^ keystr);
|
print_endline ("You pressed " ^ keystr);
|
||||||
|
@ -54,6 +53,8 @@ let rec event_loop () =
|
||||||
| _ ->
|
| _ ->
|
||||||
event_loop ()
|
event_loop ()
|
||||||
) in
|
) in
|
||||||
|
let event_opt = Sdlevent.poll ()
|
||||||
|
in
|
||||||
match event_opt with
|
match event_opt with
|
||||||
| None -> event_loop ()
|
| None -> event_loop ()
|
||||||
| Some event -> match_event event;
|
| Some event -> match_event event;
|
||||||
|
@ -66,7 +67,8 @@ let game_loop ~screen =
|
||||||
in
|
in
|
||||||
Sdlvideo.blit_surface ~src:image ~src_rect:image_from ~dst:screen ~dst_rect:image_to ();
|
Sdlvideo.blit_surface ~src:image ~src_rect:image_from ~dst:screen ~dst_rect:image_to ();
|
||||||
Sdlvideo.flip screen;
|
Sdlvideo.flip screen;
|
||||||
event_loop ();
|
let action_fun = event_loop ();
|
||||||
|
game_loop screen
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let main () =
|
let main () =
|
||||||
|
|
Loading…
Reference in a new issue