v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Groups Pages
bg2.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 #include <sifteo/video/color.h>
17 
18 namespace Sifteo {
19 
42 struct BG2Drawable {
43  _SYSAttachedVideoBuffer sys;
44 
48  static unsigned tileWidth() {
49  return _SYS_VRAM_BG2_WIDTH;
50  }
51 
55  static unsigned tileHeight() {
56  return _SYS_VRAM_BG2_WIDTH;
57  }
58 
62  static UInt2 tileSize() {
63  return vec(tileWidth(), tileHeight());
64  }
65 
69  static unsigned pixelWidth() {
70  return tileWidth() * 8;
71  }
72 
76  static unsigned pixelHeight() {
77  return tileHeight() * 8;
78  }
79 
83  static UInt2 pixelSize() {
84  return vec(pixelWidth(), pixelHeight());
85  }
86 
90  static unsigned sizeInBytes() {
91  return tileWidth() * tileHeight() * 2;
92  }
93 
97  static unsigned sizeInWords() {
98  return tileWidth() * tileHeight();
99  }
100 
106  uint16_t tileAddr(UInt2 pos) {
107  return pos.x + pos.y * tileWidth();
108  }
109 
116  void erase(uint16_t index = 0) {
117  _SYS_vbuf_fill(&sys.vbuf, 0, _SYS_TILE77(index), sizeInWords());
118  }
119 
124  void erase(const PinnedAssetImage &image) {
125  erase(image.tile(sys.cube, 0));
126  }
127 
134  void setBorder(RGB565 color) {
135  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_border) / 2,
136  color.value);
137  }
138 
142  RGB565 getBorder() const {
143  RGB565 result = { _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_border)) };
144  return result;
145  }
146 
150  void setMatrix(const AffineMatrix &m) {
151  _SYSAffine a = {
152  // Round to fixed-point
153  256.0f * m.cx + 0.5f,
154  256.0f * m.cy + 0.5f,
155  256.0f * m.xx + 0.5f,
156  256.0f * m.xy + 0.5f,
157  256.0f * m.yx + 0.5f,
158  256.0f * m.yy + 0.5f,
159  };
160  _SYS_vbuf_write(&sys.vbuf, offsetof(_SYSVideoRAM, bg2_affine)/2,
161  (const uint16_t *)&a, 6);
162  }
163 
170  void plot(UInt2 pos, uint16_t tileIndex) {
171  ASSERT(pos.x < tileWidth() && pos.y < tileHeight());
172  _SYS_vbuf_poke(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex));
173  }
174 
181  void span(UInt2 pos, unsigned width, unsigned tileIndex)
182  {
183  ASSERT(pos.x <= tileWidth() && width <= tileWidth() &&
184  (pos.x + width) <= tileWidth() && pos.y < tileHeight());
185  _SYS_vbuf_fill(&sys.vbuf, tileAddr(pos), _SYS_TILE77(tileIndex), width);
186  }
187 
194  void fill(UInt2 topLeft, UInt2 size, unsigned tileIndex)
195  {
196  while (size.y) {
197  span(topLeft, size.x, tileIndex);
198  size.y--;
199  topLeft.y++;
200  }
201  }
202 
211  void image(UInt2 pos, const AssetImage &image, unsigned frame = 0)
212  {
213  _SYS_image_BG2Draw(&sys, image, tileAddr(pos), frame);
214  }
215 
224  void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame = 0)
225  {
226  _SYS_image_BG2DrawRect(&sys, image, tileAddr(destXY),
227  frame, (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
228  }
229 
236  void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar = ' ')
237  {
238  unsigned addr = tileAddr(topLeft);
239  unsigned lineAddr = addr;
240  char c;
241 
242  while ((c = *str)) {
243  if (c == '\n') {
244  addr = (lineAddr += font.tileHeight() * tileWidth());
245  } else {
246  _SYS_image_BG2Draw(&sys, font, addr, c - firstChar);
247  addr += font.tileWidth();
248  }
249  str++;
250  }
251  }
252 
256  _SYSVideoBuffer &videoBuffer() {
257  return sys.vbuf;
258  }
259 
263  CubeID cube() const {
264  return sys.cube;
265  }
266 };
267 
272 }; // namespace Sifteo