Split SOL data structures into base, varying and rendering parts
[neverball] / ball / game_proxy.c
1 /*
2  * Copyright (C) 2003-2010 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdlib.h>
16
17 #include "game_proxy.h"
18 #include "queue.h"
19 #include "cmd.h"
20
21 static Queue cmd_queue;
22
23 /*
24  * Enqueue SRC in the game's command queue.
25  */
26 void game_proxy_enq(const union cmd *src)
27 {
28     union cmd *dst;
29
30     /*
31      * Create the queue.  This is done only once during the life time
32      * of the program.  For simplicity's sake, the queue is never
33      * destroyed.
34      */
35
36     if (!cmd_queue)
37         cmd_queue = queue_new();
38
39     /*
40      * Add a copy of the command to the end of the queue.
41      */
42
43     if ((dst = malloc(sizeof (*dst))))
44     {
45         *dst = *src;
46         queue_enq(cmd_queue, dst);
47     }
48 }
49
50 /*
51  * Dequeue and return the head element in the game's command queue.
52  * The element must be freed after use.
53  */
54 union cmd *game_proxy_deq(void)
55 {
56     return cmd_queue ? queue_deq(cmd_queue) : NULL;
57 }
58
59 /*
60  * Clear the entire queue.
61  */
62 void game_proxy_clr(void)
63 {
64     union cmd *cmdp;
65
66     while ((cmdp = game_proxy_deq()))
67         cmd_free(cmdp);
68 }