xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 820a9f16bf4d534b1dd5914f3ec130b7c9b9c8bb
parent 3ef1e747d8e742f2f63e31183f52cdcec5ba3d50
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 23 Dec 2022 10:56:49 +0900

refactor

Diffstat:
Mex3/Makefile | 2+-
Mex3/ex3.c | 76+++++++++++++++++++++++++++++++++++++++-------------------------------------
2 files changed, 40 insertions(+), 38 deletions(-)

diff --git a/ex3/Makefile b/ex3/Makefile @@ -1,5 +1,5 @@ INCS=-I/usr/X11R6/include -CFLAGS=-Wall -Wextra -std=c11 +CFLAGS=-Wall -W -Wextra -Wpointer-arith -Wbad-function-cast -std=c11 LIBS=-L/usr/X11R6/lib -lX11 -lXext -lm IN=*.c OUT=ex3 diff --git a/ex3/ex3.c b/ex3/ex3.c @@ -16,17 +16,17 @@ */ -enum Keys { - Key_D, - Key_S, - Key_A, - Key_W, - Key_Space, - Num_Key, //number of keys in this enum +enum keys { + KEY_D, + KEY_S, + KEY_A, + KEY_W, + KEY_SPACE, + NUM_KEY, //number of keys in this enum }; -enum Key_State { - Key_Up, - Key_Down, +enum key_state { + KEY_UP, + KEY_DOWN, }; /* variables */ @@ -35,7 +35,7 @@ Window window; unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; GC gc; Atom wm_delete_window; -int key_state[Num_Key]; +int key_state[NUM_KEY]; int quit; float px = 200, py = 200; float vx = 0, vy = 0; @@ -83,8 +83,6 @@ receive_events(void) XEvent event; XWindowAttributes wattr; - - while (XPending(display) > 0) { XNextEvent(display, &event); switch (event.type) { @@ -99,16 +97,16 @@ receive_events(void) quit = 1; break; case 'd': - key_state[Key_D] = Key_Down; + key_state[KEY_D] = KEY_DOWN; break; case 'a': - key_state[Key_A] = Key_Down; + key_state[KEY_A] = KEY_DOWN; break; case 'w': - key_state[Key_W] = Key_Down; + key_state[KEY_W] = KEY_DOWN; break; case 's': - key_state[Key_S] = Key_Down; + key_state[KEY_S] = KEY_DOWN; break; default: break; @@ -117,16 +115,16 @@ receive_events(void) case KeyRelease: { switch (XLookupKeysym(&event.xkey, 0)) { case 'd': - key_state[Key_D] = Key_Up; + key_state[KEY_D] = KEY_UP; break; case 'a': - key_state[Key_A] = Key_Up; + key_state[KEY_A] = KEY_UP; break; case 'w': - key_state[Key_W] = Key_Up; + key_state[KEY_W] = KEY_UP; break; case 's': - key_state[Key_S] = Key_Up; + key_state[KEY_S] = KEY_UP; break; default: break; @@ -147,15 +145,30 @@ void handle_inputs(void) { vx = vy = 0; - if (key_state[Key_D] == Key_Down) + if (key_state[KEY_D] == KEY_DOWN) vx += 300; - if (key_state[Key_A] == Key_Down) + if (key_state[KEY_A] == KEY_DOWN) vx += -300; - if (key_state[Key_S] == Key_Down) + if (key_state[KEY_S] == KEY_DOWN) vy += 300; - if (key_state[Key_W] == Key_Down) + if (key_state[KEY_W] == KEY_DOWN) vy += -300; +} +void +next_tick(long ndt) // nano second +{ + px = px + vx * ndt / 1000 / 1000 / 1000; + py = py + vy * ndt / 1000 / 1000 / 1000; + // bind within the window + if (px < 0) + px = 0; + if (win_width < px + width) + px = win_width - width; + if (py < 0) + py = 0; + if (win_height < py + height) + py = win_height - height; } void @@ -191,7 +204,6 @@ main(void) } #ifdef COUNT_FPS // count fps. - // simple but not precise. fps_count++; if (t1 < t0){ printf("fps: %u\n", fps_count); @@ -202,17 +214,7 @@ main(void) clock_gettime(CLOCK_MONOTONIC, &ts); t0 = ts.tv_nsec; - px = px + vx * dt / 1000 / 1000 / 1000; - py = py + vy * dt / 1000 / 1000 / 1000; - // bind within the window - if (px < 0) - px = 0; - if (win_width < px + width) - px = win_width - width; - if (py < 0) - py = 0; - if (win_height < py + height) - py = win_height - height; + next_tick(1000 * 1000 * 1000 / FPS); XClearArea(display, window, 0, 0, // position