diff options
author | Julian <j@teule.nl> | 2018-11-07 13:34:54 +0100 |
---|---|---|
committer | Julian <j@teule.nl> | 2018-11-07 13:34:54 +0100 |
commit | 449085f880bbe83613cd1d27cd414e24963cb985 (patch) | |
tree | 8a8f18c66909c46a4fb9fa0380a63a3433103758 | |
parent | 2ad2c5239e5c06a628559183a21d0e82c256a3ac (diff) |
Kinda rotating
-rw-r--r-- | Jetris.ino | 78 | ||||
-rw-r--r-- | ] | 62 | ||||
-rw-r--r-- | sprites.h | 46 | ||||
-rw-r--r-- | tags | 10 |
4 files changed, 100 insertions, 96 deletions
@@ -1,6 +1,7 @@ // THings that miss // Functions that return something // Make more safety +// Malloc etc. #include "sprites.h" #include "MaxCommands.h" @@ -12,6 +13,7 @@ #define leftPin 2 #define rightPin 4 #define dropPin 11 +#define rotatePin 10 #define clickTime 200 //Use the drawDot function to draw @@ -35,6 +37,7 @@ void setup() { pinMode(leftPin, INPUT_PULLUP); pinMode(rightPin, INPUT_PULLUP); pinMode(dropPin, INPUT_PULLUP); + pinMode(rotatePin, INPUT_PULLUP); //ButtonInterrupt //attachInterrupt(digitalPinToInterrupt(left), buttonHandle, FALLING); @@ -54,13 +57,13 @@ void setup() { // GAME LOGIC // ///////////////////////////// -Sprite *block; +struct Sprite block; int xPos; int yPos; -long loopTime; +unsigned long loopTime; void initGame() { initBlock(); @@ -75,7 +78,7 @@ void loop() { //Move if(checkCollision(0, 1)) { - drawSprite(buttonLayer, xPos, yPos, block); + drawSprite(buttonLayer, xPos, yPos, &block); renderAll(); initBlock(); }else{ @@ -90,7 +93,7 @@ void initBlock(){ xPos = 0; yPos = 0; - block = blocks[random(7)]; + block = *blocks[random(7)]; } void moveBlock(int xMove, int yMove) { @@ -101,7 +104,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(); @@ -114,13 +117,13 @@ 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] >> xNew) & buttonLayer[i + yNew]) + for(int i = 0; i < block.height; i++) { + if(( block.buff[i + block.yOff] >> xNew - block.xOff ) & 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; @@ -134,7 +137,46 @@ void dropBlock() { } - drawSprite(buttonLayer, xPos, yPos, block); + 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"); + } void renderAll(){ @@ -158,13 +200,14 @@ void renderToSerial() { // Control Handling // ///////////////////////////// -int leftLast, rightLast, dropLast; +unsigned long leftLast, rightLast, dropLast, rotateLast; void handleButtons() { //Read from switch int leftState = !digitalRead(leftPin); int rightState = !digitalRead(rightPin); int dropState = !digitalRead(dropPin); + int rotateState = !digitalRead(rotatePin); //Check if last click was over 100 ms ago if(millis() - leftLast > clickTime && leftState) { @@ -190,6 +233,17 @@ void handleButtons() { dropBlock(); } + + if(millis() - rotateLast > clickTime && rotateState) { + rotateLast = millis(); + + //TODO collision checks + rotateSprite(&block, 1); + + //Redraw moveblock + moveBlock(0, 0); + + } } ///////////////////////////// @@ -216,7 +270,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, Sprite* sprite) { +void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, struct Sprite* sprite) { //Check if in range if (x + sprite->width - 1 > 7 || y + sprite->height - 1> 15 ) return; @@ -224,7 +278,7 @@ void drawSprite(uint8_t layer[], uint8_t x, uint8_t y, Sprite* sprite) { for ( int i = y; i < sprite->height + y; i++) { //Calculate bits uint8_t row = layer[i]; - row |= sprite->buff[i - y] >> x; + row |= sprite->buff[i - y + sprite->yOff] >> x - sprite->xOff; layer[i] = row; @@ -1,62 +0,0 @@ -#include "maxCommands.h" - -#define cs 6 -#define clk 5 -#define dataIn 3 - - -void setup() { - Serial.begin(9600); - - Serial.println("Starting up \n\n\n"); - - pinMode(cs, OUTPUT); - pinMode(clk, OUTPUT); - pinMode(dataIn, OUTPUT); - - - digitalWrite(cs, HIGH); - - writeCommand(maxSHUTDOWN_INV, 1); - - writeCommand(maxINTENSITY, 0x00); - - -} - -void loop() { - - for ( int i = 0; i < 255; i++) { - writeCommand(maxDIGIT_1, i); - - delay(200); - } - -} - - -void writeCommand(uint8_t addr, uint8_t data) { - uint16_t byteToWrite = addr << 8 | data; - - //Set Chip select low - digitalWrite(cs, LOW); - - Serial.print("Writing: "); - for (int i = 0; i < 16; i++) { - bool bitToWrite = ( byteToWrite & 1 << 15 - i ) > 0; - Serial.print( bitToWrite ); - - //Write Data - digitalWrite(dataIn, bitToWrite); - delayMicroseconds(10); - - //Write clock - digitalWrite(clk, HIGH); - - //Wait and go low - delayMicroseconds(100); - digitalWrite(clk, LOW); - } - digitalWrite(cs, HIGH); - Serial.println(); -} @@ -5,6 +5,8 @@ struct Sprite { uint8_t buff[8]; uint8_t width; uint8_t height; + uint8_t xOff; + uint8_t yOff; }; const Sprite smiley = { @@ -17,7 +19,7 @@ const Sprite smiley = { 0b11111100, 0b00000000, 0b00000000 - }, 6, 6 + }, 6, 6, 0, 0 }; const Sprite checker = { @@ -30,7 +32,7 @@ const Sprite checker = { 0b01010101, 0b10101010, 0b01010101 - }, 8, 8 + }, 8, 8, 0, 0 }; const Sprite ball = { @@ -43,7 +45,7 @@ const Sprite ball = { 0b00000000, 0b00000000, 0b00000000 - }, 3, 3 + }, 3, 3, 0, 0 }; const Sprite miniSmiley = { @@ -56,21 +58,21 @@ const Sprite miniSmiley = { 0b00000000, 0b00000000, 0b00000000 - }, 4, 3 + }, 4, 3, 0, 0 }; const Sprite iBlock = { { - 0b11110000, 0b00000000, 0b00000000, 0b00000000, + 0b00011110, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 4, 1 + }, 4, 1, 3, 3 }; const Sprite oBlock = { @@ -83,72 +85,72 @@ const Sprite oBlock = { 0b00000000, 0b00000000, 0b00000000 - }, 2, 2 + }, 2, 2, 0, 0 }; const Sprite tBlock = { { - 0b01000000, - 0b11100000, 0b00000000, + 0b00100000, + 0b01110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 3, 2 + }, 3, 2, 1, 1 }; const Sprite sBlock = { { - 0b01100000, - 0b11000000, 0b00000000, 0b00000000, + 0b00011000, + 0b00110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 3, 2 + }, 3, 2, 2, 2 }; const Sprite zBlock = { { - 0b11000000, - 0b01100000, 0b00000000, + 0b01100000, + 0b00110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 3, 2 + }, 3, 2, 1, 1 }; const Sprite jBlock = { { - 0b10000000, - 0b11100000, 0b00000000, 0b00000000, + 0b001000000, + 0b001110000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 3, 2 + }, 3, 2, 2, 2 }; const Sprite lBlock = { { - 0b00100000, - 0b11100000, 0b00000000, 0b00000000, + 0b00001000, + 0b00111000, 0b00000000, 0b00000000, 0b00000000, 0b00000000 - }, 3, 2 + }, 3, 2, 2, 2 }; const Sprite *blocks[] = { &iBlock, &oBlock, &tBlock, &sBlock, &zBlock, &jBlock, &lBlock }; @@ -7,9 +7,13 @@ 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 @@ -24,6 +28,12 @@ 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 |