Skip to content

Commit

Permalink
fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
xesf committed Oct 20, 2024
1 parent 48bc668 commit 1ea1fd7
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ void game_introduction() {
i32 sample_hidden_index = 0;
u8 *sample_ptr = NULL;
u32 entry_size = hqr_get_hidden_entry_ptr(&sample_ptr, menu_samples, 0);

sample_play_ptr(HQR_MENU_SAMPLES * 100 + sample_hidden_index, sample_ptr, entry_size, 22050, 0, 0);
memory_free(sample_ptr);
memory_free(menu_samples);
}

void game_release(state_t *state) {
Expand Down
5 changes: 2 additions & 3 deletions src/sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,10 @@ void sample_set_position(i32 channel_index, i32 x, i32 y, i32 z, i32 hero_x, i32

void sample_play(i32 index, i32 frequency, i32 repeat, i32 pan) {
i32 sample_size = 0;
u8* sample_ptr;
u8* sample_ptr = NULL;
sample_size = hqr_get_entry_alloc(&sample_ptr, HQR_RESOURCE, index);
sample_play_position(index, sample_ptr, sample_size, frequency, repeat, 0, 0, 0, -1, 0, 0, 0);
memory_free(sample_ptr);
}

void sample_play_ptr(i32 index, u8* sample_ptr, i32 sample_size, i32 frequency, i32 repeat, i32 pan) {
Expand All @@ -71,8 +72,6 @@ void sample_play_position(i32 index, u8* sample_ptr, i32 sample_size, i32 freque
if (system_mixer_play(sample_ptr, sample_size, channel_index, repeat) == -1)
printf("Error while playing Sample %d \n", index);
}

free(sample_ptr);
}

void sample_resume() {
Expand Down
14 changes: 5 additions & 9 deletions src/system_sdl.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,13 @@ void system_init(system_t *system, c8 *title, i32 width, i32 height, i32 bpp) {
SDL_SetRenderLogicalPresentation(system->renderer, width, height, SDL_LOGICAL_PRESENTATION_LETTERBOX);
SDL_SetRenderDrawColor(system->renderer, 0, 0, 0, 255);

system->texture = SDL_CreateTexture(
system->renderer,
SDL_PIXELFORMAT_RGBA8888, // SDL_PIXELFORMAT_INDEX8
SDL_TEXTUREACCESS_STATIC,
width,
height
);

SDL_UpdateWindowSurface(system->window);
}

void system_release(system_t *system) {
SDL_DestroyTexture(system->texture);
SDL_DestroySurface(system->surface);
SDL_DestroyPalette(system->palette);
SDL_DestroyRenderer(system->renderer);
SDL_DestroyWindow(system->window);
SDL_Quit();
Expand Down Expand Up @@ -102,6 +96,9 @@ void system_create_surface(system_t *system, u8 *front_buffer) {

void system_blit(system_t *system) {
SDL_RenderClear(system->renderer);
if (system->texture) {
SDL_DestroyTexture(system->texture);
}
system->texture = SDL_CreateTextureFromSurface(system->renderer, system->surface);
SDL_RenderTexture(system->renderer, system->texture, NULL, NULL);
}
Expand All @@ -111,7 +108,6 @@ inline void system_flip(system_t *system) {
}

void system_set_palette(system_t *system, u8 *palette) {
// SDL_SetPaletteColors(system->palette, (SDL_Color *)palette, 0, 256);
for (i32 i = 0; i < 256; i++) {
system->palette->colors[i].r = palette[i * 3 + 0];
system->palette->colors[i].g = palette[i * 3 + 1];
Expand Down
11 changes: 10 additions & 1 deletion src/system_sdl_mixer.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ inline void system_mixer_set_distance(i32 channel_index, i32 distance) {

inline void system_mixer_load(u8 *sample_ptr, i32 samples_size) {
SDL_IOStream *rw = SDL_IOFromMem(sample_ptr, samples_size);
if (chunk != NULL) {
Mix_FreeChunk(chunk);
}
chunk = Mix_LoadWAV_IO(rw, 1);
SDL_FlushIO(rw);
}

inline void system_mixer_free() {
Expand Down Expand Up @@ -98,7 +102,11 @@ void system_mixer_music_fade_out(i32 ms) {

inline void system_mixer_load_music(u8 *music_ptr, i32 music_size) {
SDL_IOStream *rw = SDL_IOFromMem(music_ptr, music_size);
if (current_track != NULL) {
Mix_FreeMusic(current_track);
}
current_track = Mix_LoadMUS_IO(rw, TRUE);
SDL_FlushIO(rw);
}

i32 system_mixer_free_music() {
Expand All @@ -118,8 +126,9 @@ i32 system_mixer_play_music(u8 *music_ptr, i32 music_size, i32 loop) {
i32 system_mixer_play_music_mp3(c8 *music_file) {
int error_code = 0;
current_track = Mix_LoadMUS(music_file);
if (current_track == NULL)
if (current_track == NULL) {
printf("Mix_LoadMUS: %s\n", SDL_GetError());
}
error_code = Mix_PlayMusic(current_track, -1);
if (error_code == -1) {
printf("Mix_PlayMusic: %s\n", SDL_GetError());
Expand Down

0 comments on commit 1ea1fd7

Please # to comment.