v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
sprite.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 #ifdef NOT_USERSPACE
9 # error This is a userspace-only header, not allowed by the current build.
10 #endif
11 
12 #include <sifteo/abi.h>
13 #include <sifteo/macros.h>
14 #include <sifteo/math.h>
15 
16 namespace Sifteo {
17 
39 struct SpriteRef {
40  _SYSAttachedVideoBuffer *sys;
41  unsigned id;
42 
50  void setImage(uint16_t tile) const {
51  uint16_t word = _SYS_TILE77(tile);
52  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].tile)/2 +
53  sizeof(_SYSSpriteInfo)/2 * id );
54  _SYS_vbuf_poke(&sys->vbuf, addr, word);
55  }
56 
61  void setImage(const PinnedAssetImage &asset, int frame = 0) const {
62  setImage(asset.tile(sys->cube, vec(0,0), frame));
63  resize(asset.pixelSize());
64  }
65 
69  unsigned width() const {
70  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_x) +
71  sizeof(_SYSSpriteInfo) * id );
72  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
73  }
74 
78  unsigned height() const {
79  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y) +
80  sizeof(_SYSSpriteInfo) * id );
81  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
82  }
83 
87  UByte2 size() const {
88  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y)/2 +
89  sizeof(_SYSSpriteInfo)/2 * id );
90  uint16_t word = _SYS_vbuf_peek(&sys->vbuf, addr);
91  return vec<uint8_t>(-(int8_t)(word >> 8), -(int8_t)word);
92  }
93 
97  void setWidth(unsigned pixels) const {
98  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_x) +
99  sizeof(_SYSSpriteInfo) * id );
100  _SYS_vbuf_pokeb(&sys->vbuf, addr, -(int8_t)pixels);
101  }
102 
106  void setHeight(unsigned pixels) const {
107  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].mask_y) +
108  sizeof(_SYSSpriteInfo) * id );
109  _SYS_vbuf_pokeb(&sys->vbuf, addr, -(int8_t)pixels);
110  }
111 
115  void resize(unsigned x, unsigned y) const {
116  // Size must be a power of two in current firmwares.
117  ASSERT((x & (x - 1)) == 0 && (y & (y - 1)) == 0);
118 
119  _SYS_vbuf_spr_resize(&sys->vbuf, id, x, y);
120  }
121 
125  void resize(UInt2 size) const {
126  resize(size.x, size.y);
127  }
128 
137  bool isHidden() const {
138  return height() == 0;
139  }
140 
147  void hide() const {
148  setHeight(0);
149  }
150 
158  void move(int x, int y) const {
159  _SYS_vbuf_spr_move(&sys->vbuf, id, x, y);
160  }
161 
169  void move(Int2 pos) const {
170  move(pos.x, pos.y);
171  }
172 
181  void move(Float2 pos) const {
182  Int2 i = pos.round();
183  move(i.x, i.y);
184  }
185 
189  unsigned x() const {
190  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_x) +
191  sizeof(_SYSSpriteInfo) * id );
192  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
193  }
194 
198  unsigned y() const {
199  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_y) +
200  sizeof(_SYSSpriteInfo) * id );
201  return -(int8_t)_SYS_vbuf_peekb(&sys->vbuf, addr);
202  }
203 
207  Byte2 position() const {
208  uint16_t addr = ( offsetof(_SYSVideoRAM, spr[0].pos_y)/2 +
209  sizeof(_SYSSpriteInfo)/2 * id );
210  uint16_t word = _SYS_vbuf_peek(&sys->vbuf, addr);
211  return vec<int8_t>(-(int8_t)(word >> 8), -(int8_t)word);
212  }
213 
214  // Next sprite ID
215  SpriteRef operator++ () {
216  ++id;
217  return *this;
218  }
219 
220  // Next sprite ID
221  SpriteRef operator++ (int) {
222  SpriteRef result = *this;
223  ++id;
224  return result;
225  }
226 
227  // Previous sprite ID
228  SpriteRef operator-- () {
229  --id;
230  return *this;
231  }
232 
233  // Previous sprite ID
234  SpriteRef operator-- (int) {
235  SpriteRef result = *this;
236  --id;
237  return result;
238  }
239 
240  // Calculate a new SpriteRef with an ID relative to this one
241  SpriteRef operator[] (int index) {
242  SpriteRef result = *this;
243  result.id += index;
244  return result;
245  }
246 };
247 
248 
259 struct SpriteLayer {
260  _SYSAttachedVideoBuffer sys;
261 
262  static const unsigned NUM_SPRITES = _SYS_VRAM_SPRITES;
263 
273  SpriteRef operator[](unsigned id) {
274  ASSERT(id < NUM_SPRITES);
275  SpriteRef result = { &sys, id };
276  return result;
277  }
278 
282  void erase() {
283  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_SPR / 2, 0,
284  sizeof(_SYSSpriteInfo) / 2 * NUM_SPRITES);
285  }
286 
290  _SYSVideoBuffer &videoBuffer() {
291  return sys.vbuf;
292  }
293 
297  CubeID cube() const {
298  return sys.cube;
299  }
300 };
301 
306 }; // namespace Sifteo