다음은 OCaml의 간단한 게임 루프입니다. 상태가 표시되고 입력이 수신되며 상태가 진행됩니다. 각 루프의 0.025 초 동안 스레드를 지연 시키면 초당 프레임 수는 40이됩니다.OCaml의 스레드 지연 및 키보드 이벤트
main.ml :이 예를 들어
let rec main (* state *) frame_time =
(* Display state here. *)
Input.get_input();
(* Advance state by one frame here. *)
(* If less than 25ms have passed, delay until they have. *)
if((Sys.time()) < (frame_time +. 0.025)) then
Thread.delay ((frame_time +. 0.025) -. (Sys.time()));
main (* next_state *) (Sys.time())
;;
let init =
Graphics.open_graph " 800x500";
let start_time = (Sys.time()) in
main (* start_state *) start_time
;;
는 get_input
기능은 단순히 창에 키 입력을 인쇄합니다.
input.ml : 쉽게 테스트
let get_input() =
let s = Graphics.wait_next_event
[Graphics.Key_pressed] in
if s.Graphics.keypressed then
Graphics.draw_char s.Graphics.key
;;
메이크 :
main: input.cmo main.cmo
ocamlfind ocamlc -o [email protected] unix.cma -thread threads.cma graphics.cma $^
main.cmo: main.ml
ocamlfind ocamlc -c $< -thread
input.cmo: input.ml
ocamlfind ocamlc -c $<
이 대부분 작동하지만, 키가 매우 빠르게 누를 때 프로그램이 오류와 충돌 :
Fatal error: exception Unix.Unix_error(2, "select", "")
나는 그것이 Thread.delay
과 (과) 관련이 있습니다. 이 문제의 원인은 무엇이며 지속적인 FPS를 달성하는 가장 좋은 방법은 무엇입니까?
Jeffrey가 모든 계정에서 정상입니다. Thread에 대한 정보를 sligthly 더 얻을 수 있습니다.지연 [here] (http://ocamlunix.forge.ocamlcore.org/threads.html#htoc63). –