v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
bg0rom.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 
54  _SYSAttachedVideoBuffer sys;
55 
59  enum Palette {
60  BLACK_ON_WHITE = 0 << 10,
61  BLUE_ON_WHITE = 1 << 10,
62  ORANGE_ON_WHITE = 2 << 10,
63  YELLOW_ON_BLUE = 3 << 10,
64  RED_ON_WHITE = 4 << 10,
65  GRAY_ON_WHITE = 5 << 10,
66  WHITE_ON_BLACK = 6 << 10,
67  WHITE_ON_BLUE = 7 << 10,
68  WHITE_ON_TEAL = 8 << 10,
69  BLACK_ON_YELLOW = 9 << 10,
70  DKGRAY_ON_LTGRAY = 10 << 10,
71  GREEN_ON_WHITE = 11 << 10,
72  WHITE_ON_GREEN = 12 << 10,
73  PURPLE_ON_WHITE = 13 << 10,
74  LTBLUE_ON_DKBLUE = 14 << 10,
75  GOLD_ON_WHITE = 15 << 10,
76 
77  // Aliases for white background
78  BLACK = BLACK_ON_WHITE,
79  BLUE = BLUE_ON_WHITE,
80  ORANGE = ORANGE_ON_WHITE,
81  RED = RED_ON_WHITE,
82  GRAY = GRAY_ON_WHITE,
83  GREEN = GREEN_ON_WHITE,
84  PURPLE = PURPLE_ON_WHITE,
85  GOLD = GOLD_ON_WHITE,
86  };
87 
91  enum Tiles {
92  FONT_SPACE = 0,
93  SOLID_BG = 0,
94  SOLID_FG = 104,
95  V_BARGRAPH = 224,
96  H_BARGRAPH = 231,
97  HEART = 128,
98  FROWN = 136,
99  SMILE = 152,
100  SMILE2 = 141,
101  RIGHT_ARROW = 125,
102  LEFT_ARROW = 126,
103  };
104 
108  enum ColorMode {
109  TWO_COLOR = 0 << 9,
110  FOUR_COLOR = 1 << 9,
111  };
112 
116  static unsigned tileWidth() {
117  return _SYS_VRAM_BG0_WIDTH;
118  }
119 
123  static unsigned tileHeight() {
124  return _SYS_VRAM_BG0_WIDTH;
125  }
126 
130  static UInt2 tileSize() {
131  return vec(tileWidth(), tileHeight());
132  }
133 
137  static unsigned pixelWidth() {
138  return tileWidth() * 8;
139  }
140 
144  static unsigned pixelHeight() {
145  return tileHeight() * 8;
146  }
147 
151  static UInt2 pixelSize() {
152  return vec(pixelWidth(), pixelHeight());
153  }
154 
158  static unsigned sizeInBytes() {
159  return tileWidth() * tileHeight() * 2;
160  }
161 
165  static unsigned sizeInWords() {
166  return tileWidth() * tileHeight();
167  }
168 
174  uint16_t tileAddr(UInt2 pos) {
175  return pos.x + pos.y * tileWidth();
176  }
177 
182  void erase(uint16_t index = 0) {
183  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
184  setPanning(vec(0,0));
185  }
186 
195  void setPanning(Int2 pixels) {
196  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2,
197  umod(pixels.x, pixelWidth()) |
198  (umod(pixels.y, pixelHeight()) << 8));
199  }
200 
205  Int2 getPanning() const {
206  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
207  return vec<int>(word & 0xFF, word >> 8);
208  }
209 
213  static uint16_t charTile(char c, enum Palette palette = BLACK) {
214  return palette ^ (c - ' ' + FONT_SPACE);
215  }
216 
222  void plot(UInt2 pos, uint16_t tileIndex) {
223  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
224  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
225  }
226 
233  void span(UInt2 pos, unsigned width, unsigned tileIndex)
234  {
235  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
236  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
237  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
238  }
239 
246  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
247  {
248  while (size.y) {
249  span(topLeft, size.x, tileIndex);
250  size.y--;
251  topLeft.y++;
252  }
253  }
254 
263  void hBargraph(Int2 topLeft, unsigned pixelWidth,
264  enum Palette palette = BLACK, unsigned tileHeight = 1)
265  {
266  unsigned addr = tileAddr(topLeft);
267  int wTiles = pixelWidth / 8;
268  int wRemainder = pixelWidth % 8;
269 
270  while (tileHeight--) {
271  _SYS_vbuf_fill(&sys.vbuf, addr,
272  _SYS_TILE77(palette ^ SOLID_FG), wTiles);
273  if (wRemainder)
274  _SYS_vbuf_poke(&sys.vbuf, addr + wTiles,
275  _SYS_TILE77(palette ^ (H_BARGRAPH + wRemainder - 1)));
276  addr += tileWidth();
277  }
278  }
279 
287  void text(Int2 topLeft, const char *str, enum Palette palette = BLACK)
288  {
289  unsigned addr = tileAddr(topLeft);
290  unsigned lineAddr = addr;
291  char c;
292 
293  while ((c = *str)) {
294  if (c == '\n')
295  addr = (lineAddr += tileWidth());
296  else
297  _SYS_vbuf_poke(&sys.vbuf, addr++, _SYS_TILE77(charTile(c, palette)));
298  str++;
299  }
300  }
301 
305  _SYSVideoBuffer &videoBuffer() {
306  return sys.vbuf;
307  }
308 
312  CubeID cube() const {
313  return sys.cube;
314  }
315 };
316 
321 }; // namespace Sifteo
322 
#define offsetof(t, m)
Definition: macros.h:368
static unsigned pixelWidth()
Return the width, in pixels, of this mode.
Definition: bg0rom.h:137
void span(UInt2 pos, unsigned width, unsigned tileIndex)
Plot a horizontal span of tiles, given the position of the leftmost tile, and the number of tiles to ...
Definition: bg0rom.h:233
void plot(UInt2 pos, uint16_t tileIndex)
Plot a single tile, at location 'pos', in tile units.
Definition: bg0rom.h:222
First tile in the vertical bargraph series.
Definition: bg0rom.h:95
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
static unsigned sizeInBytes()
Returns the size of this drawable's tile data, in bytes.
Definition: bg0rom.h:158
static unsigned pixelHeight()
Return the height, in pixel, of this mode.
Definition: bg0rom.h:144
Solid foreground-colored tile.
Definition: bg0rom.h:94
void setPanning(Int2 pixels)
Change the hardware pixel-panning origin for this mode.
Definition: bg0rom.h:195
ColorMode
Color modes, XOR'ed with the tile IDs below.
Definition: bg0rom.h:108
A VRAM accessor for drawing graphics in the BG0_ROM mode.
Definition: bg0rom.h:53
T x
Vector component X.
Definition: math.h:489
static unsigned tileHeight()
Return the height, in tiles, of this mode.
Definition: bg0rom.h:123
static unsigned tileWidth()
Return the width, in tiles, of this mode.
Definition: bg0rom.h:116
Palette
Palette IDs, XOR'ed with the tile IDs below.
Definition: bg0rom.h:59
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
_SYSVideoBuffer & videoBuffer()
Return the VideoBuffer associated with this drawable.
Definition: bg0rom.h:305
uint16_t tileAddr(UInt2 pos)
Calculate the video buffer address of a particular tile.
Definition: bg0rom.h:174
void erase(uint16_t index=0)
Erase mode-specific VRAM, filling the BG0 buffer with the specified value and resetting the panning r...
Definition: bg0rom.h:182
Int2 getPanning() const
Retrieve the last value set by setPanning(), modulo the layer size in pixels.
Definition: bg0rom.h:205
Solid background-colored tile (space)
Definition: bg0rom.h:93
CubeID cube() const
Return the CubeID associated with this drawable.
Definition: bg0rom.h:312
First character in the font, ASCII space.
Definition: bg0rom.h:92
Definition: array.h:34
static UInt2 pixelSize()
Return the size of this mode as a vector, in pixels.
Definition: bg0rom.h:151
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
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: bg0rom.h:246
Tiles
Well-known tile numbers.
Definition: bg0rom.h:91
void hBargraph(Int2 topLeft, unsigned pixelWidth, enum Palette palette=BLACK, unsigned tileHeight=1)
Draw a horizontal bargraph, with its top-left corner position specified in tiles, and its width in pi...
Definition: bg0rom.h:263
First tile in the horizontal bargraph series.
Definition: bg0rom.h:96
void text(Int2 topLeft, const char *str, enum Palette palette=BLACK)
Draw text, using the builtin ROM font, starting at location 'topLeft' in tiles.
Definition: bg0rom.h:287
static uint16_t charTile(char c, enum Palette palette=BLACK)
Calculate the tile index of one character in the ROM font.
Definition: bg0rom.h:213
static unsigned sizeInWords()
Returns the size of this drawable's tile data, in 16-bit words.
Definition: bg0rom.h:165
static UInt2 tileSize()
Return the size of this mode as a vector, in tiles.
Definition: bg0rom.h:130
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658