v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
image.h
1 /* -*- mode: C; c-basic-offset: 4; intent-tabs-mode: nil -*-
2  *
3  * Sifteo SDK
4  *
5  * Copyright <c> 2012 Sifteo, Inc.
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a copy
8  * of this software and associated documentation files (the "Software"), to deal
9  * in the Software without restriction, including without limitation the rights
10  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11  * copies of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23  * THE SOFTWARE.
24  */
25 
26 #pragma once
27 
28 /*
29  * This header needs to work in both userspace and non-userspace
30  * builds, though the latter have a greatly reduced feature set.
31  */
32 
33 #ifndef NOT_USERSPACE
34 # include <sifteo/math.h>
35 # include <sifteo/macros.h>
36 # include <sifteo/asset/group.h>
37 #endif
38 
39 #include <sifteo/abi.h>
40 
41 namespace Sifteo {
42 
67 struct AssetImage {
68  _SYSAssetImage sys;
69 
70 #ifndef NOT_USERSPACE // Begin userspace-only members
71 
73  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
74 
76  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
77 
79  Int2 tileExtent() const { return tileSize() / 2; }
80 
82  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
83 
85  Int2 pixelExtent() const { return pixelSize() / 2; }
86 
87 #endif // End userspace-only members
88 
90  int tileWidth() const { return sys.width; }
91 
93  int tileHeight() const { return sys.height; }
94 
96  int pixelWidth() const { return sys.width << 3; }
97 
99  int pixelHeight() const { return sys.height << 3; }
100 
102  int numFrames() const { return sys.frames; }
103 
105  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
106 
108  int numTiles() const { return numFrames() * numTilesPerFrame(); }
109 
111  operator const _SYSAssetImage& () const { return sys; }
112  operator _SYSAssetImage& () { return sys; }
113  operator const _SYSAssetImage* () const { return &sys; }
114  operator _SYSAssetImage* () { return &sys; }
115 };
116 
117 
135  _SYSAssetImage sys;
136 
137 #ifndef NOT_USERSPACE // Begin userspace-only members
138 
140  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
141 
143  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
144 
146  Int2 tileExtent() const { return tileSize() / 2; }
147 
149  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
150 
152  Int2 pixelExtent() const { return pixelSize() / 2; }
153 
160  uint16_t tile(unsigned i) const {
161  ASSERT(i < numTiles());
162  return sys.pData + i;
163  }
164 
171  uint16_t tile(Int2 pos, unsigned frame = 0) const {
172  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
173  return sys.pData + pos.x + pos.y * tileHeight() + frame * numTilesPerFrame();
174  }
175 
182  uint16_t tile(_SYSCubeID cube, unsigned i) const {
183  ASSERT(i < numTiles());
184  return assetGroup().baseAddress(cube) + sys.pData + i;
185  }
186 
193  uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame = 0) const {
194  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
195  return assetGroup().baseAddress(cube) + sys.pData
196  + pos.x + pos.y * tileHeight() + frame * numTilesPerFrame();
197  }
198 
199 #endif // End userspace-only members
200 
202  int tileWidth() const { return sys.width; }
203 
205  int tileHeight() const { return sys.height; }
206 
208  int pixelWidth() const { return sys.width << 3; }
209 
211  int pixelHeight() const { return sys.height << 3; }
212 
214  int numFrames() const { return sys.frames; }
215 
217  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
218 
220  int numTiles() const { return numFrames() * numTilesPerFrame(); }
221 
223  operator const AssetImage& () const { return *reinterpret_cast<const AssetImage*>(this); }
224  operator AssetImage& () { return *reinterpret_cast<AssetImage*>(this); }
225  operator const AssetImage* () const { return reinterpret_cast<const AssetImage*>(this); }
226  operator AssetImage* () { return reinterpret_cast<AssetImage*>(this); }
227 
229  operator const _SYSAssetImage& () const { return sys; }
230  operator _SYSAssetImage& () { return sys; }
231  operator const _SYSAssetImage* () const { return &sys; }
232  operator _SYSAssetImage* () { return &sys; }
233 };
234 
235 
248  _SYSAssetImage sys;
249 
250 #ifndef NOT_USERSPACE // Begin userspace-only members
251 
253  AssetGroup &assetGroup() const { return *reinterpret_cast<AssetGroup*>(sys.pAssetGroup); }
254 
256  Int2 tileSize() const { return vec<int>(sys.width, sys.height); }
257 
259  Int2 tileExtent() const { return tileSize() / 2; }
260 
262  Int2 pixelSize() const { return vec<int>(sys.width << 3, sys.height << 3); }
263 
265  Int2 pixelExtent() const { return pixelSize() / 2; }
266 
273  const uint16_t *tileArray() const {
274  return reinterpret_cast<const uint16_t *>(sys.pData);
275  }
276 
283  uint16_t tile(unsigned i) const {
284  ASSERT(i < numTiles());
285  return tileArray()[i];
286  }
287 
294  uint16_t tile(Int2 pos, unsigned frame = 0) const {
295  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
296  return tileArray()[pos.x + pos.y * tileHeight() + frame * numTilesPerFrame()];
297  }
298 
305  uint16_t tile(_SYSCubeID cube, unsigned i) const {
306  ASSERT(i < numTiles());
307  return assetGroup().baseAddress(cube) + tileArray()[i];
308  }
309 
316  uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame = 0) const {
317  ASSERT(pos.x < tileWidth() && pos.y < tileHeight() && frame < numFrames());
318  return assetGroup().baseAddress(cube) + tileArray()[
319  pos.x + pos.y * tileHeight() + frame * numTilesPerFrame()];
320  }
321 
322 #endif // End userspace-only members
323 
325  int tileWidth() const { return sys.width; }
326 
328  int tileHeight() const { return sys.height; }
329 
331  int pixelWidth() const { return sys.width << 3; }
332 
334  int pixelHeight() const { return sys.height << 3; }
335 
337  int numFrames() const { return sys.frames; }
338 
340  int numTilesPerFrame() const { return tileWidth() * tileHeight(); }
341 
343  int numTiles() const { return numFrames() * numTilesPerFrame(); }
344 
346  operator const AssetImage& () const { return *reinterpret_cast<const AssetImage*>(this); }
347  operator AssetImage& () { return *reinterpret_cast<AssetImage*>(this); }
348  operator const AssetImage* () const { return reinterpret_cast<const AssetImage*>(this); }
349  operator AssetImage* () { return reinterpret_cast<AssetImage*>(this); }
350 
352  operator const _SYSAssetImage& () const { return sys; }
353  operator _SYSAssetImage& () { return sys; }
354  operator const _SYSAssetImage* () const { return &sys; }
355  operator _SYSAssetImage* () { return &sys; }
356 };
357 
362 }; // namespace Sifteo
uint16_t tile(Int2 pos, unsigned frame=0) const
Return the index of the tile at the specified (x, y) tile coordinates.
Definition: image.h:171
uint16_t tile(_SYSCubeID cube, unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:182
A bundle of compressed tile data, for use by AssetImages.
Definition: group.h:54
Int2 pixelExtent() const
Half the size of this image, in pixels.
Definition: image.h:265
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
AssetGroup & assetGroup() const
Access the AssetGroup instance associated with this AssetImage.
Definition: image.h:73
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
int pixelHeight() const
The height of this image, in pixels.
Definition: image.h:99
Int2 pixelSize() const
The (width, height) vector of this image, in pixels.
Definition: image.h:149
int numFrames() const
Access the number of 'frames' in this image.
Definition: image.h:214
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:90
int pixelHeight() const
The height of this image, in pixels.
Definition: image.h:211
int numTiles() const
Compute the total number of tiles in the image.
Definition: image.h:343
An AssetImage in which all tiles are stored sequentially in memory.
Definition: image.h:134
AssetGroup & assetGroup() const
Access the AssetGroup instance associated with this AssetImage.
Definition: image.h:140
uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame=0) const
Return the index of the tile at the specified (x, y) tile coordinates.
Definition: image.h:316
int pixelWidth() const
The width of this image, in pixels.
Definition: image.h:208
int numTiles() const
Compute the total number of tiles in the image.
Definition: image.h:108
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:202
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:205
const uint16_t * tileArray() const
Get a pointer to the raw tile data.
Definition: image.h:273
T x
Vector component X.
Definition: math.h:489
int pixelWidth() const
The width of this image, in pixels.
Definition: image.h:96
uint16_t baseAddress(_SYSCubeID cube) const
Return the base address of this asset group, as loaded onto the specified cube.
Definition: group.h:114
int numFrames() const
Access the number of 'frames' in this image.
Definition: image.h:102
uint16_t tile(_SYSCubeID cube, unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:305
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:93
Int2 tileExtent() const
Half the size of this image, in tiles.
Definition: image.h:146
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:325
Int2 pixelSize() const
The (width, height) vector of this image, in pixels.
Definition: image.h:262
int numTilesPerFrame() const
Compute the total number of tiles per frame (tileWidth * tileHeight)
Definition: image.h:105
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:160
Any kind of asset image, as defined in your stir script.
Definition: image.h:67
int pixelWidth() const
The width of this image, in pixels.
Definition: image.h:331
Int2 tileSize() const
The (width, height) vector of this image, in tiles.
Definition: image.h:143
int numTilesPerFrame() const
Compute the total number of tiles per frame (tileWidth * tileHeight)
Definition: image.h:217
Int2 tileSize() const
The (width, height) vector of this image, in tiles.
Definition: image.h:256
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:283
Definition: array.h:34
int numTilesPerFrame() const
Compute the total number of tiles per frame (tileWidth * tileHeight)
Definition: image.h:340
T y
Vector component Y.
Definition: math.h:490
uint16_t tile(_SYSCubeID cube, Int2 pos, unsigned frame=0) const
Return the index of the tile at the specified (x, y) tile coordinates.
Definition: image.h:193
Int2 tileExtent() const
Half the size of this image, in tiles.
Definition: image.h:259
int numFrames() const
Access the number of 'frames' in this image.
Definition: image.h:337
Int2 pixelExtent() const
Half the size of this image, in pixels.
Definition: image.h:85
Int2 pixelExtent() const
Half the size of this image, in pixels.
Definition: image.h:152
An AssetImage in which all tile indices are stored in a flat array, without any additional compressio...
Definition: image.h:247
Int2 tileExtent() const
Half the size of this image, in tiles.
Definition: image.h:79
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:328
Int2 tileSize() const
The (width, height) vector of this image, in tiles.
Definition: image.h:76
Int2 pixelSize() const
The (width, height) vector of this image, in pixels.
Definition: image.h:82
int numTiles() const
Compute the total number of tiles in the image.
Definition: image.h:220
int pixelHeight() const
The height of this image, in pixels.
Definition: image.h:334
uint16_t tile(Int2 pos, unsigned frame=0) const
Return the index of the tile at the specified (x, y) tile coordinates.
Definition: image.h:294
AssetGroup & assetGroup() const
Access the AssetGroup instance associated with this AssetImage.
Definition: image.h:253