v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
sprite.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 #ifdef NOT_USERSPACE
28 # error This is a userspace-only header, not allowed by the current build.
29 #endif
30 
31 #include <sifteo/abi.h>
32 #include <sifteo/macros.h>
33 #include <sifteo/math.h>
34 
35 namespace Sifteo {
36 
58 struct SpriteRef {
59  _SYSAttachedVideoBuffer *sys;
60  unsigned id;
61 
69  void setImage(uint16_t tile) const {
70  uint16_t word = _SYS_TILE77(tile);
71  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].tile)/2 +
72  sizeof(_SYSSpriteInfo)/2 * id );
73  _SYS_vbuf_poke(&sys->vbuf, addr, word);
74  }
75 
80  void setImage(const PinnedAssetImage &asset, int frame = 0) const {
81  setImage(asset.tile(sys->cube, vec(0,0), frame));
82  resize(asset.pixelSize());
83  }
84 
88  unsigned width() const {
89  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_x) +
90  sizeof(_SYSSpriteInfo) * id );
91  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
92  }
93 
97  unsigned height() const {
98  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y) +
99  sizeof(_SYSSpriteInfo) * id );
100  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
101  }
102 
106  UByte2 size() const {
107  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y)/2 +
108  sizeof(_SYSSpriteInfo)/2 * id );
109  uint16_t word = _SYS_vbuf_peek(&sys->vbuf, addr);
110  return vec<uint8_t>(-(int8_t)(word >> 8), -(int8_t)word);
111  }
112 
116  void setWidth(unsigned pixels) const {
117  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_x) +
118  sizeof(_SYSSpriteInfo) * id );
119  _SYS_vbuf_pokeb(&sys->vbuf, addr, -(int8_t)pixels);
120  }
121 
125  void setHeight(unsigned pixels) const {
126  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y) +
127  sizeof(_SYSSpriteInfo) * id );
128  _SYS_vbuf_pokeb(&sys->vbuf, addr, -(int8_t)pixels);
129  }
130 
134  void resize(unsigned x, unsigned y) const {
135  // Size must be a power of two in current firmwares.
136  ASSERT((x & (x - 1)) == 0 && (y & (y - 1)) == 0);
137 
138  _SYS_vbuf_spr_resize(&sys->vbuf, id, x, y);
139  }
140 
144  void resize(UInt2 size) const {
145  resize(size.x, size.y);
146  }
147 
156  bool isHidden() const {
157  return height() == 0;
158  }
159 
166  void hide() const {
167  setHeight(0);
168  }
169 
177  void move(int x, int y) const {
178  _SYS_vbuf_spr_move(&sys->vbuf, id, x, y);
179  }
180 
188  void move(Int2 pos) const {
189  move(pos.x, pos.y);
190  }
191 
200  void move(Float2 pos) const {
201  Int2 i = pos.round();
202  move(i.x, i.y);
203  }
204 
208  unsigned x() const {
209  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_x) +
210  sizeof(_SYSSpriteInfo) * id );
211  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
212  }
213 
217  unsigned y() const {
218  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_y) +
219  sizeof(_SYSSpriteInfo) * id );
220  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
221  }
222 
226  Byte2 position() const {
227  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_y)/2 +
228  sizeof(_SYSSpriteInfo)/2 * id );
229  uint16_t word = _SYS_vbuf_peek(&sys->vbuf, addr);
230  return vec<int8_t>(-(int8_t)(word >> 8), -(int8_t)word);
231  }
232 
233  // Next sprite ID
234  SpriteRef operator++ () {
235  ++id;
236  return *this;
237  }
238 
239  // Next sprite ID
240  SpriteRef operator++ (int) {
241  SpriteRef result = *this;
242  ++id;
243  return result;
244  }
245 
246  // Previous sprite ID
247  SpriteRef operator-- () {
248  --id;
249  return *this;
250  }
251 
252  // Previous sprite ID
253  SpriteRef operator-- (int) {
254  SpriteRef result = *this;
255  --id;
256  return result;
257  }
258 
259  // Calculate a new SpriteRef with an ID relative to this one
260  SpriteRef operator[] (int index) {
261  SpriteRef result = *this;
262  result.id += index;
263  return result;
264  }
265 };
266 
267 
278 struct SpriteLayer {
279  _SYSAttachedVideoBuffer sys;
280 
281  static const unsigned NUM_SPRITES = _SYS_VRAM_SPRITES;
282 
292  SpriteRef operator[](unsigned id) {
293  ASSERT(id < NUM_SPRITES);
294  SpriteRef result = { &sys, id };
295  return result;
296  }
297 
301  void erase() {
302  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_SPR / 2, 0,
303  sizeof(_SYSSpriteInfo) / 2 * NUM_SPRITES);
304  }
305 
309  _SYSVideoBuffer &videoBuffer() {
310  return sys.vbuf;
311  }
312 
316  CubeID cube() const {
317  return sys.cube;
318  }
319 };
320 
325 }; // namespace Sifteo
#define offsetof(t, m)
Definition: macros.h:368
UByte2 size() const
Get this sprite's current image size as a vector, in pixels.
Definition: sprite.h:106
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
void resize(unsigned x, unsigned y) const
Set this sprite's size, in pixels.
Definition: sprite.h:134
void move(Float2 pos) const
Move this sprite to a new location, in pixels, passed as a Float2.
Definition: sprite.h:200
SpriteRef operator[](unsigned id)
Return a SpriteRef which references a single sprite on a single VideoBuffer.
Definition: sprite.h:292
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
Int2 pixelSize() const
The (width, height) vector of this image, in pixels.
Definition: image.h:149
Byte2 position() const
Get this sprite's current position as a vector, in pixels.
Definition: sprite.h:226
An AssetImage in which all tiles are stored sequentially in memory.
Definition: image.h:134
T x
Vector component X.
Definition: math.h:489
unsigned y() const
Get this sprite's current Y position, in pixels.
Definition: sprite.h:217
unsigned width() const
Get this sprite's current image width, in pixels.
Definition: sprite.h:88
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
void move(Int2 pos) const
Move this sprite to a new location, in pixels, passed as an Int2.
Definition: sprite.h:188
unsigned height() const
Get this sprite's current image height, in pixels.
Definition: sprite.h:97
void setImage(const PinnedAssetImage &asset, int frame=0) const
Set a sprite's image and size, given a PinnedAssetImage and optionally a frame number.
Definition: sprite.h:80
unsigned x() const
Get this sprite's current X position, in pixels.
Definition: sprite.h:208
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:160
bool isHidden() const
Is this sprite hidden?
Definition: sprite.h:156
void hide() const
Hide a sprite.
Definition: sprite.h:166
void move(int x, int y) const
Move this sprite to a new location, in pixels.
Definition: sprite.h:177
void resize(UInt2 size) const
Set this sprite's size, in pixels, from a UInt2.
Definition: sprite.h:144
Definition: array.h:34
CubeID cube() const
Return the CubeID associated with this drawable.
Definition: sprite.h:316
T y
Vector component Y.
Definition: math.h:490
void setWidth(unsigned pixels) const
Set this sprite's width, in pixels.
Definition: sprite.h:116
void setImage(uint16_t tile) const
Set the sprite's image, given the physical address of the first tile in a pinned asset.
Definition: sprite.h:69
void setHeight(unsigned pixels) const
Set this sprite's height, in pixels.
Definition: sprite.h:125
_SYSVideoBuffer & videoBuffer()
Return the VideoBuffer associated with this drawable.
Definition: sprite.h:309
void erase()
Reset all sprites to their default hidden state.
Definition: sprite.h:301
Vector2< int > round() const
Round a floating point vector to the nearest integer.
Definition: math.h:598
A SpriteLayer represents the VRAM attributes for the sprite rendering layer in BG0_SPR_BG1 mode...
Definition: sprite.h:278
SpriteRefs refer to a single sprite on a single cube.
Definition: sprite.h:58
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658