annotate examples/tank/tank_main.c @ 120:5df7403b6fbc

Fix bug of get_now()
author Thinker K.F. Li <thinker@branda.to>
date Sun, 14 Sep 2008 23:05:30 +0800
parents e4e47d2cdbcd
children 17e97e92b76e
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;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
57 mud_t *mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
58 brick_t *brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
59 rock_t *rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
60 bush_t *bush;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
61 mb_word_t *word;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
62 mb_timeval_t start, playing;
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
63 mb_timeval_t mbtv;
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
64 int i, j;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
65
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
66 rdman = mb_rt->rdman;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
67
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
68 tank_rt->mb_rt = mb_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
69 for(i = 0; i < 12; i++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
70 for(j = 0; j < 16; j++) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
71 switch(map[i][j]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
72 case MUD:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
73 mud = mud_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
74 CHANGE_POS(mud, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
75 tank_rt->map[i][j] = (void *)mud;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
76 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
77 case BRI:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
78 brick = brick_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
79 CHANGE_POS(brick, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
80 tank_rt->map[i][j] = (void *)brick;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
81 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
82 case ROC:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
83 rock = rock_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
84 CHANGE_POS(rock, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
85 tank_rt->map[i][j] = (void *)rock;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
86 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
87 case BSH:
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
88 bush = bush_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
89 CHANGE_POS(bush, j * 50, i * 50);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
90 tank_rt->map[i][j] = (void *)bush;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
91 break;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
92 }
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 tank_rt->tank1 = tank1_new(rdman);
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
97 CHANGE_POS(tank_rt->tank1, 5 * 50, 11 * 50);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
98 tank_rt->tank2 = tank2_new(rdman);
117
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
99 CHANGE_POS(tank_rt->tank2, 10 * 50, 11 * 50);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
100 for(i = 0; i < 3; i++) {
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
101 tank_rt->tank_enemies[i] = tank_en_new(rdman);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
102 CHANGE_POS(tank_rt->tank_enemies[i], (2 + i * 3) * 50, 0);
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
103 }
Thinker K.F. Li <thinker@branda.to>
parents: 115
diff changeset
104 tank_rt->n_enemy = i;
120
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
105
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
106 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
107
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
108 MB_TIMEVAL_SET(&start, 1, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
109 MB_TIMEVAL_SET(&playing, 3, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
110 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
111
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
112 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
113 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
114
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
115 MB_TIMEVAL_SET(&start, 5, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
116 MB_TIMEVAL_SET(&playing, 3, 0);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
117 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
118
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
119 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
120 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
121
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
122 get_now(&mbtv);
5df7403b6fbc Fix bug of get_now()
Thinker K.F. Li <thinker@branda.to>
parents: 117
diff changeset
123 mb_progm_start(tank_rt->tank1_progm, mb_rt->tman, &mbtv);
115
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
124 }
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
125
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
126 int
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
127 main(int argc, char *const argv[]) {
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
128 X_MB_runtime_t rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
129 tank_rt_t tank_rt;
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
130
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
131 X_MB_init(":0.0", 800, 600, &rt);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
132
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
133 initial_tank(&tank_rt, &rt);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
134
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
135 X_MB_handle_connection(&rt);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
136
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
137 X_MB_destroy(&rt);
Thinker K.F. Li <thinker@branda.to>
parents:
diff changeset
138 }