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 <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[])
|
|
|
|
|
{
|
2024-12-18 10:46:58 -05:00
|
|
|
spritesheet *spritesheetptr = NULL;
|
|
|
|
|
sprite *spriteptr = NULL;
|
|
|
|
|
actor *actorptr = NULL;
|
|
|
|
|
character *characterptr = NULL;
|
2024-12-17 22:13:10 -05:00
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
SDL_AudioSpec spec;
|
|
|
|
|
exclib_init();
|
2024-12-08 15:39:07 -05:00
|
|
|
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");
|
|
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
heap_init();
|
2024-12-17 22:13:10 -05:00
|
|
|
registry_init_actor();
|
|
|
|
|
registry_init_sprite();
|
|
|
|
|
registry_init_spritesheet();
|
2024-12-18 10:46:58 -05:00
|
|
|
registry_init_character();
|
|
|
|
|
|
2024-12-08 15:39:07 -05:00
|
|
|
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();
|
|
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
spec.freq = MIX_DEFAULT_FREQUENCY;
|
|
|
|
|
spec.format = MIX_DEFAULT_FORMAT;
|
|
|
|
|
spec.channels = MIX_DEFAULT_CHANNELS;
|
2024-12-08 15:39:07 -05:00
|
|
|
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 {
|
2024-12-18 10:46:58 -05:00
|
|
|
spritesheetptr = heap_next_spritesheet();
|
|
|
|
|
spritesheet_initialize(spritesheetptr, 48, 48, "../assets/Actor1.png");
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
spriteptr = heap_next_sprite();
|
|
|
|
|
sprite_initialize(spriteptr, "little guy facing left", spritesheetptr);
|
|
|
|
|
spriteptr->width = 48;
|
|
|
|
|
spriteptr->height = 48;
|
|
|
|
|
spriteptr->speed = 100;
|
|
|
|
|
spriteptr->loop = true;
|
|
|
|
|
spriteptr->loopReverse = true;
|
|
|
|
|
spriteptr->frames = 3;
|
|
|
|
|
spriteptr->frameids[0] = 12;
|
|
|
|
|
spriteptr->frameids[1] = 13;
|
|
|
|
|
spriteptr->frameids[2] = 14;
|
|
|
|
|
|
|
|
|
|
characterptr = heap_next_character();
|
|
|
|
|
character_initialize(characterptr, "little guy");
|
|
|
|
|
character_sprite_add(characterptr,
|
|
|
|
|
spriteptr,
|
|
|
|
|
(ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT | ACTOR_STATE_MOVING_LEFT));
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
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);
|
2024-12-08 15:39:07 -05:00
|
|
|
} CATCH(EXC_NULLPOINTER) {
|
|
|
|
|
SDL_Log("Attempting to load asset: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
|
} ETRY;
|
|
|
|
|
|
2024-12-18 10:46:58 -05:00
|
|
|
/*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);
|
2024-12-17 22:13:10 -05:00
|
|
|
} CATCH(EXC_NULLPOINTER) {
|
|
|
|
|
SDL_Log("Attempting to setup npc: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError());
|
|
|
|
|
return SDL_APP_FAILURE;
|
2024-12-18 10:46:58 -05:00
|
|
|
} ETRY;*/
|
2024-12-17 22:13:10 -05:00
|
|
|
|
2024-12-08 15:39:07 -05:00
|
|
|
/*
|
|
|
|
|
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;
|
2024-12-19 08:25:58 -05:00
|
|
|
|
|
|
|
|
camera.x = 0;
|
|
|
|
|
camera.y = 0;
|
|
|
|
|
camera.w = 640;
|
|
|
|
|
camera.h = 480;
|
2024-12-08 15:39:07 -05:00
|
|
|
|
|
|
|
|
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;
|
2024-12-19 08:25:58 -05:00
|
|
|
int i = 0;
|
|
|
|
|
iterator opflags;
|
|
|
|
|
|
|
|
|
|
BITMASK_CLEAR(opflags.flags);
|
|
|
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_UPDATE);
|
|
|
|
|
BITMASK_ADD(opflags.flags, ITERATOR_OP_RENDER);
|
2024-12-08 15:39:07 -05:00
|
|
|
|
2024-12-19 08:25:58 -05:00
|
|
|
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);
|
|
|
|
|
}
|
2024-12-08 15:39:07 -05:00
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
|