Merge branch 'template-test' into template

This commit is contained in:
Sara 2023-11-23 20:04:09 +01:00
commit adaa01ae4b
23 changed files with 225 additions and 39 deletions

4
.gitignore vendored
View file

@ -1,4 +1,6 @@
.cache/clangd/index .vscode
.cache
compile_commands/
bin/ bin/
build/ build/
intermediate/ intermediate/

View file

@ -5,7 +5,7 @@ workspace "Fencer-Template"
startproject "Game" startproject "Game"
filter "system:windows" filter "system:windows"
defines { "SDL_MAIN_HANDLED" } defines { "SDL_MAIN_HANDLED", "_CRT_SECURE_NO_WARNINGS" }
buildoptions { "/EHsc", "/Zc:preprocessor" } buildoptions { "/EHsc", "/Zc:preprocessor" }
OutputDir = "%{cfg.system}-%{cfg.architecture}/%{cfg.buildcfg}" OutputDir = "%{cfg.system}-%{cfg.architecture}/%{cfg.buildcfg}"

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.2 MiB

View file

@ -0,0 +1,58 @@
#include "animation_sprite.h"
#include "debug.h"
#include "program.h"
struct AnimationSprite {
Spritesheet* sheet;
Sprite* sprite;
AnimationSpriteLoopMode loop_mode;
float frame_interval;
float start_time;
};
AnimationSprite* animation_sprite_new(Spritesheet* sheet, float framerate) {
AnimationSprite* self = malloc(sizeof(AnimationSprite));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate memory for AnimationSprite");
*self = (AnimationSprite){
.sheet = sheet,
.frame_interval = 1.0f / framerate,
.loop_mode = LoopMode_Loop,
.start_time = game_time(),
.sprite = sprite_from_spritesheet(sheet, 0)
};
return self;
}
void animation_sprite_destroy(AnimationSprite* self) {
sprite_destroy(self->sprite);
spritesheet_destroy(self->sheet);
free(self);
}
void animation_sprite_play_from(AnimationSprite* self, float normalized_time) {
self->start_time = game_time() - normalized_time * animation_sprite_get_length(self);
}
void animation_sprite_draw(AnimationSprite* self, Transform* transform) {
const float time = game_time() - self->start_time;
const size_t frame = time / self->frame_interval;
sprite_set_spritesheet(self->sprite, self->sheet);
sprite_set_tile(self->sprite, frame);
sprite_draw(self->sprite, *transform);
}
float animation_sprite_get_length(AnimationSprite* self) {
return (float)spritesheet_get_tile_count(self->sheet) * self->frame_interval;
}
void animation_sprite_set_framerate(AnimationSprite* self, float framerate) {
self->frame_interval = 1.0f / framerate;
}
float animation_sprite_get_framerate(const AnimationSprite* self) {
return 1.0f / self->frame_interval;
}

View file

@ -0,0 +1,26 @@
#ifndef _fencer_animation_sprite_h
#define _fencer_animation_sprite_h
#include "sprite.h"
#include "spritesheet.h"
typedef enum AnimationSpriteLoopMode {
LoopMode_Stop,
LoopMode_Hide,
LoopMode_Loop,
LoopMode_PingPong,
} AnimationSpriteLoopMode;
typedef struct AnimationSprite AnimationSprite;
extern AnimationSprite* animation_sprite_new(Spritesheet* sheet, float framerate);
extern void animation_sprite_destroy(AnimationSprite* self);
extern void animation_sprite_play_from(AnimationSprite* self, float normalized_time);
extern void animation_sprite_draw(AnimationSprite* self, Transform* transform);
extern float animation_sprite_get_length(AnimationSprite* self);
extern void animation_sprite_set_framerate(AnimationSprite* self, float framerate);
extern float animation_sprite_get_framerate(const AnimationSprite* self);
#endif // !_fencer_animation_sprite_h

View file

@ -10,16 +10,16 @@ static asset_id _next_id = 0;
static static
size_t file_length(FILE* fp) { size_t file_length(FILE* fp) {
size_t start = ftell(fp); long start = ftell(fp);
fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_END);
size_t r = ftell(fp); long r = ftell(fp);
fseek(fp, start, SEEK_SET); fseek(fp, start, SEEK_SET);
return r; return (size_t)r;
} }
static static
void read_file(FILE* fp, char* out_buffer, size_t out_size) { void read_file(FILE* fp, char* out_buffer, size_t out_size) {
size_t start = ftell(fp); long start = ftell(fp);
fread(out_buffer, 1, out_size, fp); fread(out_buffer, 1, out_size, fp);
fseek(fp, start, SEEK_SET); fseek(fp, start, SEEK_SET);
} }
@ -88,15 +88,13 @@ asset_id get_asset_id(void* asset) {
} }
void free_asset(asset_id id) { void free_asset(asset_id id) {
Asset* found; Asset* found = NULL;
size_t found_index; size_t found_index = _assets.len;
for(size_t i = 0; i < _assets.len; ++i) { for(size_t i = 0; i < _assets.len; ++i) {
found = list_at_as(Asset, &_assets, i); found = list_at_as(Asset, &_assets, i);
if(found->tc->get_id(found->data) == id) { if(found->tc->get_id(found->data) == id) {
found_index = i; found_index = i;
break; break;
} else {
found = NULL;
} }
} }
ASSERT_RETURN(found != NULL,, "Attempt to free nonexistent asset."); ASSERT_RETURN(found != NULL,, "Attempt to free nonexistent asset.");

View file

@ -31,8 +31,8 @@ size_t json_array_len(cJSON* array);
static inline static inline
Vector json_array_to_vector(cJSON* array) { Vector json_array_to_vector(cJSON* array) {
return (Vector) { return (Vector) {
cJSON_GetArrayItem(array, 0)->valuedouble, (float)cJSON_GetArrayItem(array, 0)->valuedouble,
cJSON_GetArrayItem(array, 1)->valuedouble, (float)cJSON_GetArrayItem(array, 1)->valuedouble,
}; };
} }
static inline static inline

View file

@ -21,7 +21,7 @@ SDL_FRect camera_world_to_pixel_rect(Camera* self, SDL_FRect* world_space) {
t.scale = OneVector; t.scale = OneVector;
t = transform_invert(t); t = transform_invert(t);
Vector tl = {world_space->x + (self->fov / 2.0), world_space->y + (_camera_height(self) / 2.0)}; Vector tl = {world_space->x + (self->fov / 2.0f), world_space->y + (_camera_height(self) / 2.0f)};
Vector size = {world_space->w, world_space->h}; Vector size = {world_space->w, world_space->h};
tl = vmulff(transform_point(&t, tl), g_render_resolution.x / self->fov); tl = vmulff(transform_point(&t, tl), g_render_resolution.x / self->fov);
@ -40,7 +40,7 @@ Vector camera_world_to_pixel_point(Camera* self, Vector point) {
t.scale = OneVector; t.scale = OneVector;
t = transform_invert(t); t = transform_invert(t);
point = (Vector){point.x + (self->fov / 2.0), point.y + (_camera_height(self) / 2.0)}; point = (Vector){point.x + (self->fov / 2.0f), point.y + (_camera_height(self) / 2.0f)};
return vmulff(transform_point(&t, point), g_render_resolution.x / self->fov); return vmulff(transform_point(&t, point), g_render_resolution.x / self->fov);
} }

6
core/src/drop.c Normal file
View file

@ -0,0 +1,6 @@
#include "drop.h"
#include "stdlib.h"
void default_drop(void* data) {
free(data);
}

View file

@ -19,8 +19,13 @@ static inline Drop T##_as_Drop(T* x) {\
return (Drop){.tc = &tc, .data = x};\ return (Drop){.tc = &tc, .data = x};\
} }
#define impl_default_Drop_for(T)\ extern void default_drop(void*);
static void default_drop_##T(T* v) { free(v); }\
impl_Drop_for(T, default_drop_##T)
#define impl_default_Drop_for(T)\
static inline Drop T##_as_Drop(T* x) {\
static IDrop const tc = {\
.drop = default_drop,\
};\
return (Drop){.tc = &tc, .data = x};\
}
#endif // !_fencer_drop_h #endif // !_fencer_drop_h

View file

@ -73,7 +73,7 @@ void game_world_update() {
_internal_clear_removed(); _internal_clear_removed();
} }
void game_word_draw() { void game_world_draw() {
list_foreach(BehaviourEntity*, entity, &_game_entities) { list_foreach(BehaviourEntity*, entity, &_game_entities) {
entity->tc->draw(entity->data); entity->tc->draw(entity->data);
} }

View file

@ -4,6 +4,7 @@
KeyBind* keybind_new(SDL_Scancode key) { KeyBind* keybind_new(SDL_Scancode key) {
KeyBind* self = malloc(sizeof(KeyBind)); KeyBind* self = malloc(sizeof(KeyBind));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for KeyBind instance");
*self = (KeyBind) { *self = (KeyBind) {
.device = NULL, .device = NULL,
.scancode = key, .scancode = key,
@ -31,6 +32,7 @@ void keybind_set_device(KeyBind* self, InputDevice* device) {
ControllerAxis* controlleraxis_new(int axis) { ControllerAxis* controlleraxis_new(int axis) {
ControllerAxis* self = malloc(sizeof(ControllerAxis)); ControllerAxis* self = malloc(sizeof(ControllerAxis));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for ControllerAxis instance");
*self = (ControllerAxis){ *self = (ControllerAxis){
.axis = axis, .axis = axis,
.device = NULL .device = NULL
@ -48,7 +50,6 @@ int controlleraxis_is_changed_by(ControllerAxis* self, SDL_Event event) {
InputEvent controlleraxis_evaluate(ControllerAxis* self, SDL_Event event) { InputEvent controlleraxis_evaluate(ControllerAxis* self, SDL_Event event) {
float result = (float)event.caxis.value / 32767.0; float result = (float)event.caxis.value / 32767.0;
LOG_INFO("axis %f", result);
return (InputEvent) { return (InputEvent) {
.type = InputEvent_Float, .type = InputEvent_Float,
.as_float = result .as_float = result
@ -61,6 +62,7 @@ void controlleraxis_set_device(ControllerAxis* self, InputDevice* device) {
ControllerButton* controllerbutton_new(int button) { ControllerButton* controllerbutton_new(int button) {
ControllerButton* self = malloc(sizeof(ControllerButton)); ControllerButton* self = malloc(sizeof(ControllerButton));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for ControllerButton instance");
*self = (ControllerButton) { *self = (ControllerButton) {
.button = button, .button = button,
.device = NULL .device = NULL
@ -88,6 +90,7 @@ void controllerbutton_set_device(ControllerButton* self, InputDevice* device) {
CompositeAxis1D* compositeaxis1d_new(InputAxis left, InputAxis right, InputEventType type) { CompositeAxis1D* compositeaxis1d_new(InputAxis left, InputAxis right, InputEventType type) {
CompositeAxis1D* self = malloc(sizeof(CompositeAxis1D)); CompositeAxis1D* self = malloc(sizeof(CompositeAxis1D));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for CompositeAxis1D instance");
*self = (CompositeAxis1D) { *self = (CompositeAxis1D) {
.left = left, .left = left,
.right = right, .right = right,

View file

@ -18,11 +18,11 @@ void physics_entity_debug_draw(PhysicsEntity self) {
lhs = camera_world_to_pixel_point(&g_camera, lhs); lhs = camera_world_to_pixel_point(&g_camera, lhs);
rhs = camera_world_to_pixel_point(&g_camera, rhs); rhs = camera_world_to_pixel_point(&g_camera, rhs);
SDL_SetRenderDrawColor(g_renderer, 0, 255, 0, 255); SDL_SetRenderDrawColor(g_renderer, 0, 255, 0, 255);
SDL_RenderDrawLine(g_renderer, lhs.x, lhs.y, rhs.x, rhs.y); SDL_RenderDrawLineF(g_renderer, lhs.x, lhs.y, rhs.x, rhs.y);
rhs = camera_world_to_pixel_point(&g_camera, vaddf(transform->position, rigidbody_get_force(body))); rhs = camera_world_to_pixel_point(&g_camera, vaddf(transform->position, rigidbody_get_force(body)));
SDL_SetRenderDrawColor(g_renderer, 0, 255, 255, 255); SDL_SetRenderDrawColor(g_renderer, 0, 255, 255, 255);
SDL_RenderDrawLine(g_renderer, lhs.x, lhs.y, rhs.x, rhs.y); SDL_RenderDrawLineF(g_renderer, lhs.x, lhs.y, rhs.x, rhs.y);
} }
static inline static inline
Vector _internal_calculate_contact_force(RigidBody* self, Contact* contact) { Vector _internal_calculate_contact_force(RigidBody* self, Contact* contact) {

View file

@ -1,7 +1,9 @@
#include "player_input.h" #include "player_input.h"
#include "debug.h"
PlayerInput* playerinput_new(void* target, int device) { PlayerInput* playerinput_new(void* target, int device) {
PlayerInput* self = malloc(sizeof(PlayerInput)); PlayerInput* self = malloc(sizeof(PlayerInput));
ASSERT_RETURN(self != NULL, NULL, "Could not allocate memory for PlayerInput instance");
self->listeners = list_from_type(InputListener); self->listeners = list_from_type(InputListener);
self->device = input_get_device_by_id(device); self->device = input_get_device_by_id(device);
self->target = target; self->target = target;

View file

@ -25,7 +25,7 @@ double tstos(struct timespec ts) {
struct timespec get_time() { struct timespec get_time() {
struct timespec ts; struct timespec ts;
timespec_get(&ts, TIME_UTC); (void)timespec_get(&ts, TIME_UTC);
return ts; return ts;
} }
@ -80,6 +80,7 @@ void program_run(const struct ProgramSettings* settings) {
physics_world_tick(); physics_world_tick();
} }
settings->on_draw(); settings->on_draw();
game_world_draw();
SDL_Delay(1); SDL_Delay(1);
} }
@ -132,10 +133,10 @@ void program_handle_windowevent(SDL_WindowEvent* event) {
inline inline
float delta_time() { float delta_time() {
return _target_delta_time == 0 ? _delta_time : _target_delta_time; return (float)(_target_delta_time == 0 ? _delta_time : _target_delta_time);
} }
inline inline
float game_time() { float game_time() {
return get_time_s() - _game_start_time; return (float)(get_time_s() - _game_start_time);
} }

View file

@ -42,10 +42,10 @@ void sprite_draw(Sprite* self, Transform transform) {
Vector origin = self->origin; Vector origin = self->origin;
if(self->flip_state && SDL_FLIP_HORIZONTAL) { if(self->flip_state && SDL_FLIP_HORIZONTAL) {
origin.x = 1.0-origin.x; origin.x = 1.0f-origin.x;
} }
if((self->flip_state & SDL_FLIP_VERTICAL) != 0) { if((self->flip_state & SDL_FLIP_VERTICAL) != 0) {
origin.y = 1.0-origin.y; origin.y = 1.0f-origin.y;
} }
Vector left_top = transform_point(&transform, vinvf(origin)); Vector left_top = transform_point(&transform, vinvf(origin));
@ -75,8 +75,7 @@ size_t sprite_get_tile(const Sprite* self) {
} }
void sprite_set_tile(Sprite* self, size_t frame) { void sprite_set_tile(Sprite* self, size_t frame) {
frame = frame % spritesheet_get_tile_count(self->spritesheet); self->tile_index = frame % spritesheet_get_tile_count(self->spritesheet);
self->tile_index = frame;
} }
Spritesheet* sprite_get_spritesheet(const Sprite* self) { Spritesheet* sprite_get_spritesheet(const Sprite* self) {

View file

@ -43,8 +43,8 @@ Spritesheet* spritesheet_from_texture(SDL_Texture* texture, IVector tile_size) {
SDL_QueryTexture(self->texture, NULL, NULL, &self->resolution.x, &self->resolution.y); SDL_QueryTexture(self->texture, NULL, NULL, &self->resolution.x, &self->resolution.y);
self->tile_size = tile_size; self->tile_size = tile_size;
self->tile_shear = self->resolution.x / self->tile_size.x; self->tile_shear = (size_t)self->resolution.x / self->tile_size.x;
self->tile_count = self->resolution.x / self->tile_size.x * self->resolution.y / self->tile_size.y; self->tile_count = (size_t)self->resolution.x / self->tile_size.x * self->resolution.y / self->tile_size.y;
return self; return self;
} }
@ -57,7 +57,7 @@ SDL_Texture* spritesheet_get_texture(const Spritesheet* self) {
} }
SDL_Rect spritesheet_get_tile_rect(const Spritesheet* self, size_t index) { SDL_Rect spritesheet_get_tile_rect(const Spritesheet* self, size_t index) {
IVector tile_coord = {index % self->tile_shear, index / self->tile_shear}; IVector tile_coord = {(int)(index % self->tile_shear), (int)(index / self->tile_shear)};
tile_coord = vmuli(tile_coord, self->tile_size); tile_coord = vmuli(tile_coord, self->tile_size);
return (SDL_Rect) { return (SDL_Rect) {

27
core/src/state.h Normal file
View file

@ -0,0 +1,27 @@
#ifndef _fencer_state_h
#define _fencer_state_h
#include "typeclass_helpers.h"
typedef struct State State;
struct State {
void (*const enter)(void* data);
void (*const exit)(void* data);
const State* (*const update)(void* data, float dt);
};
#define DefineState(_StateName, _DataType, enter_fn, exit_fn, update_fn)\
static inline const State* _StateName() {\
TC_FN_TYPECHECK(void, enter_fn, _DataType*);\
TC_FN_TYPECHECK(void, exit_fn, _DataType*);\
TC_FN_TYPECHECK(const State*, update_fn, _DataType*, float);\
static const State instance = {\
.enter = (void(*const)(void*)) enter_fn,\
.exit = (void(*const)(void*)) exit_fn,\
.update = (const State*(*const)(void*, float)) update_fn,\
};\
return &instance;\
}
#endif // !_fencer_state_h

42
core/src/state_machine.c Normal file
View file

@ -0,0 +1,42 @@
#include "state_machine.h"
#include "stdlib.h"
#include "debug.h"
struct StateMachine {
const State* current_state;
void* data;
};
static inline
void internal_state_machine_set_state(StateMachine* self, const State* state) {
self->current_state->exit(self->data);
self->current_state = state;
self->current_state->enter(self->data);
}
StateMachine* state_machine_init(void* data, const State* start_state) {
StateMachine* self = malloc(sizeof(StateMachine));
ASSERT_RETURN(self != NULL, NULL, "Failed to allocate space for StateMachine instance");
*self = (StateMachine){
.current_state = start_state,
.data = data
};
self->current_state->enter(self->data);
return self;
}
void state_machine_destroy(StateMachine* self) {
self->current_state->exit(self->data);
free(self);
}
void state_machine_update(StateMachine* self, float dt) {
const State* next = self->current_state->update(self->data, dt);
if(next != self->current_state)
internal_state_machine_set_state(self, next);
}
const State* state_machine_get_current_state(StateMachine* self) {
return self->current_state;
}

15
core/src/state_machine.h Normal file
View file

@ -0,0 +1,15 @@
#ifndef _fencer_state_machine_h
#define _fencer_state_machine_h
#include "state.h"
typedef struct StateMachine StateMachine;
extern StateMachine* state_machine_init(void* data, const State* start_state);
extern void state_machine_destroy(StateMachine* self);
extern void state_machine_update(StateMachine* self, float dt);
extern const State* state_machine_get_current_state(StateMachine* self);
#endif // !_fencer_state_machine_h

View file

@ -106,7 +106,7 @@ float vsqrmagnitudef(Vector a) {
static inline static inline
Vector vnormalizedf(Vector a) { Vector vnormalizedf(Vector a) {
if(veqf(a, ZeroVector)) return ZeroVector; if(veqf(a, ZeroVector)) return ZeroVector;
return vmulff(a, 1.0/vmagnitudef(a)); return vmulff(a, 1.0f/vmagnitudef(a));
} }
static inline static inline
float vdotf(Vector a, Vector b) { float vdotf(Vector a, Vector b) {
@ -122,7 +122,7 @@ float vsqrdistf(Vector a, Vector b) {
} }
static inline static inline
Vector vreciprocalf(Vector a) { Vector vreciprocalf(Vector a) {
return (Vector){1.0/a.x, 1.0/a.y}; return (Vector){1.0f/a.x, 1.0f/a.y};
} }
static inline static inline
Vector vrotatef(Vector a, float t) { Vector vrotatef(Vector a, float t) {
@ -157,7 +157,7 @@ Vector vaveragef(Vector* array, size_t count) {
for(size_t i = 0; i < count; ++i) { for(size_t i = 0; i < count; ++i) {
acc = vaddf(acc, array[i]); acc = vaddf(acc, array[i]);
} }
return vmulff(acc, 1.0/(float)count); return vmulff(acc, 1.0f/(float)count);
} }
static inline static inline

View file

@ -3,17 +3,22 @@ project "Game"
language "C" language "C"
staticruntime "Off" staticruntime "Off"
targetdir "bin/%{cfg.buildcfg}" targetdir "bin/%{cfg.buildcfg}"
debugdir "."
defines { "VMATH_SDL" } defines { "VMATH_SDL" }
files { "src/**.c", "src/**.h" } files { "src/**.c", "src/**.h" }
includedirs { includedirs {
"src/", "src/",
"../core/src" "../core/src/"
} }
links { links {
"Engine-Core" "Engine-Core"
} }
postbuildcommands {
"{COPYDIR} assets/ %{cfg.targetdir}/assets"
}
targetdir ("../bin/" .. OutputDir .. "/%{prj.name}" ) targetdir ("../bin/" .. OutputDir .. "/%{prj.name}" )
objdir ("../intermediate/" .. OutputDir .. "/%{prj.name}" ) objdir ("../intermediate/" .. OutputDir .. "/%{prj.name}" )

View file

@ -1,10 +1,7 @@
#include "camera.h"
#include "program.h" #include "program.h"
static static
void play() { void play() {}
g_camera.fov = 40;
}
static static
void tick() {} void tick() {}