Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix vga_state memory bugs #725

Merged
merged 2 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/game/objects/har.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
void har_finished(object *obj);
int har_act(object *obj, int act_type);
void har_spawn_scrap(object *obj, vec2i pos, int amount);
void har_palette_transform(damage_tracker *damage, vga_palette *pal, void *obj);

void har_free(object *obj) {
vga_state_dontuse_palette_transform(har_palette_transform, obj);
har *h = object_get_userdata(obj);
list_free(&h->har_hooks);
#ifdef DEBUGMODE
Expand Down
1 change: 1 addition & 0 deletions src/game/protos/object.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,7 @@ void object_free(object *obj) {
if(obj == NULL) {
return;
}
vga_state_dontuse_palette_transform(object_scenewide_palette_transform, obj);
if(obj->free != NULL) {
obj->free(obj);
}
Expand Down
20 changes: 19 additions & 1 deletion src/video/vga_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,26 @@ void vga_state_copy_base_palette_range(vga_index dst, vga_index src, vga_index c
}

void vga_state_use_palette_transform(vga_palette_transform transform_callback, void *userdata) {
assert(state.transformer_count < MAX_TRANSFORMER_COUNT - 1);
for(unsigned int i = 0; i < state.transformer_count; i++) {
if(state.transformers[i].callback == transform_callback && state.transformers[i].userdata == userdata) {
// don't push duplicates
return;
}
}

assert(state.transformer_count < MAX_TRANSFORMER_COUNT);
state.transformers[state.transformer_count].callback = transform_callback;
state.transformers[state.transformer_count].userdata = userdata;
state.transformer_count++;
}

bool vga_state_dontuse_palette_transform(vga_palette_transform transform_callback, void *userdata) {
for(unsigned int i = 0; i < state.transformer_count; i++) {
if(state.transformers[i].callback == transform_callback && state.transformers[i].userdata == userdata) {
memmove(&state.transformers[i], &state.transformers[i + 1],
sizeof(palette_transformer) * (--state.transformer_count - i));
return true;
}
}
return false;
}
1 change: 1 addition & 0 deletions src/video/vga_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ void vga_state_set_base_palette_range(vga_index start, vga_index count, vga_colo
void vga_state_copy_base_palette_range(vga_index dst, vga_index src, vga_index count);

void vga_state_use_palette_transform(vga_palette_transform transform_callback, void *userdata);
bool vga_state_dontuse_palette_transform(vga_palette_transform transform_callback, void *userdata);

#endif // VGA_STATE_H
Loading