ball/set: add set_free as a counterpart of set_load
[neverball] / ball / game_proxy.c
1 #include <stdlib.h>
2
3 #include "game_proxy.h"
4 #include "queue.h"
5 #include "cmd.h"
6
7 static Queue cmd_queue;
8
9 /*
10  * Enqueue SRC in the game's command queue.
11  */
12 void game_proxy_enq(const union cmd *src)
13 {
14     union cmd *dst;
15
16     /*
17      * Create the queue.  This is done only once during the life time
18      * of the program.  For simplicity's sake, the queue is never
19      * destroyed.
20      */
21
22     if (!cmd_queue)
23         cmd_queue = queue_new();
24
25     /*
26      * Add a copy of the command to the end of the queue.
27      */
28
29     if ((dst = malloc(sizeof (*dst))))
30     {
31         *dst = *src;
32         queue_enq(cmd_queue, dst);
33     }
34 }
35
36 /*
37  * Dequeue and return the head element in the game's command queue.
38  * The element must be freed after use.
39  */
40 union cmd *game_proxy_deq(void)
41 {
42     return cmd_queue ? queue_deq(cmd_queue) : NULL;
43 }
44
45 /*
46  * Clear the entire queue.
47  */
48 void game_proxy_clr(void)
49 {
50     union cmd *cmdp;
51
52     while ((cmdp = game_proxy_deq()))
53         free(cmdp);
54 }