From f4b614bf0f52e6e4f3df36061a23126eaaaba824 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 14 Mar 2024 17:25:45 +0100 Subject: [PATCH] first motion table --- game-ctrl/buttons.c | 29 ++-- game-ctrl/game.c | 2 +- game-ctrl/shape_i.c | 351 +++++++++++++------------------------------- game-ctrl/shapes.h | 1 + 4 files changed, 117 insertions(+), 266 deletions(-) diff --git a/game-ctrl/buttons.c b/game-ctrl/buttons.c index 7902e71..920fba4 100644 --- a/game-ctrl/buttons.c +++ b/game-ctrl/buttons.c @@ -15,10 +15,10 @@ uint16_t counter; static uint8_t buttonsMoveLeftPressed() { // ----------------------- // TEST CODE - if (counter == 25) { - ledGreenToggle(); - return 1; - } + //if (counter == 25) { + // ledGreenToggle(); + // return 1; + //} // ----------------------- return 0; } @@ -28,24 +28,17 @@ static uint8_t buttonsMoveRightPressed() { } static uint8_t buttonsRotateLeftPressed() { + // ----------------------- + // TEST CODE + if (counter == 35) { + ledGreenToggle(); + return 1; + } + // ----------------------- return 0; } static uint8_t buttonsRotateRightPressed() { - // ----------------------- - // TEST CODE - if (counter == 40) { - ledGreenToggle(); - return 1; - } - // ----------------------- - // ----------------------- - // TEST CODE - if (counter == 80) { - ledGreenToggle(); - return 1; - } - // ----------------------- return 0; } diff --git a/game-ctrl/game.c b/game-ctrl/game.c index 7be9739..5894f69 100644 --- a/game-ctrl/game.c +++ b/game-ctrl/game.c @@ -62,6 +62,6 @@ void gameExec(void *handle) { } void gameInit() { - schAdd(gameExec, NULL, 0, 5000); + schAdd(gameExec, NULL, 0, 1000); } diff --git a/game-ctrl/shape_i.c b/game-ctrl/shape_i.c index ccae4a4..bcd45e2 100644 --- a/game-ctrl/shape_i.c +++ b/game-ctrl/shape_i.c @@ -6,275 +6,132 @@ #define COLOR _cyan +typedef struct { + uint8_t x; + uint8_t y; +} pixel_t; + +typedef struct { + int8_t x; + int8_t y; +} offset_t; + +typedef struct { + offset_t set[4]; + offset_t reset[4]; + offset_t offset; +} motion_t; + +typedef struct { + uint8_t color; + pixel_t draw[4]; + motion_t motion[5][4]; +} motionTable_t; + +const motionTable_t motions[1] = { + { // i + .color = _cyan, + .draw = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, + .motion = { + { + // move down + { .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 0 + { .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90 + { .set = { { 0, 4}, { 0, 4}, { 0, 4}, { 0, 4} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 1} }, // 180 + { .set = { { 0, 1}, { 1, 1}, { 2, 1}, { 3, 1} }, .reset = { { 0, 0}, { 1, 0}, { 2, 0}, { 3, 0} }, .offset = { 0, 1} } // 270 + }, + { + // move left + { .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 0 + { .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 90 + { .set = { {-1, 0}, {-1, 1}, {-1, 2}, {-1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = {-1, 0} }, // 180 + { .set = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = { 0, 1} }, // 270 + }, + { + // move right + { .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 0 + { .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 90 + { .set = { { 1, 0}, { 1, 1}, { 1, 2}, { 1, 3} }, .reset = { { 0, 0}, { 0, 1}, { 0, 2}, { 0, 3} }, .offset = { 1, 0} }, // 180 + { .set = { { 4, 0}, { 4, 0}, { 4, 0}, { 4, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 1, 0} }, // 270 + }, + { + // rotate left + { .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 0 + { .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 90 + { .set = { {-1, 1}, { 1, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-1, 1} }, // 180 + { .set = { { 1,-2}, { 1,-1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-2} }, // 270 + }, + { + // rotate right + { .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 0 + { .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 90 + { .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 180 + { .set = { { 2,-2}, { 2,-1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 1, 0}, { 3, 0}, { 3, 0} }, .offset = { 2,-2} }, // 270 + }, + } + } +}; + uint8_t draw_i() { uint8_t res = 0; - if (canvasIsPixelFree(stone.x, stone.y) && - canvasIsPixelFree(stone.x, stone.y+1) && - canvasIsPixelFree(stone.x, stone.y+2) && - canvasIsPixelFree(stone.x, stone.y+3)) { - canvasSetPixel(stone.x, stone.y, COLOR); - canvasSetPixel(stone.x, stone.y+1, COLOR); - canvasSetPixel(stone.x, stone.y+2, COLOR); - canvasSetPixel(stone.x, stone.y+3, COLOR); + if (canvasIsPixelFree(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y)) { + canvasSetPixel(stone.x + motions[stone.shape].draw[0].x, stone.y + motions[stone.shape].draw[0].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].draw[1].x, stone.y + motions[stone.shape].draw[1].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].draw[2].x, stone.y + motions[stone.shape].draw[2].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].draw[3].x, stone.y + motions[stone.shape].draw[3].y, motions[stone.shape].color); + res = 1; + } + return res; +} + +uint8_t move(direction_t direction) { + uint8_t res = 0; + if (canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y) && + canvasIsPixelFree(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y)) { + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[0].y, _off); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[1].y, _off); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[2].y, _off); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].reset[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].reset[3].y, _off); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[0].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[0].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[1].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[1].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[2].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[2].y, motions[stone.shape].color); + canvasSetPixel(stone.x + motions[stone.shape].motion[direction][stone.orientation].set[3].x, stone.y + motions[stone.shape].motion[direction][stone.orientation].set[3].y, motions[stone.shape].color); + stone.x += motions[stone.shape].motion[direction][stone.orientation].offset.x; + stone.y += motions[stone.shape].motion[direction][stone.orientation].offset.y; res = 1; } return res; } uint8_t moveDown_i() { - uint8_t res = 0; - switch (stone.orientation) { - case e_0: - case e_180: - // check whether the destination pixel to the bottom are free - if (canvasIsPixelFree(stone.x, stone.y+4)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x, stone.y+4, COLOR); - // update the coordinates - stone.y += 1; - // positive result - res = 1; - } - break; - case e_90: - case e_270: - // check whether the destination pixel to the bottom are free - if (canvasIsPixelFree(stone.x, stone.y+1) && - canvasIsPixelFree(stone.x+1, stone.y+1) && - canvasIsPixelFree(stone.x+2, stone.y+1) && - canvasIsPixelFree(stone.x+3, stone.y+1)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x+1, stone.y, _off); - canvasSetPixel(stone.x+2, stone.y, _off); - canvasSetPixel(stone.x+3, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x, stone.y+1, COLOR); - canvasSetPixel(stone.x+1, stone.y+1, COLOR); - canvasSetPixel(stone.x+2, stone.y+1, COLOR); - canvasSetPixel(stone.x+3, stone.y+1, COLOR); - // update the coordinates - stone.y += 1; - // positive result - res = 1; - } - break; - } - return res; + return move(e_MoveDown); } uint8_t moveLeft_i() { - uint8_t res = 0; - switch (stone.orientation) { - case e_0: - case e_180: - // check whether the destination pixel to the bottom are free - if ((stone.x > 0) && - canvasIsPixelFree(stone.x-1, stone.y) && - canvasIsPixelFree(stone.x-1, stone.y+1) && - canvasIsPixelFree(stone.x-1, stone.y+2) && - canvasIsPixelFree(stone.x-1, stone.y+3)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x, stone.y+1, _off); - canvasSetPixel(stone.x, stone.y+2, _off); - canvasSetPixel(stone.x, stone.y+3, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x-1, stone.y, COLOR); - canvasSetPixel(stone.x-1, stone.y+1, COLOR); - canvasSetPixel(stone.x-1, stone.y+2, COLOR); - canvasSetPixel(stone.x-1, stone.y+3, COLOR); - // update the coordinates - stone.x -= 1; - // positive result - res = 1; - } - break; - case e_90: - case e_270: - // check whether the destination pixel to the bottom are free - if ((stone.x > 0) && - canvasIsPixelFree(stone.x-1, stone.y)) { - // clear the pixel we move away from - canvasSetPixel(stone.x+3, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x-1, stone.y, COLOR); - // update the coordinates - stone.x -= 1; - // positive result - res = 1; - } - break; - } - return res; + return move(e_MoveLeft); } uint8_t moveRight_i() { - uint8_t res = 0; - switch (stone.orientation) { - case e_0: - case e_180: - // check whether the destination pixel to the bottom are free - if (canvasIsPixelFree(stone.x+1, stone.y) && - canvasIsPixelFree(stone.x+1, stone.y+1) && - canvasIsPixelFree(stone.x+1, stone.y+2) && - canvasIsPixelFree(stone.x+1, stone.y+3)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x, stone.y+1, _off); - canvasSetPixel(stone.x, stone.y+2, _off); - canvasSetPixel(stone.x, stone.y+3, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x+1, stone.y, COLOR); - canvasSetPixel(stone.x+1, stone.y+1, COLOR); - canvasSetPixel(stone.x+1, stone.y+2, COLOR); - canvasSetPixel(stone.x+1, stone.y+3, COLOR); - // update the coordinates - stone.x += 1; - // positive result - res = 1; - } - break; - case e_90: - case e_270: - // check whether the destination pixel to the bottom are free - if (canvasIsPixelFree(stone.x+4, stone.y)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x+4, stone.y, COLOR); - // update the coordinates - stone.x += 1; - // positive result - res = 1; - } - break; - } - return res; + return move(e_MoveRight); } uint8_t rotateLeft_i() { - uint8_t res = 0; - switch (stone.orientation) { - case e_0: - case e_180: - // check whether the destination pixel to the bottom are free - if (stone.x > 0 && - canvasIsPixelFree(stone.x-1, stone.y+1) && - canvasIsPixelFree(stone.x+1, stone.y+1) && - canvasIsPixelFree(stone.x+2, stone.y+1)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x, stone.y+1, _off); - canvasSetPixel(stone.x, stone.y+2, _off); - canvasSetPixel(stone.x, stone.y+3, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x-1, stone.y+1, COLOR); - canvasSetPixel(stone.x, stone.y+1, COLOR); - canvasSetPixel(stone.x+1, stone.y+1, COLOR); - canvasSetPixel(stone.x+2, stone.y+1, COLOR); - // update the coordinates - stone.x -= 1; - stone.y += 1; - if (stone.orientation == e_0) { - stone.orientation = e_270; - } else { - stone.orientation = e_90; - } - // positive result - res = 1; - } - break; - case e_90: - case e_270: - // check whether the destination pixel to the bottom are free - if (stone.y > 1 && - canvasIsPixelFree(stone.x+1, stone.y-2) && - canvasIsPixelFree(stone.x+1, stone.y-1) && - canvasIsPixelFree(stone.x+1, stone.y+1)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x+2, stone.y, _off); - canvasSetPixel(stone.x+3, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x+1, stone.y-2, COLOR); - canvasSetPixel(stone.x+1, stone.y-1, COLOR); - canvasSetPixel(stone.x+1, stone.y+1, COLOR); - // update the coordinates - stone.x += 1; - stone.y -= 2; - if (stone.orientation == e_90) { - stone.orientation = e_0; - } else { - stone.orientation = e_180; - } - // positive result - res = 1; - } - break; + uint8_t res = move(e_RotateLeft); + if (res) { + stone.orientation = ((orientation_t[]){ e_270, e_0, e_90, e_180 })[stone.orientation]; } return res; } uint8_t rotateRight_i() { - uint8_t res = 0; - switch (stone.orientation) { - case e_0: - case e_180: - // check whether the destination pixel to the bottom are free - if (stone.x > 1 && - canvasIsPixelFree(stone.x-2, stone.y+1) && - canvasIsPixelFree(stone.x-1, stone.y+1) && - canvasIsPixelFree(stone.x+1, stone.y+1)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x, stone.y+2, _off); - canvasSetPixel(stone.x, stone.y+3, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x-2, stone.y+1, COLOR); - canvasSetPixel(stone.x-1, stone.y+1, COLOR); - canvasSetPixel(stone.x+1, stone.y+1, COLOR); - // update the coordinates - stone.x -= 2; - stone.y += 1; - if (stone.orientation == e_0) { - stone.orientation = e_270; - } else { - stone.orientation = e_90; - } - // positive result - res = 1; - } - break; - case e_90: - case e_270: - // check whether the destination pixel to the bottom are free - if (stone.y > 1 && - canvasIsPixelFree(stone.x+2, stone.y-2) && - canvasIsPixelFree(stone.x+2, stone.y-1) && - canvasIsPixelFree(stone.x+2, stone.y+1)) { - // clear the pixel we move away from - canvasSetPixel(stone.x, stone.y, _off); - canvasSetPixel(stone.x+1, stone.y, _off); - canvasSetPixel(stone.x+3, stone.y, _off); - // set the pixel we are moving into - canvasSetPixel(stone.x+2, stone.y-2, COLOR); - canvasSetPixel(stone.x+2, stone.y-1, COLOR); - canvasSetPixel(stone.x+2, stone.y+1, COLOR); - // update the coordinates - stone.x += 2; - stone.y -= 2; - if (stone.orientation == e_90) { - stone.orientation = e_0; - } else { - stone.orientation = e_180; - } - // positive result - res = 1; - } - break; + uint8_t res = move(e_RotateRight); + if (res) { + stone.orientation = ((orientation_t[]){ e_90, e_180, e_270, e_0 })[stone.orientation]; } return res; } diff --git a/game-ctrl/shapes.h b/game-ctrl/shapes.h index 46b9f82..f96c959 100644 --- a/game-ctrl/shapes.h +++ b/game-ctrl/shapes.h @@ -14,6 +14,7 @@ uint8_t stoneRotateRight(); typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeInvalid } shape_t; +typedef enum { e_MoveDown, e_MoveLeft, e_MoveRight, e_RotateLeft, e_RotateRight } direction_t; typedef enum { e_0, e_90, e_180, e_270 } orientation_t; typedef struct {