commit 4183d6fc6b496a0c056f65e0ebf1d5c67c8cf903
parent d77f83c24c6a6a4a52606178c5e393a0f13fe45b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sun, 18 Dec 2022 17:07:01 +0900
change obj to pointer
Diffstat:
M | main.c | | | 80 | +++++++++++++++++++++++++++++++++++++++++++++++++++---------------------------- |
M | obj.h | | | 31 | ++++++++++++++++++++++++++++--- |
2 files changed, 80 insertions(+), 31 deletions(-)
diff --git a/main.c b/main.c
@@ -19,8 +19,8 @@ Window window;
unsigned int win_width = INIT_WIDTH, win_height = INIT_HEIGHT;
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};
+Obj *square;
+Obj *block;
int quit = 0;
enum Keys {
Key_D,
@@ -142,10 +142,22 @@ start_menu(void)
void
game_init(void)
{
- square.px = 100;
- square.py = 100;
- square.vx = square.vy = 0;
- square.ax = square.ay = 0;
+ if(!square) // not allocated
+ if(!(square = obj_create(100, 100, 0, 0, 0, 0, 20, 20))){
+ fprintf(stderr, "couldn't create object: square");
+ exit(1);
+ }
+ if(!block) // not allocated
+ if(!(block = obj_create(200, 200, 0, 0, 0, 0, 20, 20))){
+ fprintf(stderr, "couldn't create object: block");
+ exit(1);
+ }
+ printf("square: %f %f\n", square->width, square->height);
+
+ square->px = 100;
+ square->py = 100;
+ square->vx = square->vy = 0;
+ square->ax = square->ay = 0;
}
void
@@ -174,17 +186,18 @@ game_play(void)
dt = t1 - t0 > 0 ? t1 - t0 : t1 - t0 + 1000 * 1000 * 1000;
}
+ //handle_interactions(); //handle interactions between objects
handle_inputs();
- obj_next_tick(&square, 1.0/FPS);
+ 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);
- draw_object(&square, pgc);
- draw_object(&block, bgc);
- XDrawString(display, window, fgc, square.px, square.py, "player", 6);
+ draw_object(square, pgc);
+ draw_object(block, bgc);
+ XDrawString(display, window, fgc, square->px, square->py, "player", 6);
}
}
@@ -262,38 +275,49 @@ handle_inputs(void)
}
}
- square.vx = 0;
+ square->vx = 0;
if (key_state[Key_D] == Key_Down)
- square.vx += 300;
+ square->vx += 300;
if (key_state[Key_A] == Key_Down)
- square.vx += -300;
+ square->vx += -300;
- square.vy = 0;
+ square->vy = 0;
if (key_state[Key_S] == Key_Down)
- square.vy += 300;
+ square->vy += 300;
if (key_state[Key_W] == Key_Down)
- square.vy += -300;
+ square->vy += -300;
}
void
handle_collision(void)
{
- if (square.px <= 0){
- square.px = 0;
- square.vx = 0;
+ if (square->px <= 0){
+ square->px = 0;
+ square->vx = 0;
}
- if (win_width <= square.px + square.width){
- square.px = win_width - square.width;
- square.vx = 0;
+ if (win_width <= square->px + square->width){
+ square->px = win_width - square->width;
+ square->vx = 0;
}
- if (square.py <= 0){
- square.py = 0;
- square.vy = 0;
+ if (square->py <= 0){
+ square->py = 0;
+ square->vy = 0;
}
- if (win_height <= square.py + square.height){
- square.py = win_height - square.height;
- square.vy = 0;
+ if (win_height <= square->py + square->height){
+ square->py = win_height - square->height;
+ square->vy = 0;
+ }
+
+ if (block->py < square->py + square->height &&
+ square->py < block->py + block->height){
+ if (block->px < square->px + square->width &&
+ square->px < block->px + block->width){
+ if (square->vx > 0)
+ block->px = square->px + square->width;
+ else
+ block->px = square->px - block->width;
+ }
}
}
diff --git a/obj.h b/obj.h
@@ -1,14 +1,39 @@
typedef struct Obj{
- float px, py;
- float vx, vy;
- float ax, ay;
+ float ppx, ppy; //previous position
+ float px, py; //current position
+ float vx, vy; //velocity
+ float ax, ay; //acceleration
float width, height;
} Obj;
+Obj *obj_create(float px, float py,
+ float vx, float vy,
+ float ax, float ay,
+ float width, float height);
void obj_move(Obj *obj, float px, float py); // move obj to the pos
void obj_next_tick(Obj *obj, float dt); // move obj by velocity * dt
void obj_accel(Obj *obj, float ax, float ay); // accelerate obj by accel
+
+Obj
+*obj_create(float px, float py,
+ float vx, float vy,
+ float ax, float ay,
+ float width, float height)
+{
+ Obj *obj;
+ obj = (Obj *) malloc(sizeof(Obj));
+ obj->ppx = obj->px = px;
+ obj->ppy = obj->py = py;
+ obj->vx = vx;
+ obj->vy = vy;
+ obj->ax = ax;
+ obj->ay = ay;
+ obj->width = width;
+ obj->height = height;
+ return obj;
+}
+
void
obj_move(Obj *obj, float px, float py)
{