88ca873e10ea3c58e1eae636d980ac8dbcb8d0fc
[rodin/chimara.git] / src / glk.c
1 #include <glib.h>
2 #include <gtk/gtk.h>
3
4 #include "glk.h"
5 #include "abort.h"
6
7 /**
8  * glk_exit:
9  * 
10  * Shuts down the Glk program. This function does not return.
11  *
12  * If you print some text to a window and then shut down your program, you can
13  * assume that the player will be able to read it.
14  *
15  * <note><para>
16  *  You should only shut down your program with glk_exit() or by returning from
17  *  your glk_main() function. If you call the ANSI <function>exit()</function> 
18  *  function, bad things may happen. This Glk library is designed for multiple 
19  *  sessions, for example, and you would be cutting off all the sessions instead
20  *  of just yours. You would also prevent final text from being visible to the 
21  *  player.
22  * </para></note>
23  */
24 void
25 glk_exit(void)
26 {
27         cleanup();
28         g_thread_exit(NULL);
29 }
30
31 /**
32  * glk_tick:
33  *
34  * Many platforms have some annoying thing that has to be done every so often,
35  * or the gnurrs come from the voodvork out and eat your computer.
36  * 
37  * Well, not really. But you should call glk_tick() every so often, just in
38  * case. It may be necessary to yield time to other applications in a
39  * cooperative-multitasking OS, or to check for player interrupts in an infinite
40  * loop.
41  * 
42  * This call is fast; in fact, on average, it does nothing at all. So you can
43  * call it often. (In a virtual machine interpreter, once per opcode is
44  * appropriate. In a program with lots of computation, pick a comparable rate.)
45  * 
46  * glk_tick() does not try to update the screen, or check for player input, or
47  * any other interface task. For that, you should call glk_select() or 
48  * glk_select_poll().
49  * 
50  * Basically, you must ensure there's some fixed upper bound on the amount of
51  * computation that can occur before a glk_tick() (or glk_select()) occurs. In a
52  * VM interpreter, where the VM code might contain an infinite loop, this is
53  * critical. In a C program, you can often eyeball it.
54  */
55 void
56 glk_tick()
57 {
58         check_for_abort();
59         
60         /* Do one iteration of the main loop if there are any events */
61         gdk_threads_enter();
62         if(gtk_events_pending())
63                 gtk_main_iteration();
64         gdk_threads_leave();
65 }