engine base

This commit is contained in:
KikooDX 2021-06-11 01:32:47 +02:00
commit 9d7549e57b
15 changed files with 267 additions and 0 deletions

9
.clang-format Normal file
View File

@ -0,0 +1,9 @@
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: AlignWithSpaces
BreakBeforeBraces: Linux
AllowShortIfStatementsOnASingleLine: false
IndentCaseLabels: false
ColumnLimit: 80
AlignConsecutiveMacros: true
AlwaysBreakAfterReturnType: TopLevelDefinitions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
build-cg/
build-fx/
*.g3a
CMakeLists.txt~
*.png~

45
CMakeLists.txt Normal file
View File

@ -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)

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 593 B

22
include/entity.h Normal file
View File

@ -0,0 +1,22 @@
#pragma once
#include <gint/display.h>
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);

12
include/player.h Normal file
View File

@ -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);

7
include/tiles.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
enum Tile {
TILE_VOID,
TILE_SOLID,
};
#define TILE_OOB TILE_VOID

8
include/wall.h Normal file
View File

@ -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);

34
src/entity/collide.c Normal file
View File

@ -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;
}

23
src/entity/draw_hitbox.c Normal file
View File

@ -0,0 +1,23 @@
#include "entity.h"
#include <gint/display.h>
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]);
}

46
src/entity/init.c Normal file
View File

@ -0,0 +1,46 @@
#include "entity.h"
#include <gint/display.h>
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;
}

36
src/main.c Normal file
View File

@ -0,0 +1,36 @@
#include "entity.h"
#include "player.h"
#include "wall.h"
#include <gint/display.h>
#include <gint/keyboard.h>
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;
}

12
src/player/init.c Normal file
View File

@ -0,0 +1,12 @@
#include "player.h"
#include <gint/display.h>
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;
}

8
src/wall/init.c Normal file
View File

@ -0,0 +1,8 @@
#include "wall.h"
#include <gint/display.h>
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);
}