commit 9d7549e57b25dfe8fcfaa74967b0611c81513f8f Author: KikooDX Date: Fri Jun 11 01:32:47 2021 +0200 engine base diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..57fd90a --- /dev/null +++ b/.clang-format @@ -0,0 +1,9 @@ +BasedOnStyle: LLVM +IndentWidth: 8 +UseTab: AlignWithSpaces +BreakBeforeBraces: Linux +AllowShortIfStatementsOnASingleLine: false +IndentCaseLabels: false +ColumnLimit: 80 +AlignConsecutiveMacros: true +AlwaysBreakAfterReturnType: TopLevelDefinitions diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f867ea7 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build-cg/ +build-fx/ +*.g3a +CMakeLists.txt~ +*.png~ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4842738 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,45 @@ +# Configure with [fxsdk build-fx] or [fxsdk build-cg], which provide the +# toolchain file and module path of the fxSDK + +cmake_minimum_required(VERSION 3.18) +project(RC C) + +include(GenerateG3A) +include(Fxconv) + +find_package(Gint 2.5.3 REQUIRED) +find_package(LibImg 2.4.0 REQUIRED) + +include_directories(include) + +set(SOURCES + src/main.c + src/entity/collide.c + src/entity/draw_hitbox.c + src/entity/init.c + src/wall/init.c + src/player/init.c +) + +set(ASSETS +) + +set(FLAGS + -Wall -Wextra -Wshadow -Wswitch-default -Wswitch-enum + -Wunreachable-code -Wstrict-prototypes -Wmissing-prototypes + -Wold-style-definition -Werror-implicit-function-declaration + -fms-extensions -std=c99 -O3 +) + +fxconv_declare_assets(${ASSETS} WITH_METADATA) + +add_executable(${PROJECT_NAME} ${SOURCES} ${ASSETS}) +target_compile_options(${PROJECT_NAME} PRIVATE ${FLAGS}) +target_link_libraries(${PROJECT_NAME} Gint::Gint) +target_link_libraries(${PROJECT_NAME} LibImg::LibImg) +target_link_options(${PROJECT_NAME} PRIVATE -Wl,-Map=map) + +generate_g3a(TARGET ${PROJECT_NAME} + OUTPUT "${PROJECT_NAME}.g3a" + NAME "${PROJECT_NAME}" + ICONS assets/graphics/icon-uns.png assets/graphics/icon-sel.png) diff --git a/assets/graphics/icon-sel.png b/assets/graphics/icon-sel.png new file mode 100644 index 0000000..e37d124 Binary files /dev/null and b/assets/graphics/icon-sel.png differ diff --git a/assets/graphics/icon-uns.png b/assets/graphics/icon-uns.png new file mode 100644 index 0000000..cd5bb32 Binary files /dev/null and b/assets/graphics/icon-uns.png differ diff --git a/include/entity.h b/include/entity.h new file mode 100644 index 0000000..f4b5810 --- /dev/null +++ b/include/entity.h @@ -0,0 +1,22 @@ +#pragma once +#include + +struct Entity { + int x; + int y; + int hb_x; + int hb_y; + int hb_w; + int hb_h; + color_t hb_color; +}; + +#define ENTITY_GRID_SIZE 256 +extern struct Entity *g_entity_grid[ENTITY_GRID_SIZE]; + +void entity_init(void *restrict entity, int x, int y, int hb_x, int hb_y, + int hb_w, int hb_h, color_t hb_color); +void entity_deinit(void *restrict entity); +void entity_draw_hitbox(void *restrict entity); +void entity_grid_draw_hitboxes(void); +int entity_collide(void *restrict entity); diff --git a/include/player.h b/include/player.h new file mode 100644 index 0000000..4a85313 --- /dev/null +++ b/include/player.h @@ -0,0 +1,12 @@ +#pragma once +#include "entity.h" + +struct Player { + struct Entity; + float spd_x; + float spd_y; + float rem_x; + float rem_y; +}; + +void player_init(struct Player *restrict, int x, int y); diff --git a/include/tiles.h b/include/tiles.h new file mode 100644 index 0000000..8e7aac6 --- /dev/null +++ b/include/tiles.h @@ -0,0 +1,7 @@ +#pragma once + +enum Tile { + TILE_VOID, + TILE_SOLID, +}; +#define TILE_OOB TILE_VOID diff --git a/include/wall.h b/include/wall.h new file mode 100644 index 0000000..402735c --- /dev/null +++ b/include/wall.h @@ -0,0 +1,8 @@ +#pragma once +#include "entity.h" + +struct Wall { + struct Entity; +}; + +void wall_init(struct Wall *restrict, int x, int y, int width, int height); diff --git a/src/entity/collide.c b/src/entity/collide.c new file mode 100644 index 0000000..5111879 --- /dev/null +++ b/src/entity/collide.c @@ -0,0 +1,34 @@ +#include "entity.h" + +static int entity_collide_with(struct Entity *restrict, + struct Entity *restrict); + +int +entity_collide(void *restrict entity) +{ + struct Entity *const e = entity; + int i; + + i = ENTITY_GRID_SIZE; + while (i-- > 0) + if (entity_collide_with(e, g_entity_grid[i])) + return 1; + + return 0; +} + +/* axis aligned bounding box collision checking */ +static int +entity_collide_with(struct Entity *restrict self, struct Entity *restrict other) +{ + const int sx = self->x + self->hb_x; + const int sy = self->y + self->hb_y; + const int ox = other->x + other->hb_x; + const int oy = other->y + other->hb_y; + + if (self == NULL || other == NULL || self == other) + return 0; + + return sx < ox + other->hb_w && sx + self->hb_w > ox && + sy < oy + other->hb_h && sy + self->hb_h > oy; +} diff --git a/src/entity/draw_hitbox.c b/src/entity/draw_hitbox.c new file mode 100644 index 0000000..4f0629f --- /dev/null +++ b/src/entity/draw_hitbox.c @@ -0,0 +1,23 @@ +#include "entity.h" +#include + +void +entity_draw_hitbox(void *restrict entity) +{ + const struct Entity *const e = entity; + const int x = e->x + e->hb_x; + const int y = e->y + e->hb_y; + drect_border(e->x, e->y, e->x + 2, e->y + 2, C_NONE, 1, e->hb_color); + drect_border(x, y, x + e->hb_w - 1, y + e->hb_h - 1, C_NONE, 1, + e->hb_color); +} + +void +entity_grid_draw_hitboxes(void) +{ + int i; + i = ENTITY_GRID_SIZE; + while (i-- > 0) + if (g_entity_grid[i] != NULL) + entity_draw_hitbox(g_entity_grid[i]); +} diff --git a/src/entity/init.c b/src/entity/init.c new file mode 100644 index 0000000..d29facd --- /dev/null +++ b/src/entity/init.c @@ -0,0 +1,46 @@ +#include "entity.h" +#include + +struct Entity *g_entity_grid[ENTITY_GRID_SIZE]; + +void +entity_init(void *restrict entity, int x, int y, int hb_x, int hb_y, int hb_w, + int hb_h, color_t hb_color) +{ + static int init_grid = 1; + struct Entity *const e = entity; + int i; + e->x = x; + e->y = y; + e->hb_x = hb_x; + e->hb_y = hb_y; + e->hb_w = hb_w; + e->hb_h = hb_h; + e->hb_color = hb_color; + + if (init_grid) { + init_grid = 0; + i = ENTITY_GRID_SIZE; + while (i-- > 0) + g_entity_grid[i] = NULL; + } + + /* add to grid */ + i = ENTITY_GRID_SIZE; + while (i-- > 0) + if (g_entity_grid[i] == NULL) + break; + g_entity_grid[i] = entity; +} + +void +entity_deinit(void *restrict entity) +{ + int i; + /* remove from grid */ + i = ENTITY_GRID_SIZE; + while (i-- > 0) + if (g_entity_grid[i] == entity) + break; + g_entity_grid[i] = NULL; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..c875535 --- /dev/null +++ b/src/main.c @@ -0,0 +1,36 @@ +#include "entity.h" +#include "player.h" +#include "wall.h" +#include +#include + +void +main(void) +{ + struct Player player; + struct Wall walls[5]; + + player_init(&player, 16, 32); + wall_init(&walls[0], 16, 0, DWIDTH - 32, 16); + wall_init(&walls[1], 16, DHEIGHT - 16, DWIDTH - 32, 16); + wall_init(&walls[2], 0, 0, 16, DHEIGHT); + wall_init(&walls[3], DWIDTH - 16, 0, 16, DHEIGHT); + wall_init(&walls[4], DWIDTH / 2 - 24, DHEIGHT - 64, 48, 48); + + do { + const int previous_x = player.x; + const int previous_y = player.y; + dclear(C_BLACK); + entity_grid_draw_hitboxes(); + dupdate(); + clearevents(); + player.x += keydown(KEY_RIGHT) - keydown(KEY_LEFT); + player.y += keydown(KEY_DOWN) - keydown(KEY_UP); + if (entity_collide(&player)) { + player.x = previous_x; + player.y = previous_y; + } + } while (!keydown(KEY_EXIT)); + + return; +} diff --git a/src/player/init.c b/src/player/init.c new file mode 100644 index 0000000..f250f97 --- /dev/null +++ b/src/player/init.c @@ -0,0 +1,12 @@ +#include "player.h" +#include + +void +player_init(struct Player *restrict p, int x, int y) +{ + entity_init(p, x, y, 2, 2, 12, 12, C_BLUE); + p->spd_x = 0.0; + p->spd_y = 0.0; + p->rem_x = 0.0; + p->rem_y = 0.0; +} diff --git a/src/wall/init.c b/src/wall/init.c new file mode 100644 index 0000000..8b3fc2c --- /dev/null +++ b/src/wall/init.c @@ -0,0 +1,8 @@ +#include "wall.h" +#include + +void +wall_init(struct Wall *restrict w, int x, int y, int width, int height) +{ + entity_init(w, x, y, 0, 0, width, height, C_DARK); +}