Added a font registry, a text rendering helper, and an FPS counter function
This commit is contained in:
@@ -186,7 +186,7 @@ akerr_ErrorContext *character_load_json(char *filename)
|
||||
);
|
||||
CATCH(errctx, character_load_json_inner(json, obj));
|
||||
CATCH(errctx, get_json_integer_value(json, "movementspeed", (int *)&obj->movementspeed));
|
||||
obj->movementspeed = obj->movementspeed * 1000000;
|
||||
obj->movementspeed = obj->movementspeed * TIME_ONESEC_MS;
|
||||
CATCH(errctx, get_json_number_value(json, "velocity_x", &obj->vx));
|
||||
CATCH(errctx, get_json_number_value(json, "velocity_y", &obj->vy));
|
||||
} CLEANUP {
|
||||
|
||||
29
src/game.c
29
src/game.c
@@ -1,6 +1,7 @@
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
#include <SDL3_ttf/SDL_ttf.h>
|
||||
#include <stdio.h>
|
||||
#include <akerror.h>
|
||||
|
||||
@@ -32,6 +33,9 @@ akerr_ErrorContext AKERR_NOIGNORE *GAME_init()
|
||||
int i = 0;
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
game.gameStartTime = SDL_GetTicksNS();
|
||||
game.lastIterTime = game.gameStartTime;
|
||||
game.lastFPSTime = game.gameStartTime;
|
||||
FAIL_ZERO_BREAK(errctx, strlen((char *)&game.name), AKERR_NULLPOINTER, "Must provide game name");
|
||||
FAIL_ZERO_BREAK(errctx, strlen((char *)&game.version), AKERR_NULLPOINTER, "Must provide game version");
|
||||
FAIL_ZERO_BREAK(errctx, strlen((char *)&game.uri), AKERR_NULLPOINTER, "Must provide game uri");
|
||||
@@ -40,6 +44,8 @@ akerr_ErrorContext AKERR_NOIGNORE *GAME_init()
|
||||
CATCH(errctx, registry_init_sprite());
|
||||
CATCH(errctx, registry_init_spritesheet());
|
||||
CATCH(errctx, registry_init_character());
|
||||
CATCH(errctx, registry_init_font());
|
||||
CATCH(errctx, registry_init_music());
|
||||
CATCH(errctx, registry_init_actor_state_strings());
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
@@ -87,9 +93,28 @@ akerr_ErrorContext AKERR_NOIGNORE *GAME_init()
|
||||
"Unable to create mixer device: %s",
|
||||
SDL_GetError());
|
||||
|
||||
FAIL_ZERO_RETURN(
|
||||
errctx,
|
||||
TTF_Init(),
|
||||
AKERR_SDL,
|
||||
"Couldn't initialize front engine: %s",
|
||||
SDL_GetError());
|
||||
|
||||
camera.x = 0;
|
||||
camera.y = 0;
|
||||
camera.w = game.screenwidth;
|
||||
camera.h = game.screenheight;
|
||||
|
||||
camera.h = game.screenheight;
|
||||
}
|
||||
|
||||
void GAME_updateFPS()
|
||||
{
|
||||
SDL_Time curTime;
|
||||
curTime = SDL_GetTicksNS();
|
||||
if ( (curTime - game.lastFPSTime) > TIME_ONESEC_NS ) {
|
||||
game.fps = game.framesSinceUpdate;
|
||||
game.framesSinceUpdate = 0;
|
||||
game.lastFPSTime = curTime;
|
||||
}
|
||||
game.framesSinceUpdate += 1;
|
||||
game.lastIterTime = curTime;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,8 @@ SDL_PropertiesID REGISTRY_ACTOR_STATE_STRINGS;
|
||||
SDL_PropertiesID REGISTRY_SPRITE;
|
||||
SDL_PropertiesID REGISTRY_SPRITESHEET;
|
||||
SDL_PropertiesID REGISTRY_CHARACTER;
|
||||
SDL_PropertiesID REGISTRY_MUSIC;
|
||||
SDL_PropertiesID REGISTRY_FONT;
|
||||
|
||||
akerr_ErrorContext *registry_init()
|
||||
{
|
||||
@@ -21,6 +23,8 @@ akerr_ErrorContext *registry_init()
|
||||
CATCH(errctx, registry_init_character());
|
||||
CATCH(errctx, registry_init_actor());
|
||||
CATCH(errctx, registry_init_actor_state_strings());
|
||||
CATCH(errctx, registry_init_font());
|
||||
CATCH(errctx, registry_init_music());
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH(errctx, true);
|
||||
@@ -35,6 +39,22 @@ akerr_ErrorContext *registry_init_actor()
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *registry_init_font()
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
REGISTRY_FONT = SDL_CreateProperties();
|
||||
FAIL_ZERO_RETURN(errctx, REGISTRY_FONT, AKERR_NULLPOINTER, "Error initializing font registry");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *registry_init_music()
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
REGISTRY_MUSIC = SDL_CreateProperties();
|
||||
FAIL_ZERO_RETURN(errctx, REGISTRY_MUSIC, AKERR_NULLPOINTER, "Error initializing music registry");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext *registry_init_actor_state_strings()
|
||||
{
|
||||
int i = 0;
|
||||
@@ -54,7 +74,7 @@ akerr_ErrorContext *registry_init_actor_state_strings()
|
||||
|
||||
akerr_ErrorContext *registry_init_sprite()
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
PREPARE_ERROR(errctx);
|
||||
REGISTRY_SPRITE = SDL_CreateProperties();
|
||||
FAIL_ZERO_RETURN(errctx, REGISTRY_SPRITE, AKERR_NULLPOINTER, "Error initializing sprite registry");
|
||||
SUCCEED_RETURN(errctx);
|
||||
|
||||
@@ -99,7 +99,7 @@ akerr_ErrorContext *sprite_load_json(char *filename)
|
||||
CATCH(errctx, get_json_integer_value((json_t *)json, "width", &obj->width));
|
||||
CATCH(errctx, get_json_integer_value((json_t *)json, "height", &obj->height));
|
||||
CATCH(errctx, get_json_integer_value((json_t *)json, "speed", (int *)&obj->speed));
|
||||
obj->speed = obj->speed * 1000000;
|
||||
obj->speed = obj->speed * TIME_ONESEC_MS;
|
||||
CATCH(errctx, get_json_boolean_value((json_t *)json, "loop", &obj->loop));
|
||||
CATCH(errctx, get_json_boolean_value((json_t *)json, "loopReverse", &obj->loopReverse));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user