Permalink
Browse files

Initial viobyte code.

  • Loading branch information...
dulsi committed Aug 30, 2017
0 parents commit b20eccda0e917ffd8076053f4f675d3501d34729
Showing with 853 additions and 0 deletions.
  1. +88 −0 Sprite.cpp
  2. +37 −0 Sprite.h
  3. +661 −0 viobyte.ino
  4. +67 −0 viobytetitle.h
@@ -0,0 +1,88 @@
#include <TinyScreen.h>
#include <SPI.h>
#include <Wire.h>
#include "Sprite.h"
void drawSprites(SpriteInst* sprites, int numSprites, const TileMap5pix *tilemap, uint8_t bgCol, TinyScreen *display, particleCallback fn)
{
display->goTo(0,0);
display->startData();
uint8_t lineBuffer[96];
for(int lines = 0; lines < 64; ++lines)
{
if (( tilemap == 0 ) || (lines < tilemap->yPixOffset))
{
memset(lineBuffer,bgCol,96);
}
else
{
memset(lineBuffer,bgCol,96);
int tileGridY = (lines - tilemap->yPixOffset) / 5;
int thisY = (lines - tilemap->yPixOffset) - (tileGridY * 5);
int startX = tilemap->xPixOffset;
int tileGridX = -tilemap->xPixOffset / 5;
int startPos = (tileGridX * 5)+tilemap->xPixOffset;
for(int x = startPos; x < 96; x+=5)
{
int thisTileStartX = 0;
int thisTileLength = 5;
if ( x < 0 )
{
thisTileStartX = -x;
thisTileLength -= thisTileStartX;
}
else if ( x > 91 )
{
thisTileLength = 96-x;
}
if (tileGridX >= 0 && tileGridY >= 0
&& tileGridX < tilemap->xWidth && tileGridY < tilemap->yHeight &&
tilemap->tilemap[tileGridX+(tileGridY*tilemap->xWidth)] != 0 )
{
// memcpy(lineBuffer+x+thisTileStartX,tilemap->tilemap[tileGridX+(tileGridY*tilemap->xWidth)]->data+ (thisTileStartX+(thisY*5)),thisTileLength);
memcpy_P(lineBuffer+x+thisTileStartX,tilemap->tilemap[tileGridX+(tileGridY*tilemap->xWidth)]->data+ (thisTileStartX+(thisY*5)),thisTileLength);
}
else
{
memset(lineBuffer+x+thisTileStartX,bgCol,thisTileLength);
}
tileGridX++;
}
}
if (fn)
(*fn)(tilemap, lines, lineBuffer);
for(int index = 0; index < numSprites; ++index)
{
if(sprites[index].enabled)
{
const Sprite* spriteType = sprites[index].sprite;
int curLine = lines - sprites[index].y;
if ( curLine >= 0 && curLine < spriteType->height )
{
for(int x = 0; x < spriteType->width; ++x)
{
int tx = x + sprites[index].x;
if ( tx >= 0 && tx < 96 )
{
// uint8_t col = sprites[index].flip ? spriteType->data[(spriteType->width-(x+1))+(curLine*spriteType->width)] : spriteType->data[x+(curLine*spriteType->width)];
int offset = sprites[index].flip ? (spriteType->width-(x+1))+(curLine*spriteType->width) : x+(curLine*spriteType->width);
uint8_t col = pgm_read_byte_near(spriteType->data + offset);
if ( col != ALPHA )
{
lineBuffer[tx] = col;
}
}
}
}
}
}
display->writeBuffer(lineBuffer,96);
}
display->endTransfer();
}
@@ -0,0 +1,37 @@
#pragma once
#include <avr/pgmspace.h>
#define ALPHA 0xBA
class Sprite
{
public:
int width;
int height;
const uint8_t* data;
};
class SpriteInst
{
public:
const Sprite* sprite;
int x;
int y;
bool enabled;
bool flip;
};
class TileMap5pix
{
public:
int xWidth;
int yHeight;
int xPixOffset;
int yPixOffset;
const Sprite** tilemap;
};
typedef void (*particleCallback)(const TileMap5pix *tilemap, int line, uint8_t lineBuffer[96]);
void drawSprites(SpriteInst* sprites, int numSprites, const TileMap5pix *tilemap, uint8_t bgCol, TinyScreen *display, particleCallback fn);
Oops, something went wrong.

0 comments on commit b20eccd

Please sign in to comment.