Evolution of main_loop().

This commit is contained in:
Glenn Y. Rolland 2008-02-20 01:24:56 +01:00
parent 2eaecd6bd6
commit 9a5ad6c8e8
4 changed files with 48 additions and 3 deletions

35
Game.ml Normal file
View 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
View file

@ -0,0 +1,4 @@
let handle_event ev =
ignore ev
;;

4
Mouse.ml Normal file
View file

@ -0,0 +1,4 @@
let handle_event ev =
ignore ev
;;

View file

@ -38,14 +38,13 @@ let string_of_keyboard_event event =
let rec event_loop () =
print_endline "Event_loop...";
Sdltimer.delay 20;
let event_opt = Sdlevent.poll ()
in
let match_event event = (
match event with
| Sdlevent.KEYDOWN {Sdlevent.keysym=Sdlkey.KEY_ESCAPE} ->
print_endline "You pressed escape! The fun is over now."
| Sdlevent.KEYDOWN event ->
Keyboard.handle_event event
let keystr = string_of_keyboard_event event
in
print_endline ("You pressed " ^ keystr);
@ -54,6 +53,8 @@ let rec event_loop () =
| _ ->
event_loop ()
) in
let event_opt = Sdlevent.poll ()
in
match event_opt with
| None -> event_loop ()
| Some event -> match_event event;
@ -66,7 +67,8 @@ let game_loop ~screen =
in
Sdlvideo.blit_surface ~src:image ~src_rect:image_from ~dst:screen ~dst_rect:image_to ();
Sdlvideo.flip screen;
event_loop ();
let action_fun = event_loop ();
game_loop screen
;;
let main () =