annotate examples/tank/tank_main.c @ 122:17e97e92b76e

Encapsulate X_MB_runtime_t and support X keyboard events.
author Thinker K.F. Li <thinker@branda.to>
date Mon, 15 Sep 2008 20:33:06 +0800
parents 5df7403b6fbc
children ba581d8a4b9b
rev   line source
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
1 #include <sys/time.h>
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
2 #include <mb/mb.h>
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
3 #include "svgs.h"
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
4
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
5 enum { MUD, ROC, BRI, BSH };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
6
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
7 static char map[12][16] = {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
8 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
9 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
10 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
11 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
12 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
13 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
14 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
15 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
16 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
17 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
18 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
19 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
20 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
21 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
22 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
23 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
24 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
25 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
26 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
27 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
28 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
29 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD},
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
30 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD,
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
31 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD}
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
32 };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
33
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
34 typedef struct _tank_rt tank_rt_t;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
35
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
36 struct _tank_rt {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
37 tank1_t *tank1;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
38 tank2_t *tank2;
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
39 int n_enemy;
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
40 tank_en_t *tank_enemies[10];
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
41 void *map[12][16];
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
42 X_MB_runtime_t *mb_rt;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
43 mb_progm_t *tank1_progm;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
44 };
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
45
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
46 #define CHANGE_POS(g, x, y) do { \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
47 (g)->root_coord->matrix[0] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
48 (g)->root_coord->matrix[2] = x; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
49 (g)->root_coord->matrix[4] = 1.0; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
50 (g)->root_coord->matrix[5] = y; \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
51 rdman_coord_changed(rdman, (g)->root_coord); \
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
52 } while(0)
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
53
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
54 void
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
55 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
56 redraw_man_t *rdman;
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
57 mb_tman_t *tman;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 mud_t *mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 brick_t *brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 rock_t *rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
61 bush_t *bush;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
62 mb_word_t *word;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
63 mb_timeval_t start, playing;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
64 mb_timeval_t mbtv;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65 int i, j;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
67 rdman = X_MB_rdman(mb_rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 tank_rt->mb_rt = mb_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 for(i = 0; i < 12; i++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 for(j = 0; j < 16; j++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 switch(map[i][j]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 case MUD:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 mud = mud_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 CHANGE_POS(mud, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 tank_rt->map[i][j] = (void *)mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 case BRI:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 brick = brick_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 CHANGE_POS(brick, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 tank_rt->map[i][j] = (void *)brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 case ROC:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 rock = rock_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 CHANGE_POS(rock, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 tank_rt->map[i][j] = (void *)rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 case BSH:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 bush = bush_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 CHANGE_POS(bush, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 tank_rt->map[i][j] = (void *)bush;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
93 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
94 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
95 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
96
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
97 tank_rt->tank1 = tank1_new(rdman);
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
98 CHANGE_POS(tank_rt->tank1, 5 * 50, 11 * 50);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
99 tank_rt->tank2 = tank2_new(rdman);
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
100 CHANGE_POS(tank_rt->tank2, 10 * 50, 11 * 50);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
101 for(i = 0; i < 3; i++) {
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
102 tank_rt->tank_enemies[i] = tank_en_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
103 CHANGE_POS(tank_rt->tank_enemies[i], (2 + i * 3) * 50, 0);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
104 }
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
105 tank_rt->n_enemy = i;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
106
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
107 tank_rt->tank1_progm = mb_progm_new(4, rdman);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
108
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
109 MB_TIMEVAL_SET(&start, 1, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
110 MB_TIMEVAL_SET(&playing, 3, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
111 word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
112
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
113 mb_shift_new(0, -150, tank_rt->tank1->root_coord, word);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
114 mb_shift_new(0, -150, tank_rt->tank2->root_coord, word);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
115
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
116 MB_TIMEVAL_SET(&start, 5, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
117 MB_TIMEVAL_SET(&playing, 3, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
118 word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
119
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
120 mb_shift_new(0, 150, tank_rt->tank1->root_coord, word);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
121 mb_shift_new(0, 150, tank_rt->tank2->root_coord, word);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
122
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
123 tman = X_MB_tman(mb_rt);
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
124 get_now(&mbtv);
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
125 mb_progm_start(tank_rt->tank1_progm, tman, &mbtv);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 int
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 main(int argc, char *const argv[]) {
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
130 X_MB_runtime_t *rt;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 tank_rt_t tank_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
133 rt = X_MB_new(":0.0", 800, 600);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
135 initial_tank(&tank_rt, rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
137 X_MB_handle_connection(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138
122
17e97e92b76e Encapsulate X_MB_runtime_t and support X keyboard events.
Thinker K.F. Li <thinker@branda.to>
parents: 120
diff changeset
139 X_MB_free(rt);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
140 }