commit 656adde5ac0ee1b361bb18fe4f7cd1ed26308588
parent c2d31b1964cdc6f691c9fa31f7f40fae79de4183
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 18 Dec 2022 10:20:28 +0900
refactor
Diffstat:
M | main.c | | | 29 | ++++++++++++++++++++--------- |
1 file changed, 20 insertions(+), 9 deletions(-)
diff --git a/main.c b/main.c
@@ -17,9 +17,10 @@
Display *display;
Window window;
unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT;
-GC gc, fgc;
+GC pgc, fgc, bgc;
Atom wm_delete_window;
Obj square = {100, 100, 0, 0, 0, 0, 20, 20};
+Obj block = {200, 200, 0, 0, 0, 0, 20, 20};
int quit = 0;
enum Keys {
Key_D,
@@ -48,7 +49,8 @@ void setup(void);
void start_menu(void);
void game_init(void);
void game_play(void);
-void square_handle_collision(void);
+void draw_object(Obj *, GC);
+void handle_collision(void);
void handle_inputs(void);
void end_menu(void);
void next_tick(float);
@@ -69,8 +71,9 @@ setup(void)
0, 0,
0);
XStoreName(display, window, "UNKO");
- gc = XCreateGC(display, window, 0, NULL);
+ pgc = XCreateGC(display, window, 0, NULL);
fgc = XCreateGC(display, window, 0, NULL);
+ bgc = XCreateGC(display, window, 0, NULL);
wm_delete_window = XInternAtom(display,
"WM_DELETE_WINDOW", False);
@@ -79,8 +82,9 @@ setup(void)
XSelectInput(display, window,
ExposureMask|KeyPressMask|KeyReleaseMask);
- XSetForeground(display, gc, 0x00FF00);
- XSetForeground(display, fgc, 0xFF00FF);
+ XSetForeground(display, pgc, 0x00FF00);
+ XSetForeground(display, fgc, 0xFFFFFF);
+ XSetForeground(display, bgc, 0x0000FF);
XMapWindow(display, window);
}
@@ -170,20 +174,27 @@ game_play(void)
dt = t1 - t0 > 0 ? t1 - t0 : t1 - t0 + 1000 * 1000 * 1000;
}
- obj_next_tick(&square, 1.0/FPS);
- square_handle_collision();
handle_inputs();
+ obj_next_tick(&square, 1.0/FPS);
+ handle_collision();
clock_gettime(CLOCK_MONOTONIC, &ts);
t0 = ts.tv_nsec;
XClearArea(display, window, 0, 0, win_width, win_height, False);
- XFillRectangle(display, window, gc, square.px, square.py, square.width, square.height);
+ draw_object(&square, pgc);
+ draw_object(&block, bgc);
XDrawString(display, window, fgc, square.px, square.py, "player", 6);
}
}
void
+draw_object(Obj *obj, GC gc)
+{
+ XFillRectangle(display, window, gc, obj->px, obj->py, obj->width, obj->height);
+}
+
+void
end_menu(void)
{
next_menu = Start_Menu;
@@ -265,7 +276,7 @@ handle_inputs(void)
}
void
-square_handle_collision(void)
+handle_collision(void)
{
if (square.px <= 0){
square.px = 0;