commit c30d1d25cd402a1fda5dc4fe5a44db49b3fbce45
parent 7f1318fb2cb94a2c782910b1d6c7e7ff3ccf8068
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 24 Dec 2022 10:23:30 +0900
implement simple collision detection
Diffstat:
M | ex4/ex4.c | | | 52 | +++++++++++++++++++++++++++++++++++----------------- |
1 file changed, 35 insertions(+), 17 deletions(-)
diff --git a/ex4/ex4.c b/ex4/ex4.c
@@ -47,7 +47,7 @@ struct square {
Display *display;
Window window;
unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT;
-GC gc;
+GC gc, sgc[NUM_SQUARE];
Atom wm_delete_window;
struct square square[NUM_SQUARE];
int next_menu = START_MENU;
@@ -60,7 +60,8 @@ void start_menu(void);
void game_play(void);
void receive_events(int[]);
void handle_inputs(int[]);
-void next_tick(long);
+void next_tick(struct square *, long);
+int test_collision(struct square *, struct square*);
void game_over(void);
@@ -80,6 +81,11 @@ setup(void)
0);
XStoreName(display, window, "UNKO");
gc = XCreateGC(display, window, 0, NULL);
+ XSetForeground(display, gc, 0x00FFFF);
+ for (int i = 0; i < NUM_SQUARE; i++){
+ sgc[i] = XCreateGC(display, window, 0, NULL);
+ XSetForeground(display, sgc[i], 0x00FF00);
+ }
wm_delete_window = XInternAtom(display,
"WM_DELETE_WINDOW", False);
@@ -88,7 +94,6 @@ setup(void)
XSelectInput(display, window,
ExposureMask | KeyPressMask | KeyReleaseMask);
- XSetForeground(display, gc, 0x00FFFF);
XMapWindow(display, window);
}
@@ -234,19 +239,27 @@ handle_inputs(int key_state[])
}
void
-next_tick(long ndt) // nano second
+next_tick(struct square *s, long ndt) // nano second
{
- 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;
+ s->px = s->px + s->vx * ndt / 1000 / 1000 / 1000;
+ s->py = s->py + s->vy * ndt / 1000 / 1000 / 1000;
+
// bind within the window
- 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;
+ if (s->px < 0)
+ s->px = 0;
+ if (win_width < s->px + s->w)
+ s->px = win_width - s->w;
+ if (s->py < 0)
+ s->py = 0;
+ if (win_height < s->py + s->h)
+ s->py = win_height - s->h;
+}
+
+int
+test_collision(struct square *s1, struct square* s2)
+{
+ return s1->px < s2->px + s2->w && s2->px < s1->px + s1->w &&
+ s2->py < s1->py + s1->h && s1->py < s2->py + s2->h;
}
void
@@ -293,15 +306,20 @@ game_play(void)
clock_gettime(CLOCK_MONOTONIC, &ts);
t0 = ts.tv_nsec;
- next_tick(1000 * 1000 * 1000 / FPS);
+ for (int i = 0; i < NUM_SQUARE; i++)
+ next_tick(&square[i], 1000 * 1000 * 1000 / FPS);
+
+ if (test_collision(&square[0], &square[1]))
+ XSetForeground(display, sgc[1], 0xFF0000);
+ else
+ XSetForeground(display, sgc[1], 0x00FF00);
XClearArea(display, window,
0, 0, // position
win_width, win_height, // width and height
False);
for (int i = 0; i < NUM_SQUARE; i++) {
- XSetForeground(display, gc, 0x000FFF << i * 12);
- XFillRectangle(display, window, gc,
+ XFillRectangle(display, window, sgc[i],
square[i].px, square[i].py, // position
square[i].w, square[i].h); // width and height
}