diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Jetris.ino | 124 | ||||
-rw-r--r-- | sprites.h | 206 | ||||
-rw-r--r-- | sprites.ino | 29 | ||||
-rw-r--r-- | tags | 39 |
5 files changed, 210 insertions, 189 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e92f57 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +tags @@ -1,7 +1,6 @@ // THings that miss // Functions that return something // Make more safety -// Malloc etc. #include "sprites.h" #include "MaxCommands.h" @@ -48,6 +47,8 @@ void setup() { setDisplay(1); initDisplay(); + + initSprites(); initGame(); @@ -57,13 +58,13 @@ void setup() { // GAME LOGIC // ///////////////////////////// -struct Sprite block; +Sprite *block; int xPos; int yPos; -unsigned long loopTime; +long loopTime; void initGame() { initBlock(); @@ -78,7 +79,8 @@ void loop() { //Move if(checkCollision(0, 1)) { - drawSprite(buttonLayer, xPos, yPos, &block); + drawSprite(buttonLayer, xPos, yPos, block); + handleFullRows(0); renderAll(); initBlock(); }else{ @@ -93,7 +95,7 @@ void initBlock(){ xPos = 0; yPos = 0; - block = *blocks[random(7)]; + block = blocks[random(7)]; } void moveBlock(int xMove, int yMove) { @@ -104,7 +106,7 @@ void moveBlock(int xMove, int yMove) { drawRegion(topLayer, 0xFF, 0xFFFF, false); //Draw ball - drawSprite(topLayer, xPos, yPos, &block); + drawSprite(topLayer, xPos, yPos, block); //Render renderAll(); @@ -117,66 +119,43 @@ int checkCollision(int xMove, int yMove) { int yNew = yPos + yMove; int xNew = xPos + xMove; - for(int i = 0; i < block.height; i++) { - if(( block.buff[i + block.yOff] >> xNew - block.xOff ) & buttonLayer[i + yNew]) + for(int i = 0; i < block->height; i++) { + if(( block->buff[i] >> xNew) & buttonLayer[i + yNew]) return 1; } //Check if out of bounds - if(xNew + block.width > 8 || xNew < 0 || yNew + block.height > 16) + if(xNew + block->width > 8 || xNew < 0 || yNew + block->height > 16) return 1; return 0; } +void rotateBlock() { + Serial.println("Rotate"); + + //If next block exist, switch to that + if( block->rotateNext ) { + Serial.println((uint16_t)block); + Serial.println((uint16_t)block->rotateNext); + + block = block->rotateNext; + Serial.println((uint16_t)block); + } + + //And render + moveBlock(0, 0); +} + void dropBlock() { //Move down until it hits something int count = 0; while( !checkCollision(0, 1) ) { moveBlock(0, 1); - + delay(10); } - drawSprite(buttonLayer, xPos, yPos, &block); -} - -//Rotate sprite with the clock -void rotateSprite(struct Sprite *sprite, int count) { - uint8_t oldBuffer[8]; - - Serial.println(oldBuffer[0]); - Serial.println(oldBuffer[2]); - Serial.println(sprite->buff[0]); - Serial.println(sprite->buff[2]); - - //VULN TODO - memcpy(oldBuffer, sprite->buff, sizeof(oldBuffer) ); - - Serial.println("Took copy"); - Serial.println(oldBuffer[0]); - Serial.println(sprite->buff[0]); - - for(int i = 0; i < 8; i++) { - sprite->buff[i] = 0; - } - - //Cannot explain how this works in text TODO - for(int i = 0; i < 8; i++) { - uint8_t row = oldBuffer[7 - i]; - for(int j = 0; j < 8; j++) { - sprite->buff[j] |= row & ( 1 << i ); - } - } - - Serial.println("Rotated array"); - - //Switch height/width - uint8_t temp = sprite->height; - sprite->height = sprite->width; - sprite->width = temp; - - Serial.println("Changed dimensions"); - + drawSprite(buttonLayer, xPos, yPos, block); } void renderAll(){ @@ -186,6 +165,18 @@ void renderAll(){ render(8); } +void handleFullRows(int start) { + for(int i = start; i < 16; i++ ) { + if(buttonLayer[15 - i] == 0xFF) { + for(int j = i; j < 16; j++) { + buttonLayer[15 - j] = buttonLayer[14 - j]; + } + handleFullRows(i); + return; + } + } +} + void renderToSerial() { Serial.write(0x33); Serial.println("[2J"); for(int i = 0; i < 16; i++) { @@ -231,18 +222,12 @@ void handleButtons() { dropLast = millis(); dropBlock(); - } - if(millis() - rotateLast > clickTime && rotateState) { + if(millis() - rotateLast > clickTime+100 && rotateState) { rotateLast = millis(); - //TODO collision checks - rotateSprite(&block, 1); - - //Redraw moveblock - moveBlock(0, 0); - + rotateBlock(); } } @@ -270,7 +255,7 @@ void initDisplay() { //Draw a region, where mask specifies where to draw. // xMask = 0b00111100, yMask = 0b01111110 will draw a small 4x6 box -void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, struct Sprite* sprite) { +void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, Sprite* sprite) { //Check if in range if (x + sprite->width - 1 > 7 || y + sprite->height - 1> 15 ) return; @@ -278,7 +263,7 @@ void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, struct Sprite* sprite) { for ( int i = y; i < sprite->height + y; i++) { //Calculate bits uint8_t row = layer[i]; - row |= sprite->buff[i - y + sprite->yOff] >> x - sprite->xOff; + row |= sprite->buff[i - y] >> x; layer[i] = row; @@ -305,15 +290,26 @@ void drawRegion(uint8_t layer[], uint8_t xMask, uint16_t yMask, bool state) { } } +unsigned long reRenderLast; + void render(int where) { + static uint8_t onScreen[16]; + + bool reRender = (millis() - reRenderLast ) > 1000; + for(int i = where; i < (where + 8); i++ ) { + uint8_t toWrite = buttonLayer[i] | topLayer[i]; + if(onScreen[i] == toWrite && !reRender) + continue; + writeCommand(maxDIGIT_0 + i - where, buttonLayer[i] | topLayer[i]); + + onScreen[i] = toWrite; } - - //Shift everything throught - for( int i = 0; i < curDisplay; i++) { - writeCommand(0, 0); + + if(reRender) { + reRenderLast = millis(); } } @@ -1,158 +1,192 @@ #ifndef sprites #define sprites +#include <stdint.h> +#include <stddef.h> + struct Sprite { - uint8_t buff[8]; + uint8_t buff[4]; uint8_t width; uint8_t height; - uint8_t xOff; - uint8_t yOff; + struct Sprite *rotateNext; }; -const Sprite smiley = { - { - 0b11001100, - 0b11001100, - 0b00000000, - 0b00110000, - 0b10000000, - 0b11111100, - 0b00000000, - 0b00000000 - }, 6, 6, 0, 0 -}; -const Sprite checker = { - { - 0b10101010, - 0b01010101, - 0b10101010, - 0b01010101, - 0b10101010, - 0b01010101, - 0b10101010, - 0b01010101 - }, 8, 8, 0, 0 -}; -const Sprite ball = { +struct Sprite ball = { { 0b11100000, 0b10100000, 0b11100000, 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000, - 0b00000000 - }, 3, 3, 0, 0 + }, 3, 3, NULL }; -const Sprite miniSmiley = { + +struct Sprite iBlock = { { - 0b01010000, - 0b10000000, 0b11110000, 0b00000000, 0b00000000, 0b00000000, - 0b00000000, - 0b00000000 - }, 4, 3, 0, 0 + }, 4, 1, NULL +}; +struct Sprite iBlockR = { + { + 0b10000000, + 0b10000000, + 0b10000000, + 0b10000000, + }, 1, 4, NULL }; -const Sprite iBlock = { +struct Sprite oBlock = { { + 0b11000000, + 0b11000000, 0b00000000, 0b00000000, - 0b00000000, - 0b00011110, - 0b00000000, + }, 2, 2, NULL +}; + +struct Sprite tBlock = { + { + 0b01000000, + 0b11100000, 0b00000000, 0b00000000, - 0b00000000 - }, 4, 1, 3, 3 + }, 3, 2, NULL }; - -const Sprite oBlock = { +struct Sprite tBlockR = { { + 0b10000000, 0b11000000, - 0b11000000, - 0b00000000, + 0b10000000, 0b00000000, + }, 2, 3, NULL +}; +struct Sprite tBlockD = { + { + 0b11100000, + 0b01000000, 0b00000000, 0b00000000, + }, 3, 2, NULL +}; +struct Sprite tBlockL = { + { + 0b01000000, + 0b11000000, + 0b01000000, 0b00000000, - 0b00000000 - }, 2, 2, 0, 0 + }, 2, 3, NULL }; -const Sprite tBlock = { + +struct Sprite sBlock = { { + 0b01100000, + 0b11000000, 0b00000000, 0b00000000, + }, 3, 2, NULL +}; +struct Sprite sBlockR = { + { + 0b10000000, + 0b11000000, + 0b01000000, 0b00000000, - 0b00001000, - 0b00011100, - 0b00000000, - 0b00000000, - 0b00000000 - }, 3, 2, 3, 3 + }, 2, 3, NULL }; -const Sprite sBlock = { +struct Sprite zBlock = { { + 0b11000000, + 0b01100000, 0b00000000, 0b00000000, + }, 3, 2, NULL +}; +struct Sprite zBlockR = { + { + 0b01000000, + 0b11000000, + 0b10000000, 0b00000000, - 0b00001100, - 0b00011000, - 0b00000000, - 0b00000000, - 0b00000000 - }, 3, 2, 3, 3 + }, 2, 3, NULL }; -const Sprite zBlock = { + +// JBLOCk +struct Sprite jBlock = { { + 0b10000000, + 0b11100000, 0b00000000, 0b00000000, - 0b00000000, - 0b00011000, - 0b00001100, - 0b00000000, - 0b00000000, - 0b00000000 - }, 3, 2, 3, 3 + }, 3, 2, NULL }; - -const Sprite jBlock = { +struct Sprite jBlockR = { { + 0b11000000, + 0b10000000, + 0b10000000, 0b00000000, + }, 2, 3, NULL +}; +struct Sprite jBlockD = { + { + 0b11100000, + 0b00100000, 0b00000000, 0b00000000, - 0b00010000, - 0b00011100, - 0b00000000, + }, 3, 2, NULL +}; +struct Sprite jBlockL = { + { + 0b01000000, + 0b01000000, + 0b11000000, 0b00000000, - 0b00000000 - }, 3, 2, 3, 3 + }, 2, 3, NULL }; -const Sprite lBlock = { +//Lblock +struct Sprite lBlock = { { + 0b00100000, + 0b11100000, 0b00000000, 0b00000000, + }, 3, 2, NULL +}; +struct Sprite lBlockR = { + { + 0b10000000, + 0b10000000, + 0b11000000, + 0b00000000, + }, 2, 3, NULL +}; +struct Sprite lBlockD = { + { + 0b11100000, + 0b10000000, 0b00000000, - 0b00000100, - 0b00011100, 0b00000000, + }, 3, 2, NULL +}; +struct Sprite lBlockL = { + { + 0b11000000, + 0b01000000, + 0b01000000, 0b00000000, - 0b00000000 - }, 3, 2, 2, 2 + }, 3, 2, NULL }; -const Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock }; +struct Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock }; #endif diff --git a/sprites.ino b/sprites.ino new file mode 100644 index 0000000..d519095 --- /dev/null +++ b/sprites.ino @@ -0,0 +1,29 @@ +#include "sprites.h" + +void initSprites() { + iBlock.rotateNext = &iBlockR; + iBlockR.rotateNext = &iBlock; + + oBlock.rotateNext = &oBlock; + + tBlock.rotateNext = &tBlockR; + tBlockR.rotateNext = &tBlockD; + tBlockD.rotateNext = &tBlockL; + tBlockL.rotateNext = &tBlock; + + sBlock.rotateNext = &sBlockR; + sBlockR.rotateNext = &sBlock; + + zBlock.rotateNext = &zBlockR; + zBlockR.rotateNext = &zBlock; + + jBlock.rotateNext = &jBlockR; + jBlockR.rotateNext = &jBlockD; + jBlockD.rotateNext = &jBlockL; + jBlockL.rotateNext = &jBlock; + + lBlock.rotateNext = &lBlockR; + lBlockR.rotateNext = &lBlockD; + lBlockD.rotateNext = &lBlockL; + lBlockL.rotateNext = &lBlock; +} @@ -1,39 +0,0 @@ -!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ -!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ -!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ -!_TAG_PROGRAM_NAME Exuberant Ctags // -!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ -!_TAG_PROGRAM_VERSION 5.8 // -MaxCommands MaxCommands.h 2;" d -Sprite sprites.h /^struct Sprite {$/;" s -ball sprites.h /^const Sprite ball = {$/;" v -blocks sprites.h /^const Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock };$/;" v -buff sprites.h /^ uint8_t buff[8];$/;" m struct:Sprite -checker sprites.h /^const Sprite checker = {$/;" v -height sprites.h /^ uint8_t height;$/;" m struct:Sprite -iBlock sprites.h /^const Sprite iBlock = {$/;" v -jBlock sprites.h /^const Sprite jBlock = {$/;" v -lBlock sprites.h /^const Sprite lBlock = {$/;" v -maxDECODE_MODE MaxCommands.h 15;" d -maxDIGIT_0 MaxCommands.h 7;" d -maxDIGIT_1 MaxCommands.h 8;" d -maxDIGIT_2 MaxCommands.h 9;" d -maxDIGIT_3 MaxCommands.h 10;" d -maxDIGIT_4 MaxCommands.h 11;" d -maxDIGIT_5 MaxCommands.h 12;" d -maxDIGIT_6 MaxCommands.h 13;" d -maxDIGIT_7 MaxCommands.h 14;" d -maxINTENSITY MaxCommands.h 6;" d -maxSCAN_LIMIT MaxCommands.h 16;" d -maxSHUTDOWN_INV MaxCommands.h 4;" d -maxTEST MaxCommands.h 5;" d -miniSmiley sprites.h /^const Sprite miniSmiley = {$/;" v -oBlock sprites.h /^const Sprite oBlock = {$/;" v -sBlock sprites.h /^const Sprite sBlock = {$/;" v -smiley sprites.h /^const Sprite smiley = { $/;" v -sprites sprites.h 2;" d -tBlock sprites.h /^const Sprite tBlock = {$/;" v -width sprites.h /^ uint8_t width;$/;" m struct:Sprite -xOff sprites.h /^ uint8_t xOff;$/;" m struct:Sprite -yOff sprites.h /^ uint8_t yOff;$/;" m struct:Sprite -zBlock sprites.h /^const Sprite zBlock = {$/;" v |