Skip to content

Commit

Permalink
Fix vga palette tricks using objects after free
Browse files Browse the repository at this point in the history
  • Loading branch information
Nopey committed Nov 2, 2024
1 parent e9cae17 commit f1249ce
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
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
11 changes: 11 additions & 0 deletions src/video/vga_state.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,14 @@ void vga_state_use_palette_transform(vga_palette_transform transform_callback, v
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

0 comments on commit f1249ce

Please sign in to comment.