comparison examples/tank/tank_main.c @ 190:0a924eb9ccab

Group and document code of example tank.
author Thinker K.F. Li <thinker@branda.to>
date Tue, 18 Nov 2008 13:25:52 +0800
parents 257af0ed5852
children 54fdc2a65242
comparison
equal deleted inserted replaced
189:257af0ed5852 190:0a924eb9ccab
3 #include <string.h> 3 #include <string.h>
4 #include <mb.h> 4 #include <mb.h>
5 #include <mb_tools.h> 5 #include <mb_tools.h>
6 #include "svgs.h" 6 #include "svgs.h"
7 7
8 /*! \defgroup tank Example Tank
9 * @{
10 */
11 /*! \brief Tile types in a map. */
8 enum { MUD, ROC, BRI, BSH }; 12 enum { MUD, ROC, BRI, BSH };
9 13
14 /*! \brief Map of the game. */
10 static char map[12][16] = { 15 static char map[12][16] = {
11 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD, 16 { MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD,
12 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD}, 17 MUD, MUD, MUD, MUD, MUD, MUD, MUD, MUD},
13 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC, 18 { MUD, ROC, ROC, ROC, MUD, BSH, BSH, ROC,
14 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD}, 19 BSH, ROC, MUD, BSH, BSH, ROC, ROC, MUD},
34 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD} 39 MUD, BRI, MUD, BRI, MUD, MUD, MUD, MUD}
35 }; 40 };
36 41
37 #define MAP_W 16 42 #define MAP_W 16
38 #define MAP_H 12 43 #define MAP_H 12
39 44 /* @} */
40 /*! \defgroup tank_elf Tank Elf 45
41 * \brief Tank elf module provides control functions of tanks in game. 46 /*! \defgroup bullet_elf Bullet Elf
47 * \ingroup tank
42 * @{ 48 * @{
49 */
50 /*! \brief Information about bullet elf
43 */ 51 */
44 struct _tank_bullet { 52 struct _tank_bullet {
45 redraw_man_t *rdman; 53 redraw_man_t *rdman;
46 coord_t *coord_pos; 54 coord_t *coord_pos;
47 coord_t *coord_rot; 55 coord_t *coord_rot;
53 observer_t *ob_redraw; 61 observer_t *ob_redraw;
54 mb_timer_t *hit_tmr; 62 mb_timer_t *hit_tmr;
55 mb_tman_t *tman; 63 mb_tman_t *tman;
56 }; 64 };
57 typedef struct _tank_bullet tank_bullet_t; 65 typedef struct _tank_bullet tank_bullet_t;
66 /*! \brief The direction a bullet is going.
67 */
58 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT }; 68 enum { BU_UP = 0, BU_RIGHT, BU_DOWN, BU_LEFT };
59 69 /* @} */
70
71 /*! \defgroup tank_elf Tank Elf
72 * \brief Tank elf module provides control functions of tanks in game.
73 * \ingroup tank
74 * @{
75 */
76 /*! \brief Information about a tank elf. */
60 struct _tank { 77 struct _tank {
61 coord_t *coord_pos; /*!< \brief coordinate for position */ 78 coord_t *coord_pos; /*!< \brief coordinate for position */
62 coord_t *coord_rot; /*!< \brief coordinate for rotation */ 79 coord_t *coord_rot; /*!< \brief coordinate for rotation */
63 coord_t *coord_center; 80 coord_t *coord_center;
64 int map_x, map_y; 81 int map_x, map_y;
74 91
75 /* @} */ 92 /* @} */
76 93
77 typedef struct _tank_rt tank_rt_t; 94 typedef struct _tank_rt tank_rt_t;
78 95
96 /*! \brief Runtime information for tank, this game/example.
97 */
79 struct _tank_rt { 98 struct _tank_rt {
80 tank_t *tank1; 99 tank_t *tank1;
81 tank1_t *tank1_o; 100 tank1_t *tank1_o;
82 tank_t *tank2; 101 tank_t *tank2;
83 tank2_t *tank2_o; 102 tank2_t *tank2_o;
254 mb_progm_start(progm, tman, &now); 273 mb_progm_start(progm, tman, &now);
255 } 274 }
256 275
257 /* @} */ 276 /* @} */
258 277
278 /*! \ingroup bullet_elf
279 * @{
280 */
259 /*! \brief Make coord objects for bullet elfs. */ 281 /*! \brief Make coord objects for bullet elfs. */
260 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, 282 static void make_bullet_elf_coords(redraw_man_t *rdman, coord_t **coord_pos,
261 coord_t **coord_rot, 283 coord_t **coord_rot,
262 coord_t **coord_center) { 284 coord_t **coord_center) {
263 coord_t *coord_back; 285 coord_t *coord_back;
374 /*! \todo Remove bang and program when program stops. */ 396 /*! \todo Remove bang and program when program stops. */
375 get_now(&now); 397 get_now(&now);
376 mb_progm_start(progm, tman, &now); 398 mb_progm_start(progm, tman, &now);
377 } 399 }
378 400
401 /*! \brief To check if a bullet hits walls or tanks. */
379 static void bullet_hit_chk(const mb_timeval_t *tmo, 402 static void bullet_hit_chk(const mb_timeval_t *tmo,
380 const mb_timeval_t *now, 403 const mb_timeval_t *now,
381 void *arg) { 404 void *arg) {
382 tank_t *tank = (tank_t *)arg; 405 tank_t *tank = (tank_t *)arg;
383 tank_rt_t *tank_rt = tank->tank_rt; 406 tank_rt_t *tank_rt = tank->tank_rt;
453 MB_TIMEVAL_ADD(&next, now); 476 MB_TIMEVAL_ADD(&next, now);
454 bullet->hit_tmr = mb_tman_timeout(bullet->tman, &next, 477 bullet->hit_tmr = mb_tman_timeout(bullet->tman, &next,
455 bullet_hit_chk, arg); 478 bullet_hit_chk, arg);
456 } 479 }
457 480
481 /*! \brief To fire a bullet for a tank. */
458 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) { 482 static void tank_fire_bullet(tank_rt_t *tank_rt, tank_t *tank) {
459 X_MB_runtime_t *xmb_rt; 483 X_MB_runtime_t *xmb_rt;
460 redraw_man_t *rdman; 484 redraw_man_t *rdman;
461 int map_x, map_y; 485 int map_x, map_y;
462 int shift_x, shift_y; 486 int shift_x, shift_y;
532 MB_TIMEVAL_SET(&next, 0, 100000); 556 MB_TIMEVAL_SET(&next, 0, 100000);
533 MB_TIMEVAL_ADD(&next, &now); 557 MB_TIMEVAL_ADD(&next, &now);
534 bullet->hit_tmr = mb_tman_timeout(tman, &next, bullet_hit_chk, tank); 558 bullet->hit_tmr = mb_tman_timeout(tman, &next, bullet_hit_chk, tank);
535 } 559 }
536 560
561 /* @} */
562
563 /*! \ingroup tank
564 * @{
565 */
537 #define CHANGE_POS(g, x, y) do { \ 566 #define CHANGE_POS(g, x, y) do { \
538 (g)->root_coord->matrix[0] = 1.0; \ 567 (g)->root_coord->matrix[0] = 1.0; \
539 (g)->root_coord->matrix[2] = x; \ 568 (g)->root_coord->matrix[2] = x; \
540 (g)->root_coord->matrix[4] = 1.0; \ 569 (g)->root_coord->matrix[4] = 1.0; \
541 (g)->root_coord->matrix[5] = y; \ 570 (g)->root_coord->matrix[5] = y; \
595 624
596 tank_rt->kb_observer = 625 tank_rt->kb_observer =
597 subject_add_observer(factory, kbevents, keyboard_handler, tank_rt); 626 subject_add_observer(factory, kbevents, keyboard_handler, tank_rt);
598 } 627 }
599 628
600 /*! \brief Make coord objects for elfs (tanks). */ 629 /*! \brief Make coord objects to decorate elfs (tanks).
630 *
631 * These coords are used to shift (move) and rotate decorated graphic.
632 * The coords can easy work of programmer to manipulate geometry of
633 * decorated graphic.
634 */
601 static void make_elf_coords(redraw_man_t *rdman, coord_t **coord_pos, 635 static void make_elf_coords(redraw_man_t *rdman, coord_t **coord_pos,
602 coord_t **coord_rot, coord_t **coord_center) { 636 coord_t **coord_rot, coord_t **coord_center) {
603 coord_t *coord_back; 637 coord_t *coord_back;
604 638
605 *coord_pos = rdman_coord_new(rdman, rdman->root_coord); 639 *coord_pos = rdman_coord_new(rdman, rdman->root_coord);
700 734
701 X_MB_handle_connection(rt); 735 X_MB_handle_connection(rt);
702 736
703 X_MB_free(rt); 737 X_MB_free(rt);
704 } 738 }
739
740 /* @} */