Skip to content

Commit

Permalink
Ensure all resources are freed (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jan 4, 2025
1 parent a05a616 commit e974d8b
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct AppContext {
SDL_Renderer* renderer;
SDL_Texture* messageTex;
SDL_FRect messageDest;
SDL_AudioDeviceID audioDevice;
Mix_Music* music;
SDL_AppResult app_quit = SDL_APP_CONTINUE;
};

Expand Down Expand Up @@ -67,6 +69,10 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
// make a texture from the surface
SDL_Texture* messageTex = SDL_CreateTextureFromSurface(renderer, surfaceMessage);

// we no longer need the font or the surface, so we can destroy those now.
TTF_CloseFont(font);
SDL_DestroySurface(surfaceMessage);

// get the on-screen dimensions of the text. this is necessary for rendering it
auto texprops = SDL_GetTextureProperties(messageTex);
SDL_FRect text_rect{
Expand Down Expand Up @@ -113,7 +119,9 @@ SDL_AppResult SDL_AppInit(void** appstate, int argc, char* argv[]) {
.window = window,
.renderer = renderer,
.messageTex = messageTex,
.messageDest = text_rect
.messageDest = text_rect,
.audioDevice = audioDevice,
.music = music,
};

SDL_Log("Application started successfully!");
Expand Down Expand Up @@ -145,7 +153,6 @@ SDL_AppResult SDL_AppIterate(void *appstate) {
SDL_RenderTexture(app->renderer, app->messageTex, NULL, &app->messageDest);
SDL_RenderPresent(app->renderer);


return app->app_quit;
}

Expand All @@ -154,8 +161,15 @@ void SDL_AppQuit(void* appstate, SDL_AppResult result) {
if (app) {
SDL_DestroyRenderer(app->renderer);
SDL_DestroyWindow(app->window);

Mix_FadeOutMusic(500); // prevent the music from abruptly ending. this call blocks until the music has finished fading
Mix_FreeMusic(app->music);
SDL_CloseAudioDevice(app->audioDevice);

delete app;
}
TTF_Quit();
Mix_Quit();

SDL_Log("Application quit successfully!");
}

0 comments on commit e974d8b

Please sign in to comment.