style: refactor to functions

This commit is contained in:
orangerot 2025-10-21 14:57:37 +02:00
parent 464069851c
commit 3a2daedb9d
4 changed files with 87 additions and 52 deletions

View file

@ -65,9 +65,11 @@ void brick_previews(const struct brick active, const struct bricks bricks, struc
)
);
}
if (!previews[ii].front.val)
if (!previews[ii].front.val) {
previews[ii].front.val = p.front.val;
bricks_append(preview, previews[ii]);
}
}
}
}

1
draw.c
View file

@ -6,6 +6,7 @@ void draw_image( struct image canvas, struct image texture, size_t xpos, size_t
for (size_t x = 0; x < texture.width; x++) {
int canvas_y = (vertical ? x : y) + ypos;
int canvas_x = (vertical ? y : x) + xpos;
if (texture.color[y * texture.width + x].a == 0) continue;
if (canvas_x < 0 || canvas_x >= (int) canvas.width - 1 || canvas_y < 0 || canvas_y >= (int) canvas.height - 1) continue;
canvas.buf[canvas_y * canvas.width + canvas_x] = texture.buf[y * texture.width + x];
}

126
game.c
View file

@ -14,12 +14,17 @@
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))
#define CLAMP(x,a,b) (MIN(MAX(x,a),b))
#define VEC2_EQ(a,b) ((a).x == (b).x && (a).y == (b).y)
const uint32_t glyph_heart = 0b00010101111111111011100010000000;
const uint32_t glyph_flag = 0b00011000111001100010000100011100;
const uint32_t glyph_monster = 0b00111011111101011111111111101010;
const uint32_t glyph_sword = 0b00000010001110110011001101010000;
const uint32_t glyph_shield = 0b00111111000110001100010101000100;
int mouse_x = 0, mouse_y = 0;
struct vec2 {int x,y;};
struct vec2 mouse = {0,0};
struct bricks bricks = {0};
@ -31,16 +36,15 @@ bool has_active = 0;
struct bricks preview = {0};
int camera_x = 0;
int camera_y = 0;
struct vec2 camera = {0,0};
bool is_dragging = 0;
int goal_x = 5;
int goal_y = 5;
struct vec2 goal = {5,5};
bool is_goal_reached = 0;
struct enemy {
int x, y, attack, health;
bool is_reachable;
};
struct enemy enemies[20] = {0};
@ -72,11 +76,29 @@ void key_callback(int key, int scancode, int action, int mods) {
void cursor_position_callback(int xpos, int ypos) {
if (is_dragging) {
camera_x += xpos - mouse_x;
camera_y += ypos - mouse_y;
camera.x += xpos - mouse.x;
camera.y += ypos - mouse.y;
}
mouse_x = xpos;
mouse_y = ypos;
mouse.x = xpos;
mouse.y = ypos;
}
void preview_closest(const struct bricks preview, struct brick *closest, size_t *min_dist) {
size_t min_dist_i = -1;
*min_dist = (size_t)-1;
for (size_t i = 0; i < preview.count; i++) {
int active_x = active.front.x + mouse.x + DOMINO_WIDTH / 2;
int active_y = active.front.y + mouse.y + DOMINO_HEIGHT / 2;
int preview_x = camera.x + preview.items.brick[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2;
int preview_y = camera.y + preview.items.brick[i].front.y * EYE_SIZE + DOMINO_HEIGHT / 2;
size_t dist = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
if (min_dist_i == (size_t)-1) min_dist_i = i;
if (dist < *min_dist) {
min_dist_i = i;
*min_dist = dist;
}
}
*closest = preview.items.brick[min_dist_i];
}
void mouse_button_callback(int button, int action, int mods) {
@ -95,13 +117,13 @@ void mouse_button_callback(int button, int action, int mods) {
hand[i - 1] = hand[i];
continue;
}
if (b->front.x <= mouse_x && mouse_x <= b->front.x + DOMINO_WIDTH &&
b->front.y <= mouse_y && mouse_y <= b->front.y + DOMINO_HEIGHT
if (b->front.x <= mouse.x && mouse.x <= b->front.x + DOMINO_WIDTH &&
b->front.y <= mouse.y && mouse.y <= b->front.y + DOMINO_HEIGHT
) {
has_active = 1;
active = *b;
active.front.x -= mouse_x;
active.front.y -= mouse_y;
active.front.x -= mouse.x;
active.front.y -= mouse.y;
}
}
if (has_active) {
@ -116,53 +138,43 @@ void mouse_button_callback(int button, int action, int mods) {
// get preview with minimal distance from active brick
size_t min_dist = -1;
for (size_t i = 0; i < preview.count; i++) {
int active_x = active.front.x + mouse_x + DOMINO_WIDTH / 2;
int active_y = active.front.y + mouse_y + DOMINO_HEIGHT / 2;
int preview_x = camera_x + preview.items.brick[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2;
int preview_y = camera_y + preview.items.brick[i].front.y * EYE_SIZE + DOMINO_HEIGHT / 2;
preview.items.brick[i].front.val = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
if (min_dist == (size_t)-1) min_dist = i;
if (preview.items.brick[i].front.val < preview.items.brick[min_dist].front.val) min_dist = i;
}
struct brick closest = {0};
preview_closest(preview, &closest, &min_dist);
if (preview.items.brick[min_dist].front.val < EYE_SIZE * EYE_SIZE && preview.count) {
preview.items.brick[min_dist].front.val = active.front.val;
preview.items.brick[min_dist].front.vertical = active.front.vertical;
preview.items.brick[min_dist].back.val = active.back.val;
bricks_append(&bricks, preview.items.brick[min_dist]);
if (min_dist < EYE_SIZE * EYE_SIZE && preview.count) {
bricks_append(&bricks, closest);
for (size_t i = 0; i < enemy_count; i++) {
enemies[i].is_reachable |= VEC2_EQ(closest.front, enemies[i]) || VEC2_EQ(closest.back, enemies[i]);
}
hand[hand_count++] = (struct brick) {
.front = {.val = rand() % 6},
.back = {.val = rand() % 6},
};
if ((preview.items.brick[min_dist].front.x == goal_x &&
preview.items.brick[min_dist].front.y == goal_y) ||
(preview.items.brick[min_dist].back.x == goal_x &&
preview.items.brick[min_dist].back.y == goal_y)) {
is_goal_reached = 1;
}
is_goal_reached |= VEC2_EQ(closest.front, goal) || VEC2_EQ(closest.back, goal);
for (size_t i = 0; i < bricks.count; i++) brick_print(bricks.items.brick[i]);
} else {
hand[hand_count++] = active;
}
printf("dist: %d\n", preview.items.brick[min_dist].front.val);
printf("dist: %d\n", closest.front.val);
preview.count = 0;
}
}
}
void draw_enemy(struct image canvas, int attack, int health, size_t xpos, size_t ypos) {
char a[] = {(attack + '0'), 0};
char h[] = {(health + '0'), 0};
draw_glyph(canvas, glyph_monster, camera_x + xpos * EYE_SIZE + 2, camera_y + ypos * EYE_SIZE + 3, (struct color) {255, 0, 0, 255});
draw_text(canvas, a, camera_x + xpos * EYE_SIZE + 8, camera_y + ypos * EYE_SIZE, (struct color) {255, 0, 0, 255});
draw_text(canvas, h, camera_x + xpos * EYE_SIZE + 8, camera_y + ypos * EYE_SIZE + 6, (struct color) {255, 0, 0, 255});
void draw_enemy(struct image canvas, struct enemy enemy, struct color color) {
char attack[] = {(enemy.attack + '0'), 0};
char health[] = {(enemy.health + '0'), 0};
int x = camera.x + enemy.x * EYE_SIZE;
int y = camera.y + enemy.y * EYE_SIZE;
draw_glyph(canvas, glyph_monster, x + 2, y + 3, color);
draw_text(canvas , attack , x + 8, y + 0, color);
draw_text(canvas , health , x + 8, y + 6, color);
}
void init(struct image canvas) {
camera_x = canvas.width / 2;
camera_y = canvas.height / 2;
camera.x = canvas.width / 2;
camera.y = canvas.height / 2;
bricks_append(
&bricks,
@ -178,6 +190,14 @@ void init(struct image canvas) {
.back = {.val = rand() % 6},
};
}
enemies[enemy_count++] = (struct enemy) {
.x = 0,
.y = 3,
.attack = 6,
.health = 2,
.is_reachable = 0
};
}
void render(struct image canvas) {
@ -186,17 +206,23 @@ void render(struct image canvas) {
// domino playground
for (size_t i = 0; i < bricks.count; i++) {
struct brick *b = &bricks.items.brick[i];
draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], camera_x + b->front.x * EYE_SIZE, camera_y + b->front.y * EYE_SIZE, b->front.vertical);
draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], camera.x + b->front.x * EYE_SIZE, camera.y + b->front.y * EYE_SIZE, b->front.vertical);
}
// preview
for (size_t i = 0; i < preview.count; i++) {
draw_image(canvas, white_and_blue_dominoes[active.back.val][active.front.val], camera_x + preview.items.brick[i].front.x * EYE_SIZE, camera_y + preview.items.brick[i].front.y * EYE_SIZE, active.front.vertical);
draw_image(canvas, white_and_blue_dominoes[active.back.val][active.front.val], camera.x + preview.items.brick[i].front.x * EYE_SIZE, camera.y + preview.items.brick[i].front.y * EYE_SIZE, active.front.vertical);
}
draw_glyph(canvas, glyph_flag, camera_x + goal_x * EYE_SIZE + 3, camera_y + goal_y * EYE_SIZE + 3, (struct color) {255, 255, 0, 255});
draw_glyph(canvas, glyph_flag, camera.x + goal.x * EYE_SIZE + 3, camera.y + goal.y * EYE_SIZE + 3, (struct color) {255, 255, 0, 255});
draw_enemy(canvas, 6, 3, 0, 3);
for (size_t i = 0; i < enemy_count; i++) {
struct color color = {100, 0, 0, 255};
if (enemies[i].is_reachable && has_active) {
color = (struct color) {255, 0, 0, 255};
}
draw_enemy(canvas, enemies[i], color);
}
// hand
for (size_t i = 0; i < hand_count; i++) {
@ -208,7 +234,13 @@ void render(struct image canvas) {
// active
if (has_active) {
draw_image(canvas, red_and_peach_dominoes[active.back.val][active.front.val], mouse_x + active.front.x, mouse_y + active.front.y, active.front.vertical);
int x = mouse.x + active.front.x;
int y = mouse.y + active.front.y;
draw_image(canvas, red_and_peach_dominoes[active.back.val][active.front.val], x, y, active.front.vertical);
if (active.front.vertical) {
draw_glyph(canvas, glyph_sword, x - 5, y, (struct color) {0, 0, 255, 255});
draw_glyph(canvas, glyph_shield, x - 5, y + EYE_SIZE, (struct color) {0, 255, 0, 255});
}
}
// character