Add non-animated SSSE3 blitter
[openttd/fttd.git] / src / newgrf_spritegroup.h
blobe6eb4fbcbd914296a0a4a20dfa6e54ff66f8dff2
1 /* $Id$ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file newgrf_spritegroup.h Action 2 handling. */
12 #ifndef NEWGRF_SPRITEGROUP_H
13 #define NEWGRF_SPRITEGROUP_H
15 #include "town_type.h"
16 #include "engine_type.h"
17 #include "house_type.h"
19 #include "newgrf_callbacks.h"
20 #include "newgrf_generic.h"
21 #include "newgrf_storage.h"
22 #include "newgrf_commons.h"
24 /**
25 * Gets the value of a so-called newgrf "register".
26 * @param i index of the register
27 * @pre i < 0x110
28 * @return the value of the register
30 static inline uint32 GetRegister(uint i)
32 extern TemporaryStorageArray<int32, 0x110> _temp_store;
33 return _temp_store.GetValue(i);
36 /**
37 * Clears the value of a so-called newgrf "register".
38 * @param i index of the register
39 * @pre i < 0x110
41 static inline void ClearRegister(uint i)
43 extern TemporaryStorageArray<int32, 0x110> _temp_store;
44 _temp_store.StoreValue(i, 0);
47 /* List of different sprite group types */
48 enum SpriteGroupType {
49 SGT_REAL,
50 SGT_DETERMINISTIC,
51 SGT_RANDOMIZED,
52 SGT_CALLBACK,
53 SGT_RESULT,
54 SGT_TILELAYOUT,
55 SGT_INDUSTRY_PRODUCTION,
58 struct SpriteGroup;
59 typedef uint32 SpriteGroupID;
60 struct ResolverObject;
62 /* SPRITE_WIDTH is 24. ECS has roughly 30 sprite groups per real sprite.
63 * Adding an 'extra' margin would be assuming 64 sprite groups per real
64 * sprite. 64 = 2^6, so 2^30 should be enough (for now) */
65 typedef Pool<SpriteGroup, SpriteGroupID, 1024, 1 << 30, PT_DATA> SpriteGroupPool;
66 extern SpriteGroupPool _spritegroup_pool;
68 /* Common wrapper for all the different sprite group types */
69 struct SpriteGroup : SpriteGroupPool::PoolItem<&_spritegroup_pool> {
70 protected:
71 SpriteGroup(SpriteGroupType type) : type(type) {}
72 /** Base sprite group resolver */
73 virtual const SpriteGroup *Resolve(ResolverObject &object) const { return this; };
75 public:
76 virtual ~SpriteGroup() {}
78 SpriteGroupType type;
80 virtual SpriteID GetResult() const { return 0; }
81 virtual byte GetNumResults() const { return 0; }
82 virtual uint16 GetCallbackResult() const { return CALLBACK_FAILED; }
84 static const SpriteGroup *Resolve(const SpriteGroup *group, ResolverObject &object, bool top_level = true);
88 /* 'Real' sprite groups contain a list of other result or callback sprite
89 * groups. */
90 struct RealSpriteGroup : SpriteGroup {
91 RealSpriteGroup() : SpriteGroup(SGT_REAL) {}
92 ~RealSpriteGroup();
94 /* Loaded = in motion, loading = not moving
95 * Each group contains several spritesets, for various loading stages */
97 /* XXX: For stations the meaning is different - loaded is for stations
98 * with small amount of cargo whilst loading is for stations with a lot
99 * of da stuff. */
101 byte num_loaded; ///< Number of loaded groups
102 byte num_loading; ///< Number of loading groups
103 const SpriteGroup **loaded; ///< List of loaded groups (can be SpriteIDs or Callback results)
104 const SpriteGroup **loading; ///< List of loading groups (can be SpriteIDs or Callback results)
106 protected:
107 const SpriteGroup *Resolve(ResolverObject &object) const;
110 /* Shared by deterministic and random groups. */
111 enum VarSpriteGroupScope {
112 VSG_BEGIN,
114 VSG_SCOPE_SELF = VSG_BEGIN, ///< Resolved object itself
115 VSG_SCOPE_PARENT, ///< Related object of the resolved one
116 VSG_SCOPE_RELATIVE, ///< Relative position (vehicles only)
118 VSG_END
120 DECLARE_POSTFIX_INCREMENT(VarSpriteGroupScope)
122 enum DeterministicSpriteGroupSize {
123 DSG_SIZE_BYTE,
124 DSG_SIZE_WORD,
125 DSG_SIZE_DWORD,
128 enum DeterministicSpriteGroupAdjustType {
129 DSGA_TYPE_NONE,
130 DSGA_TYPE_DIV,
131 DSGA_TYPE_MOD,
134 enum DeterministicSpriteGroupAdjustOperation {
135 DSGA_OP_ADD, ///< a + b
136 DSGA_OP_SUB, ///< a - b
137 DSGA_OP_SMIN, ///< (signed) min(a, b)
138 DSGA_OP_SMAX, ///< (signed) max(a, b)
139 DSGA_OP_UMIN, ///< (unsigned) min(a, b)
140 DSGA_OP_UMAX, ///< (unsigned) max(a, b)
141 DSGA_OP_SDIV, ///< (signed) a / b
142 DSGA_OP_SMOD, ///< (signed) a % b
143 DSGA_OP_UDIV, ///< (unsigned) a / b
144 DSGA_OP_UMOD, ///< (unsigned) a & b
145 DSGA_OP_MUL, ///< a * b
146 DSGA_OP_AND, ///< a & b
147 DSGA_OP_OR, ///< a | b
148 DSGA_OP_XOR, ///< a ^ b
149 DSGA_OP_STO, ///< store a into temporary storage, indexed by b. return a
150 DSGA_OP_RST, ///< return b
151 DSGA_OP_STOP, ///< store a into persistent storage, indexed by b, return a
152 DSGA_OP_ROR, ///< rotate a b positions to the right
153 DSGA_OP_SCMP, ///< (signed) comparison (a < b -> 0, a == b = 1, a > b = 2)
154 DSGA_OP_UCMP, ///< (unsigned) comparison (a < b -> 0, a == b = 1, a > b = 2)
155 DSGA_OP_SHL, ///< a << b
156 DSGA_OP_SHR, ///< (unsigned) a >> b
157 DSGA_OP_SAR, ///< (signed) a >> b
161 struct DeterministicSpriteGroupAdjust {
162 DeterministicSpriteGroupAdjustOperation operation;
163 DeterministicSpriteGroupAdjustType type;
164 byte variable;
165 byte parameter; ///< Used for variables between 0x60 and 0x7F inclusive.
166 byte shift_num;
167 uint32 and_mask;
168 uint32 add_val;
169 uint32 divmod_val;
170 const SpriteGroup *subroutine;
174 struct DeterministicSpriteGroupRange {
175 const SpriteGroup *group;
176 uint32 low;
177 uint32 high;
181 struct DeterministicSpriteGroup : SpriteGroup {
182 DeterministicSpriteGroup() : SpriteGroup(SGT_DETERMINISTIC) {}
183 ~DeterministicSpriteGroup();
185 VarSpriteGroupScope var_scope;
186 DeterministicSpriteGroupSize size;
187 uint num_adjusts;
188 byte num_ranges;
189 DeterministicSpriteGroupAdjust *adjusts;
190 DeterministicSpriteGroupRange *ranges; // Dynamically allocated
192 /* Dynamically allocated, this is the sole owner */
193 const SpriteGroup *default_group;
195 protected:
196 const SpriteGroup *Resolve(ResolverObject &object) const;
199 enum RandomizedSpriteGroupCompareMode {
200 RSG_CMP_ANY,
201 RSG_CMP_ALL,
204 struct RandomizedSpriteGroup : SpriteGroup {
205 RandomizedSpriteGroup() : SpriteGroup(SGT_RANDOMIZED) {}
206 ~RandomizedSpriteGroup();
208 VarSpriteGroupScope var_scope; ///< Take this object:
210 RandomizedSpriteGroupCompareMode cmp_mode; ///< Check for these triggers:
211 byte triggers;
212 byte count;
214 byte lowest_randbit; ///< Look for this in the per-object randomized bitmask:
215 byte num_groups; ///< must be power of 2
217 const SpriteGroup **groups; ///< Take the group with appropriate index:
219 protected:
220 const SpriteGroup *Resolve(ResolverObject &object) const;
224 /* This contains a callback result. A failed callback has a value of
225 * CALLBACK_FAILED */
226 struct CallbackResultSpriteGroup : SpriteGroup {
228 * Creates a spritegroup representing a callback result
229 * @param value The value that was used to represent this callback result
230 * @param grf_version8 True, if we are dealing with a new NewGRF which uses GRF version >= 8.
232 CallbackResultSpriteGroup(uint16 value, bool grf_version8) :
233 SpriteGroup(SGT_CALLBACK),
234 result(value)
236 /* Old style callback results (only valid for version < 8) have the highest byte 0xFF so signify it is a callback result.
237 * New style ones only have the highest bit set (allows 15-bit results, instead of just 8) */
238 if (!grf_version8 && (this->result >> 8) == 0xFF) {
239 this->result &= ~0xFF00;
240 } else {
241 this->result &= ~0x8000;
245 uint16 result;
246 uint16 GetCallbackResult() const { return this->result; }
250 /* A result sprite group returns the first SpriteID and the number of
251 * sprites in the set */
252 struct ResultSpriteGroup : SpriteGroup {
254 * Creates a spritegroup representing a sprite number result.
255 * @param sprite The sprite number.
256 * @param num_sprites The number of sprites per set.
257 * @return A spritegroup representing the sprite number result.
259 ResultSpriteGroup(SpriteID sprite, byte num_sprites) :
260 SpriteGroup(SGT_RESULT),
261 sprite(sprite),
262 num_sprites(num_sprites)
266 SpriteID sprite;
267 byte num_sprites;
268 SpriteID GetResult() const { return this->sprite; }
269 byte GetNumResults() const { return this->num_sprites; }
273 * Action 2 sprite layout for houses, industry tiles, objects and airport tiles.
275 struct TileLayoutSpriteGroup : SpriteGroup {
276 TileLayoutSpriteGroup() : SpriteGroup(SGT_TILELAYOUT) {}
277 ~TileLayoutSpriteGroup() {}
279 NewGRFSpriteLayout dts;
281 const DrawTileSprites *ProcessRegisters(uint8 *stage) const;
284 struct IndustryProductionSpriteGroup : SpriteGroup {
285 IndustryProductionSpriteGroup() : SpriteGroup(SGT_INDUSTRY_PRODUCTION) {}
287 uint8 version;
288 int16 subtract_input[3]; // signed
289 uint16 add_output[2]; // unsigned
290 uint8 again;
294 * Interface to query and set values specific to a single #VarSpriteGroupScope (action 2 scope).
296 * Multiple of these interfaces are combined into a #ResolverObject to allow access
297 * to different game entities from a #SpriteGroup-chain (action 1-2-3 chain).
299 struct ScopeResolver {
300 ResolverObject &ro; ///< Surrounding resolver object.
302 ScopeResolver(ResolverObject &ro);
303 virtual ~ScopeResolver();
305 virtual uint32 GetRandomBits() const;
306 virtual uint32 GetTriggers() const;
307 virtual void SetTriggers(int triggers) const;
309 virtual uint32 GetVariable(byte variable, uint32 parameter, bool *available) const;
310 virtual void StorePSA(uint reg, int32 value);
314 * Interface for #SpriteGroup-s to access the gamestate.
316 * Using this interface #SpriteGroup-chains (action 1-2-3 chains) can be resolved,
317 * to get the results of callbacks, rerandomisations or normal sprite lookups.
319 struct ResolverObject {
320 ResolverObject(const GRFFile *grffile, CallbackID callback = CBID_NO_CALLBACK, uint32 callback_param1 = 0, uint32 callback_param2 = 0);
321 virtual ~ResolverObject();
323 ScopeResolver default_scope; ///< Default implementation of the grf scope.
325 CallbackID callback; ///< Callback being resolved.
326 uint32 callback_param1; ///< First parameter (var 10) of the callback.
327 uint32 callback_param2; ///< Second parameter (var 18) of the callback.
329 byte trigger;
331 uint32 last_value; ///< Result of most recent DeterministicSpriteGroup (including procedure calls)
332 uint32 reseed[VSG_END]; ///< Collects bits to rerandomise while triggering triggers.
334 const GRFFile *grffile; ///< GRFFile the resolved SpriteGroup belongs to
336 virtual const SpriteGroup *ResolveReal(const RealSpriteGroup *group) const;
338 virtual ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, byte relative = 0);
341 * Returns the OR-sum of all bits that need reseeding
342 * independent of the scope they were accessed with.
343 * @return OR-sum of the bits.
345 uint32 GetReseedSum() const
347 uint32 sum = 0;
348 for (VarSpriteGroupScope vsg = VSG_BEGIN; vsg < VSG_END; vsg++) {
349 sum |= this->reseed[vsg];
351 return sum;
355 * Resets the dynamic state of the resolver object.
356 * To be called before resolving an Action-1-2-3 chain.
358 void ResetState()
360 this->last_value = 0;
361 this->trigger = 0;
362 memset(this->reseed, 0, sizeof(this->reseed));
366 #endif /* NEWGRF_SPRITEGROUP_H */