Moved json helpers out into their own json_helpers file Made `sprite_load_json()`, you can load sprite definitions from json now. Spritesheets are automatically loaded if they're not in the registry. See assets/sprites/ for an example json sprite definition.
177 lines
5.6 KiB
C
177 lines
5.6 KiB
C
#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 <aklabs/exclib.h>
|
|
#include <box2d/box2d.h>
|
|
|
|
#include "tilemap.h"
|
|
#include "game.h"
|
|
#include "physics.h"
|
|
#include "draw.h"
|
|
#include "assets.h"
|
|
#include "sprite.h"
|
|
|
|
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
|
|
{
|
|
spritesheet *spritesheetptr = NULL;
|
|
sprite *spriteptr = NULL;
|
|
actor *actorptr = NULL;
|
|
character *characterptr = NULL;
|
|
|
|
SDL_AudioSpec spec;
|
|
exclib_init();
|
|
exclib_name_exception(EXC_SDL_INIT, "SDL Initialization Failure");
|
|
exclib_name_exception(EXC_SDL_MUSICMIXER, "SDL Music Mixer Failure");
|
|
exclib_name_exception(EXC_GAME_UNDEFINED, "Undefined method or value");
|
|
exclib_name_exception(EXC_ATTRIBUTEERROR, "Attribute Error");
|
|
exclib_name_exception(EXC_TYPEERROR, "Type Error");
|
|
exclib_name_exception(EXC_KEYERROR, "Key Error");
|
|
|
|
heap_init();
|
|
registry_init_actor();
|
|
registry_init_sprite();
|
|
registry_init_spritesheet();
|
|
registry_init_character();
|
|
|
|
SDL_SetAppMetadata("SDL3-GameTest", "0.1", "net.aklabs.sdl3-gametest");
|
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO )) {
|
|
SDL_Log("Couldn't initialize SDL: %s", SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
GAME_init_physics();
|
|
|
|
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");
|
|
}
|
|
|
|
TRY {
|
|
sprite_load_json("../assets/sprites/little_guy_walking_left.json");
|
|
|
|
characterptr = heap_next_character();
|
|
character_initialize(characterptr, "little guy");
|
|
character_sprite_add(
|
|
characterptr,
|
|
SDL_GetPointerProperty(
|
|
REGISTRY_SPRITE,
|
|
"little guy walking left",
|
|
NULL),
|
|
(ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT | ACTOR_STATE_MOVING_LEFT)
|
|
);
|
|
|
|
actorptr = heap_next_actor();
|
|
actor_initialize(actorptr, "player");
|
|
actorptr->basechar = characterptr;
|
|
actorptr->x = 0;
|
|
actorptr->y = 0;
|
|
actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT | ACTOR_STATE_MOVING_LEFT);
|
|
} CATCH(EXC_NULLPOINTER) {
|
|
SDL_Log("Attempting to load asset: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
} ETRY;
|
|
|
|
/*TRY {
|
|
actorptr = heap_next_actor();
|
|
actor_initialize(actorptr, "npc");
|
|
actorptr->basechar = characterptr;
|
|
actorptr->x = 0;
|
|
actorptr->y = 0;
|
|
actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT | ACTOR_STATE_MOVING_LEFT);
|
|
} CATCH(EXC_NULLPOINTER) {
|
|
SDL_Log("Attempting to setup npc: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
} ETRY;*/
|
|
|
|
/*
|
|
TRY {
|
|
//load_start_bgm("../assets/nutcracker.mid");
|
|
load_start_bgm("../assets/memories.mp3");
|
|
} CATCH(EXC_NULLPOINTER) {
|
|
} CATCH_GROUP(EXC_SDL_INIT) {
|
|
} CATCH_GROUP(EXC_SDL_MUSICMIXER) {
|
|
SDL_Log("Attempting to load and play background music: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
} ETRY;
|
|
*/
|
|
|
|
TRY {
|
|
tilemap_load("../assets/tilemap.tmj", &gamemap);
|
|
} DEFAULT {
|
|
SDL_Log("Exception while loading tilemap: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
return SDL_APP_FAILURE;
|
|
} ETRY;
|
|
|
|
camera.x = 0;
|
|
camera.y = 0;
|
|
camera.w = 640;
|
|
camera.h = 480;
|
|
|
|
return SDL_APP_CONTINUE;
|
|
}
|
|
|
|
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
|
|
{
|
|
if (event->type == SDL_EVENT_QUIT) {
|
|
return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */
|
|
} else if (event->type == SDL_EVENT_JOYSTICK_BUTTON_UP) {
|
|
GAME_handle_joystick_button_up(appstate, event);
|
|
} else if (event->type == SDL_EVENT_JOYSTICK_ADDED) {
|
|
GAME_handle_joystick_added(appstate, event);
|
|
} else if (event->type == SDL_EVENT_JOYSTICK_REMOVED) {
|
|
GAME_handle_joystick_removed(appstate, event);
|
|
}
|
|
return SDL_APP_CONTINUE; /* carry on with the program! */
|
|
}
|
|
|
|
SDL_AppResult SDL_AppIterate(void *appstate)
|
|
{
|
|
SDL_FRect dest;
|
|
b2Vec2 position;
|
|
int i = 0;
|
|
iterator opflags;
|
|
|
|
BITMASK_CLEAR(opflags.flags);
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_UPDATE);
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_RENDER);
|
|
|
|
for ( i = 0; i < gamemap.numlayers; i++ ) {
|
|
opflags.layerid = i;
|
|
tilemap_draw(renderer, &gamemap, &camera, i);
|
|
SDL_EnumerateProperties(REGISTRY_ACTOR, ®istry_iterate_actor, (void *)&opflags);
|
|
}
|
|
SDL_RenderPresent(renderer);
|
|
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();
|
|
}
|