Files
libakgl/src/game.c

121 lines
3.3 KiB
C
Raw Normal View History

#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>
#include <sdl3game/game.h>
#include <sdl3game/controller.h>
#include <sdl3game/tilemap.h>
#include <sdl3game/sprite.h>
#include <sdl3game/heap.h>
#include <sdl3game/registry.h>
#include <sdl3game/staticstring.h>
#include <sdl3game/iterator.h>
2025-08-09 13:53:37 -04:00
#include <sdl3game/SDL_GameControllerDB.h>
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
akgl_Frame ball;
akgl_Frame paddle1;
akgl_Frame paddle2;
akgl_Frame table;
akgl_Tilemap gamemap;
MIX_Audio *bgm = NULL;
MIX_Mixer *GAME_mixer = NULL;
MIX_Track *GAME_tracks[64];
SDL_FRect camera;
akgl_Game game;
akerr_ErrorContext AKERR_NOIGNORE *akgl_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");
CATCH(errctx, akgl_heap_init());
CATCH(errctx, akgl_registry_init_actor());
CATCH(errctx, akgl_registry_init_sprite());
CATCH(errctx, akgl_registry_init_spritesheet());
CATCH(errctx, akgl_registry_init_character());
CATCH(errctx, akgl_registry_init_font());
CATCH(errctx, akgl_registry_init_music());
CATCH(errctx, akgl_registry_init_actor_state_strings());
} CLEANUP {
} PROCESS(errctx) {
} FINISH(errctx, true)
SDL_SetAppMetadata(game.name, game.version, game.uri);
for ( i = 0 ; i < AKGL_MAX_CONTROL_MAPS; i++ ) {
memset(&GAME_ControlMaps[i], 0x00, sizeof(akgl_ControlMap));
}
FAIL_ZERO_RETURN(
errctx,
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD | SDL_INIT_AUDIO),
AKGL_ERR_SDL,
"Couldn't initialize SDL: %s",
SDL_GetError());
2025-08-09 13:53:37 -04:00
// Load the Game Controller DB
for ( i = 0; i < AKGL_SDL_GAMECONTROLLER_DB_LEN ; i++ ) {
2025-08-09 13:53:37 -04:00
if ( SDL_AddGamepadMapping(SDL_GAMECONTROLLER_DB[i]) == -1 ) {
FAIL_ZERO_RETURN(errctx, 0, AKGL_ERR_SDL, "%s", SDL_GetError());
2025-08-09 13:53:37 -04:00
}
}
FAIL_ZERO_RETURN(
errctx,
SDL_CreateWindowAndRenderer(game.uri, game.screenwidth, game.screenheight, 0, &window, &renderer),
AKGL_ERR_SDL,
"Couldn't create window/renderer: %s",
SDL_GetError());
FAIL_ZERO_RETURN(
errctx,
MIX_Init(),
AKGL_ERR_SDL,
"Couldn't initialize audio: %s",
SDL_GetError());
game.mixer = MIX_CreateMixerDevice(SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK, 0);
FAIL_ZERO_RETURN(
errctx,
game.mixer,
AKGL_ERR_SDL,
"Unable to create mixer device: %s",
SDL_GetError());
FAIL_ZERO_RETURN(
errctx,
TTF_Init(),
AKGL_ERR_SDL,
"Couldn't initialize front engine: %s",
SDL_GetError());
camera.x = 0;
camera.y = 0;
camera.w = game.screenwidth;
camera.h = game.screenheight;
}
void akgl_game_updateFPS()
{
SDL_Time curTime;
curTime = SDL_GetTicksNS();
if ( (curTime - game.lastFPSTime) > AKGL_TIME_ONESEC_NS ) {
game.fps = game.framesSinceUpdate;
game.framesSinceUpdate = 0;
game.lastFPSTime = curTime;
}
game.framesSinceUpdate += 1;
game.lastIterTime = curTime;
}