commit ea32f24469e433989224246640e2550614c08c5f
parent 24023f9a5f4d742e6ebb574c3bb04da7ac5742b3
Author: Matsuda Kenji <info@mtkn.jp>
Date: Thu, 12 Jan 2023 10:46:01 +0900
add debug messages
Diffstat:
M | ex9/main.c | | | 73 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------- |
1 file changed, 58 insertions(+), 15 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -24,11 +24,12 @@ enum {
};
enum keys {
+ KEY_Q,
+ KEY_I,
KEY_D,
KEY_S,
KEY_A,
KEY_W,
- KEY_Q,
KEY_SPACE,
NUM_KEY, //number of keys in this enum
};
@@ -43,6 +44,13 @@ enum next_menu {
GAME_CLEAR,
QUIT,
};
+enum debug_msg {
+ DFPS,
+ DONFLOOR,
+ DPOS,
+ NDMSG,
+ DMAXLEN = 128,
+};
/* variables */
enum next_menu next_menu = START_MENU;
@@ -57,7 +65,7 @@ void game_over(void);
void game_clear(void);
/* events */
void receive_events(enum key_state[]);
-void handle_inputs(enum key_state[], Object *);
+void handle_inputs(enum key_state[], Object *, int *);
/* collision functions */
void col_pf(Object *, Object *); // player-flag
void col_pb(Object *, Object *); // player-block
@@ -125,14 +133,17 @@ game_play(void)
{
enum key_state key_state[NUM_KEY];
long t0, t1, dt;
-#ifdef COUNT_FPS
- int fps_count = 0;
-#endif
struct timespec ts;
List *ol; // block list
Object *player;
List *olc; // cursor used to scan ol
int scroll_dst = 0;
+ int scroll_margin = WIN_WIDTH * 2 / 5;
+
+ int debug = 0;
+ int frame_count = 0;
+ int cur_fps = 0;
+ char debug_msg[NDMSG][DMAXLEN] = {0};
ol = NULL;
@@ -188,7 +199,11 @@ game_play(void)
while (next_menu == GAME_PLAY){
receive_events(key_state);
- handle_inputs(key_state, player);
+ handle_inputs(key_state, player, &debug);
+ if (debug) {
+ snprintf(debug_msg[DONFLOOR], DMAXLEN,
+ "on_floor: %d", player->on_floor);
+ }
clock_gettime(CLOCK_MONOTONIC, &ts);
t0 = ts.tv_nsec;
@@ -213,7 +228,6 @@ game_play(void)
}
// scrolling
- int scroll_margin = WIN_WIDTH * 2 / 5;
if (player->p.x - scroll_dst > WIN_WIDTH - scroll_margin) {
scroll_dst = player->p.x - WIN_WIDTH + scroll_margin;
if (scroll_dst > WORLD_WIDTH * BLOCK_SIZE - WIN_WIDTH)
@@ -239,20 +253,33 @@ game_play(void)
dt = t1 > t0 ? t1 - t0 : t1 - t0 + 1e9;
}
-#ifdef COUNT_FPS
// count fps.
- fps_count++;
- if (t1 < t0){
- printf("fps: %u\n", fps_count);
- fps_count = 0;
+ if (debug) {
+ frame_count++;
+ if (t1 < t0){
+ cur_fps = frame_count;
+ snprintf(debug_msg[DFPS], DMAXLEN, "fps: %d", cur_fps);
+ frame_count = 0;
+ }
+ }
+
+ if (debug) {
+ snprintf(debug_msg[DPOS], DMAXLEN,
+ "pos: (%3.2f, %3.2f)", player->p.x, player->p.y);
}
-#endif
x_clear_area();
for (olc = ol; olc != NULL; olc = olc->next) {
draw_object_scroll((Object *)olc->item, scroll_dst);
}
+ if (debug) {
+ for (int i = 0; i < NDMSG; i++) {
+ x_draw_string(0xFFFFFF,
+ 10, (i + 1) * 12,
+ debug_msg[i], strlen(debug_msg[i]));
+ }
+ }
}
lfreei(ol, (void (*)(void *))&free_object);
}
@@ -310,6 +337,9 @@ receive_events(enum key_state key_state[])
case 'q':
key_state[KEY_Q] = KEY_DOWN;
break;
+ case 'i':
+ key_state[KEY_I] = KEY_DOWN;
+ break;
case 'd':
key_state[KEY_D] = KEY_DOWN;
break;
@@ -334,6 +364,9 @@ receive_events(enum key_state key_state[])
case 'q':
key_state[KEY_Q] = KEY_UP;
break;
+ case 'i':
+ key_state[KEY_I] = KEY_UP;
+ break;
case 'd':
key_state[KEY_D] = KEY_UP;
break;
@@ -358,12 +391,22 @@ receive_events(enum key_state key_state[])
}
void
-handle_inputs(enum key_state key_state[], Object *player)
+handle_inputs(enum key_state key_state[], Object *player, int *debug)
{
- if (key_state[KEY_Q] == KEY_DOWN){
+ static int key_i_before = KEY_UP;
+
+ if (key_state[KEY_Q] == KEY_DOWN) {
next_menu = GAME_OVER;
return;
}
+ if (key_state[KEY_I] == KEY_DOWN && key_i_before == KEY_UP) {
+ *debug = !*debug;
+ key_i_before = KEY_DOWN;
+ }
+ if (key_state[KEY_I] == KEY_UP && key_i_before == KEY_DOWN) {
+ key_i_before = KEY_UP;
+ }
+
if (key_state[KEY_D] == KEY_DOWN && key_state[KEY_A] != KEY_DOWN) {
if (player->v.x > 0) {
player->a.x = 500;