v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
bg0.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 #include <sifteo/asset.h>
16 
17 namespace Sifteo {
18 
29 struct BG0Drawable {
30  _SYSAttachedVideoBuffer sys;
31 
35  static unsigned tileWidth() {
36  return _SYS_VRAM_BG0_WIDTH;
37  }
38 
42  static unsigned tileHeight() {
43  return _SYS_VRAM_BG0_WIDTH;
44  }
45 
49  static UInt2 tileSize() {
50  return vec(tileWidth(), tileHeight());
51  }
52 
56  static unsigned pixelWidth() {
57  return tileWidth() * 8;
58  }
59 
63  static unsigned pixelHeight() {
64  return tileHeight() * 8;
65  }
66 
70  static UInt2 pixelSize() {
71  return vec(pixelWidth(), pixelHeight());
72  }
73 
77  static unsigned sizeInBytes() {
78  return tileWidth() * tileHeight() * 2;
79  }
80 
84  static unsigned sizeInWords() {
85  return tileWidth() * tileHeight();
86  }
87 
92  void erase(uint16_t index = 0) {
93  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
94  setPanning(vec(0,0));
95  }
96 
101  void erase(const PinnedAssetImage &image) {
102  erase(image.tile(sys.cube, 0));
103  }
104 
113  void setPanning(Int2 pixels) {
114  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2,
115  umod(pixels.x, pixelWidth()) |
116  (umod(pixels.y, pixelHeight()) << 8));
117  }
118 
123  Int2 getPanning() const {
124  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
125  return vec<int>(word & 0xFF, word >> 8);
126  }
127 
133  uint16_t tileAddr(UInt2 pos) {
134  return pos.x + pos.y * tileWidth();
135  }
136 
141  uint16_t tile(UInt2 pos) {
142  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
143  return _SYS_INVERSE_TILE77(_SYS_vbuf_peek(&sys.vbuf, tileAddr(pos)));
144  }
145 
152  void plot(UInt2 pos, uint16_t tileIndex) {
153  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
154  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
155  }
156 
163  void span(UInt2 pos, unsigned width, unsigned tileIndex)
164  {
165  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
166  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
167  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
168  }
169 
175  void span(UInt2 pos, unsigned width, const PinnedAssetImage &image)
176  {
177  span(pos, width, image.tile(sys.cube,0));
178  }
179 
186  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
187  {
188  while (size.y) {
189  span(topLeft, size.x, tileIndex);
190  size.y--;
191  topLeft.y++;
192  }
193  }
194 
201  void fill(UInt2 topLeft, UInt2 size, const PinnedAssetImage &image)
202  {
203  fill(topLeft, size, image.tile(sys.cube, 0));
204  }
205 
215  void image(UInt2 pos, const AssetImage &image, unsigned frame = 0)
216  {
217  _SYS_image_BG0Draw(&sys, image, tileAddr(pos), frame);
218  }
219 
229  void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame = 0)
230  {
231  _SYS_image_BG0DrawRect(&sys, image, tileAddr(destXY),
232  frame, (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
233  }
234 
241  void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar = ' ')
242  {
243  unsigned addr = tileAddr(topLeft);
244  unsigned lineAddr = addr;
245  char c;
246 
247  while ((c = *str)) {
248  if (c == '\n') {
249  addr = (lineAddr += font.tileHeight() * tileWidth());
250  } else {
251  _SYS_image_BG0Draw(&sys, font, addr, c - firstChar);
252  addr += font.tileWidth();
253  }
254  str++;
255  }
256  }
257 
261  _SYSVideoBuffer &videoBuffer() {
262  return sys.vbuf;
263  }
264 
268  CubeID cube() const {
269  return sys.cube;
270  }
271 };
272 
277 }; // namespace Sifteo