1
0
Fork 0

(svn r16336) -Codechange: make the SpriteGroup pool more like the 'normal' pools

release/1.0
rubidium 2009-05-17 14:32:13 +00:00
parent 76784501a8
commit 10ea72a08e
3 changed files with 35 additions and 63 deletions

View File

@ -2595,9 +2595,7 @@ static void ReserveChangeInfo(byte *buf, size_t len)
*/ */
static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value) static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value)
{ {
SpriteGroup *group = AllocateSpriteGroup(); SpriteGroup *group = new SpriteGroup(SGT_CALLBACK);
group->type = SGT_CALLBACK;
/* Old style callback results have the highest byte 0xFF so signify it is a callback result /* Old style callback results have the highest byte 0xFF so signify it is a callback result
* New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */ * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
@ -2620,8 +2618,7 @@ static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value)
*/ */
static const SpriteGroup *NewResultSpriteGroup(SpriteID sprite, byte num_sprites) static const SpriteGroup *NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
{ {
SpriteGroup *group = AllocateSpriteGroup(); SpriteGroup *group = new SpriteGroup(SGT_RESULT);
group->type = SGT_RESULT;
group->g.result.sprite = sprite; group->g.result.sprite = sprite;
group->g.result.num_sprites = num_sprites; group->g.result.num_sprites = num_sprites;
return group; return group;
@ -2768,8 +2765,7 @@ static void NewSpriteGroup(byte *buf, size_t len)
/* Check we can load the var size parameter */ /* Check we can load the var size parameter */
if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return; if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
group = AllocateSpriteGroup(); group = new SpriteGroup(SGT_DETERMINISTIC);
group->type = SGT_DETERMINISTIC;
group->g.determ.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; group->g.determ.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
switch (GB(type, 2, 2)) { switch (GB(type, 2, 2)) {
@ -2843,8 +2839,7 @@ static void NewSpriteGroup(byte *buf, size_t len)
{ {
if (!check_length(bufend - buf, HasBit(type, 2) ? 8 : 7, "NewSpriteGroup (Randomized) (1)")) return; if (!check_length(bufend - buf, HasBit(type, 2) ? 8 : 7, "NewSpriteGroup (Randomized) (1)")) return;
group = AllocateSpriteGroup(); group = new SpriteGroup(SGT_RANDOMIZED);
group->type = SGT_RANDOMIZED;
group->g.random.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; group->g.random.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
if (HasBit(type, 2)) { if (HasBit(type, 2)) {
@ -2893,8 +2888,7 @@ static void NewSpriteGroup(byte *buf, size_t len)
if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return; if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return;
group = AllocateSpriteGroup(); group = new SpriteGroup(SGT_REAL);
group->type = SGT_REAL;
group->g.real.num_loaded = num_loaded; group->g.real.num_loaded = num_loaded;
group->g.real.num_loading = num_loading; group->g.real.num_loading = num_loading;
@ -2925,8 +2919,7 @@ static void NewSpriteGroup(byte *buf, size_t len)
byte num_sprites = max((uint8)1, type); byte num_sprites = max((uint8)1, type);
uint i; uint i;
group = AllocateSpriteGroup(); group = new SpriteGroup(SGT_TILELAYOUT);
group->type = SGT_TILELAYOUT;
group->g.layout.num_sprites = sprites; group->g.layout.num_sprites = sprites;
group->g.layout.dts = CallocT<DrawTileSprites>(1); group->g.layout.dts = CallocT<DrawTileSprites>(1);
@ -2987,8 +2980,7 @@ static void NewSpriteGroup(byte *buf, size_t len)
break; break;
} }
group = AllocateSpriteGroup(); group = new SpriteGroup(SGT_INDUSTRY_PRODUCTION);
group->type = SGT_INDUSTRY_PRODUCTION;
group->g.indprod.version = type; group->g.indprod.version = type;
if (type == 0) { if (type == 0) {
for (uint i = 0; i < 3; i++) { for (uint i = 0; i < 3; i++) {
@ -5631,7 +5623,8 @@ static void ResetNewGRFData()
_grf_id_overrides.clear(); _grf_id_overrides.clear();
InitializeSoundPool(); InitializeSoundPool();
InitializeSpriteGroupPool(); _SpriteGroup_pool.CleanPool();
_SpriteGroup_pool.AddBlockToPool();
} }
static void BuildCargoTranslationMap() static void BuildCargoTranslationMap()

View File

@ -7,68 +7,38 @@
#include "newgrf.h" #include "newgrf.h"
#include "newgrf_spritegroup.h" #include "newgrf_spritegroup.h"
#include "sprite.h" #include "sprite.h"
#include "oldpool_func.h"
static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item); DEFINE_OLD_POOL_GENERIC(SpriteGroup, SpriteGroup)
static uint _spritegroup_count = 0; SpriteGroup::~SpriteGroup()
STATIC_OLD_POOL(SpriteGroup, SpriteGroup, 9, 250, NULL, SpriteGroupPoolCleanBlock)
static void DestroySpriteGroup(SpriteGroup *group)
{ {
/* Free dynamically allocated memory */ /* Free dynamically allocated memory */
/* XXX Cast away the consts due to MSVC being buggy... */ switch (this->type) {
switch (group->type) {
case SGT_REAL: case SGT_REAL:
free((SpriteGroup**)group->g.real.loaded); free((SpriteGroup**)this->g.real.loaded);
free((SpriteGroup**)group->g.real.loading); free((SpriteGroup**)this->g.real.loading);
break; break;
case SGT_DETERMINISTIC: case SGT_DETERMINISTIC:
free(group->g.determ.adjusts); free(this->g.determ.adjusts);
free(group->g.determ.ranges); free(this->g.determ.ranges);
break; break;
case SGT_RANDOMIZED: case SGT_RANDOMIZED:
free((SpriteGroup**)group->g.random.groups); free((SpriteGroup**)this->g.random.groups);
break; break;
case SGT_TILELAYOUT: case SGT_TILELAYOUT:
free((void*)group->g.layout.dts->seq); free((void*)this->g.layout.dts->seq);
free(group->g.layout.dts); free(this->g.layout.dts);
break; break;
default: default:
break; break;
} }
}
static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item) this->type = SGT_INVALID;
{
uint i;
for (i = start_item; i <= end_item; i++) {
DestroySpriteGroup(GetSpriteGroup(i));
}
}
/* Allocate a new SpriteGroup */
SpriteGroup *AllocateSpriteGroup()
{
/* This is totally different to the other pool allocators, as we never remove an item from the pool. */
if (_spritegroup_count == GetSpriteGroupPoolSize()) {
if (!_SpriteGroup_pool.AddBlockToPool()) return NULL;
}
return GetSpriteGroup(_spritegroup_count++);
}
void InitializeSpriteGroupPool()
{
_SpriteGroup_pool.CleanPool();
_spritegroup_count = 0;
} }
TemporaryStorageArray<uint32, 0x110> _temp_store; TemporaryStorageArray<uint32, 0x110> _temp_store;

View File

@ -11,6 +11,7 @@
#include "gfx_type.h" #include "gfx_type.h"
#include "engine_type.h" #include "engine_type.h"
#include "tile_type.h" #include "tile_type.h"
#include "oldpool.h"
#include "newgrf_cargo.h" #include "newgrf_cargo.h"
#include "newgrf_callbacks.h" #include "newgrf_callbacks.h"
@ -182,8 +183,18 @@ enum SpriteGroupType {
SGT_INDUSTRY_PRODUCTION, SGT_INDUSTRY_PRODUCTION,
}; };
typedef uint32 SpriteGroupID;
DECLARE_OLD_POOL(SpriteGroup, SpriteGroup, 9, 250)
/* Common wrapper for all the different sprite group types */ /* Common wrapper for all the different sprite group types */
struct SpriteGroup { struct SpriteGroup : PoolItem<SpriteGroup, SpriteGroupID, &_SpriteGroup_pool> {
SpriteGroup(SpriteGroupType type = SGT_INVALID) :
type(type)
{
}
~SpriteGroup();
SpriteGroupType type; SpriteGroupType type;
union { union {
@ -195,13 +206,11 @@ struct SpriteGroup {
TileLayoutSpriteGroup layout; TileLayoutSpriteGroup layout;
IndustryProductionSpriteGroup indprod; IndustryProductionSpriteGroup indprod;
} g; } g;
inline bool IsValid() const { return this->type != SGT_INVALID; }
}; };
SpriteGroup *AllocateSpriteGroup();
void InitializeSpriteGroupPool();
struct ResolverObject { struct ResolverObject {
CallbackID callback; CallbackID callback;
uint32 callback_param1; uint32 callback_param1;