diff options
author | peter1138 <peter1138@openttd.org> | 2007-05-10 06:42:43 +0000 |
---|---|---|
committer | peter1138 <peter1138@openttd.org> | 2007-05-10 06:42:43 +0000 |
commit | fe5830c4b7c34f3b7b89e3451c464363de8d431b (patch) | |
tree | 6092409bb54686ca643f99762914594cd871f7dd | |
parent | 4500c365e1b599ee0034242f958506dca6c6b6ad (diff) | |
download | openttd-fe5830c4b7c34f3b7b89e3451c464363de8d431b.tar.xz |
(svn r9818) -Codechange: Cache wagon override sprite set whenever consist is changed, instead of 'recalculating' it every time it is used or just tested.
-rw-r--r-- | src/newgrf_engine.cpp | 17 | ||||
-rw-r--r-- | src/newgrf_engine.h | 1 | ||||
-rw-r--r-- | src/train_cmd.cpp | 3 | ||||
-rw-r--r-- | src/vehicle.h | 3 |
4 files changed, 15 insertions, 9 deletions
diff --git a/src/newgrf_engine.cpp b/src/newgrf_engine.cpp index 4e72919cb..f7b6fd112 100644 --- a/src/newgrf_engine.cpp +++ b/src/newgrf_engine.cpp @@ -63,22 +63,22 @@ void SetWagonOverrideSprites(EngineID engine, CargoID cargo, const SpriteGroup * memcpy(wo->train_id, train_id, trains); } -static const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, byte overriding_engine) +const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, byte overriding_engine) { const WagonOverrides *wos = &_engine_wagon_overrides[engine]; - int i; /* XXX: This could turn out to be a timesink on profiles. We could * always just dedicate 65535 bytes for an [engine][train] trampoline * for O(1). Or O(logMlogN) and searching binary tree or smt. like * that. --pasky */ - for (i = 0; i < wos->overrides_count; i++) { + for (int i = 0; i < wos->overrides_count; i++) { const WagonOverride *wo = &wos->overrides[i]; - int j; - for (j = 0; j < wo->trains; j++) { - if (wo->train_id[j] == overriding_engine && (wo->cargo == cargo || wo->cargo == CT_DEFAULT)) return wo->group; + if (wo->cargo != cargo && wo->cargo != CT_DEFAULT) continue; + + for (int j = 0; j < wo->trains; j++) { + if (wo->train_id[j] == overriding_engine) return wo->group; } } return NULL; @@ -850,8 +850,7 @@ static const SpriteGroup *GetVehicleSpriteGroup(EngineID engine, const Vehicle * cargo = v->cargo_type; if (v->type == VEH_TRAIN) { - group = GetWagonOverrideSpriteSet(engine, cargo, v->u.rail.first_engine); - + group = v->u.rail.cached_override; if (group != NULL) return group; } } @@ -912,7 +911,7 @@ SpriteID GetRotorOverrideSprite(EngineID engine, const Vehicle *v, bool info_vie bool UsesWagonOverride(const Vehicle* v) { assert(v->type == VEH_TRAIN); - return GetWagonOverrideSpriteSet(v->engine_type, v->cargo_type, v->u.rail.first_engine) != NULL; + return v->u.rail.cached_override != NULL; } /** diff --git a/src/newgrf_engine.h b/src/newgrf_engine.h index 812f30b43..e77cdac0f 100644 --- a/src/newgrf_engine.h +++ b/src/newgrf_engine.h @@ -14,6 +14,7 @@ extern int _traininfo_vehicle_width; void SetWagonOverrideSprites(EngineID engine, CargoID cargo, const struct SpriteGroup *group, byte *train_id, int trains); +const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, byte overriding_engine); void SetCustomEngineSprites(EngineID engine, byte cargo, const struct SpriteGroup *group); void SetRotorOverrideSprites(EngineID engine, const struct SpriteGroup *group); SpriteID GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction direction); diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 7ed30d147..0b80bbb89 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -166,6 +166,9 @@ void TrainConsistChanged(Vehicle* v) if (IsTrainEngine(u)) first_engine = u->engine_type; + /* Cache wagon override sprite group. NULL is returned if there is none */ + u->u.rail.cached_override = GetWagonOverrideSpriteSet(u->engine_type, u->cargo_type, u->u.rail.first_engine); + if (rvi_u->visual_effect != 0) { u->u.rail.cached_vis_effect = rvi_u->visual_effect; } else { diff --git a/src/vehicle.h b/src/vehicle.h index 6fe8f5521..54f9da6ac 100644 --- a/src/vehicle.h +++ b/src/vehicle.h @@ -141,6 +141,9 @@ struct VehicleRail { /* Link between the two ends of a multiheaded engine */ Vehicle *other_multiheaded_part; + + /* Cached wagon override spritegroup */ + const struct SpriteGroup *cached_override; }; enum { |