Mercurial > MadButterfly
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 | 1 #include <sys/time.h> |
115 | 2 #include <mb/mb.h> |
3 #include "svgs.h" | |
4 | |
5 enum { MUD, ROC, BRI, BSH }; | |
6 | |
7 static char map[12][16] = { | |
8 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
9 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
10 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, | |
11 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, | |
12 { MUD, MUD, BRI, MUD, MUD, MUD, MUD, MUD, | |
13 MUD, MUD, MUD, BRI, MUD, MUD, BSH, MUD}, | |
14 { BRI, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
15 BRI, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
16 { MUD, MUD, BRI, MUD, BRI, BSH, BRI, BRI, | |
17 BRI, BRI, BSH, ROC, ROC, MUD, BRI, MUD}, | |
18 { MUD, BRI, BRI, MUD, BRI, MUD, BRI, MUD, | |
19 ROC, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, | |
20 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, | |
21 MUD, MUD, MUD, BRI, BRI, BRI, BRI, MUD}, | |
22 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
23 BSH, BRI, MUD, MUD, MUD, MUD, MUD, MUD}, | |
24 { MUD, BRI, MUD, MUD, MUD, MUD, MUD, MUD, | |
25 MUD, MUD, MUD, BRI, BRI, MUD, BRI, BRI}, | |
26 { MUD, BRI, MUD, BRI, BRI, MUD, BRI, BRI, | |
27 BRI, BRI, MUD, BRI, MUD, MUD, MUD, MUD}, | |
28 { MUD, BSH, MUD, BRI, MUD, MUD, BRI, MUD, | |
29 MUD, BRI, MUD, MUD, MUD, BRI, BRI, MUD}, | |
30 { MUD, MUD, MUD, MUD, MUD, MUD, BRI, MUD, | |
31 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} | |
32 }; | |
33 | |
34 typedef struct _tank_rt tank_rt_t; | |
35 | |
36 struct _tank_rt { | |
37 tank1_t *tank1; | |
38 tank2_t *tank2; | |
117 | 39 int n_enemy; |
40 tank_en_t *tank_enemies[10]; | |
115 | 41 void *map[12][16]; |
42 X_MB_runtime_t *mb_rt; | |
120 | 43 mb_progm_t *tank1_progm; |
115 | 44 }; |
45 | |
46 #define CHANGE_POS(g, x, y) do { \ | |
47 (g)->root_coord->matrix[0] = 1.0; \ | |
48 (g)->root_coord->matrix[2] = x; \ | |
49 (g)->root_coord->matrix[4] = 1.0; \ | |
50 (g)->root_coord->matrix[5] = y; \ | |
51 rdman_coord_changed(rdman, (g)->root_coord); \ | |
52 } while(0) | |
53 | |
54 void | |
55 initial_tank(tank_rt_t *tank_rt, X_MB_runtime_t *mb_rt) { | |
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 | 58 mud_t *mud; |
59 brick_t *brick; | |
60 rock_t *rock; | |
61 bush_t *bush; | |
120 | 62 mb_word_t *word; |
63 mb_timeval_t start, playing; | |
64 mb_timeval_t mbtv; | |
115 | 65 int i, j; |
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 | 68 |
69 tank_rt->mb_rt = mb_rt; | |
70 for(i = 0; i < 12; i++) { | |
71 for(j = 0; j < 16; j++) { | |
72 switch(map[i][j]) { | |
73 case MUD: | |
74 mud = mud_new(rdman); | |
75 CHANGE_POS(mud, j * 50, i * 50); | |
76 tank_rt->map[i][j] = (void *)mud; | |
77 break; | |
78 case BRI: | |
79 brick = brick_new(rdman); | |
80 CHANGE_POS(brick, j * 50, i * 50); | |
81 tank_rt->map[i][j] = (void *)brick; | |
82 break; | |
83 case ROC: | |
84 rock = rock_new(rdman); | |
85 CHANGE_POS(rock, j * 50, i * 50); | |
86 tank_rt->map[i][j] = (void *)rock; | |
87 break; | |
88 case BSH: | |
89 bush = bush_new(rdman); | |
90 CHANGE_POS(bush, j * 50, i * 50); | |
91 tank_rt->map[i][j] = (void *)bush; | |
92 break; | |
93 } | |
94 } | |
95 } | |
96 | |
97 tank_rt->tank1 = tank1_new(rdman); | |
117 | 98 CHANGE_POS(tank_rt->tank1, 5 * 50, 11 * 50); |
115 | 99 tank_rt->tank2 = tank2_new(rdman); |
117 | 100 CHANGE_POS(tank_rt->tank2, 10 * 50, 11 * 50); |
101 for(i = 0; i < 3; i++) { | |
102 tank_rt->tank_enemies[i] = tank_en_new(rdman); | |
103 CHANGE_POS(tank_rt->tank_enemies[i], (2 + i * 3) * 50, 0); | |
104 } | |
105 tank_rt->n_enemy = i; | |
120 | 106 |
107 tank_rt->tank1_progm = mb_progm_new(4, rdman); | |
108 | |
109 MB_TIMEVAL_SET(&start, 1, 0); | |
110 MB_TIMEVAL_SET(&playing, 3, 0); | |
111 word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing); | |
112 | |
113 mb_shift_new(0, -150, tank_rt->tank1->root_coord, word); | |
114 mb_shift_new(0, -150, tank_rt->tank2->root_coord, word); | |
115 | |
116 MB_TIMEVAL_SET(&start, 5, 0); | |
117 MB_TIMEVAL_SET(&playing, 3, 0); | |
118 word = mb_progm_next_word(tank_rt->tank1_progm, &start, &playing); | |
119 | |
120 mb_shift_new(0, 150, tank_rt->tank1->root_coord, word); | |
121 mb_shift_new(0, 150, tank_rt->tank2->root_coord, word); | |
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 | 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 | 126 } |
127 | |
128 int | |
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 | 131 tank_rt_t tank_rt; |
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 | 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 | 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 | 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 | 140 } |