#define SDL_MAIN_USE_CALLBACKS #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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" }; char dirnamebuf[1024]; akerr_ErrorContext AKERR_NOIGNORE *music_toggle(actor *obj, SDL_Event *event) { SDL_PropertiesID bgmprops = 0; PREPARE_ERROR(errctx); if ( MIX_TrackPlaying(game.tracks[GAME_AUDIO_TRACK_BGM]) ) { FAIL_ZERO_RETURN( errctx, MIX_StopTrack(game.tracks[GAME_AUDIO_TRACK_BGM], 0), AKERR_SDL, "%s", SDL_GetError()); } else { SDL_SetNumberProperty(bgmprops, MIX_PROP_PLAY_LOOPS_NUMBER, -1); FAIL_ZERO_RETURN( errctx, MIX_PlayTrack(game.tracks[GAME_AUDIO_TRACK_BGM], bgmprops), AKERR_SDL, "%s", SDL_GetError()); } SUCCEED_RETURN(errctx); } akerr_ErrorContext AKERR_NOIGNORE *user_breakpoint(actor *obj, SDL_Event *event) { PREPARE_ERROR(errctx); SDL_Log("User breakpoint hit"); SUCCEED_RETURN(errctx); } SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[]) { SDL3GControlMap *controlmap; actor *actorptr = NULL; *appstate = (void *)&game.state; PREPARE_ERROR(errctx); ATTEMPT { FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer"); 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++) { strcpy((char *)&dirnamebuf, spritepaths[i]); CATCH(errctx, sprite_load_json((char *)&dirnamebuf)); } strcpy((char *)&dirnamebuf, "assets/characters/littleguy.json"); CATCH(errctx, character_load_json((char *)&dirnamebuf)); 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, AKERR_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; strcpy((char *)&dirnamebuf, "assets/memories.mp3"); CATCH(errctx, load_start_bgm((char *)&dirnamebuf)); strcpy((char *)&dirnamebuf, "assets/tilemap.tmj"); CATCH(errctx, tilemap_load((char *)&dirnamebuf, (tilemap *)&gamemap)); // 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].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[0].event_off = SDL_EVENT_KEY_UP; controlmap->controls[0].handler_on = &SDL3GActor_cmhf_down_on; controlmap->controls[0].handler_off = &SDL3GActor_cmhf_down_off; // Move up controlmap->controls[1].key = SDLK_UP; controlmap->controls[1].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[1].event_off = SDL_EVENT_KEY_UP; controlmap->controls[1].handler_on = &SDL3GActor_cmhf_up_on; controlmap->controls[1].handler_off = &SDL3GActor_cmhf_up_off; // Move left controlmap->controls[2].key = SDLK_LEFT; controlmap->controls[2].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[2].event_off = SDL_EVENT_KEY_UP; controlmap->controls[2].handler_on = &SDL3GActor_cmhf_left_on; controlmap->controls[2].handler_off = &SDL3GActor_cmhf_left_off; // Move right controlmap->controls[3].key = SDLK_RIGHT; controlmap->controls[3].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[3].event_off = SDL_EVENT_KEY_UP; controlmap->controls[3].handler_on = &SDL3GActor_cmhf_right_on; controlmap->controls[3].handler_off = &SDL3GActor_cmhf_right_off; // Breakpoint controlmap->controls[4].key = SDLK_ESCAPE; controlmap->controls[4].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[4].handler_on = &user_breakpoint; // Toggle the music controlmap->controls[5].key = SDLK_M; controlmap->controls[5].event_on = SDL_EVENT_KEY_DOWN; controlmap->controls[5].handler_on = &music_toggle; } CLEANUP { } PROCESS(errctx) { } HANDLE_DEFAULT(errctx) { LOG_ERROR(errctx); return SDL_APP_FAILURE; } FINISH_NORETURN(errctx); return SDL_APP_CONTINUE; } SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event) { PREPARE_ERROR(errctx); ATTEMPT { FAIL_ZERO_BREAK(errctx, appstate, AKERR_NULLPOINTER, "NULL appstate pointer"); FAIL_ZERO_BREAK(errctx, event, AKERR_NULLPOINTER, "NULL event pointer"); CATCH(errctx, controller_handle_event(appstate, event)); if (event->type == SDL_EVENT_QUIT) { return SDL_APP_SUCCESS; /* end the program, reporting success to the OS. */ } } CLEANUP { } PROCESS(errctx) { } FINISH_NORETURN(errctx); 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; 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); 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; } 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(); }