2024-12-08 15:39:07 -05:00
|
|
|
#define SDL_MAIN_USE_CALLBACKS
|
|
|
|
|
#include <SDL3/SDL.h>
|
|
|
|
|
#include <SDL3/SDL_main.h>
|
2024-12-17 22:13:10 -05:00
|
|
|
#include <SDL3/SDL_properties.h>
|
2024-12-08 15:39:07 -05:00
|
|
|
#include <SDL3_image/SDL_image.h>
|
|
|
|
|
#include <SDL3_mixer/SDL_mixer.h>
|
|
|
|
|
|
2025-08-03 15:15:07 -04:00
|
|
|
#include <sdlerror.h>
|
|
|
|
|
#include <sdl3game/assets.h>
|
|
|
|
|
#include <sdl3game/iterator.h>
|
|
|
|
|
#include <sdl3game/tilemap.h>
|
|
|
|
|
#include <sdl3game/heap.h>
|
|
|
|
|
#include <sdl3game/game.h>
|
2025-08-03 21:42:33 -04:00
|
|
|
#include <sdl3game/controller.h>
|
2025-08-03 15:15:07 -04:00
|
|
|
#include <sdl3game/draw.h>
|
|
|
|
|
#include <sdl3game/sprite.h>
|
|
|
|
|
#include <sdl3game/actor.h>
|
|
|
|
|
#include <sdl3game/registry.h>
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2025-01-01 21:53:21 -05:00
|
|
|
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"
|
|
|
|
|
};
|
|
|
|
|
|
2024-12-08 15:39:07 -05:00
|
|
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
|
|
|
|
{
|
2025-08-03 21:42:33 -04:00
|
|
|
SDL3GControlMap *controlmap;
|
2025-01-01 13:56:15 -05:00
|
|
|
actor *actorptr = NULL;
|
2025-08-03 15:15:07 -04:00
|
|
|
*appstate = (void *)&game.state;
|
2025-01-01 13:56:15 -05:00
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
ATTEMPT {
|
2025-01-01 15:33:14 -05:00
|
|
|
FAIL_ZERO_BREAK(errctx, appstate, ERR_NULLPOINTER, "NULL appstate pointer");
|
2025-08-03 15:15:07 -04:00
|
|
|
|
|
|
|
|
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, GAME_init());
|
|
|
|
|
|
|
|
|
|
for ( int i = 0; i < numsprites ; i++) {
|
|
|
|
|
CATCH(errctx, sprite_load_json(spritepaths[i]));
|
|
|
|
|
}
|
2025-08-03 21:42:33 -04:00
|
|
|
CATCH(errctx, character_load_json("../assets/characters/littleguy.json"));
|
2025-08-03 15:15:07 -04:00
|
|
|
CATCH(errctx, heap_next_actor(&actorptr));
|
|
|
|
|
CATCH(errctx, actor_initialize((actor *)actorptr, "player"));
|
|
|
|
|
actorptr->basechar = SDL_GetPointerProperty(
|
|
|
|
|
REGISTRY_CHARACTER,
|
|
|
|
|
"little guy",
|
|
|
|
|
NULL);
|
|
|
|
|
FAIL_ZERO_BREAK(errctx, actorptr->basechar, ERR_REGISTRY, "Can't load character 'little guy' from the registry");
|
|
|
|
|
actorptr->movement_controls_face = false;
|
|
|
|
|
actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT);
|
|
|
|
|
actorptr->x = 320;
|
|
|
|
|
actorptr->y = 240;
|
|
|
|
|
actorptr->visible = true;
|
|
|
|
|
|
|
|
|
|
CATCH(errctx, load_start_bgm("../assets/memories.mp3"));
|
|
|
|
|
|
|
|
|
|
CATCH(errctx, tilemap_load("../assets/tilemap.tmj", (tilemap *)&gamemap));
|
|
|
|
|
|
2025-08-03 21:42:33 -04:00
|
|
|
// set up the control map
|
|
|
|
|
controlmap = &GAME_ControlMaps[0];
|
|
|
|
|
controlmap->kbid = 0;
|
|
|
|
|
controlmap->target = SDL_GetPointerProperty(REGISTRY_ACTOR, "player", NULL);
|
|
|
|
|
// Move down
|
|
|
|
|
controlmap->controls[0].key = SDLK_DOWN;
|
|
|
|
|
controlmap->controls[0].target_state_gate = ACTOR_STATE_MOVING_DOWN;
|
|
|
|
|
controlmap->controls[0].target_add_state_on = ACTOR_STATE_MOVING_DOWN | ACTOR_STATE_FACE_DOWN;
|
|
|
|
|
controlmap->controls[0].target_del_state_on = ACTOR_STATE_MOVING_ALL | ACTOR_STATE_FACE_ALL;
|
|
|
|
|
controlmap->controls[0].target_del_state_off = ACTOR_STATE_MOVING_ALL;
|
|
|
|
|
controlmap->controls[0].event_on = SDL_EVENT_KEY_DOWN;
|
|
|
|
|
controlmap->controls[0].event_off = SDL_EVENT_KEY_UP;
|
|
|
|
|
|
|
|
|
|
// Move up
|
|
|
|
|
controlmap->controls[1].key = SDLK_UP;
|
|
|
|
|
controlmap->controls[1].target_state_gate = ACTOR_STATE_MOVING_UP;
|
|
|
|
|
controlmap->controls[1].target_add_state_on = ACTOR_STATE_MOVING_UP | ACTOR_STATE_FACE_UP;
|
|
|
|
|
controlmap->controls[1].target_del_state_on = ACTOR_STATE_MOVING_ALL | ACTOR_STATE_FACE_ALL;
|
|
|
|
|
controlmap->controls[1].target_del_state_off = ACTOR_STATE_MOVING_ALL;
|
|
|
|
|
controlmap->controls[1].event_on = SDL_EVENT_KEY_DOWN;
|
|
|
|
|
controlmap->controls[1].event_off = SDL_EVENT_KEY_UP;
|
|
|
|
|
|
|
|
|
|
// Move left
|
|
|
|
|
controlmap->controls[2].key = SDLK_LEFT;
|
|
|
|
|
controlmap->controls[2].target_state_gate = ACTOR_STATE_MOVING_LEFT;
|
|
|
|
|
controlmap->controls[2].target_add_state_on = ACTOR_STATE_MOVING_LEFT | ACTOR_STATE_FACE_LEFT;
|
|
|
|
|
controlmap->controls[2].target_del_state_on = ACTOR_STATE_MOVING_ALL | ACTOR_STATE_FACE_ALL;
|
|
|
|
|
controlmap->controls[2].target_del_state_off = ACTOR_STATE_MOVING_ALL;
|
|
|
|
|
controlmap->controls[2].event_on = SDL_EVENT_KEY_DOWN;
|
|
|
|
|
controlmap->controls[2].event_off = SDL_EVENT_KEY_UP;
|
|
|
|
|
|
|
|
|
|
// Move right
|
|
|
|
|
controlmap->controls[3].key = SDLK_RIGHT;
|
|
|
|
|
controlmap->controls[3].target_state_gate = ACTOR_STATE_MOVING_RIGHT;
|
|
|
|
|
controlmap->controls[3].target_add_state_on = ACTOR_STATE_MOVING_RIGHT | ACTOR_STATE_FACE_RIGHT;
|
|
|
|
|
controlmap->controls[3].target_del_state_on = ACTOR_STATE_MOVING_ALL | ACTOR_STATE_FACE_ALL;
|
|
|
|
|
controlmap->controls[3].target_del_state_off = ACTOR_STATE_MOVING_ALL;
|
|
|
|
|
controlmap->controls[3].event_on = SDL_EVENT_KEY_DOWN;
|
|
|
|
|
controlmap->controls[3].event_off = SDL_EVENT_KEY_UP;
|
2025-01-01 13:56:15 -05:00
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE_DEFAULT(errctx) {
|
|
|
|
|
LOG_ERROR(errctx);
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2025-08-03 21:42:33 -04:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
return SDL_APP_CONTINUE;
|
2024-12-08 15:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
|
|
|
|
{
|
2025-01-01 15:33:14 -05:00
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
ATTEMPT {
|
|
|
|
|
FAIL_ZERO_BREAK(errctx, appstate, ERR_NULLPOINTER, "NULL appstate pointer");
|
|
|
|
|
FAIL_ZERO_BREAK(errctx, event, ERR_NULLPOINTER, "NULL event pointer");
|
|
|
|
|
|
2025-08-03 21:42:33 -04:00
|
|
|
CATCH(errctx, controller_handle_event(appstate, event));
|
2025-01-01 15:33:14 -05:00
|
|
|
if (event->type == SDL_EVENT_QUIT) {
|
|
|
|
|
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
|
|
|
|
}
|
|
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2025-01-01 13:56:15 -05:00
|
|
|
return SDL_APP_CONTINUE; /* carry on with the program! */
|
2024-12-08 15:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
|
|
|
|
{
|
2025-01-01 13:56:15 -05:00
|
|
|
//SDL_FRect dest;
|
|
|
|
|
//b2Vec2 position;
|
|
|
|
|
int i = 0;
|
|
|
|
|
iterator opflags;
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
PREPARE_ERROR(errctx);
|
|
|
|
|
|
|
|
|
|
BITMASK_CLEAR(opflags.flags);
|
|
|
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_UPDATE);
|
|
|
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_RENDER);
|
|
|
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_LAYERMASK);
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
for ( i = 0; i < TILEMAP_MAX_LAYERS; i++ ) {
|
|
|
|
|
opflags.layerid = i;
|
|
|
|
|
ATTEMPT {
|
|
|
|
|
CATCH(errctx, tilemap_draw(renderer, (tilemap *)&gamemap, &camera, i));
|
|
|
|
|
SDL_EnumerateProperties(REGISTRY_ACTOR, ®istry_iterate_actor, (void *)&opflags);
|
|
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE_DEFAULT(errctx) {
|
|
|
|
|
LOG_ERROR(errctx);
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
|
|
|
|
}
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
return SDL_APP_CONTINUE;
|
2024-12-08 15:39:07 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SDL_AppQuit(void *appstate, SDL_AppResult result)
|
|
|
|
|
{
|
2025-01-01 13:56:15 -05:00
|
|
|
/* SDL will clean up the window/renderer for us. */
|
2025-08-03 15:15:07 -04:00
|
|
|
// SDL_DestroyTexture(ball.texture);
|
2025-01-05 19:53:54 -08:00
|
|
|
//b2DestroyWorld(physicsWorldId);
|
2025-01-01 13:56:15 -05:00
|
|
|
SDL_Log("Freeing music resources");
|
|
|
|
|
if ( bgm != NULL ) {
|
2025-08-03 15:15:07 -04:00
|
|
|
//Mix_FreeMusic(bgm);
|
2025-01-01 13:56:15 -05:00
|
|
|
}
|
|
|
|
|
SDL_Log("Quitting mixer");
|
2025-08-03 15:15:07 -04:00
|
|
|
// Mix_Quit();
|
2024-12-08 15:39:07 -05:00
|
|
|
}
|