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>
|
|
|
|
|
#include <box2d/box2d.h>
|
|
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
#include "assets.h"
|
|
|
|
|
#include "iterator.h"
|
2024-12-08 15:39:07 -05:00
|
|
|
#include "tilemap.h"
|
2025-01-01 13:56:15 -05:00
|
|
|
#include "heap.h"
|
2024-12-08 15:39:07 -05:00
|
|
|
#include "game.h"
|
2025-01-01 15:33:14 -05:00
|
|
|
#include "gamepad.h"
|
2024-12-08 15:39:07 -05:00
|
|
|
#include "physics.h"
|
|
|
|
|
#include "draw.h"
|
|
|
|
|
#include "sprite.h"
|
2025-01-01 13:56:15 -05:00
|
|
|
#include "actor.h"
|
2024-12-23 23:25:53 -05:00
|
|
|
#include "error.h"
|
|
|
|
|
#include "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-01-01 13:56:15 -05:00
|
|
|
actor *actorptr = NULL;
|
|
|
|
|
PREPARE_ERROR(errctx);
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
SDL_AudioSpec spec;
|
2025-01-01 15:33:14 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
ATTEMPT {
|
|
|
|
|
CATCH(errctx, heap_init());
|
|
|
|
|
CATCH(errctx, registry_init_actor());
|
|
|
|
|
CATCH(errctx, registry_init_sprite());
|
|
|
|
|
CATCH(errctx, registry_init_spritesheet());
|
|
|
|
|
CATCH(errctx, registry_init_character());
|
2025-01-01 15:33:14 -05:00
|
|
|
FAIL_ZERO_BREAK(errctx, appstate, ERR_NULLPOINTER, "NULL appstate pointer");
|
2025-01-01 13:56:15 -05:00
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE_DEFAULT(errctx) {
|
|
|
|
|
LOG_ERROR(errctx);
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2025-01-01 15:33:14 -05:00
|
|
|
*appstate = (void *)&gamestate;
|
|
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
SDL_SetAppMetadata("SDL3-GameTest", "0.1", "net.aklabs.sdl3-gametest");
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2025-01-01 15:33:14 -05:00
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_GAMEPAD | SDL_INIT_AUDIO )) {
|
2025-01-01 13:56:15 -05:00
|
|
|
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
}
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
if (!SDL_CreateWindowAndRenderer("net/aklabs/sdl3-gametest", 640, 480, 0, &window, &renderer)) {
|
|
|
|
|
SDL_Log("Couldn't create window/renderer: %s", SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
}
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
/*
|
|
|
|
|
ATTEMPT {
|
|
|
|
|
spritesheet *sheet;
|
|
|
|
|
sprite *spr;
|
|
|
|
|
character *basechar;
|
|
|
|
|
CATCH(errctx, heap_next_spritesheet(&sheet));
|
|
|
|
|
CATCH(errctx, spritesheet_initialize(sheet, 48, 48, "../assets/Actor1.png"));
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
CATCH(errctx, heap_next_sprite(&spr));
|
|
|
|
|
CATCH(errctx, sprite_initialize(spr, "tester", sheet));
|
|
|
|
|
spr->frameids[0] = 13;
|
|
|
|
|
spr->speed = 100;
|
|
|
|
|
spr->loop = false;
|
|
|
|
|
spr->loopReverse = false;
|
|
|
|
|
spr->width = 48;
|
|
|
|
|
spr->height = 48;
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
CATCH(errctx, heap_next_character(&basechar));
|
|
|
|
|
CATCH(errctx, character_initialize(basechar, "tester"));
|
|
|
|
|
CATCH(errctx, character_sprite_add(basechar, spr, (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT)));
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
CATCH(errctx, heap_next_actor(&actorptr));
|
|
|
|
|
CATCH(errctx, actor_initialize((actor *)actorptr, "player"));
|
|
|
|
|
actorptr->basechar = basechar;
|
|
|
|
|
actorptr->visible = true;
|
|
|
|
|
actorptr->x = 120;
|
|
|
|
|
actorptr->y = 120;
|
|
|
|
|
actorptr->layer = 0;
|
|
|
|
|
actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT);
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2024-12-24 11:35:22 -05:00
|
|
|
*/
|
2024-12-23 23:25:53 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
GAME_init_physics();
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
spec.freq = MIX_DEFAULT_FREQUENCY;
|
|
|
|
|
spec.format = MIX_DEFAULT_FORMAT;
|
|
|
|
|
spec.channels = MIX_DEFAULT_CHANNELS;
|
|
|
|
|
if (!Mix_OpenAudio(0, &spec)) {
|
|
|
|
|
SDL_Log("Couldn't initialize the audio subsystem: %s", SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} else {
|
|
|
|
|
Mix_QuerySpec(&spec.freq, &spec.format, &spec.channels);
|
|
|
|
|
SDL_Log("Opened audio at %d Hz %d bit%s %s audio buffer\n", spec.freq,
|
|
|
|
|
(spec.format&0xFF),
|
|
|
|
|
(SDL_AUDIO_ISFLOAT(spec.format) ? " (float)" : ""),
|
|
|
|
|
(spec.channels > 2) ? "surround" : (spec.channels > 1) ? "stereo" : "mono");
|
|
|
|
|
}
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
ATTEMPT {
|
2025-01-01 21:53:21 -05:00
|
|
|
for ( int i = 0; i < numsprites ; i++) {
|
|
|
|
|
CATCH(errctx, sprite_load_json(spritepaths[i]));
|
|
|
|
|
}
|
2025-01-01 13:56:15 -05:00
|
|
|
CATCH(errctx, character_load_json("../assets/characters/littleguy.json"));
|
|
|
|
|
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");
|
2025-01-03 15:34:31 -05:00
|
|
|
actorptr->movement_controls_face = false;
|
2025-01-01 13:56:15 -05:00
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE(errctx, ERR_NULLPOINTER) {
|
|
|
|
|
SDL_Log("Attempting to load asset: %s (%s)", errctx->message, SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2025-01-01 21:53:21 -05:00
|
|
|
actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT);
|
2025-01-01 13:56:15 -05:00
|
|
|
actorptr->x = 320;
|
|
|
|
|
actorptr->y = 240;
|
|
|
|
|
actorptr->visible = true;
|
|
|
|
|
|
|
|
|
|
ATTEMPT {
|
|
|
|
|
//load_start_bgm("../assets/nutcracker.mid");
|
|
|
|
|
CATCH(errctx, load_start_bgm("../assets/memories.mp3"));
|
|
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE(errctx, ERR_NULLPOINTER) {
|
|
|
|
|
} HANDLE_GROUP(errctx, ERR_SDL) {
|
|
|
|
|
LOG_ERROR(errctx);
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2024-12-19 09:32:36 -05:00
|
|
|
|
2024-12-22 16:32:54 -05:00
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
ATTEMPT {
|
|
|
|
|
CATCH(errctx, tilemap_load("../assets/tilemap.tmj", (tilemap *)&gamemap));
|
|
|
|
|
} CLEANUP {
|
|
|
|
|
} PROCESS(errctx) {
|
|
|
|
|
} HANDLE_DEFAULT(errctx) {
|
|
|
|
|
SDL_Log("Error while loading tilemap: %s", SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} FINISH_NORETURN(errctx);
|
2024-12-23 23:25:53 -05:00
|
|
|
|
|
|
|
|
|
2025-01-01 13:56:15 -05:00
|
|
|
camera.x = 0;
|
|
|
|
|
camera.y = 0;
|
|
|
|
|
camera.w = 640;
|
|
|
|
|
camera.h = 480;
|
2024-12-08 15:39:07 -05: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");
|
|
|
|
|
|
|
|
|
|
if (event->type == SDL_EVENT_QUIT) {
|
|
|
|
|
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
|
|
|
|
} else if (event->type == SDL_EVENT_GAMEPAD_BUTTON_DOWN) {
|
|
|
|
|
CATCH(errctx, gamepad_handle_button_down(appstate, event));
|
|
|
|
|
} else if ( event->type == SDL_EVENT_GAMEPAD_BUTTON_UP) {
|
|
|
|
|
CATCH(errctx, gamepad_handle_button_up(appstate, event));
|
|
|
|
|
} else if (event->type == SDL_EVENT_GAMEPAD_ADDED) {
|
|
|
|
|
CATCH(errctx, gamepad_handle_added(appstate, event));
|
|
|
|
|
} else if (event->type == SDL_EVENT_GAMEPAD_REMOVED) {
|
|
|
|
|
CATCH(errctx, gamepad_handle_removed(appstate, event));
|
|
|
|
|
}
|
|
|
|
|
} 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. */
|
|
|
|
|
SDL_DestroyTexture(ball.texture);
|
|
|
|
|
b2DestroyWorld(physicsWorldId);
|
|
|
|
|
SDL_Log("Freeing music resources");
|
|
|
|
|
if ( bgm != NULL ) {
|
|
|
|
|
Mix_FreeMusic(bgm);
|
|
|
|
|
}
|
|
|
|
|
SDL_Log("Quitting mixer");
|
|
|
|
|
Mix_Quit();
|
2024-12-08 15:39:07 -05:00
|
|
|
}
|