v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
bg0.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 #include <sifteo/asset.h>
35 
36 namespace Sifteo {
37 
48 struct BG0Drawable {
49  _SYSAttachedVideoBuffer sys;
50 
54  static unsigned tileWidth() {
55  return _SYS_VRAM_BG0_WIDTH;
56  }
57 
61  static unsigned tileHeight() {
62  return _SYS_VRAM_BG0_WIDTH;
63  }
64 
68  static UInt2 tileSize() {
69  return vec(tileWidth(), tileHeight());
70  }
71 
75  static unsigned pixelWidth() {
76  return tileWidth() * 8;
77  }
78 
82  static unsigned pixelHeight() {
83  return tileHeight() * 8;
84  }
85 
89  static UInt2 pixelSize() {
90  return vec(pixelWidth(), pixelHeight());
91  }
92 
96  static unsigned sizeInBytes() {
97  return tileWidth() * tileHeight() * 2;
98  }
99 
103  static unsigned sizeInWords() {
104  return tileWidth() * tileHeight();
105  }
106 
111  void erase(uint16_t index = 0) {
112  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
113  setPanning(vec(0,0));
114  }
115 
120  void erase(const PinnedAssetImage &image) {
121  erase(image.tile(sys.cube, 0));
122  }
123 
132  void setPanning(Int2 pixels) {
133  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2,
134  umod(pixels.x, pixelWidth()) |
135  (umod(pixels.y, pixelHeight()) << 8));
136  }
137 
142  Int2 getPanning() const {
143  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
144  return vec<int>(word & 0xFF, word >> 8);
145  }
146 
152  uint16_t tileAddr(UInt2 pos) {
153  return pos.x + pos.y * tileWidth();
154  }
155 
160  uint16_t tile(UInt2 pos) {
161  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
162  return _SYS_INVERSE_TILE77(_SYS_vbuf_peek(&sys.vbuf, tileAddr(pos)));
163  }
164 
171  void plot(UInt2 pos, uint16_t tileIndex) {
172  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
173  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
174  }
175 
182  void span(UInt2 pos, unsigned width, unsigned tileIndex)
183  {
184  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
185  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
186  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
187  }
188 
194  void span(UInt2 pos, unsigned width, const PinnedAssetImage &image)
195  {
196  span(pos, width, image.tile(sys.cube,0));
197  }
198 
205  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
206  {
207  while (size.y) {
208  span(topLeft, size.x, tileIndex);
209  size.y--;
210  topLeft.y++;
211  }
212  }
213 
220  void fill(UInt2 topLeft, UInt2 size, const PinnedAssetImage &image)
221  {
222  fill(topLeft, size, image.tile(sys.cube, 0));
223  }
224 
234  void image(UInt2 pos, const AssetImage &image, unsigned frame = 0)
235  {
236  _SYS_image_BG0Draw(&sys, image, tileAddr(pos), frame);
237  }
238 
248  void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame = 0)
249  {
250  _SYS_image_BG0DrawRect(&sys, image, tileAddr(destXY),
251  frame, (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
252  }
253 
260  void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar = ' ')
261  {
262  unsigned addr = tileAddr(topLeft);
263  unsigned lineAddr = addr;
264  char c;
265 
266  while ((c = *str)) {
267  if (c == '\n') {
268  addr = (lineAddr += font.tileHeight() * tileWidth());
269  } else {
270  _SYS_image_BG0Draw(&sys, font, addr, c - firstChar);
271  addr += font.tileWidth();
272  }
273  str++;
274  }
275  }
276 
280  _SYSVideoBuffer &videoBuffer() {
281  return sys.vbuf;
282  }
283 
287  CubeID cube() const {
288  return sys.cube;
289  }
290 };
291 
296 }; // namespace Sifteo
static unsigned sizeInBytes()
Returns the size of this drawable's tile data, in bytes.
Definition: bg0.h:96
#define offsetof(t, m)
Definition: macros.h:368
void fill(UInt2 topLeft, UInt2 size, const PinnedAssetImage &image)
Fill a rectangle of identical tiles, using the first tile of a pinned asset.
Definition: bg0.h:220
void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
Fill a rectangle of identical tiles, specified as a top-left corner location and a size...
Definition: bg0.h:205
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
void setPanning(Int2 pixels)
Change the hardware pixel-panning origin for this mode.
Definition: bg0.h:132
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:90
static unsigned tileWidth()
Return the width, in tiles, of this mode.
Definition: bg0.h:54
An AssetImage in which all tiles are stored sequentially in memory.
Definition: image.h:134
uint16_t tileAddr(UInt2 pos)
Calculate the video buffer address of a particular tile.
Definition: bg0.h:152
void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame=0)
Draw part of an AssetImage frame, with its top-left corner at the specified location.
Definition: bg0.h:248
void erase(uint16_t index=0)
Erase mode-specific VRAM, filling the BG0 buffer with the specified absolute tile index value and res...
Definition: bg0.h:111
T x
Vector component X.
Definition: math.h:489
void plot(UInt2 pos, uint16_t tileIndex)
Plot a single tile, by absolute tile index, at location 'pos' in tile units.
Definition: bg0.h:171
A VRAM accessor for drawing graphics in the BG0 mode.
Definition: bg0.h:48
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:93
static unsigned sizeInWords()
Returns the size of this drawable's tile data, in 16-bit words.
Definition: bg0.h:103
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
static UInt2 tileSize()
Return the size of this mode as a vector, in tiles.
Definition: bg0.h:68
CubeID cube() const
Return the CubeID associated with this drawable.
Definition: bg0.h:287
void span(UInt2 pos, unsigned width, unsigned tileIndex)
Plot a horizontal span of tiles, by absolute tile index, given the position of the leftmost tile and ...
Definition: bg0.h:182
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:160
Int2 getPanning() const
Retrieve the last value set by setPanning(), modulo the layer size in pixels.
Definition: bg0.h:142
Any kind of asset image, as defined in your stir script.
Definition: image.h:67
uint16_t tile(UInt2 pos)
Retrieve the absolute tile index currently set at the given address. The inverse of plot()...
Definition: bg0.h:160
_SYSVideoBuffer & videoBuffer()
Return the VideoBuffer associated with this drawable.
Definition: bg0.h:280
void erase(const PinnedAssetImage &image)
Erase mode-specific VRAM, filling the BG0 buffer with the first tile from the specified PinnedAssetIm...
Definition: bg0.h:120
static UInt2 pixelSize()
Return the size of this mode as a vector, in pixels.
Definition: bg0.h:89
Definition: array.h:34
void span(UInt2 pos, unsigned width, const PinnedAssetImage &image)
Plot a horizontal span of tiles, using the first tile of a pinned asset.
Definition: bg0.h:194
T y
Vector component Y.
Definition: math.h:490
unsigned umod(int a, int b)
Compute the unsigned remainder from dividing two signed integers.
Definition: math.h:208
static unsigned tileHeight()
Return the height, in tiles, of this mode.
Definition: bg0.h:61
static unsigned pixelWidth()
Return the width, in pixels, of this mode.
Definition: bg0.h:75
static unsigned pixelHeight()
Return the height, in pixel, of this mode.
Definition: bg0.h:82
void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar= ' ')
Draw text, using an AssetImage as a fixed width font.
Definition: bg0.h:260
void image(UInt2 pos, const AssetImage &image, unsigned frame=0)
Draw a full AssetImage frame, with its top-left corner at the specified location. ...
Definition: bg0.h:234
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658