Compare commits

..

3 Commits

21 changed files with 109 additions and 312 deletions

View File

@ -7,20 +7,21 @@
"${workspaceFolder}/src",
"${workspaceFolder}/src/components",
"${workspaceFolder}/core",
"${workspaceFolder}/core/host/generic",
"${workspaceFolder}/core/host/linux",
"/usr/include/lua5.4",
"/usr/include/pipewire-0.3",
"/usr/include/spa-0.2",
"/usr/include/libdrm",
"/usr/include/libdecor-0",
"/usr/include/x86_64-linux-gnu",
"${env:HOME}/.local/share/fxsdk/sysroot/sh3eb-elf/include"
"/usr/include/x86_64-linux-gnu"//,
// "${env:HOME}/.local/share/fxsdk/sysroot/sh3eb-elf/include"
],
"compilerPath": "/usr/bin/gcc",
"defines": [
"_REENTRANT",
"FXCG50",
"TARGET_FXCG50"
"TARGET_FXCG50",
"ASSET_FILE_NAME=\"game.data\""
],
"cStandard": "c17",
"browse": {

View File

@ -82,7 +82,6 @@ $(ASSET_OUTPUT): FORCE
FORCE:
.PHONY: clean clean-all run print-var-%
clean:
$(Q)rm -rf $(BUILD_DIR)
@ -94,3 +93,5 @@ run: all
print-var-% :
@echo $($*)
.PHONY: clean clean-all run print-var-%

0
assets/maps/tiled-extensions/install.sh Normal file → Executable file
View File

21
configure vendored Normal file → Executable file
View File

@ -7,7 +7,7 @@
#
#
# Project settings no matter current and target hosts
# Project settings
#
CONFIG_FILE="config.mk"
@ -34,19 +34,23 @@ for i in "$@"; do
TARGET_HOST="${i#*=}"
shift
;;
--current-host=*)
HOST="${i#*=}"
shift
;;
--clean)
CLEAN=1
EXIT_AFTER_CLEAN=1
shift
;;
--reconfigure)
CLEAN=1
shift
;;
-h|--help)
echo "Usage : ./configure [OPTION]..."
echo "Add submodules depending on target host platform and"
@ -66,9 +70,11 @@ for i in "$@"; do
echo -e "\tshow help and exit"
exit 0
;;
-*)
exit_error_message "Unknown option $i"
;;
*)
;;
esac
@ -102,7 +108,7 @@ if [ "${HOST}" = "guess" ]; then
esac
fi
echo "Configuring for current host: ${HOST}"
echo "Configuring for host: ${HOST}"
# If target host is guess then configure for host
if [ "${TARGET_HOST}" = "guess" ]; then
@ -154,13 +160,16 @@ case "${TARGET_HOST}" in
TARGET_HOST_PKG_CONFIG=pkg-config
TARGET_HOST_CFLAGS=(
"--std=c17" "-Os"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-D_GNU_SOURCE=1"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\""
)
TARGET_HOST_LDFLAGS=(
"-lm"
)
TARGET_HOST_LDFLAGS=()
OUTPUT_EXT=""
DYNAMIC_LIB_OUTPUT_EXT=".so"
STATIC_LIB_OUTPUT_EXT=".a"
;;
*)
exit_error_message "Cross compiling from ${HOST} for ${TARGET_HOST} is not supported yet."
;;
@ -168,7 +177,6 @@ case "${TARGET_HOST}" in
TARGET_HOST_LIBS=(
"sdl3|3.2.10"
"lua|5.4.7"
)
;;
@ -176,7 +184,7 @@ case "${TARGET_HOST}" in
echo "Configuring for target: ${TARGET_HOST}"
case "${HOST}" in
windows)
windows|linux)
TARGET_HOST_CC=x86_64-w64-mingw32-gcc
TARGET_HOST_AR=x86_64-w64-mingw32-gcc-ar
TARGET_HOST_PKG_CONFIG=x86_64-w64-mingw32-pkg-config
@ -197,7 +205,6 @@ case "${TARGET_HOST}" in
TARGET_HOST_LIBS=(
"sdl3|3.3.4"
"lua|5.4.7"
)
;;

View File

@ -42,6 +42,14 @@ int destroy_asset(asset_t *asset)
return EXIT_SUCCESS;
}
// No args
int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args)
{
asset_header_def->name = NULL;
return EXIT_SUCCESS;
}
int destroy_asset_header_def(asset_header_def_t *asset_header_def)
{
free(asset_header_def->name);
@ -52,7 +60,7 @@ int destroy_asset_header_def(asset_header_def_t *asset_header_def)
int asset_manager_init(void)
{
linked_list_init(&game.asset_manager.assets, sizeof(asset_t), (allocator_t)create_asset, (deleter_t)destroy_asset);
linked_list_init(&game.asset_manager.assets_header_defs, sizeof(asset_header_def_t), NULL, (deleter_t)destroy_asset_header_def);
linked_list_init(&game.asset_manager.assets_header_defs, sizeof(asset_header_def_t), (allocator_t)create_asset_header_def, (deleter_t)destroy_asset_header_def);
game.asset_manager.asset_file = fopen(ASSET_FILE_NAME, "rb");
if(!game.asset_manager.asset_file)
@ -64,7 +72,7 @@ int asset_manager_init(void)
uint32_t nb_assets;
if(fread(&nb_assets, sizeof(nb_assets), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read asset file.");
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return EXIT_FAILURE;
}
@ -76,17 +84,33 @@ int asset_manager_init(void)
if(!asset_header_def_elem) return_failure_int;
asset_header_def_t *asset_header_def = asset_header_def_elem->data;
size_t cap = 0;
if(getdelim(&asset_header_def->name, &cap, '\0', game.asset_manager.asset_file) == -1)
{
error_printf("Failed to read asset file.");
size_t name_size = 0;
int ch = EOF;
do
{
name_size++;
asset_header_def->name = realloc(asset_header_def->name, name_size);
ch = fgetc(game.asset_manager.asset_file);
if(ch == EOF)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return EXIT_FAILURE;
}
asset_header_def->name[name_size - 1] = ch;
}
while(ch != '\0');
asset_header_def->name[name_size] = '\0';
}
if(fread(&asset_header_def->start, sizeof(asset_header_def->start), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read asset file.");
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return EXIT_FAILURE;
}

View File

@ -29,6 +29,7 @@ typedef struct asset_header_def_t {
uint32_t start;
} asset_header_def_t;
int create_asset_header_def(asset_header_def_t *asset_header_def, va_list args);
int destroy_asset_header_def(asset_header_def_t *asset_header_def);
typedef struct asset_manager_t {

View File

@ -1,28 +0,0 @@
#ifndef DISPLAY_H
#define DISPLAY_H
#include "texture.h"
#include "vector2d.h"
#define C_BLACK 0, 0, 0
#define C_RED 255, 0, 0
#define C_GREEN 0, 255, 0
#define C_BLUE 0, 0, 255
#define C_WHITE 255, 255, 255
#define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480
typedef struct display_t {
} display_t;
int display_init(void);
void display_exit(void);
int display_clear(void);
int display_update(void);
int display_draw_texture(texture_t *texture, const rect_t *src_rect, const rect_t *dst_rect, bool flip);
int display_draw_frect(const frect_t *frect, unsigned int r, unsigned int g, unsigned int b, unsigned int a);
#endif // DISPLAY_H

View File

@ -1,19 +0,0 @@
#ifndef KEYBOARD_H
#define KEYBOARD_H
#include <stdint.h>
#include <stdbool.h>
#include "keycodes.h"
typedef struct input_event_t {
uint32_t type;
int key;
} input_event_t;
int poll_event(input_event_t *event);
typedef struct inputs_t {
bool keys[MAX_KEYCODES];
} inputs_t;
#endif // KEYBOARD_H

View File

@ -1,17 +0,0 @@
#ifndef KEYCODES_H
#define KEYCODES_H
#define MAX_KEYCODES 0xFF
#define KEY_UP 0x01
#define KEY_DOWN 0x02
#define KEY_LEFT 0x03
#define KEY_RIGHT 0x04
#define KEY_MOD 0x05
#define KEY_INTERACT 0x06
#define INPUT_EVENT_KEY_UP 0x01
#define INPUT_EVENT_KEY_DOWN 0x02
#define INPUT_EVENT_QUIT 0xFF
#endif // KEYCODES_H

View File

@ -1,6 +0,0 @@
#ifndef MEMORY_ALLOC_H
#define MEMORY_ALLOC_H
#include <stdlib.h>
#endif // MEMORY_ALLOC_H

View File

@ -1,14 +0,0 @@
#ifndef TEXTURE_H
#define TEXTURE_H
#define TEXTURE_TRANSPARENT_COLOR 0x0001
typedef struct texture_t texture_t;
#define get_width(T) 1
#define get_height(T) 1
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);
#endif // TEXTURE_H

View File

@ -1,6 +0,0 @@
#ifndef TIME_H
#define TIME_H
void update_time(void);
#endif // TIME_H

View File

@ -1,163 +0,0 @@
#ifndef VECTOR2D_H
#define VECTOR2D_H
#include <SDL3/SDL_rect.h>
#include <math.h>
/**
* @brief Point struct. (int)
*
* @param x X pos
* @param y Y pos
*/
typedef struct vector2d_t {
int x, y;
} vector2d_t;
/**
* @brief Point struct. (float)
*
* @param x X pos
* @param y Y pos
*/
typedef struct fvector2d_t {
float x, y;
} fvector2d_t;
/**
* @brief Rectangle struct. (int)
*
* @param x X pos
* @param y Y pos
* @param w Width
* @param h Height
*/
typedef struct rect_t {
int x, y;
int w, h;
} rect_t;
/**
* @brief Rectangle struct. (float)
*
* @param x X pos
* @param y Y pos
* @param w Width
* @param h Height
*/
typedef struct frect_t {
float x, y;
float w, h;
} frect_t;
#define EPSILON 0.0f
#define f_is_zero(X) (fabsf(X) <= EPSILON)
#define f_is_not_zero(X) (fabsf(X) > EPSILON)
void rect_to_frect(const rect_t *rect, frect_t *frect);
void frect_to_rect(const frect_t *frect, rect_t *rect);
/**
* @brief Verify if a point is in a rectangle. (int)
*
* @param p Vector2d
* @param r Rectangle
*
* @return True if the point is in the rectangle, else false.
*/
bool point_in_rect(const vector2d_t *p, const rect_t *r);
/**
* @brief Verify if a point is in a rectangle. (float)
*
* @param p Vector2d
* @param r Rectangle
*
* @return True if the point is in the rectangle, else false.
*/
bool fpoint_in_rect(const fvector2d_t *p, const frect_t *r);
/**
* @brief Verify if a rectangle is empty
*
* @param r Rectangle
*
* @return True if the rectangle is empty, else false.
*/
bool rect_empty(const rect_t *r);
/**
* @brief Verify if a rectangle is empty
*
* @param r Rectangle
*
* @return True if the rectangle is empty, else false.
*/
bool frect_empty(const frect_t *r);
/**
* @brief Verify if there is a intersction between two rectangles. (int)
*
* @param A Rectangle A
* @param B Rectangle B
*
* @return True if there is an intersection, else false.
*/
bool has_intersection(const rect_t *a, const rect_t *b);
/**
* @brief Verify if there is a intersction between two rectangles. (float)
*
* @param A Rectangle A
* @param B Rectangle B
*
* @return True if there is an intersection, else false.
*/
bool fhas_intersection(const frect_t *a, const frect_t *b);
/**
* @brief Verify if there is an intersection between two rectangles and get the intersection rectangle. (int)
*
* @param A Rectangle A
* @param B Rectangle B
* @param result The intersection rectangle
*
* @return True if there is an intersection, else false.
*/
bool intersect_rect(const rect_t *a, const rect_t *b, rect_t *result);
/**
* @brief Verify if there is an intersection between two rectangles and get the intersection rectangle. (float)
*
* @param A Rectangle A
* @param B Rectangle B
* @param result The intersection rectangle
*
* @return True if there is an intersection, else false.
*/
bool intersect_frect(const frect_t *a, const frect_t *b, frect_t *result);
/**
* @brief Lerp a to b with smoothing factor s.
*
* @param a Current value
* @param b Target value
* @param s Smoothing factor
*/
float lerp(float a, float b, float s);
/**
* @brief Lerp a to b with smoothing factor s and time basis t.
* Smoothing factor is for 60 fps call time.
*
* @param a Current value
* @param b Target value
* @param s Smoothing factor
* @param t Delta time
*/
float lerp_time(float a, float b, float s, float t);
#endif // VECTOR2D_H

View File

@ -7,6 +7,8 @@
#include "texture.h"
#include "errors.h"
#define DRIVERS "vulkan,gpu,software"
inline int display_init(void)
{
if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS))
@ -29,13 +31,13 @@ inline int display_init(void)
}
#ifdef DRIVERS
warning_printf("This build of 2D_Engine_C only innitialize for the following drivers : %s", DRIVERS);
debug_printf("Listing available render drivers.");
const int NB_DRIVERS = SDL_GetNumRenderDrivers();
for(int i = 0; i < NB_DRIVERS; i++)
debug_printf("%d : %s", i, SDL_GetRenderDriver(i));
warning_printf("This build of 2D_Engine_C only support the following drivers : %s", DRIVERS);
game.display.renderer = SDL_CreateRenderer(game.display.window, DRIVERS);
#else
game.display.renderer = SDL_CreateRenderer(game.display.window, NULL);

View File

@ -14,8 +14,6 @@
#define DISPLAY_WIDTH 720
#define DISPLAY_HEIGHT 480
#define DRIVERS "vulkan,gpu,software"
typedef struct display_t {
SDL_Window *window;
SDL_Renderer *renderer;

View File

@ -11,13 +11,13 @@ texture_t *load_texture(void)
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -36,7 +36,7 @@ texture_t *load_texture(void)
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
{
error_printf("Failed to read asset file.");
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
goto_failure;
}

View File

@ -7,8 +7,8 @@
typedef SDL_Texture texture_t;
#define get_width(T) T->w
#define get_height(T) T->h
#define texture_get_width(T) T->w
#define texture_get_height(T) T->h
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);

View File

@ -46,25 +46,25 @@ inline int display_init(void)
error_printf("Failed to create renderer : %s", SDL_GetError());
return EXIT_FAILURE;
}
/*
if(!SDL_SetRenderVSync(game.display.renderer, SDL_RENDERER_VSYNC_ADAPTIVE))
{
error_printf("Failed to activate adaptative vsync : %s", SDL_GetError());
return EXIT_FAILURE;
}
*/
if(!SDL_SetRenderDrawBlendMode(game.display.renderer, SDL_BLENDMODE_BLEND))
{
error_printf("Failed to set renderer draw blend mode : %s", SDL_GetError());
return EXIT_FAILURE;
}
/*
if(!SDL_SetDefaultTextureScaleMode(game.display.renderer, SDL_SCALEMODE_NEAREST))
{
error_printf("Failed to set default texture scale mode : %s", SDL_GetError());
return EXIT_FAILURE;
}
*/
debug_printf("Initialized display.");
return EXIT_SUCCESS;

View File

@ -11,13 +11,13 @@ texture_t *load_texture(void)
if(fread(&width, sizeof(width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&height, sizeof(height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file.");
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -36,7 +36,7 @@ texture_t *load_texture(void)
if(fread(tmp_surface->pixels, sizeof(uint16_t), width * height, game.asset_manager.asset_file) != (size_t)width * (size_t)height)
{
error_printf("Failed to read asset file.");
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
goto_failure;
}

View File

@ -7,8 +7,8 @@
typedef SDL_Texture texture_t;
#define get_width(T) T->w
#define get_height(T) T->h
#define texture_get_width(T) T->w
#define texture_get_height(T) T->h
texture_t *load_texture(void);
void destroy_texture(texture_t *texture);

View File

@ -15,19 +15,19 @@ map_t *load_map(void)
if(fread(&map->width, sizeof(map->width), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&map->height, sizeof(map->height), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&map->backgroundlayernb, sizeof(map->backgroundlayernb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -42,7 +42,7 @@ map_t *load_map(void)
uint16_t *backgroundlayer = backgroundlayer_elem->data;
if(fread(backgroundlayer, sizeof(*backgroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -52,20 +52,20 @@ map_t *load_map(void)
map->foregroundlayer = malloc(map->width * map->height * sizeof(*map->foregroundlayer));
if(fread(map->foregroundlayer, sizeof(*map->foregroundlayer), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
map->hitboxes = malloc(map->width * map->height * sizeof(*map->hitboxes));
if(fread(map->hitboxes, sizeof(*map->hitboxes), map->width * map->height, game.asset_manager.asset_file) != (size_t)map->width * (size_t)map->height)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&map->entities_nb, sizeof(map->entities_nb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -77,13 +77,13 @@ map_t *load_map(void)
if(fread(&entity_def->id, sizeof(entity_def->id), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
if(fread(&entity_def->components_nb, sizeof(entity_def->components_nb), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -95,7 +95,7 @@ map_t *load_map(void)
if(fread(&component_def->type, sizeof(component_def->type), 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
@ -109,19 +109,35 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
case SPRITE_COMPONENT:
{
size_t cap = 0;
if(getdelim((char **)&component_def->attributes, &cap, '\0', game.asset_manager.asset_file) == -1)
size_t name_size = 0;
int ch = EOF;
do
{
error_printf("Failed to read in asset file : %d", errno);
name_size++;
component_def->attributes = realloc(component_def->attributes, name_size);
ch = fgetc(game.asset_manager.asset_file);
if(ch == EOF)
{
error_printf("Failed to read asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
((uint8_t *)component_def->attributes)[name_size - 1] = ch;
}
while(ch != '\0');
((uint8_t *)component_def->attributes)[name_size] = '\0';
break;
}
@ -130,7 +146,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
@ -140,7 +156,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
@ -150,7 +166,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
@ -160,7 +176,7 @@ map_t *load_map(void)
component_def->attributes = malloc(data_size);
if(fread(component_def->attributes, data_size, 1, game.asset_manager.asset_file) != 1)
{
error_printf("Failed to read in asset file : %d", errno);
error_printf("Failed to read in asset file : %d", ferror(game.asset_manager.asset_file));
return NULL;
}
break;
@ -406,8 +422,8 @@ static inline int draw_tile(uint16_t tile_x, uint16_t tile_y, uint16_t tile_id)
if(!tile_id) return EXIT_SUCCESS;
rect_t src_frect = {
.x = tile_id * TILE_SIZE % get_width(game.map->tileset),
.y = tile_id * TILE_SIZE / get_width(game.map->tileset) * TILE_SIZE,
.x = tile_id * TILE_SIZE % texture_get_width(game.map->tileset),
.y = tile_id * TILE_SIZE / texture_get_width(game.map->tileset) * TILE_SIZE,
.w = TILE_SIZE,
.h = TILE_SIZE
};