Browse Source

engine base

main
KikooDX 4 months ago
commit
9d7549e57b
  1. 9
      .clang-format
  2. 5
      .gitignore
  3. 45
      CMakeLists.txt
  4. BIN
      assets/graphics/icon-sel.png
  5. BIN
      assets/graphics/icon-uns.png
  6. 22
      include/entity.h
  7. 12
      include/player.h
  8. 7
      include/tiles.h
  9. 8
      include/wall.h
  10. 34
      src/entity/collide.c
  11. 23
      src/entity/draw_hitbox.c
  12. 46
      src/entity/init.c
  13. 36
      src/main.c
  14. 12
      src/player/init.c
  15. 8
      src/wall/init.c

9
.clang-format

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

5
.gitignore

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

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

BIN
assets/graphics/icon-sel.png

After

Width: 92  |  Height: 64  |  Size: 2.4 KiB

BIN
assets/graphics/icon-uns.png

After

Width: 92  |  Height: 64  |  Size: 593 B

22
include/entity.h

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

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

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

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

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

23
src/entity/draw_hitbox.c

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

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

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

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

@ -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);
}
Loading…
Cancel
Save