commit 2d09861a9c093fa2cf783b7b4ceb9a0c0634cb94
parent 9a9cbb71d6dc189b217a137baa8928399e812b4b
Author: Matsuda Kenji <info@mtkn.jp>
Date: Sat, 7 Jan 2023 10:01:22 +0900
add draw_object function
Diffstat:
4 files changed, 46 insertions(+), 16 deletions(-)
diff --git a/ex9/main.c b/ex9/main.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include <time.h>
#include <unistd.h>
#include <string.h>
@@ -145,19 +146,22 @@ game_play(void)
if (world_map[i] == 'b') {
int xi = i % WORLD_WIDTH;
// TODO: don't forget to free these things.
- append_ol(blh[xi], create_object(TBLOCK, i % WORLD_WIDTH * BLOCK_SIZE,
+ append_ol(blh[xi], create_object(TBLOCK, 0x00FF00,
+ i % WORLD_WIDTH * BLOCK_SIZE,
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, 0,
BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE * BLOCK_SIZE));
} else if (world_map[i] == 'p') {
- player = create_object(TPLAYER, i % WORLD_WIDTH * BLOCK_SIZE,
+ player = create_object(TPLAYER, 0x0000FF,
+ i % WORLD_WIDTH * BLOCK_SIZE,
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, GRAVITY,
BLOCK_SIZE, BLOCK_SIZE,
BLOCK_SIZE * BLOCK_SIZE);
} else if (world_map[i] == 'f') {
- append_ol(flh, create_object(TFLAG, i % WORLD_WIDTH * BLOCK_SIZE,
+ append_ol(flh, create_object(TFLAG, 0xFFFF00,
+ i % WORLD_WIDTH * BLOCK_SIZE,
i / WORLD_WIDTH * BLOCK_SIZE,
0, 0, 0, 0,
BLOCK_SIZE, BLOCK_SIZE,
@@ -261,23 +265,15 @@ game_play(void)
xi < (WIN_WIDTH + scroll_dst) / BLOCK_SIZE + 1 &&
xi < WORLD_WIDTH; xi++) {
for (blc = blh[xi]->first; blc != NULL; blc = blc->next) {
- x_draw_rectangle(0x00FF00,
- blc->o->p.x - scroll_dst, blc->o->p.y, // position
- blc->o->body.rectangle.w,
- blc->o->body.rectangle.h);
+ draw_object_scroll(blc->o, scroll_dst);
}
}
- x_draw_rectangle(0x009FFF,
- player->p.x - scroll_dst, player->p.y, // position
- player->body.rectangle.w, player->body.rectangle.h);
+ draw_object_scroll(player, scroll_dst);
for (flc = flh->first; flc != NULL; flc = flc->next) {
if (0 <= flc->o->p.x - scroll_dst &&
flc->o->p.x - scroll_dst < WIN_WIDTH) {
- x_draw_rectangle(0xFFFF00,
- flc->o->p.x - scroll_dst, flc->o->p.y, // position
- flc->o->body.rectangle.w,
- flc->o->body.rectangle.h);
+ draw_object_scroll(flc->o, scroll_dst);
} else {
break;
}
diff --git a/ex9/main.h b/ex9/main.h
@@ -42,6 +42,7 @@ union Body {
struct Object {
enum object_type type;
enum object_shape shape;
+ uint32_t color;
struct Point pp;
struct Point p;
struct Point v;
@@ -78,7 +79,7 @@ int test_collision(struct Object *, struct Object *); // 1 if collide, 0 if not
void next_tick(struct Object *o, long);
void handle_collision_mf(struct Object *, struct Object *);
int object_is_falling(struct Object *o, struct Object *fb, int num_f);
-struct Object *create_object(enum object_type,
+struct Object *create_object(enum object_type, uint32_t,
float, float, float, float, float, float,
int, int, int);
int is_on_floor_before(struct Object *, struct Object *);
@@ -100,6 +101,8 @@ void x_clear_area(void);
void x_draw_string(unsigned long color, int x, int y, const char *str, int length);
void x_draw_rectangle(unsigned long color, int x, int y,
unsigned int w, unsigned int h);
+void draw_object(const struct Object *);
+void draw_object_scroll(const struct Object *, int);
void x_flush(void);
void x_clean_up(void);
int x_next_event(char *c);
diff --git a/ex9/util.c b/ex9/util.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <stdint.h>
#include "main.h"
@@ -8,13 +9,14 @@ static void rect_next_tick(struct Object *, long);
static void rect_handle_collision_mf(struct Object *, struct Object *);
struct Object *
-create_object(enum object_type t,
+create_object(enum object_type t, uint32_t color,
float px, float py, float vx, float vy, float ax, float ay,
int w, int h, int m)
{
struct Object *o;
o = (struct Object *)malloc(sizeof(struct Object));
o->type = t;
+ o->color = color;
o->shape = SRECTANGLE;
o->pp.x = o->p.x = px;
o->pp.y = o->p.y = py;
diff --git a/ex9/x.c b/ex9/x.c
@@ -1,4 +1,5 @@
#include <stdio.h>
+#include <stdint.h>
#include <X11/Xlib.h>
@@ -79,6 +80,34 @@ x_draw_rectangle(unsigned long color, int x, int y,
}
void
+draw_object(const struct Object *op)
+{
+ if (op->shape == SRECTANGLE) {
+ x_draw_rectangle(op->color,
+ op->p.x, op->p.y,
+ op->body.rectangle.w,
+ op->body.rectangle.h);
+ } else {
+ fprintf(stderr, "draw_object: Drawing object other than rectangle is"
+ "not implemented yet.\n");
+ }
+}
+
+void
+draw_object_scroll(const struct Object *op, int sd)
+{
+ if (op->shape == SRECTANGLE) {
+ x_draw_rectangle(op->color,
+ op->p.x - sd, op->p.y,
+ op->body.rectangle.w,
+ op->body.rectangle.h);
+ } else {
+ fprintf(stderr, "draw_object_scroll: Drawing object other than"
+ "rectangle is not implemented yet.\n");
+ }
+}
+
+void
x_flush(void)
{
XFlush(display);