Update Position and Main modules.
This commit is contained in:
parent
953633b8ec
commit
9e602e666a
2 changed files with 50 additions and 18 deletions
37
Position.ml
37
Position.ml
|
@ -1,10 +1,31 @@
|
||||||
type t = {
|
(* vim: set ts=2 sw=2 et : *)
|
||||||
mutable x : int ;
|
type t = int * int
|
||||||
mutable y : int ;
|
|
||||||
}
|
|
||||||
|
|
||||||
let zero = {
|
let zero = (0, 0);;
|
||||||
x = 0 ;
|
|
||||||
y = 0 ;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
let ( + ) x y =
|
||||||
|
let (xa, xb) = x
|
||||||
|
and (ya, yb) = y
|
||||||
|
in (xa + ya, xb + yb)
|
||||||
|
;;
|
||||||
|
|
||||||
|
let ( - ) x y =
|
||||||
|
let (xa, xb) = x
|
||||||
|
and (ya, yb) = y
|
||||||
|
in (xa + ya, xb + yb)
|
||||||
|
;;
|
||||||
|
|
||||||
|
let ( * ) x y =
|
||||||
|
let (xa, xb) = x
|
||||||
|
and (ya, yb) = y
|
||||||
|
in (xa * ya, xb * yb)
|
||||||
|
;;
|
||||||
|
|
||||||
|
let dot x y =
|
||||||
|
let (xa, xb) = x
|
||||||
|
and (ya, yb) = y
|
||||||
|
in
|
||||||
|
let na = xa * yb
|
||||||
|
and nb = xb * ya
|
||||||
|
in ( na, nb )
|
||||||
|
;;
|
||||||
|
|
31
main.ml
31
main.ml
|
@ -20,10 +20,12 @@ type game_event_t =
|
||||||
|
|
||||||
|
|
||||||
let dispatch_status optstatus =
|
let dispatch_status optstatus =
|
||||||
let dispatch_keypress =
|
let dispatch_keypress key =
|
||||||
function
|
match key with
|
||||||
| 'q' -> Quit
|
| 'q' -> Quit
|
||||||
| _ -> NoEvent
|
| _ ->
|
||||||
|
Printf.printf "Unknown action for key (%c)\n" key;
|
||||||
|
NoEvent
|
||||||
in
|
in
|
||||||
match optstatus with
|
match optstatus with
|
||||||
| None -> (* only draw *)
|
| None -> (* only draw *)
|
||||||
|
@ -41,7 +43,7 @@ let e_send chan data =
|
||||||
Event.sync ( Event.send chan data )
|
Event.sync ( Event.send chan data )
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let ticker pfd_out req_channel notif_channel =
|
let tick_run pfd_out req_channel notif_channel =
|
||||||
let buf = String.make 1 ' ' in
|
let buf = String.make 1 ' ' in
|
||||||
while true do
|
while true do
|
||||||
let delay = e_receive req_channel in
|
let delay = e_receive req_channel in
|
||||||
|
@ -54,10 +56,10 @@ let ticker pfd_out req_channel notif_channel =
|
||||||
done
|
done
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let key_listener notif_channel =
|
let graphic_run listen_lst notif_channel =
|
||||||
while true do
|
while true do
|
||||||
let event =
|
let event =
|
||||||
Graphics.wait_next_event [Graphics.Key_pressed]
|
Graphics.wait_next_event listen_lst
|
||||||
in
|
in
|
||||||
ignore (e_send notif_channel (Graphics_status event));
|
ignore (e_send notif_channel (Graphics_status event));
|
||||||
done
|
done
|
||||||
|
@ -67,12 +69,13 @@ let game_loop () =
|
||||||
let ( pfd_out, pfd_in ) = Unix.pipe ()
|
let ( pfd_out, pfd_in ) = Unix.pipe ()
|
||||||
and tick_request_channel = Event.new_channel ()
|
and tick_request_channel = Event.new_channel ()
|
||||||
and tick_notification_channel = Event.new_channel ()
|
and tick_notification_channel = Event.new_channel ()
|
||||||
and key_notification_channel = Event.new_channel ()
|
and graphic_notification_channel = Event.new_channel ()
|
||||||
in
|
in
|
||||||
let tick_listener = ticker pfd_out tick_request_channel
|
let tick_listener = tick_run pfd_out tick_request_channel
|
||||||
|
and graphic_listener = graphic_run [Graphics.Key_pressed]
|
||||||
in
|
in
|
||||||
let thr_tick = Thread.create tick_listener tick_notification_channel
|
let thr_tick = Thread.create tick_listener tick_notification_channel
|
||||||
and thr_input = Thread.create key_listener key_notification_channel
|
and thr_input = Thread.create graphic_listener graphic_notification_channel
|
||||||
in
|
in
|
||||||
let ack () =
|
let ack () =
|
||||||
ignore ( Unix.write pfd_in "." 0 1 )
|
ignore ( Unix.write pfd_in "." 0 1 )
|
||||||
|
@ -80,7 +83,7 @@ let game_loop () =
|
||||||
let time_wait_next_event time lst =
|
let time_wait_next_event time lst =
|
||||||
e_send tick_request_channel time;
|
e_send tick_request_channel time;
|
||||||
match Event.select [Event.receive tick_notification_channel;
|
match Event.select [Event.receive tick_notification_channel;
|
||||||
Event.receive key_notification_channel]
|
Event.receive graphic_notification_channel]
|
||||||
with
|
with
|
||||||
| Tick -> None
|
| Tick -> None
|
||||||
| Graphics_status status ->
|
| Graphics_status status ->
|
||||||
|
@ -94,6 +97,14 @@ let game_loop () =
|
||||||
let opt_status = time_wait_next_event 0.05 [Graphics.Key_pressed]
|
let opt_status = time_wait_next_event 0.05 [Graphics.Key_pressed]
|
||||||
in
|
in
|
||||||
match dispatch_status opt_status with
|
match dispatch_status opt_status with
|
||||||
|
| MoveLeft -> print_string "Move Left...\n";
|
||||||
|
| MoveRight -> print_string "Move Right...\n";
|
||||||
|
| MoveUp -> print_string "Move up\n";
|
||||||
|
| MoveDown -> print_string "Move down\n";
|
||||||
|
| Action -> print_string "Action\n";
|
||||||
|
| ActionTwo -> print_string "ActionTwo\n";
|
||||||
|
| Help -> print_string "Help\n";
|
||||||
|
| NoEvent -> ();
|
||||||
| Quit ->
|
| Quit ->
|
||||||
continue := false ;
|
continue := false ;
|
||||||
print_string "Exiting...\n";
|
print_string "Exiting...\n";
|
||||||
|
|
Loading…
Reference in a new issue