v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
image.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * This file is part of the public interface to the Sifteo SDK.
4  * Copyright <c> 2012 Sifteo, Inc. All rights reserved.
5  */
6 
7 #pragma once
8 
9 /*
10  * This header needs to work in both userspace and non-userspace
11  * builds, though the latter have a greatly reduced feature set.
12  */
13 
14 #ifndef NOT_USERSPACE
15 # include <sifteo/math.h>
16 # include <sifteo/macros.h>
17 # include <sifteo/asset/group.h>
18 #endif
19 
20 #include <sifteo/abi.h>
21 
22 namespace Sifteo {
23 
48 struct AssetImage {
49  _SYSAssetImage sys;
50 
51 #ifndef NOT_USERSPACE // Begin userspace-only members
52 
54  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
55 
57  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
58 
60  Int2 tileExtent() const { return tileSize() / 2; }
61 
63  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
64 
66  Int2 pixelExtent() const { return pixelSize() / 2; }
67 
68 #endif // End userspace-only members
69 
71  int tileWidth() const { return sys.width; }
72 
74  int tileHeight() const { return sys.height; }
75 
77  int pixelWidth() const { return sys.width << 3; }
78 
80  int pixelHeight() const { return sys.height << 3; }
81 
83  int numFrames() const { return sys.frames; }
84 
86  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
87 
89  int numTiles() const { return numFrames() * numTilesPerFrame(); }
90 
92  operator const _SYSAssetImage& () const { return sys; }
93  operator _SYSAssetImage& () { return sys; }
94  operator const _SYSAssetImage* () const { return &sys; }
95  operator _SYSAssetImage* () { return &sys; }
96 };
97 
98 
116  _SYSAssetImage sys;
117 
118 #ifndef NOT_USERSPACE // Begin userspace-only members
119 
121  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
122 
124  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
125 
127  Int2 tileExtent() const { return tileSize() / 2; }
128 
130  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
131 
133  Int2 pixelExtent() const { return pixelSize() / 2; }
134 
141  uint16_t tile(unsigned i) const {
142  ASSERT(i < numTiles());
143  return sys.pData + i;
144  }
145 
152  uint16_t tile(Int2 pos, unsigned frame = 0) const {
153  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
154  return sys.pData + pos.x + pos.y * tileHeight() + frame * numTilesPerFrame();
155  }
156 
163  uint16_t tile(_SYSCubeID cube, unsigned i) const {
164  ASSERT(i < numTiles());
165  return assetGroup().baseAddress(cube) + sys.pData + i;
166  }
167 
174  uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame = 0) const {
175  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
176  return assetGroup().baseAddress(cube) + sys.pData
177  + pos.x + pos.y * tileHeight() + frame * numTilesPerFrame();
178  }
179 
180 #endif // End userspace-only members
181 
183  int tileWidth() const { return sys.width; }
184 
186  int tileHeight() const { return sys.height; }
187 
189  int pixelWidth() const { return sys.width << 3; }
190 
192  int pixelHeight() const { return sys.height << 3; }
193 
195  int numFrames() const { return sys.frames; }
196 
198  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
199 
201  int numTiles() const { return numFrames() * numTilesPerFrame(); }
202 
204  operator const AssetImage& () const { return *reinterpret_cast<const AssetImage*>(this); }
205  operator AssetImage& () { return *reinterpret_cast<AssetImage*>(this); }
206  operator const AssetImage* () const { return reinterpret_cast<const AssetImage*>(this); }
207  operator AssetImage* () { return reinterpret_cast<AssetImage*>(this); }
208 
210  operator const _SYSAssetImage& () const { return sys; }
211  operator _SYSAssetImage& () { return sys; }
212  operator const _SYSAssetImage* () const { return &sys; }
213  operator _SYSAssetImage* () { return &sys; }
214 };
215 
216 
229  _SYSAssetImage sys;
230 
231 #ifndef NOT_USERSPACE // Begin userspace-only members
232 
234  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
235 
237  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
238 
240  Int2 tileExtent() const { return tileSize() / 2; }
241 
243  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
244 
246  Int2 pixelExtent() const { return pixelSize() / 2; }
247 
254  const uint16_t *tileArray() const {
255  return reinterpret_cast<const uint16_t *>(sys.pData);
256  }
257 
264  uint16_t tile(unsigned i) const {
265  ASSERT(i < numTiles());
266  return tileArray()[i];
267  }
268 
275  uint16_t tile(Int2 pos, unsigned frame = 0) const {
276  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
277  return tileArray()[pos.x + pos.y * tileHeight() + frame * numTilesPerFrame()];
278  }
279 
286  uint16_t tile(_SYSCubeID cube, unsigned i) const {
287  ASSERT(i < numTiles());
288  return assetGroup().baseAddress(cube) + tileArray()[i];
289  }
290 
297  uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame = 0) const {
298  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
299  return assetGroup().baseAddress(cube) + tileArray()[
300  pos.x + pos.y * tileHeight() + frame * numTilesPerFrame()];
301  }
302 
303 #endif // End userspace-only members
304 
306  int tileWidth() const { return sys.width; }
307 
309  int tileHeight() const { return sys.height; }
310 
312  int pixelWidth() const { return sys.width << 3; }
313 
315  int pixelHeight() const { return sys.height << 3; }
316 
318  int numFrames() const { return sys.frames; }
319 
321  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
322 
324  int numTiles() const { return numFrames() * numTilesPerFrame(); }
325 
327  operator const AssetImage& () const { return *reinterpret_cast<const AssetImage*>(this); }
328  operator AssetImage& () { return *reinterpret_cast<AssetImage*>(this); }
329  operator const AssetImage* () const { return reinterpret_cast<const AssetImage*>(this); }
330  operator AssetImage* () { return reinterpret_cast<AssetImage*>(this); }
331 
333  operator const _SYSAssetImage& () const { return sys; }
334  operator _SYSAssetImage& () { return sys; }
335  operator const _SYSAssetImage* () const { return &sys; }
336  operator _SYSAssetImage* () { return &sys; }
337 };
338 
343 }; // namespace Sifteo