xlib_playground

Xlib playground for experiments.
Log | Files | Refs

commit 9d1848f4cfb5963a20608b2499b1267313715e9d
parent 6b31a9d0f5cc1e4cef4336e4cd57e4e4a05fda70
Author: Matsuda Kenji <info@mtkn.jp>
Date:   Thu, 29 Dec 2022 10:54:04 +0900

make player constraint in the world, not in the window

Diffstat:
Mex7/ex7.c | 58+++++++++++++++++++++++-----------------------------------
1 file changed, 23 insertions(+), 35 deletions(-)

diff --git a/ex7/ex7.c b/ex7/ex7.c @@ -148,17 +148,15 @@ start_menu(void) { char *menu_char_q = "press q to quit."; char *menu_char_s = "press <space> to start."; - int win_width, win_height; - x_get_win_wh(&win_width, &win_height); x_clear_area(); x_draw_string(0x00FFFF, - win_width/2 - 10 * strlen(menu_char_q)/2, - win_height/2, + WORLD_WIDTH * BLOCK_SIZE/2 - 10 * strlen(menu_char_q)/2, + WORLD_HEIGHT * BLOCK_SIZE/2, menu_char_q, strlen(menu_char_q)); x_draw_string(0x00FFFF, - win_width/2 - 10 * strlen(menu_char_s)/2, - win_height/2 + 20, + WORLD_WIDTH * BLOCK_SIZE/2 - 10 * strlen(menu_char_s)/2, + WORLD_HEIGHT * BLOCK_SIZE/2 + 20, menu_char_s, strlen(menu_char_s)); while (next_menu == START_MENU) { @@ -168,12 +166,12 @@ start_menu(void) switch (event) { case XEXPOSE: x_draw_string(0x00FFFF, - win_width/2 - 10 * strlen(menu_char_q)/2, - win_height/2, + WORLD_WIDTH * BLOCK_SIZE/2 - 10 * strlen(menu_char_q)/2, + WORLD_HEIGHT * BLOCK_SIZE/2, menu_char_q, strlen(menu_char_q)); x_draw_string(0x00FFFF, - win_width/2 - 10 * strlen(menu_char_s)/2, - win_height/2 + 20, + WORLD_WIDTH * BLOCK_SIZE/2 - 10 * strlen(menu_char_s)/2, + WORLD_HEIGHT * BLOCK_SIZE/2 + 20, menu_char_s, strlen(menu_char_s)); break; @@ -302,9 +300,6 @@ handle_inputs(enum key_state key_state[]) void rect_next_tick(struct rect *s, long ndt) // nano second { - int win_width, win_height; - x_get_win_wh(&win_width, &win_height); - s->ppx = s->px; s->ppy = s->py; s->vx += s->ax * ndt / 1000 / 1000 / 1000; @@ -312,13 +307,13 @@ rect_next_tick(struct rect *s, long ndt) // nano second s->px += s->vx * ndt / 1000 / 1000 / 1000; s->py += s->vy * ndt / 1000 / 1000 / 1000; - // bind within the window + // bind within the world if (s->px < 0) { s->px = 0; //s->vx *= -1; } - if (win_width < s->px + s->w) { - s->px = win_width - s->w; + if (WORLD_WIDTH * BLOCK_SIZE < s->px + s->w) { + s->px = WORLD_WIDTH * BLOCK_SIZE - s->w; //s->vx *= -1; } /* @@ -326,21 +321,19 @@ rect_next_tick(struct rect *s, long ndt) // nano second s->py = 0; s->vy *= -1; } - if (win_height < s->py + s->h) { - s->py = win_height - s->h; + if (WORLD_HEIGHT * BLOCK_SIZE < s->py + s->h) { + s->py = WORLD_HEIGHT * BLOCK_SIZE - s->h; s->vy *= -1; } */ // game over when fall out of the screen - if (s->py > win_height) + if (s->py > WORLD_HEIGHT * BLOCK_SIZE) next_menu = GAME_OVER; } void circle_next_tick(struct circle *c, long ndt) { - int win_width, win_height; - x_get_win_wh(&win_width, &win_height); c->ppx = c->px; c->ppy = c->py; @@ -349,21 +342,21 @@ circle_next_tick(struct circle *c, long ndt) c->px += c->vx * ndt / 1000 / 1000 / 1000; c->py += c->vy * ndt / 1000 / 1000 / 1000; - // bind within the window + // bind within the world if (c->px - c->r < 0) { c->px = c->r; c->vx *= -1; } - if (win_width < c->px + c->r) { - c->px = win_width - c->r; + if (WORLD_WIDTH * BLOCK_SIZE < c->px + c->r) { + c->px = WORLD_WIDTH * BLOCK_SIZE - c->r; c->vx *= -1; } if (c->py - c->r < 0) { c->py = c->r; c->vy *= -1; } - if (win_height < c->py + c->r) { - c->py = win_height - c->r; + if (WORLD_HEIGHT * BLOCK_SIZE < c->py + c->r) { + c->py = WORLD_HEIGHT * BLOCK_SIZE - c->r; c->vy *= -1; } } @@ -659,14 +652,11 @@ game_play(void) void game_over(void) { - int win_width, win_height; - x_get_win_wh(&win_width, &win_height); - char *menu_char = "GAME OVER"; x_clear_area(); x_draw_string(0x00FFFF, - win_width/2 - 10 * strlen(menu_char)/2, win_height/2, + WORLD_WIDTH * BLOCK_SIZE/2 - 10 * strlen(menu_char)/2, WORLD_HEIGHT * BLOCK_SIZE/2, menu_char, strlen(menu_char)); x_flush(); @@ -677,11 +667,9 @@ game_over(void) int main(void) { - int win_width, win_height; - - win_width = 800; - win_height = 600; - x_setup_window(0, 0, win_width, win_height, 0x000000, "UNKO"); + x_setup_window(0, 0, + WORLD_WIDTH * BLOCK_SIZE, WORLD_HEIGHT * BLOCK_SIZE, + 0x000000, "UNKO"); while (next_menu != QUIT){ switch (next_menu){ case START_MENU: