xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 7f1318fb2cb94a2c782910b1d6c7e7ff3ccf8068
parent 5bd341966a2d5705f2b58898183fb54f18e67399
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Fri, 23 Dec 2022 17:07:07 +0900

add another object

Diffstat:
Mex4/ex4.c | 77++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 46 insertions(+), 31 deletions(-)

diff --git a/ex4/ex4.c b/ex4/ex4.c @@ -8,9 +8,10 @@ #include <X11/Xlib.h> /* macros */ -#define INIT_WIDTH 800 -#define INIT_HEIGHT 600 -#define FPS 60 +#define INIT_WIDTH (800) +#define INIT_HEIGHT (600) +#define FPS (60) +#define NUM_SQUARE (2) /* #define COUNT_FPS @@ -36,16 +37,20 @@ enum next_menu { QUIT, }; +struct square { + float px, py; + float vx, vy; + int w, h; +}; + /* variables */ -Display *display; -Window window; -unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; -GC gc; -Atom wm_delete_window; -float px = 200, py = 200; -float vx = 0, vy = 0; -int width = 40, height = 40; -int next_menu = START_MENU; +Display *display; +Window window; +unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT; +GC gc; +Atom wm_delete_window; +struct square square[NUM_SQUARE]; +int next_menu = START_MENU; /* function prototypes */ @@ -217,31 +222,31 @@ handle_inputs(int key_state[]) next_menu = GAME_OVER; return; } - vx = vy = 0; + square[0].vx = square[0].vy = 0; if (key_state[KEY_D] == KEY_DOWN) - vx += 300; + square[0].vx += 300; if (key_state[KEY_A] == KEY_DOWN) - vx += -300; + square[0].vx += -300; if (key_state[KEY_S] == KEY_DOWN) - vy += 300; + square[0].vy += 300; if (key_state[KEY_W] == KEY_DOWN) - vy += -300; + square[0].vy += -300; } void next_tick(long ndt) // nano second { - px = px + vx * ndt / 1000 / 1000 / 1000; - py = py + vy * ndt / 1000 / 1000 / 1000; + square[0].px = square[0].px + square[0].vx * ndt / 1000 / 1000 / 1000; + square[0].py = square[0].py + square[0].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; + if (square[0].px < 0) + square[0].px = 0; + if (win_width < square[0].px + square[0].w) + square[0].px = win_width - square[0].w; + if (square[0].py < 0) + square[0].py = 0; + if (win_height < square[0].py + square[0].h) + square[0].py = win_height - square[0].h; } void @@ -254,6 +259,13 @@ game_play(void) #endif struct timespec ts; + for(int i = 0; i < NUM_SQUARE; i++){ + square[i].px = 100 * (i + 1); + square[i].py = 100; + square[i].vx = square[i].vy = 0; + square[i].w = square[i].h = 20; + } + while (next_menu == GAME_PLAY){ clock_gettime(CLOCK_MONOTONIC, &ts); t0 = ts.tv_nsec; @@ -287,16 +299,19 @@ game_play(void) 0, 0, // position win_width, win_height, // width and height False); - XFillRectangle(display, window, gc, - px, py, // position - width, height); // width and height + for (int i = 0; i < NUM_SQUARE; i++) { + XSetForeground(display, gc, 0x000FFF << i * 12); + XFillRectangle(display, window, gc, + square[i].px, square[i].py, // position + square[i].w, square[i].h); // width and height + } } + XSetForeground(display, gc, 0x00FFFF); } void game_over(void) { - XEvent event; char *menu_char = "GAME OVER"; XClearArea(display, window,