(svn r3016) -NewGRF: Implement the start of reference counting for sprite groups. This will allow unloading.

This commit is contained in:
peter1138 2005-10-04 20:58:10 +00:00
parent 4879e04986
commit f614b86114
3 changed files with 15 additions and 2 deletions

View File

@ -266,6 +266,7 @@ void SetWagonOverrideSprites(EngineID engine, SpriteGroup *group, byte *train_id
* to prevent leaks. But first we need to refcount the SpriteGroup. * to prevent leaks. But first we need to refcount the SpriteGroup.
* --pasky */ * --pasky */
wo->group = group; wo->group = group;
group->ref_count++;
wo->trains = trains; wo->trains = trains;
wo->train_id = malloc(trains); wo->train_id = malloc(trains);
memcpy(wo->train_id, train_id, trains); memcpy(wo->train_id, train_id, trains);
@ -306,6 +307,7 @@ void SetCustomEngineSprites(EngineID engine, byte cargo, SpriteGroup *group)
* to prevent leaks. But first we need to refcount the SpriteGroup. * to prevent leaks. But first we need to refcount the SpriteGroup.
* --pasky */ * --pasky */
engine_custom_sprites[engine][cargo] = group; engine_custom_sprites[engine][cargo] = group;
group->ref_count++;
} }
typedef SpriteGroup *(*resolve_callback)(const SpriteGroup *spritegroup, typedef SpriteGroup *(*resolve_callback)(const SpriteGroup *spritegroup,

View File

@ -1374,6 +1374,7 @@ static void NewSpriteGroup(byte *buf, int len)
groupid = grf_load_word(&buf); groupid = grf_load_word(&buf);
if (HASBIT(groupid, 15)) { if (HASBIT(groupid, 15)) {
dg->ranges[i].group = NewCallBackResultSpriteGroup(groupid); dg->ranges[i].group = NewCallBackResultSpriteGroup(groupid);
dg->ranges[i].group->ref_count++;
} else if (groupid >= _cur_grffile->spritegroups_count) { } else if (groupid >= _cur_grffile->spritegroups_count) {
/* This doesn't exist for us. */ /* This doesn't exist for us. */
grf_load_word(&buf); // skip range grf_load_word(&buf); // skip range
@ -1383,6 +1384,7 @@ static void NewSpriteGroup(byte *buf, int len)
/* XXX: If multiple surreal sets attach a surreal /* XXX: If multiple surreal sets attach a surreal
* set this way, we are in trouble. */ * set this way, we are in trouble. */
dg->ranges[i].group = _cur_grffile->spritegroups[groupid]; dg->ranges[i].group = _cur_grffile->spritegroups[groupid];
dg->ranges[i].group->ref_count++;
} }
dg->ranges[i].low = grf_load_byte(&buf); dg->ranges[i].low = grf_load_byte(&buf);
@ -1400,8 +1402,10 @@ static void NewSpriteGroup(byte *buf, int len)
} else { } else {
dg->default_group = _cur_grffile->spritegroups[groupid]; dg->default_group = _cur_grffile->spritegroups[groupid];
} }
dg->default_group->ref_count++;
_cur_grffile->spritegroups[setid] = group; _cur_grffile->spritegroups[setid] = group;
group->ref_count++;
return; return;
} else if (numloaded == 0x80 || numloaded == 0x83) { } else if (numloaded == 0x80 || numloaded == 0x83) {
@ -1438,19 +1442,20 @@ static void NewSpriteGroup(byte *buf, int len)
if (HASBIT(groupid, 15)) { if (HASBIT(groupid, 15)) {
rg->groups[i] = NewCallBackResultSpriteGroup(groupid); rg->groups[i] = NewCallBackResultSpriteGroup(groupid);
rg->groups[i]->ref_count++;
} else if (groupid >= _cur_grffile->spritegroups_count) { } else if (groupid >= _cur_grffile->spritegroups_count) {
/* This doesn't exist for us. */ /* This doesn't exist for us. */
i--; i--;
rg->num_groups--; rg->num_groups--;
continue; continue;
} else { } else {
/* XXX: If multiple surreal sets attach a surreal
* set this way, we are in trouble. */
rg->groups[i] = _cur_grffile->spritegroups[groupid]; rg->groups[i] = _cur_grffile->spritegroups[groupid];
rg->groups[i]->ref_count++;
} }
} }
_cur_grffile->spritegroups[setid] = group; _cur_grffile->spritegroups[setid] = group;
group->ref_count++;
return; return;
} }
@ -1499,6 +1504,7 @@ static void NewSpriteGroup(byte *buf, int len)
} else { } else {
rg->loaded[i] = NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents, rg->sprites_per_set); rg->loaded[i] = NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents, rg->sprites_per_set);
} }
rg->loaded[i]->ref_count++;
DEBUG(grf, 8) ("NewSpriteGroup: + rg->loaded[%i] = %u (subset %u)", i, rg->loaded[i]->g.result.result, spriteset_id); DEBUG(grf, 8) ("NewSpriteGroup: + rg->loaded[%i] = %u (subset %u)", i, rg->loaded[i]->g.result.result, spriteset_id);
} }
@ -1509,10 +1515,12 @@ static void NewSpriteGroup(byte *buf, int len)
} else { } else {
rg->loading[i] = NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents, rg->sprites_per_set); rg->loading[i] = NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents, rg->sprites_per_set);
} }
rg->loading[i]->ref_count++;
DEBUG(grf, 8) ("NewSpriteGroup: + rg->loading[%i] = %u (subset %u)", i, rg->loading[i]->g.result.result, spriteset_id); DEBUG(grf, 8) ("NewSpriteGroup: + rg->loading[%i] = %u (subset %u)", i, rg->loading[i]->g.result.result, spriteset_id);
} }
_cur_grffile->spritegroups[setid] = group; _cur_grffile->spritegroups[setid] = group;
group->ref_count++;
} }
/* Action 0x03 */ /* Action 0x03 */
@ -1584,6 +1592,7 @@ static void NewVehicle_SpriteGroupMapping(byte *buf, int len)
} }
stat->spritegroup[1] = _cur_grffile->spritegroups[groupid]; stat->spritegroup[1] = _cur_grffile->spritegroups[groupid];
stat->spritegroup[1]->ref_count++;
} }
} }
@ -1602,6 +1611,7 @@ static void NewVehicle_SpriteGroupMapping(byte *buf, int len)
StationSpec *stat = &_cur_grffile->stations[stid]; StationSpec *stat = &_cur_grffile->stations[stid];
stat->spritegroup[0] = _cur_grffile->spritegroups[groupid]; stat->spritegroup[0] = _cur_grffile->spritegroups[groupid];
stat->spritegroup[0]->ref_count++;
stat->grfid = _cur_grffile->grfid; stat->grfid = _cur_grffile->grfid;
SetCustomStation(stid, stat); SetCustomStation(stid, stat);
stat->sclass = STAT_CLASS_NONE; stat->sclass = STAT_CLASS_NONE;

View File

@ -129,6 +129,7 @@ typedef enum SpriteGroupType {
struct SpriteGroup { struct SpriteGroup {
SpriteGroupType type; SpriteGroupType type;
byte ref_count;
union { union {
RealSpriteGroup real; RealSpriteGroup real;