में थ्रेड विलंब और कीबोर्ड ईवेंट ओकैमल में एक साधारण गेम लूप है। राज्य प्रदर्शित होता है, इनपुट प्राप्त होता है, और राज्य उन्नत है। प्रत्येक लूप 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
;;
Makefile:
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
के साथ करने के लिए। इस मुद्दे का कारण क्या है, और निरंतर एफपीएस प्राप्त करने का सबसे अच्छा तरीका क्या है?
जेफ़री सभी खातों पर सही है। आप थ्रेड के बारे में अधिक जानकारी प्राप्त कर सकते हैं।देरी [यहां] (http://ocamlunix.forge.ocamlcore.org/threads.html#htoc63)। –