#define SDL_MAIN_USE_CALLBACKS #include #include #include #include #include #include #include #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"); sprite_load_json("../assets/sprites/little_guy_facing_left.json"); character_load_json("../assets/characters/littleguy.json"); actorptr = heap_next_actor(); actor_initialize(actorptr, "player"); actorptr->basechar = SDL_GetPointerProperty( REGISTRY_CHARACTER, "little guy", NULL); actorptr->state = (ACTOR_STATE_ALIVE | ACTOR_STATE_FACE_LEFT); } EXCEPT { } CATCH(EXC_NULLPOINTER) { SDL_Log("Attempting to load asset: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError()); return SDL_APP_FAILURE; } FINALLY { } ETRY; /* TRY { //load_start_bgm("../assets/nutcracker.mid"); load_start_bgm("../assets/memories.mp3"); } EXCEPT { } 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; } FINALLY { } ETRY; */ TRY { tilemap_load("../assets/tilemap.tmj", &gamemap); } EXCEPT { } DEFAULT { SDL_Log("Exception while loading tilemap: %s (%s)", EXCLIB_EXCEPTION->description, SDL_GetError()); return SDL_APP_FAILURE; } FINALLY { } 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(); }