What I'm trying to do is just
- read the data at position x,y on the screen and save it.
draw the ball at x,y
wait some milliseconds for visibility
restore the data at position x,y to what it was before the ball was drawn.
increment x and y as necessary
I'm, of course, not drawing just a single space in the actual code - I'm saving a buffer of all the pixels needed - I've just stripped this down for clarity.
The reason I have to read and restore is otherwise the ball will erase the net or the clock numerals.
The code below simply generates a dashed line with a blank space every 6 or so iterations. Video of what it does is here: https://www.dropbox.com/s/wq0zxsttc2nbk ... 1.mp4?dl=0 - pardon the bit of glare.
This has to be a data type problem or a timing issue or something I'm just not understanding. Any help would be appreciated
Code: Select all
#include <glcd.h>
#define GLCD_CS_Invert 1
#include "fonts/allFonts.h" // system and arial14 fonts are used
#include "bitmaps/allBitmaps.h" // all images in the bitmap dir
int courtTop=2;
int courtBottom=58;
int courtLeft=0;
int courtRight=127;
int ballHeight=2; int ballWidth=2;
int ballDirectionY = 1; // X direction of the ball
int ballDirectionX = 1; // Y direction of the ball
int ballBuf[40]; //save screen data
int xPos, yPos;
void setup()
{
GLCD.Init(); // initialise the library, non inverted writes pixels onto a clear screen
GLCD.SetDisplayMode(NON_INVERTED);
GLCD.SelectFont(fixednums8x16, BLACK); // font for the default text area
GLCD.ClearScreen();
xPos = 14;
yPos = 24;
}
void loop(){
moveBall();
}
void saveRectangle(int x, int y) {
GLCD.GotoXY(x,y);
ballBuf[0]=GLCD.ReadData();
}
void restoreRectangle(int x, int y) {
GLCD.GotoXY(x,y);
GLCD.WriteData(ballBuf[0]);
}
void drawBall(int x, int y) {
GLCD.SetDot(x,y,BLACK);
}
void moveBall() {
xPos += ballDirectionX;
if (xPos+ballWidth >= courtRight || xPos-ballWidth <= courtLeft)
ballDirectionX = -ballDirectionX;
yPos += ballDirectionY;
if (yPos+ballHeight >= courtBottom || yPos-ballHeight <= courtTop)
ballDirectionY = -ballDirectionY;
saveRectangle(xPos,yPos);
drawBall(xPos,yPos);
delay(30);
restoreRectangle(xPos,yPos);
}