Compare commits
5 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dd4a0085c5 | ||
|
|
3a2daedb9d | ||
|
|
464069851c | ||
|
|
d6f9adad13 | ||
|
|
83967090c2 |
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -4,4 +4,5 @@ domino-dungeon.html
|
|||
domino-dungeon.js
|
||||
domino-dungeon.wasm
|
||||
src_build/domino_assets
|
||||
index.*
|
||||
|
||||
|
|
|
|||
15
Makefile
15
Makefile
|
|
@ -1,11 +1,18 @@
|
|||
CC = gcc
|
||||
CFLAGS := -ggdb -Wall -Wextra
|
||||
# -fsanitize=address
|
||||
LDFLAGS = -lglfw -lm -lGL -I./glad/include
|
||||
|
||||
SOURCES = main.c game.c game.h glad/src/glad.c assets/white_and_blue_dominoes.h assets/red_and_peach_dominoes.h domino.c domino.h
|
||||
SOURCES = main.c \
|
||||
game.c game.h \
|
||||
domino.c domino.h \
|
||||
draw.c draw.h \
|
||||
assets/white_and_blue_dominoes.h \
|
||||
assets/red_and_peach_dominoes.h \
|
||||
glad/src/glad.c \
|
||||
|
||||
domino-dungeon: ${SOURCES}
|
||||
$(CC) ${CFLAGS} -o $@ $^ ${LDFLAGS}
|
||||
$(CC) ${CFLAGS} -o $@ $^ ${LDFLAGS}
|
||||
|
||||
assets/%.h: src_build/domino_assets
|
||||
$< assets/1bit_dominoes_asset_pack/$*.png $* > $@
|
||||
|
|
@ -14,6 +21,6 @@ src_build/domino_assets: src_build/domino_assets.c
|
|||
$(CC) -o $@ $^ -lm
|
||||
|
||||
# https://gist.github.com/ousttrue/0f3a11d5d28e365b129fe08f18f4e141
|
||||
domino-dungeon.html: ${SOURCES}
|
||||
emcc -sUSE_WEBGL2=1 -sUSE_GLFW=3 -sWASM=1 -I./glad/include $(filter %.c,$^) -o $@
|
||||
index.html: ${SOURCES}
|
||||
emcc -sUSE_WEBGL2=1 -sUSE_GLFW=3 -sWASM=1 --shell-file=minshell.html -I./glad/include $(filter %.c,$^) -o $@
|
||||
|
||||
|
|
|
|||
63
domino.c
63
domino.c
|
|
@ -1,6 +1,18 @@
|
|||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "domino.h"
|
||||
|
||||
#define DIRECTIONS 6
|
||||
const struct eye direction[DIRECTIONS] = {
|
||||
{.x = 0, .y = -1},
|
||||
{.x = -2, .y = 0},
|
||||
{.x = 1, .y = 0},
|
||||
{.x = 0, .y = 1},
|
||||
{.x = -1, .y = -1},
|
||||
{.x = -1, .y = 1},
|
||||
};
|
||||
|
||||
void bricks_append(struct bricks *bricks, struct brick brick) {
|
||||
if (bricks->count+1 > bricks->capacity) {
|
||||
if (bricks->capacity == 0) bricks->capacity = 256;
|
||||
|
|
@ -10,3 +22,54 @@ void bricks_append(struct bricks *bricks, struct brick brick) {
|
|||
bricks->items.brick[bricks->count++] = brick;
|
||||
}
|
||||
|
||||
void brick_print(const struct brick b) {
|
||||
struct eye *eyes = (struct eye*) &b;
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
printf("{.x = %d, .y = %d, .val = %d, .vertical = %d}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical); }
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
|
||||
void brick_previews(const struct brick active, const struct bricks bricks, struct bricks *preview) {
|
||||
preview->count = 0;
|
||||
for (size_t i = 0; i < bricks.count * 2; i++) {
|
||||
struct eye e = bricks.items.eye[i];
|
||||
if (e.val != active.front.val && e.val != active.back.val) continue;
|
||||
|
||||
struct brick p = active;
|
||||
|
||||
p.front.x = e.x;
|
||||
p.front.y = e.y;
|
||||
p.back.x = e.x + !active.front.vertical;
|
||||
p.back.y = e.y + active.front.vertical;
|
||||
|
||||
struct brick previews[DIRECTIONS] = {0};
|
||||
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
|
||||
int offset_x = active.front.vertical ? direction[ii].y : direction[ii].x;
|
||||
int offset_y = active.front.vertical ? direction[ii].x : direction[ii].y;
|
||||
previews[ii] = p;
|
||||
previews[ii].front.x += offset_x;
|
||||
previews[ii].front.y += offset_y;
|
||||
previews[ii].front.val = 0;
|
||||
|
||||
previews[ii].back.x += offset_x;
|
||||
previews[ii].back.y += offset_y;
|
||||
|
||||
for (size_t iii = 0; iii < bricks.count * 2; iii++) {
|
||||
previews[ii].front.val |= (
|
||||
(bricks.items.eye[iii].x == previews[ii].back.x && bricks.items.eye[iii].y == previews[ii].back.y) ||
|
||||
(bricks.items.eye[iii].x == previews[ii].front.x && bricks.items.eye[iii].y == previews[ii].front.y)
|
||||
|| (
|
||||
(active.front.val != active.back.val) &&
|
||||
((e.val == active.front.val && direction[ii].x < 0) || (e.val == active.back.val && direction[ii].x >= 0))
|
||||
)
|
||||
);
|
||||
}
|
||||
if (!previews[ii].front.val) {
|
||||
previews[ii].front.val = p.front.val;
|
||||
bricks_append(preview, previews[ii]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
2
domino.h
2
domino.h
|
|
@ -17,6 +17,8 @@ struct bricks {
|
|||
};
|
||||
|
||||
void bricks_append(struct bricks *bricks, struct brick brick);
|
||||
void brick_print(const struct brick b);
|
||||
void brick_previews(const struct brick active, const struct bricks bricks, struct bricks *preview);
|
||||
|
||||
#endif // DOMINO_H
|
||||
|
||||
|
|
|
|||
40
draw.c
Normal file
40
draw.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include "characters.h"
|
||||
#include "draw.h"
|
||||
|
||||
void draw_image(struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical) {
|
||||
for (size_t y = 0; y < texture.height; y++) {
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_glyph(struct image canvas, uint32_t glyph, size_t xpos, size_t ypos, struct color color) {
|
||||
struct image texture = {
|
||||
.width = 5,
|
||||
.height = 6,
|
||||
.bufsize = 5 * 6,
|
||||
.buf = (uint32_t[30]) {0}
|
||||
};
|
||||
|
||||
for (size_t y = 0; y < texture.height; y++) {
|
||||
for (size_t x = 0; x < texture.width; x++) {
|
||||
if ((glyph >> ((texture.height - y - 1) * texture.width + (texture.width - x - 1))) & 1) {
|
||||
texture.color[y * texture.width + x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
draw_image(canvas, texture, xpos, ypos, 0);
|
||||
}
|
||||
|
||||
void draw_text(struct image canvas, char *string, size_t xpos, size_t ypos, struct color color) {
|
||||
for (size_t i = 0; string[i]; i++) {
|
||||
uint32_t glyph = characters[string[i] - ' '];
|
||||
draw_glyph(canvas, glyph, xpos + i * 5, ypos, color);
|
||||
}
|
||||
}
|
||||
|
||||
8
draw.h
Normal file
8
draw.h
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include "game.h"
|
||||
|
||||
void draw_image(struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical);
|
||||
void draw_glyph(struct image canvas, uint32_t glyph, size_t xpos, size_t ypos, struct color color);
|
||||
void draw_text(struct image canvas, char *string, size_t xpos, size_t ypos, struct color color);
|
||||
|
||||
331
game.c
331
game.c
|
|
@ -7,15 +7,24 @@
|
|||
|
||||
#include "game.h"
|
||||
#include "domino.h"
|
||||
#include "characters.h"
|
||||
#include "draw.h"
|
||||
#include "assets/white_and_blue_dominoes.h"
|
||||
#include "assets/red_and_peach_dominoes.h"
|
||||
|
||||
#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)
|
||||
|
||||
int mouse_x = 0, mouse_y = 0;
|
||||
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;
|
||||
|
||||
struct vec2 {int x,y;};
|
||||
|
||||
struct vec2 mouse = {0,0};
|
||||
|
||||
struct bricks bricks = {0};
|
||||
|
||||
|
|
@ -25,77 +34,25 @@ size_t hand_count = 0;
|
|||
struct brick active = {0};
|
||||
bool has_active = 0;
|
||||
|
||||
struct brick preview[256] = {0};
|
||||
size_t preview_count = 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;
|
||||
|
||||
#define DIRECTIONS 6
|
||||
struct eye direction[DIRECTIONS] = {
|
||||
{.x = 0, .y = -1},
|
||||
{.x = -2, .y = 0},
|
||||
{.x = 1, .y = 0},
|
||||
{.x = 0, .y = 1},
|
||||
{.x = -1, .y = -1},
|
||||
{.x = -1, .y = 1},
|
||||
struct enemy {
|
||||
int x, y, attack, health;
|
||||
bool is_reachable;
|
||||
};
|
||||
|
||||
void get_prewiews() {
|
||||
preview_count = 0;
|
||||
for (size_t i = 0; i < bricks.count * 2; i++) {
|
||||
struct eye e = bricks.items.eye[i];
|
||||
if (e.val != active.front.val && e.val != active.back.val) continue;
|
||||
struct enemy enemies[20] = {0};
|
||||
size_t enemy_count = 0;
|
||||
|
||||
struct brick p = active;
|
||||
size_t player_health = 10;
|
||||
char player_health_s[4] = " ";
|
||||
|
||||
p.front.x = e.x;
|
||||
p.front.y = e.y;
|
||||
p.back.x = e.x + !active.front.vertical;
|
||||
p.back.y = e.y + active.front.vertical;
|
||||
|
||||
struct brick previews[DIRECTIONS] = {0};
|
||||
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
|
||||
int offset_x = active.front.vertical ? direction[ii].y : direction[ii].x;
|
||||
int offset_y = active.front.vertical ? direction[ii].x : direction[ii].y;
|
||||
previews[ii] = p;
|
||||
previews[ii].front.x += offset_x;
|
||||
previews[ii].front.y += offset_y;
|
||||
previews[ii].front.val = 0;
|
||||
|
||||
previews[ii].back.x += offset_x;
|
||||
previews[ii].back.y += offset_y;
|
||||
|
||||
for (size_t iii = 0; iii < bricks.count * 2; iii++) {
|
||||
previews[ii].front.val |= (
|
||||
(bricks.items.eye[iii].x == previews[ii].back.x && bricks.items.eye[iii].y == previews[ii].back.y) ||
|
||||
(bricks.items.eye[iii].x == previews[ii].front.x && bricks.items.eye[iii].y == previews[ii].front.y)
|
||||
|| (
|
||||
(active.front.val != active.back.val) &&
|
||||
((e.val == active.front.val && direction[ii].x < 0) || (e.val == active.back.val && direction[ii].x >= 0))
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t ii = 0; ii < DIRECTIONS; ii++) {
|
||||
if (!previews[ii].front.val)
|
||||
preview[preview_count++] = previews[ii];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void print_brick(struct brick b) {
|
||||
struct eye *eyes = (struct eye*) &b;
|
||||
for (size_t i = 0; i < 2; i++) {
|
||||
printf("{.x = %d, .y = %d, .val = %d, .vertical = %d}" , eyes[i].x, eyes[i].y, eyes[i].val, eyes[i].vertical); }
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void key_callback(int key, int scancode, int action, int mods) {
|
||||
(void) scancode;
|
||||
|
|
@ -110,7 +67,7 @@ void key_callback(int key, int scancode, int action, int mods) {
|
|||
active.back.val = tmp.val;
|
||||
}
|
||||
active.front.vertical = !active.front.vertical;
|
||||
get_prewiews();
|
||||
brick_previews(active, bricks, &preview);
|
||||
printf("rotate\n");
|
||||
break;
|
||||
case GLFW_KEY_ENTER:
|
||||
|
|
@ -122,15 +79,52 @@ 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 enemy_closest_get(const struct enemy *enemies, const size_t enemy_count, size_t *closest_i, size_t *min_dist) {
|
||||
*closest_i = -1;
|
||||
*min_dist = (size_t)-1;
|
||||
for (size_t i = 0; i < enemy_count; i++) {
|
||||
if (!enemies[i].is_reachable) continue;
|
||||
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 + enemies[i].x * EYE_SIZE + EYE_SIZE / 2;
|
||||
int preview_y = camera.y + enemies[i].y * EYE_SIZE + EYE_SIZE / 2;
|
||||
size_t dist = (active_x - preview_x) * (active_x - preview_x) + (active_y - preview_y) * (active_y - preview_y);
|
||||
if (*closest_i == (size_t)-1) *closest_i = i;
|
||||
if (dist < *min_dist) {
|
||||
*closest_i = i;
|
||||
*min_dist = dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void brick_preview_closest_get(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) {
|
||||
(void) mods;
|
||||
printf("click!\n");
|
||||
|
||||
if (button == GLFW_MOUSE_BUTTON_RIGHT) {
|
||||
is_dragging = (action == GLFW_PRESS);
|
||||
|
|
@ -140,68 +134,105 @@ void mouse_button_callback(int button, int action, int mods) {
|
|||
// pick up brick from hand
|
||||
for (size_t i = 0; i < hand_count; i++) {
|
||||
struct brick *b = &hand[i];
|
||||
if (!has_active) {
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
if (has_active) {
|
||||
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
|
||||
) {
|
||||
has_active = 1;
|
||||
active = *b;
|
||||
active.front.x -= mouse.x;
|
||||
active.front.y -= mouse.y;
|
||||
}
|
||||
}
|
||||
if (has_active) {
|
||||
hand_count--;
|
||||
get_prewiews();
|
||||
brick_previews(active, bricks, &preview);
|
||||
}
|
||||
}
|
||||
|
||||
printf("click!\n");
|
||||
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE) {
|
||||
if (has_active) {
|
||||
has_active = 0;
|
||||
|
||||
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[i].front.x * EYE_SIZE + DOMINO_WIDTH / 2;
|
||||
int preview_y = camera_y + preview[i].front.y * EYE_SIZE + DOMINO_HEIGHT / 2;
|
||||
preview[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[i].front.val < preview[min_dist].front.val) min_dist = i;
|
||||
}
|
||||
// get preview with minimal distance from active brick
|
||||
size_t brick_preview_min_dist = -1;
|
||||
struct brick brick_preview_closest = {0};
|
||||
brick_preview_closest_get(preview, &brick_preview_closest, &brick_preview_min_dist);
|
||||
|
||||
if (preview[min_dist].front.val < EYE_SIZE * EYE_SIZE && preview_count) {
|
||||
preview[min_dist].front.val = active.front.val;
|
||||
preview[min_dist].front.vertical = active.front.vertical;
|
||||
preview[min_dist].back.val = active.back.val;
|
||||
bricks_append(&bricks, preview[min_dist]);
|
||||
hand[hand_count++] = (struct brick) {
|
||||
.front = {.val = rand() % 6},
|
||||
.back = {.val = rand() % 6},
|
||||
};
|
||||
if ((preview[min_dist].front.x == goal_x && preview[min_dist].front.y == goal_y) ||
|
||||
(preview[min_dist].back.x == goal_x && preview[min_dist].back.y == goal_y)) {
|
||||
is_goal_reached = 1;
|
||||
size_t enemy_min_dist = -1;
|
||||
size_t enemy_closest_i = -1;
|
||||
if (active.front.vertical) enemy_closest_get(enemies, enemy_count, &enemy_closest_i, &enemy_min_dist);
|
||||
|
||||
if (enemy_min_dist < brick_preview_min_dist && enemy_min_dist < EYE_SIZE * EYE_SIZE && enemy_count) {
|
||||
enemies[enemy_closest_i].health -= active.front.val + 1;
|
||||
if (enemies[enemy_closest_i].health <= 0) {
|
||||
enemies[enemy_closest_i].health = 0;
|
||||
for (size_t i = enemy_closest_i + 1; i < enemy_count; i++) {
|
||||
enemies[i - 1] = enemies[i];
|
||||
}
|
||||
enemy_count--;
|
||||
enemies[enemy_count++] = (struct enemy) {
|
||||
.attack = rand() % 6 + 1,
|
||||
.health = rand() % 6 + 1,
|
||||
.x = bricks.items.brick[bricks.count - 1].front.x + rand() % 8 - 4,
|
||||
.y = bricks.items.brick[bricks.count - 1].front.y + rand() % 8 - 4,
|
||||
.is_reachable = 0,
|
||||
};
|
||||
for (size_t i = 0; i < bricks.count * 2; i++) {
|
||||
if (VEC2_EQ(bricks.items.eye[i], enemies[enemy_count - 1])) {
|
||||
enemies[enemy_count - 1].is_reachable = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (size_t i = 0; i < 3; i++) {
|
||||
hand[hand_count = MIN(hand_count + 1, 4)] = (struct brick) {
|
||||
.front = {.val = rand() % 6},
|
||||
.back = {.val = rand() % 6},
|
||||
};
|
||||
}
|
||||
} else {
|
||||
player_health -= enemies[enemy_closest_i].attack - (active.back.val + 1);
|
||||
sprintf(player_health_s, "%zu", player_health);
|
||||
}
|
||||
for (size_t i = 0; i < bricks.count; i++) print_brick(bricks.items.brick[i]);
|
||||
} else if (brick_preview_min_dist < EYE_SIZE * EYE_SIZE && preview.count) {
|
||||
bricks_append(&bricks, brick_preview_closest);
|
||||
for (size_t i = 0; i < enemy_count; i++) {
|
||||
enemies[i].is_reachable |= VEC2_EQ(brick_preview_closest.front, enemies[i]) || VEC2_EQ(brick_preview_closest.back, enemies[i]);
|
||||
}
|
||||
// hand[hand_count++] = (struct brick) {
|
||||
// .front = {.val = rand() % 6},
|
||||
// .back = {.val = rand() % 6},
|
||||
// };
|
||||
is_goal_reached |= VEC2_EQ(brick_preview_closest.front, goal) || VEC2_EQ(brick_preview_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[min_dist].front.val);
|
||||
preview_count = 0;
|
||||
printf("dist: %d\n", brick_preview_closest.front.val);
|
||||
preview.count = 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
sprintf(player_health_s, "%zu", player_health);
|
||||
|
||||
bricks_append(
|
||||
&bricks,
|
||||
|
|
@ -217,73 +248,61 @@ void init(struct image canvas) {
|
|||
.back = {.val = rand() % 6},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
void draw( struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical) {
|
||||
for (size_t y = 0; y < texture.height; y++) {
|
||||
for (size_t x = 0; x < texture.width; x++) {
|
||||
int canvas_y = (vertical ? x : y) + ypos;
|
||||
int canvas_x = (vertical ? y : x) + xpos;
|
||||
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];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void draw_glyph( struct image canvas, uint32_t glyph, size_t xpos, size_t ypos, struct color color) {
|
||||
struct image texture = {
|
||||
.width = 5,
|
||||
.height = 6,
|
||||
.bufsize = 5 * 6,
|
||||
.buf = (uint32_t[30]) {0}
|
||||
enemies[enemy_count++] = (struct enemy) {
|
||||
.x = 0,
|
||||
.y = 3,
|
||||
.attack = 6,
|
||||
.health = 2,
|
||||
.is_reachable = 0
|
||||
};
|
||||
|
||||
texture.buf = (uint32_t[30]) {0};
|
||||
|
||||
for (size_t y = 0; y < texture.height; y++) {
|
||||
for (size_t x = 0; x < texture.width; x++) {
|
||||
if ((glyph >> ((texture.height - y - 1) * texture.width + (texture.width - x - 1))) & 1) {
|
||||
texture.color[y * texture.width + x] = color;
|
||||
}
|
||||
}
|
||||
}
|
||||
draw(canvas, texture, xpos, ypos, 0);
|
||||
}
|
||||
|
||||
void print(struct image canvas, char *string, size_t xpos, size_t ypos, struct color color) {
|
||||
for (size_t i = 0; string[i]; i++) {
|
||||
uint32_t glyph = characters[string[i] - ' '];
|
||||
draw_glyph(canvas, glyph, xpos + i * 5, ypos, color);
|
||||
}
|
||||
}
|
||||
|
||||
void render(struct image canvas) {
|
||||
for (size_t i = 0; i < canvas.bufsize; i++) canvas.buf[i] = 0;
|
||||
for (size_t i = 0; i < canvas.bufsize; i++) canvas.color[i] = (struct color) {0x12, 0x0b, 0x10, 0xFF};
|
||||
|
||||
// domino playground
|
||||
for (size_t i = 0; i < bricks.count; i++) {
|
||||
struct brick *b = &bricks.items.brick[i];
|
||||
draw(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(canvas, white_and_blue_dominoes[active.back.val][active.front.val], camera_x + preview[i].front.x * EYE_SIZE, camera_y + preview[i].front.y * EYE_SIZE, active.front.vertical);
|
||||
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_glyph(canvas, (uint32_t) 0b00011000111001100010000100011100, 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});
|
||||
|
||||
for (size_t i = 0; i < enemy_count; i++) {
|
||||
// struct color color = {100, 0, 0, 255};
|
||||
struct color color = {0xFF, 0x00, 0x55, 0xFF};
|
||||
if (enemies[i].is_reachable) {
|
||||
color = (struct color) {255, 0, 0, 255};
|
||||
if (has_active) {
|
||||
color = (struct color) {0, 0, 255, 255};
|
||||
}
|
||||
}
|
||||
draw_enemy(canvas, enemies[i], color);
|
||||
}
|
||||
|
||||
// hand
|
||||
for (size_t i = 0; i < hand_count; i++) {
|
||||
struct brick *b = &hand[i];
|
||||
b->front.x = (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4);
|
||||
b->front.y = canvas.height - DOMINO_WIDTH;
|
||||
draw(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y, b->front.vertical);
|
||||
draw_image(canvas, red_and_peach_dominoes[b->back.val][b->front.val], b->front.x, b->front.y, b->front.vertical);
|
||||
}
|
||||
|
||||
// active
|
||||
if (has_active) {
|
||||
draw(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
|
||||
|
|
@ -291,12 +310,14 @@ void render(struct image canvas) {
|
|||
char inventory[] = "Inventory";
|
||||
char game_over[] = "You reached the goal!";
|
||||
// print(canvas, title, (canvas.width - (sizeof(title)-1) * 5) / 2, 5, (struct color) {255, 255, 255, 255});
|
||||
print(canvas, inventory, (canvas.width - (sizeof(inventory)-1) * 5) / 2, canvas.height - DOMINO_WIDTH - 10, (struct color) {255, 255, 255, 255});
|
||||
print(canvas, "Drag Dominos from your inventory onto the chain to", 3, 2, (struct color) {255, 255, 255, 255});
|
||||
print(canvas, "reach the goal. Press [R] to rotate", 3, 9, (struct color) {255, 255, 255, 255});
|
||||
draw_text(canvas, inventory, (canvas.width - (sizeof(inventory)-1) * 5) / 2, canvas.height - DOMINO_WIDTH - 10, (struct color) {255, 255, 255, 255});
|
||||
draw_text(canvas, "Drag Dominos from your inventory onto the chain to", 3, 2, (struct color) {255, 255, 255, 255});
|
||||
draw_text(canvas, "reach the goal. Press [R] to rotate", 3, 9, (struct color) {255, 255, 255, 255});
|
||||
draw_glyph(canvas, glyph_heart, 3, 20, (struct color) {255, 0, 0, 255});
|
||||
draw_text(canvas, player_health_s, 10, 20, (struct color) {255, 255, 255, 255});
|
||||
|
||||
if (is_goal_reached) {
|
||||
print(canvas, game_over, (canvas.width - (sizeof(game_over)-1) * 5) / 2, 20, (struct color) {255, 255, 255, 255});
|
||||
draw_text(canvas, game_over, (canvas.width - (sizeof(game_over)-1) * 5) / 2, 20, (struct color) {255, 255, 255, 255});
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
4
main.c
4
main.c
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
#include "game.h"
|
||||
|
||||
unsigned int SCR_WIDTH = 800;
|
||||
unsigned int SCR_HEIGHT = 600;
|
||||
unsigned int SCR_WIDTH = 768;
|
||||
unsigned int SCR_HEIGHT = 720;
|
||||
|
||||
GLFWwindow* window;
|
||||
unsigned int texture;
|
||||
|
|
|
|||
58
minshell.html
Normal file
58
minshell.html
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
<!doctype html>
|
||||
<html lang="EN-us">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
||||
|
||||
<title>domino dungeon</title>
|
||||
|
||||
<style>
|
||||
body { margin: 0px; overflow: hidden; background-color: black; }
|
||||
canvas.emscripten { width: 100%; border: 0px none; background-color: black; }
|
||||
</style>
|
||||
<script type='text/javascript' src="https://cdn.jsdelivr.net/gh/eligrey/FileSaver.js/dist/FileSaver.min.js"> </script>
|
||||
<script type='text/javascript'>
|
||||
function saveFileFromMEMFSToDisk(memoryFSname, localFSname) // This can be called by C/C++ code
|
||||
{
|
||||
var isSafari = false; // Not supported, navigator.userAgent access is being restricted
|
||||
//var isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
|
||||
var data = FS.readFile(memoryFSname);
|
||||
var blob;
|
||||
|
||||
if (isSafari) blob = new Blob([data.buffer], { type: "application/octet-stream" });
|
||||
else blob = new Blob([data.buffer], { type: "application/octet-binary" });
|
||||
|
||||
// NOTE: SaveAsDialog is a browser setting. For example, in Google Chrome,
|
||||
// in Settings/Advanced/Downloads section you have a setting:
|
||||
// 'Ask where to save each file before downloading' - which you can set true/false.
|
||||
// If you enable this setting it would always ask you and bring the SaveAsDialog
|
||||
saveAs(blob, localFSname);
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<canvas class=emscripten id=canvas oncontextmenu=event.preventDefault() tabindex=-1></canvas>
|
||||
<p id="output" />
|
||||
<script>
|
||||
var Module = {
|
||||
print: (function() {
|
||||
var element = document.getElementById('output');
|
||||
if (element) element.value = ''; // clear browser cache
|
||||
return function(text) {
|
||||
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
|
||||
console.log(text);
|
||||
if (element) {
|
||||
element.value += text + "\n";
|
||||
element.scrollTop = element.scrollHeight; // focus on bottom
|
||||
}
|
||||
};
|
||||
})(),
|
||||
canvas: (function() {
|
||||
var canvas = document.getElementById('canvas');
|
||||
return canvas;
|
||||
})()
|
||||
};
|
||||
</script>
|
||||
{{{ SCRIPT }}}
|
||||
</body>
|
||||
</html>
|
||||
Loading…
Reference in a new issue