Compile all dependencies from git submodules, convert to cmake
This commit is contained in:
233
src/akgltest.c
Normal file
233
src/akgltest.c
Normal file
@@ -0,0 +1,233 @@
|
||||
#define SDL_MAIN_USE_CALLBACKS
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL_properties.h>
|
||||
#include <SDL3_image/SDL_image.h>
|
||||
#include <SDL3_mixer/SDL_mixer.h>
|
||||
|
||||
#include <akgl/assets.h>
|
||||
#include <akgl/error.h>
|
||||
#include <akgl/iterator.h>
|
||||
#include <akgl/tilemap.h>
|
||||
#include <akgl/heap.h>
|
||||
#include <akgl/game.h>
|
||||
#include <akgl/controller.h>
|
||||
#include <akgl/draw.h>
|
||||
#include <akgl/sprite.h>
|
||||
#include <akgl/actor.h>
|
||||
#include <akgl/registry.h>
|
||||
#include <akgl/text.h>
|
||||
|
||||
int numsprites = 8;
|
||||
char *spritepaths[] = {
|
||||
"assets/sprites/little_guy_walking_left.json",
|
||||
"assets/sprites/little_guy_walking_right.json",
|
||||
"assets/sprites/little_guy_walking_up.json",
|
||||
"assets/sprites/little_guy_walking_down.json",
|
||||
"assets/sprites/little_guy_facing_left.json",
|
||||
"assets/sprites/little_guy_facing_right.json",
|
||||
"assets/sprites/little_guy_facing_up.json",
|
||||
"assets/sprites/little_guy_facing_down.json"
|
||||
};
|
||||
|
||||
char dirnamebuf[1024];
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *music_toggle(akgl_Actor *obj, SDL_Event *event)
|
||||
{
|
||||
SDL_PropertiesID bgmprops = 0;
|
||||
PREPARE_ERROR(errctx);
|
||||
if ( MIX_TrackPlaying(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM]) ) {
|
||||
FAIL_ZERO_RETURN(
|
||||
errctx,
|
||||
MIX_StopTrack(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM], 0),
|
||||
AKGL_ERR_SDL,
|
||||
"%s",
|
||||
SDL_GetError());
|
||||
} else {
|
||||
SDL_SetNumberProperty(bgmprops, MIX_PROP_PLAY_LOOPS_NUMBER, -1);
|
||||
FAIL_ZERO_RETURN(
|
||||
errctx,
|
||||
MIX_PlayTrack(akgl_tracks[AKGL_GAME_AUDIO_TRACK_BGM], bgmprops),
|
||||
AKGL_ERR_SDL,
|
||||
"%s",
|
||||
SDL_GetError());
|
||||
}
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *savegame(akgl_Actor *obj, SDL_Event *event)
|
||||
{
|
||||
return akgl_game_save("assets/savegame.bin");
|
||||
}
|
||||
|
||||
akerr_ErrorContext AKERR_NOIGNORE *user_breakpoint(akgl_Actor *obj, SDL_Event *event)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
SDL_Log("User breakpoint hit");
|
||||
SUCCEED_RETURN(errctx);
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
||||
{
|
||||
akgl_Control control;
|
||||
akgl_Actor *actorptr = NULL;
|
||||
*appstate = (void *)&game.state;
|
||||
PREPARE_ERROR(errctx);
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer");
|
||||
|
||||
strcpy((char *)&game.name, "sdl3-gametest");
|
||||
strcpy((char *)&game.version, "0.0.1");
|
||||
strcpy((char *)&game.uri, "net.aklabs.games.sdl3-gametest");
|
||||
game.screenwidth = 640;
|
||||
game.screenheight = 480;
|
||||
|
||||
//CATCH(errctx, akgl_game_load("assets/savegame.bin"));
|
||||
CATCH(errctx, akgl_game_init());
|
||||
CATCH(errctx, akgl_registry_load_properties("assets/properties.json"));
|
||||
|
||||
for ( int i = 0; i < numsprites ; i++) {
|
||||
strcpy((char *)&dirnamebuf, spritepaths[i]);
|
||||
CATCH(errctx, akgl_sprite_load_json((char *)&dirnamebuf));
|
||||
}
|
||||
strcpy((char *)&dirnamebuf, "assets/characters/littleguy.json");
|
||||
CATCH(errctx, akgl_character_load_json((char *)&dirnamebuf));
|
||||
CATCH(errctx, akgl_heap_next_actor(&actorptr));
|
||||
CATCH(errctx, akgl_actor_initialize((akgl_Actor *)actorptr, "player"));
|
||||
actorptr->basechar = SDL_GetPointerProperty(
|
||||
AKGL_REGISTRY_CHARACTER,
|
||||
"little guy",
|
||||
NULL);
|
||||
FAIL_ZERO_BREAK(errctx, actorptr->basechar, AKERR_REGISTRY, "Can't load character 'little guy' from the registry");
|
||||
actorptr->movement_controls_face = false;
|
||||
actorptr->state = (AKGL_ACTOR_STATE_ALIVE | AKGL_ACTOR_STATE_FACE_LEFT);
|
||||
actorptr->x = 320;
|
||||
actorptr->y = 240;
|
||||
actorptr->visible = true;
|
||||
|
||||
strcpy((char *)&dirnamebuf, "assets/memories.mp3");
|
||||
CATCH(errctx, akgl_load_start_bgm((char *)&dirnamebuf));
|
||||
CATCH(errctx, music_toggle(NULL, NULL));
|
||||
|
||||
strcpy((char *)&dirnamebuf, "assets/tilemap.tmj");
|
||||
CATCH(errctx, akgl_tilemap_load((char *)&dirnamebuf, (akgl_Tilemap *)&gamemap));
|
||||
|
||||
CATCH(errctx, akgl_controller_default(0, "player", 0, 0));
|
||||
|
||||
// set custom control maps */
|
||||
memset((void *)&control, 0x00, sizeof(akgl_Control));
|
||||
|
||||
// Breakpoint
|
||||
control.key = SDLK_ESCAPE;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &user_breakpoint;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Toggle the music
|
||||
control.key = SDLK_M;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &music_toggle;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
// Save the game
|
||||
control.key = SDLK_S;
|
||||
control.event_on = SDL_EVENT_KEY_DOWN;
|
||||
control.handler_on = &savegame;
|
||||
CATCH(errctx, akgl_controller_pushmap(0, &control));
|
||||
|
||||
CATCH(errctx, akgl_text_loadfont("C64Pro", "assets/C64_Pro-STYLE.ttf", 18));
|
||||
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
LOG_ERROR(errctx);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(errctx);
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
||||
{
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
ATTEMPT {
|
||||
FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer");
|
||||
FAIL_ZERO_BREAK(errctx, event, AKERR_NULLPOINTER, "NULL event pointer");
|
||||
|
||||
CATCH(errctx, akgl_controller_handle_event(appstate, event));
|
||||
if (event->type == SDL_EVENT_QUIT) {
|
||||
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
||||
}
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} FINISH_NORETURN(errctx);
|
||||
return SDL_APP_CONTINUE; /* carry on with the program! */
|
||||
}
|
||||
|
||||
SDL_AppResult SDL_AppIterate(void *appstate)
|
||||
{
|
||||
//SDL_FRect dest;
|
||||
//b2Vec2 position;
|
||||
int i = 0;
|
||||
akgl_Iterator opflags;
|
||||
char fpsText[32];
|
||||
|
||||
PREPARE_ERROR(errctx);
|
||||
|
||||
akgl_game_updateFPS();
|
||||
|
||||
AKGL_BITMASK_CLEAR(opflags.flags);
|
||||
AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_UPDATE);
|
||||
AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_RENDER);
|
||||
AKGL_BITMASK_ADD(opflags.flags, AKGL_ITERATOR_OP_LAYERMASK);
|
||||
|
||||
for ( i = 0; i < AKGL_TILEMAP_MAX_LAYERS; i++ ) {
|
||||
opflags.layerid = i;
|
||||
ATTEMPT {
|
||||
CATCH(errctx, akgl_tilemap_draw(renderer, (akgl_Tilemap *)&gamemap, &camera, i));
|
||||
SDL_EnumerateProperties(AKGL_REGISTRY_ACTOR, &akgl_registry_iterate_actor, (void *)&opflags);
|
||||
} CLEANUP {
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
LOG_ERROR(errctx);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(errctx);
|
||||
}
|
||||
ATTEMPT {
|
||||
memset((char *)&fpsText, 0x00, 32);
|
||||
snprintf((char *)&fpsText, 31, "FPS : %d", game.fps);
|
||||
CATCH(errctx, akgl_text_rendertextat(
|
||||
SDL_GetPointerProperty(
|
||||
AKGL_REGISTRY_FONT,
|
||||
"C64Pro",
|
||||
NULL),
|
||||
(char *)&fpsText,
|
||||
(SDL_Color){255, 255, 255, 255},
|
||||
0,
|
||||
450,
|
||||
10)
|
||||
);
|
||||
} CLEANUP {
|
||||
SDL_RenderPresent(renderer);
|
||||
} PROCESS(errctx) {
|
||||
} HANDLE_DEFAULT(errctx) {
|
||||
LOG_ERROR(errctx);
|
||||
return SDL_APP_FAILURE;
|
||||
} FINISH_NORETURN(errctx);
|
||||
|
||||
return SDL_APP_CONTINUE;
|
||||
}
|
||||
|
||||
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
||||
{
|
||||
/* SDL will clean up the window/renderer for us. */
|
||||
// SDL_DestroyTexture(ball.texture);
|
||||
//b2DestroyWorld(physicsWorldId);
|
||||
SDL_Log("Freeing music resources");
|
||||
if ( bgm != NULL ) {
|
||||
//Mix_FreeMusic(bgm);
|
||||
}
|
||||
SDL_Log("Quitting mixer");
|
||||
// Mix_Quit();
|
||||
}
|
||||
Reference in New Issue
Block a user