v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
bg2.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 #include <sifteo/video/color.h>
36 
37 namespace Sifteo {
38 
61 struct BG2Drawable {
62  _SYSAttachedVideoBuffer sys;
63 
67  static unsigned tileWidth() {
68  return _SYS_VRAM_BG2_WIDTH;
69  }
70 
74  static unsigned tileHeight() {
75  return _SYS_VRAM_BG2_WIDTH;
76  }
77 
81  static UInt2 tileSize() {
82  return vec(tileWidth(), tileHeight());
83  }
84 
88  static unsigned pixelWidth() {
89  return tileWidth() * 8;
90  }
91 
95  static unsigned pixelHeight() {
96  return tileHeight() * 8;
97  }
98 
102  static UInt2 pixelSize() {
103  return vec(pixelWidth(), pixelHeight());
104  }
105 
109  static unsigned sizeInBytes() {
110  return tileWidth() * tileHeight() * 2;
111  }
112 
116  static unsigned sizeInWords() {
117  return tileWidth() * tileHeight();
118  }
119 
125  uint16_t tileAddr(UInt2 pos) {
126  return pos.x + pos.y * tileWidth();
127  }
128 
135  void erase(uint16_t index = 0) {
136  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
137  }
138 
143  void erase(const PinnedAssetImage &image) {
144  erase(image.tile(sys.cube, 0));
145  }
146 
153  void setBorder(RGB565 color) {
154  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_border) / 2,
155  color.value);
156  }
157 
161  RGB565 getBorder() const {
162  RGB565 result = { _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_border)) };
163  return result;
164  }
165 
169  void setMatrix(const AffineMatrix &m) {
170  _SYSAffine a = {
171  // Round to fixed-point
172  256.0f * m.cx + 0.5f,
173  256.0f * m.cy + 0.5f,
174  256.0f * m.xx + 0.5f,
175  256.0f * m.xy + 0.5f,
176  256.0f * m.yx + 0.5f,
177  256.0f * m.yy + 0.5f,
178  };
179  _SYS_vbuf_write(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_affine)/2,
180  (const uint16_t *)&a, 6);
181  }
182 
189  void plot(UInt2 pos, uint16_t tileIndex) {
190  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
191  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
192  }
193 
200  void span(UInt2 pos, unsigned width, unsigned tileIndex)
201  {
202  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
203  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
204  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
205  }
206 
213  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
214  {
215  while (size.y) {
216  span(topLeft, size.x, tileIndex);
217  size.y--;
218  topLeft.y++;
219  }
220  }
221 
230  void image(UInt2 pos, const AssetImage &image, unsigned frame = 0)
231  {
232  _SYS_image_BG2Draw(&sys, image, tileAddr(pos), frame);
233  }
234 
243  void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame = 0)
244  {
245  _SYS_image_BG2DrawRect(&sys, image, tileAddr(destXY),
246  frame, (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
247  }
248 
255  void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar = ' ')
256  {
257  unsigned addr = tileAddr(topLeft);
258  unsigned lineAddr = addr;
259  char c;
260 
261  while ((c = *str)) {
262  if (c == '\n') {
263  addr = (lineAddr += font.tileHeight() * tileWidth());
264  } else {
265  _SYS_image_BG2Draw(&sys, font, addr, c - firstChar);
266  addr += font.tileWidth();
267  }
268  str++;
269  }
270  }
271 
275  _SYSVideoBuffer &videoBuffer() {
276  return sys.vbuf;
277  }
278 
282  CubeID cube() const {
283  return sys.cube;
284  }
285 };
286 
291 }; // namespace Sifteo
#define offsetof(t, m)
Definition: macros.h:368
static UInt2 pixelSize()
Return the size of this mode as a vector, in pixels.
Definition: bg2.h:102
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
static unsigned pixelHeight()
Return the height, in pixel, of this mode.
Definition: bg2.h:95
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: bg2.h:230
static unsigned sizeInWords()
Returns the size of this drawable's tile data, in 16-bit words.
Definition: bg2.h:116
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: bg2.h:213
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
static unsigned tileWidth()
Return the width, in tiles, of this mode.
Definition: bg2.h:67
float cx
Matrix member cx, the constant offset for X.
Definition: math.h:1043
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:90
float cy
Matrix member cy, the constant offset for Y.
Definition: math.h:1044
void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar= ' ')
Draw text, using an AssetImage as a fixed width font.
Definition: bg2.h:255
void span(UInt2 pos, unsigned width, unsigned tileIndex)
Plot a horizontal span of tiles, by absolue tile index, given the position of the leftmost tile and t...
Definition: bg2.h:200
A VRAM accessor for drawing graphics in the BG2 mode.
Definition: bg2.h:61
An AssetImage in which all tiles are stored sequentially in memory.
Definition: image.h:134
static unsigned sizeInBytes()
Returns the size of this drawable's tile data, in bytes.
Definition: bg2.h:109
static UInt2 tileSize()
Return the size of this mode as a vector, in tiles.
Definition: bg2.h:81
static unsigned tileHeight()
Return the height, in tiles, of this mode.
Definition: bg2.h:74
float yy
Matrix member yy, the vertical Y delta.
Definition: math.h:1048
T x
Vector component X.
Definition: math.h:489
void setMatrix(const AffineMatrix &m)
Set the current affine transform matrix.
Definition: bg2.h:169
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:93
float xy
Matrix member xy, the horizontal Y delta.
Definition: math.h:1046
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
RGB565 getBorder() const
Get the last border color set by setBorder().
Definition: bg2.h:161
void erase(const PinnedAssetImage &image)
Erase mode-specific VRAM, filling the BG2 buffer with the first tile from the specified PinnedAssetIm...
Definition: bg2.h:143
CubeID cube() const
Return the CubeID associated with this drawable.
Definition: bg2.h:282
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:160
An augmented 3x2 matrix, for doing 2D affine transforms.
Definition: math.h:1042
Any kind of asset image, as defined in your stir script.
Definition: image.h:67
void erase(uint16_t index=0)
Erase mode-specific VRAM, filling the BG2 buffer with the specified absolute tile index value...
Definition: bg2.h:135
Definition: array.h:34
T y
Vector component Y.
Definition: math.h:490
float yx
Matrix member yx, the vertical X delta.
Definition: math.h:1047
void plot(UInt2 pos, uint16_t tileIndex)
Plot a single tile, by absolute tile index, at location 'pos' in tile units.
Definition: bg2.h:189
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: bg2.h:243
static unsigned pixelWidth()
Return the width, in pixels, of this mode.
Definition: bg2.h:88
void setBorder(RGB565 color)
Set the border color, given an arbitrary 16-bit RGB565 color.
Definition: bg2.h:153
Represents a 16-bit 5:6:5 color, the native format used by our display.
Definition: color.h:50
uint16_t value
The raw color value, as a 16-bit integer.
Definition: color.h:52
uint16_t tileAddr(UInt2 pos)
Calculate the video buffer address of a particular tile.
Definition: bg2.h:125
_SYSVideoBuffer & videoBuffer()
Return the VideoBuffer associated with this drawable.
Definition: bg2.h:275
float xx
Matrix member xx, the horizontal X delta.
Definition: math.h:1045
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658