uint8_ttilemapBuffer[VISABLE_TILES_PER_ROW];// a small buffer to store one horizontal row of tiles from the tilemap
voidloop(){
if(!arduboy.nextFrame())return;// return until it's time to draw a new frame
arduboy.pollButtons();// pollButtons required for the justPressed() function
if((arduboy.justPressed(A_BUTTON)&&ballsVisible<MAX_BALLS))ballsVisible++;// Pressing A button increases the number of visible balls until the maximum has been reached
if((arduboy.justPressed(B_BUTTON)&&ballsVisible>0))ballsVisible--;// Pressing B reduces the number of visible balls until none are visible
if(arduboy.pressed(UP_BUTTON)&&mapLocation.y>16)mapLocation.y--;// Pressing directional buttons will scroll the tilemap
camera.x=mapLocation.x+circlePoints[pos].x;// circle around a fixed point
camera.y=mapLocation.y+circlePoints[pos].y;
//draw tilemap
for(int8_ty=0;y<VISABLE_TILES_PER_COLUMN;y++)
{
FX::readDataArray(FX_DATA_TILEMAP,// read the visible tiles on a horizontal row from the tilemap in external flash
y+camera.y/tileHeight,// the tilemap row
camera.x/tileWidth,// the column within tilemap row
tilemapWidth,// use the width of tilemap as array element size
tilemapBuffer,// reading tiles into a small buffer is faster then reading each tile individually
VISABLE_TILES_PER_ROW);
for(uint8_tx=0;x<VISABLE_TILES_PER_ROW;x++)
{
FX::drawBitmap(x*tileWidth-camera.x%tileWidth,// we're substracting the tile width and height modulus for scrolling effect
y*tileHeight-camera.y%tileHeight,//
FX_DATA_TILES,// the tilesheet bitmap offset in external flash
tilemapBuffer[x],// tile index
dbmNormal);// draw a row of normal tiles
}
}
if(arduboy.notPressed(UP_BUTTON|DOWN_BUTTON|LEFT_BUTTON|RIGHT_BUTTON))pos=++pos%CIRCLE_POINTS;//only circle around when no directional buttons are pressed
//draw balls
for(uint8_ti=0;i<ballsVisible;i++)
FX::drawBitmap(ball[i].x,// although this function is called drawBitmap it can also draw masked sprites
ball[i].y,
FX_DATA_BALLS,// the ball sprites masked bitmap offset in external flash memory
0,// the fxdata was build using the single ball sprite.png image so there's only frame 0
//i % 16, // comment above and uncomment this one if the fxdata is rebuild using the ball_16x16.png image
dbmMasked/* | dbmReverse */);// remove the '/*' and '/*' to reverse the balls into white balls
//when uploading the drawballs-singe-datafile.bin into the development area,
//you can replace the "0" value in the drawBitmap function above with "i % 16" without the quotes to display 16 different balls
//update ball movements
for(uint8_ti=0;i<ballsVisible;i++)
{
if(ball[i].xspeed>0)// Moving right
{
ball[i].x+=ball[i].xspeed;
if(ball[i].x>WIDTH-ballWidth)//off the right
{
ball[i].x=WIDTH-ballWidth;
ball[i].xspeed=-ball[i].xspeed;
}
}
else// moving left
{
ball[i].x+=ball[i].xspeed;
if(ball[i].x<0)// off the left
{
ball[i].x=0;
ball[i].xspeed=-ball[i].xspeed;
}
}
if(ball[i].yspeed>0)// moving down
{
ball[i].y+=ball[i].yspeed;
if(ball[i].y>HEIGHT-tileHeight)// off the bottom
{
ball[i].y=HEIGHT-tileHeight;
ball[i].yspeed=-ball[i].yspeed;
}
}
else// moving up
{
ball[i].y+=ball[i].yspeed;
if(ball[i].y<0)// off the top
{
ball[i].y=0;
ball[i].yspeed=-ball[i].yspeed;
}
}
}
FX::enableOLED();// only enable OLED for updating the display
arduboy.display(CLEAR_BUFFER);// Using CLEAR_BUFFER will clear the display buffer after it is displayed
FX::disableOLED();// disable display again so external flash can be accessed at any time