v1.1.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Modules Pages
bg1.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 
55 struct BG1Mask {
56  // Matches the memory layout of BG1, but uses faster 32-bit arithmetic.
57  uint32_t row10;
58  uint32_t row32;
59  uint32_t row54;
60  uint32_t row76;
61  uint32_t row98;
62  uint32_t rowBA;
63  uint32_t rowDC;
64  uint32_t rowFE;
65 
69  static BG1Mask empty() {
70  BG1Mask result = { 0, 0, 0, 0, 0, 0, 0, 0 };
71  return result;
72  }
73 
83  static BG1Mask filled(UInt2 topLeft, UInt2 size) {
84  unsigned xBits = ((1 << size.x) - 1) << topLeft.x;
85  unsigned yBits = ((1 << size.y) - 1) << topLeft.y;
86 
87  unsigned row0 = yBits & 0x0001 ? xBits : 0;
88  unsigned row1 = yBits & 0x0002 ? xBits : 0;
89  unsigned row2 = yBits & 0x0004 ? xBits : 0;
90  unsigned row3 = yBits & 0x0008 ? xBits : 0;
91  unsigned row4 = yBits & 0x0010 ? xBits : 0;
92  unsigned row5 = yBits & 0x0020 ? xBits : 0;
93  unsigned row6 = yBits & 0x0040 ? xBits : 0;
94  unsigned row7 = yBits & 0x0080 ? xBits : 0;
95  unsigned row8 = yBits & 0x0100 ? xBits : 0;
96  unsigned row9 = yBits & 0x0200 ? xBits : 0;
97  unsigned rowA = yBits & 0x0400 ? xBits : 0;
98  unsigned rowB = yBits & 0x0800 ? xBits : 0;
99  unsigned rowC = yBits & 0x1000 ? xBits : 0;
100  unsigned rowD = yBits & 0x2000 ? xBits : 0;
101  unsigned rowE = yBits & 0x4000 ? xBits : 0;
102  unsigned rowF = yBits & 0x8000 ? xBits : 0;
103 
104  BG1Mask result = {
105  (row1 << 16) | row0,
106  (row3 << 16) | row2,
107  (row5 << 16) | row4,
108  (row7 << 16) | row6,
109  (row9 << 16) | row8,
110  (rowB << 16) | rowA,
111  (rowD << 16) | rowC,
112  (rowF << 16) | rowE,
113  };
114  return result;
115  }
116 
120  void clear() {
121  _SYS_memset32(&row10, 0, 8);
122  }
123 
127  uint16_t *rows() {
128  return (uint16_t*) this;
129  }
130  const uint16_t *rows() const {
131  return (const uint16_t*) this;
132  }
133 
139  void plot(unsigned x, unsigned y) {
140  ASSERT(x < 16 && y < 16);
141  rows()[y] |= 1 << x;
142  }
143 
149  void plot(UInt2 pos) {
150  plot(pos.x, pos.y);
151  }
152 
158  void fill(UInt2 topLeft, UInt2 size) {
159  for (unsigned y = 0; y < size.y; y++)
160  for (unsigned x = 0; x < size.x; x++)
161  plot(topLeft.x + x, topLeft.y + y);
162  }
163 
166  row10 |= other.row10;
167  row32 |= other.row32;
168  row54 |= other.row54;
169  row76 |= other.row76;
170  row98 |= other.row98;
171  rowBA |= other.rowBA;
172  rowDC |= other.rowDC;
173  rowFE |= other.rowFE;
174  return *this;
175  }
176 
179  row10 &= other.row10;
180  row32 &= other.row32;
181  row54 &= other.row54;
182  row76 &= other.row76;
183  row98 &= other.row98;
184  rowBA &= other.rowBA;
185  rowDC &= other.rowDC;
186  rowFE &= other.rowFE;
187  return *this;
188  }
189 
192  row10 ^= other.row10;
193  row32 ^= other.row32;
194  row54 ^= other.row54;
195  row76 ^= other.row76;
196  row98 ^= other.row98;
197  rowBA ^= other.rowBA;
198  rowDC ^= other.rowDC;
199  rowFE ^= other.rowFE;
200  return *this;
201  }
202 
204  BG1Mask operator| (BG1Mask other) const {
205  BG1Mask result = {
206  row10 | other.row10,
207  row32 | other.row32,
208  row54 | other.row54,
209  row76 | other.row76,
210  row98 | other.row98,
211  rowBA | other.rowBA,
212  rowDC | other.rowDC,
213  rowFE | other.rowFE,
214  };
215  return result;
216  }
217 
219  BG1Mask operator& (BG1Mask other) const {
220  BG1Mask result = {
221  row10 & other.row10,
222  row32 & other.row32,
223  row54 & other.row54,
224  row76 & other.row76,
225  row98 & other.row98,
226  rowBA & other.rowBA,
227  rowDC & other.rowDC,
228  rowFE & other.rowFE,
229  };
230  return result;
231  }
232 
234  BG1Mask operator^ (BG1Mask other) const {
235  BG1Mask result = {
236  row10 ^ other.row10,
237  row32 ^ other.row32,
238  row54 ^ other.row54,
239  row76 ^ other.row76,
240  row98 ^ other.row98,
241  rowBA ^ other.rowBA,
242  rowDC ^ other.rowDC,
243  rowFE ^ other.rowFE,
244  };
245  return result;
246  }
247 
249  BG1Mask operator~ () const {
250  BG1Mask result = {
251  ~row10,
252  ~row32,
253  ~row54,
254  ~row76,
255  ~row98,
256  ~rowBA,
257  ~rowDC,
258  ~rowFE,
259  };
260  return result;
261  }
262 };
263 
264 
285 struct BG1Drawable {
286  _SYSAttachedVideoBuffer sys;
287 
291  static unsigned tileWidth() {
292  return _SYS_VRAM_BG1_WIDTH;
293  }
294 
298  static unsigned tileHeight() {
299  return _SYS_VRAM_BG1_WIDTH;
300  }
301 
305  static UInt2 tileSize() {
306  return vec(tileWidth(), tileHeight());
307  }
308 
312  static unsigned pixelWidth() {
313  return tileWidth() * 8;
314  }
315 
319  static unsigned pixelHeight() {
320  return tileHeight() * 8;
321  }
322 
326  static UInt2 pixelSize() {
327  return vec(pixelWidth(), pixelHeight());
328  }
329 
333  static unsigned numTiles() {
334  return _SYS_VRAM_BG1_TILES;
335  }
336 
342  void erase(uint16_t index = 0) {
343  eraseMask();
344  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_BG1_TILES / 2,
345  _SYS_TILE77(index), numTiles());
346  setPanning(vec(0,0));
347  }
348 
354  void erase(const PinnedAssetImage &image) {
355  erase(image.tile(sys.cube, 0));
356  }
357 
362  void fill(uint16_t index = 0) {
363  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_BG1_TILES / 2,
364  _SYS_TILE77(index), numTiles());
365  }
366 
371  void fill(const PinnedAssetImage &image) {
372  fill(image.tile(sys.cube, 0));
373  }
374 
388  void eraseMask() {
389  _SYS_finish();
390  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_BG1_BITMAP/2, 0, _SYS_VRAM_BG1_WIDTH);
391  }
392 
405  void setMask(const BG1Mask &mask) {
406  _SYS_finish();
407  _SYS_vbuf_write(&sys.vbuf, offsetof(_SYSVideoRAM, bg1_bitmap)/2,
408  mask.rows(), 16);
409  }
410 
432  void fillMask(UInt2 topLeft, UInt2 size) {
433  _SYS_finish();
434  _SYS_vbuf_fill(&sys.vbuf,
435  offsetof(_SYSVideoRAM, bg1_bitmap)/2 + topLeft.y,
436  ((1 << size.x) - 1) << topLeft.x, size.y);
437  }
438 
449  void setPanning(Int2 pixels) {
450  unsigned x = pixels.x & 0xFF;
451  unsigned y = pixels.y & 0xFF;
452  _SYS_vbuf_poke(&sys.vbuf, offsetof(_SYSVideoRAM, bg1_x) / 2, x | (y << 8));
453  }
454 
458  Int2 getPanning() const {
459  unsigned word = _SYS_vbuf_peek(&sys.vbuf, offsetof(_SYSVideoRAM, bg0_x) / 2);
460  return vec<int>((int8_t)(word & 0xFF), (int8_t)(word >> 8));
461  }
462 
467  void plot(unsigned locationIndex, uint16_t tileIndex) {
468  ASSERT(locationIndex < numTiles());
469  _SYS_vbuf_poke(&sys.vbuf, _SYS_VA_BG1_TILES / 2 + locationIndex,
470  _SYS_TILE77(tileIndex));
471  }
472 
479  void span(unsigned locationIndex, unsigned count, unsigned tileIndex)
480  {
481  ASSERT(locationIndex <= numTiles() && count <= numTiles() &&
482  (locationIndex + count) < numTiles());
483  _SYS_vbuf_fill(&sys.vbuf, _SYS_VA_BG1_TILES / 2 + locationIndex,
484  _SYS_TILE77(tileIndex), count);
485  }
486 
498  void image(UInt2 pos, const AssetImage &image, unsigned frame = 0)
499  {
500  _SYS_image_BG1Draw(&sys, image, (_SYSInt2*) &pos, frame);
501  }
502 
514  void image(UInt2 destXY, UInt2 size, const AssetImage &image, UInt2 srcXY, unsigned frame = 0)
515  {
516  _SYS_image_BG1DrawRect(&sys, image, (_SYSInt2*) &destXY,
517  frame, (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
518  }
519 
532  void maskedImage(const AssetImage &image, const PinnedAssetImage &key,
533  unsigned frame = 0)
534  {
535  _SYS_finish();
536  _SYS_image_BG1MaskedDraw(&sys, image, key.tile(sys.cube, 0), frame);
537  }
538 
552  void maskedImage(UInt2 size, const AssetImage &image,
553  const PinnedAssetImage &key, UInt2 srcXY,
554  unsigned frame = 0)
555  {
556  _SYS_finish();
557  _SYS_image_BG1MaskedDrawRect(&sys, image, key.tile(sys.cube, 0), frame,
558  (_SYSInt2*) &srcXY, (_SYSInt2*) &size);
559  }
560 
567  void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar = ' ')
568  {
569  Int2 pos = topLeft;
570  char c;
571 
572  while ((c = *str)) {
573  if (c == '\n') {
574  pos.x = topLeft.x;
575  pos.y += font.tileHeight();
576  } else {
577  _SYS_image_BG1Draw(&sys, font, (_SYSInt2*) &pos, c - firstChar);
578  pos.x += font.tileWidth();
579  }
580  str++;
581  }
582  }
583 
587  _SYSVideoBuffer &videoBuffer() {
588  return sys.vbuf;
589  }
590 
594  CubeID cube() const {
595  return sys.cube;
596  }
597 };
598 
603 }; // namespace Sifteo
A BG1 tile mask. In other words, this is a 16x16-bit two-dimensional vector.
Definition: bg1.h:55
void span(unsigned locationIndex, unsigned count, unsigned tileIndex)
Plot a horizontal span of tiles in consecutively allocated locations in the BG1 tile array...
Definition: bg1.h:479
static UInt2 tileSize()
Return the size of this mode as a vector, in tiles.
Definition: bg1.h:305
CubeID cube() const
Return the CubeID associated with this drawable.
Definition: bg1.h:594
#define offsetof(t, m)
Definition: macros.h:368
void eraseMask()
Erase just the allocation mask. All tiles will now be unallocated, and BG1 will be fully transparent...
Definition: bg1.h:388
void fill(const PinnedAssetImage &image)
Fill all BG1 tiles with first tile from the specified PinnedAssetImage, without modifying the allocat...
Definition: bg1.h:371
BG1Mask operator^(BG1Mask other) const
Bitwise XOR with another mask.
Definition: bg1.h:234
void text(Int2 topLeft, const AssetImage &font, const char *str, char firstChar= ' ')
Draw text, using an AssetImage as a fixed width font.
Definition: bg1.h:567
void maskedImage(UInt2 size, const AssetImage &image, const PinnedAssetImage &key, UInt2 srcXY, unsigned frame=0)
Draw part of an AssetImage, automatically allocating tiles on the BG1 mask.
Definition: bg1.h:552
Generalized two-element cartesian coordinate vector.
Definition: math.h:488
void erase(const PinnedAssetImage &image)
Erase mode-specific VRAM, filling the BG1 buffer with the first tile from the specified PinnedAssetIm...
Definition: bg1.h:354
static unsigned numTiles()
Returns the maximum number of allcoated tiles.
Definition: bg1.h:333
#define ASSERT(_x)
Runtime debug assertion.
Definition: macros.h:205
BG1Mask & operator&=(BG1Mask other)
Bitwise AND with another mask.
Definition: bg1.h:178
static unsigned pixelHeight()
Return the height, in pixel, of this mode.
Definition: bg1.h:319
int tileWidth() const
The width of this image, in tiles.
Definition: image.h:90
static BG1Mask filled(UInt2 topLeft, UInt2 size)
Create a mask with a filled rectangle it.
Definition: bg1.h:83
void plot(unsigned locationIndex, uint16_t tileIndex)
Plot a single tile, by absolute tile index, at a specific location in the 144-tile array...
Definition: bg1.h:467
An AssetImage in which all tiles are stored sequentially in memory.
Definition: image.h:134
static unsigned pixelWidth()
Return the width, in pixels, of this mode.
Definition: bg1.h:312
uint16_t * rows()
Get a pointer to the rows in this mask, as 16-bit integers.
Definition: bg1.h:127
void setMask(const BG1Mask &mask)
Change the tile allocation bitmap.
Definition: bg1.h:405
static unsigned tileWidth()
Return the width, in tiles, of this mode.
Definition: bg1.h:291
Int2 getPanning() const
Retrieve the last value set by setPanning(). .
Definition: bg1.h:458
static UInt2 pixelSize()
Return the size of this mode as a vector, in pixels.
Definition: bg1.h:326
T x
Vector component X.
Definition: math.h:489
void erase(uint16_t index=0)
Erase mode-specific VRAM, filling the BG1 buffer with the specified absolute tile index value...
Definition: bg1.h:342
A VRAM accessor for drawing graphics in the BG1 mode.
Definition: bg1.h:285
int tileHeight() const
The height of this image, in tiles.
Definition: image.h:93
A lightweight identifier for one Sifteo cube.
Definition: cube.h:85
static BG1Mask empty()
Create an empty mask. All bits are zero.
Definition: bg1.h:69
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: bg1.h:514
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: bg1.h:498
uint16_t tile(unsigned i) const
Returns the index of the tile at linear position 'i' in the image.
Definition: image.h:160
Any kind of asset image, as defined in your stir script.
Definition: image.h:67
void setPanning(Int2 pixels)
Change the hardware pixel-panning origin for this mode.
Definition: bg1.h:449
void fill(UInt2 topLeft, UInt2 size)
Mark a rectangular region of the bitmap.
Definition: bg1.h:158
static unsigned tileHeight()
Return the height, in tiles, of this mode.
Definition: bg1.h:298
void clear()
Erase a TileMask, setting all bits to zero.
Definition: bg1.h:120
Definition: array.h:34
BG1Mask & operator^=(BG1Mask other)
Bitwise XOR with another mask.
Definition: bg1.h:191
T y
Vector component Y.
Definition: math.h:490
BG1Mask operator|(BG1Mask other) const
Bitwise OR with another mask.
Definition: bg1.h:204
void plot(unsigned x, unsigned y)
Mark one tile in the bitmap, given as (x,y) coordinates.
Definition: bg1.h:139
BG1Mask operator&(BG1Mask other) const
Bitwise AND with another mask.
Definition: bg1.h:219
void maskedImage(const AssetImage &image, const PinnedAssetImage &key, unsigned frame=0)
Draw an AssetImage, automatically allocating tiles on the BG1 mask.
Definition: bg1.h:532
void fillMask(UInt2 topLeft, UInt2 size)
This is a specialized alternative to setMask(), for cases where each row of BG1 has a single contiguo...
Definition: bg1.h:432
void plot(UInt2 pos)
Mark one tile in the bitmap, given as a vector.
Definition: bg1.h:149
void fill(uint16_t index=0)
Fill all BG1 tiles with the specified absolute index, without modifying the allocation mask...
Definition: bg1.h:362
BG1Mask operator~() const
Bitwise complement.
Definition: bg1.h:249
_SYSVideoBuffer & videoBuffer()
Return the VideoBuffer associated with this drawable.
Definition: bg1.h:587
BG1Mask & operator|=(BG1Mask other)
Bitwise OR with another mask.
Definition: bg1.h:165
Vector2< T > vec(T x, T y)
Create a Vector2, from a set of (x,y) coordinates.
Definition: math.h:658