motion table and next orientation table down

This commit is contained in:
Wolfgang Hottgenroth 2024-03-15 10:22:11 +01:00
parent f4b614bf0f
commit 14f6018f5a
18 changed files with 130 additions and 435 deletions

View File

@ -11,7 +11,7 @@ CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o shape_i.o shape_j.o shape_l.o shape_o.o shape_s.o shape_t.o shape_z.o game.o buttons.o
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o game.o buttons.o
$(CC) -o $@ $(LDFLAGS) $^
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt

View File

@ -43,7 +43,7 @@ static uint8_t buttonsRotateRightPressed() {
}
void buttonsExec(void *handle) {
if (stone.shape == e_ShapeInvalid) {
if (! stoneIsValid()) {
// don't do anything, the stone has not been initialized
return;
}

View File

@ -1,137 +0,0 @@
#include "shapes.h"
#include "shape_i.h"
#include "canvas.h"
#include "../rgb-driver/colors.h"
#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 + 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() {
return move(e_MoveDown);
}
uint8_t moveLeft_i() {
return move(e_MoveLeft);
}
uint8_t moveRight_i() {
return move(e_MoveRight);
}
uint8_t rotateLeft_i() {
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 = move(e_RotateRight);
if (res) {
stone.orientation = ((orientation_t[]){ e_90, e_180, e_270, e_0 })[stone.orientation];
}
return res;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_I_H_
#define _SHAPE_I_H_
#include <stdint.h>
uint8_t draw_i();
uint8_t moveDown_i();
uint8_t moveRight_i();
uint8_t moveLeft_i();
uint8_t rotateLeft_i();
uint8_t rotateRight_i();
#endif // _SHAPE_I_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_j.h"
uint8_t draw_j() {
return 0;
}
uint8_t moveDown_j() {
return 0;
}
uint8_t moveLeft_j() {
return 0;
}
uint8_t moveRight_j() {
return 0;
}
uint8_t rotateLeft_j() {
return 0;
}
uint8_t rotateRight_j() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_J_H_
#define _SHAPE_J_H_
#include <stdint.h>
uint8_t draw_j();
uint8_t moveDown_j();
uint8_t moveRight_j();
uint8_t moveLeft_j();
uint8_t rotateLeft_j();
uint8_t rotateRight_j();
#endif // _SHAPE_J_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_l.h"
uint8_t draw_l() {
return 0;
}
uint8_t moveDown_l() {
return 0;
}
uint8_t moveLeft_l() {
return 0;
}
uint8_t moveRight_l() {
return 0;
}
uint8_t rotateLeft_l() {
return 0;
}
uint8_t rotateRight_l() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_L_H_
#define _SHAPE_L_H_
#include <stdint.h>
uint8_t draw_l();
uint8_t moveDown_l();
uint8_t moveRight_l();
uint8_t moveLeft_l();
uint8_t rotateLeft_l();
uint8_t rotateRight_l();
#endif // _SHAPE_L_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_o.h"
uint8_t draw_o() {
return 0;
}
uint8_t moveDown_o() {
return 0;
}
uint8_t moveLeft_o() {
return 0;
}
uint8_t moveRight_o() {
return 0;
}
uint8_t rotateLeft_o() {
return 0;
}
uint8_t rotateRight_o() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_O_H_
#define _SHAPE_O_H_
#include <stdint.h>
uint8_t draw_o();
uint8_t moveDown_o();
uint8_t moveRight_o();
uint8_t moveLeft_o();
uint8_t rotateLeft_o();
uint8_t rotateRight_o();
#endif // _SHAPE_O_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_s.h"
uint8_t draw_s() {
return 0;
}
uint8_t moveDown_s() {
return 0;
}
uint8_t moveLeft_s() {
return 0;
}
uint8_t moveRight_s() {
return 0;
}
uint8_t rotateLeft_s() {
return 0;
}
uint8_t rotateRight_s() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_S_H_
#define _SHAPE_S_H_
#include <stdint.h>
uint8_t draw_s();
uint8_t moveDown_s();
uint8_t moveRight_s();
uint8_t moveLeft_s();
uint8_t rotateLeft_s();
uint8_t rotateRight_s();
#endif // _SHAPE_S_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_t.h"
uint8_t draw_t() {
return 0;
}
uint8_t moveDown_t() {
return 0;
}
uint8_t moveLeft_t() {
return 0;
}
uint8_t moveRight_t() {
return 0;
}
uint8_t rotateLeft_t() {
return 0;
}
uint8_t rotateRight_t() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_T_H_
#define _SHAPE_T_H_
#include <stdint.h>
uint8_t draw_t();
uint8_t moveDown_t();
uint8_t moveRight_t();
uint8_t moveLeft_t();
uint8_t rotateLeft_t();
uint8_t rotateRight_t();
#endif // _SHAPE_T_H_

View File

@ -1,26 +0,0 @@
#include "shapes.h"
#include "shape_z.h"
uint8_t draw_z() {
return 0;
}
uint8_t moveDown_z() {
return 0;
}
uint8_t moveLeft_z() {
return 0;
}
uint8_t moveRight_z() {
return 0;
}
uint8_t rotateLeft_z() {
return 0;
}
uint8_t rotateRight_z() {
return 0;
}

View File

@ -1,14 +0,0 @@
#ifndef _SHAPE_Z_H_
#define _SHAPE_Z_H_
#include <stdint.h>
uint8_t draw_z();
uint8_t moveDown_z();
uint8_t moveRight_z();
uint8_t moveLeft_z();
uint8_t rotateLeft_z();
uint8_t rotateRight_z();
#endif // _SHAPE_Z_H_

View File

@ -2,33 +2,95 @@
#include <stddef.h>
#include "shapes.h"
#include "shape_i.h"
#include "shape_o.h"
#include "shape_t.h"
#include "shape_z.h"
#include "shape_s.h"
#include "shape_l.h"
#include "shape_j.h"
#include "canvas.h"
#include "../rgb-driver/colors.h"
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, e_Keep } orientation_t;
typedef struct {
uint8_t (* draw)();
uint8_t (*moveDown)();
uint8_t (*moveLeft)();
uint8_t (*moveRight)();
uint8_t (*rotateLeft)();
uint8_t (*rotateRight)();
} stoneOperations_t;
shape_t shape;
orientation_t orientation;
uint8_t x; // column
uint8_t y; // row
} stone_t;
const stoneOperations_t stoneOperations[] = {
{ .draw = draw_i, .moveDown = moveDown_i, .moveLeft = moveLeft_i, .moveRight = moveRight_i, .rotateLeft = rotateLeft_i, .rotateRight = rotateRight_i },
{ .draw = draw_o, .moveDown = moveDown_o, .moveLeft = moveLeft_o, .moveRight = moveRight_o, .rotateLeft = rotateLeft_o, .rotateRight = rotateRight_o },
{ .draw = draw_t, .moveDown = moveDown_t, .moveLeft = moveLeft_t, .moveRight = moveRight_t, .rotateLeft = rotateLeft_t, .rotateRight = rotateRight_t },
{ .draw = draw_z, .moveDown = moveDown_z, .moveLeft = moveLeft_z, .moveRight = moveRight_z, .rotateLeft = rotateLeft_z, .rotateRight = rotateRight_z },
{ .draw = draw_s, .moveDown = moveDown_s, .moveLeft = moveLeft_s, .moveRight = moveRight_s, .rotateLeft = rotateLeft_s, .rotateRight = rotateRight_s },
{ .draw = draw_l, .moveDown = moveDown_l, .moveLeft = moveLeft_l, .moveRight = moveRight_l, .rotateLeft = rotateLeft_l, .rotateRight = rotateRight_l },
{ .draw = draw_j, .moveDown = moveDown_j, .moveLeft = moveLeft_j, .moveRight = moveRight_j, .rotateLeft = rotateLeft_j, .rotateRight = rotateRight_j }
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
},
}
}
};
const orientation_t nextOrientation[5][4] = { // 5 = number of directions to move, 4 = number of orientation a stone can have
{ e_Keep, e_Keep, e_Keep, e_Keep }, // move down, current orientation: 0, 90, 180, 270
{ e_Keep, e_Keep, e_Keep, e_Keep }, // move left
{ e_Keep, e_Keep, e_Keep, e_Keep }, // move right
{ e_270, e_0, e_90, e_180 }, // rotate left
{ e_90, e_180, e_270, e_0 } // rotate right
};
stone_t stone;
@ -44,29 +106,66 @@ void stoneCreate() {
stone.y = 0;
}
uint8_t stoneIsValid() {
return stone.shape != e_ShapeInvalid;
}
// all of them return 1 in case of success and 0 in case of error
static 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;
stone.orientation = (nextOrientation[direction][stone.orientation] == e_Keep) ? stone.orientation : nextOrientation[direction][stone.orientation];
res = 1;
}
return res;
}
uint8_t stoneDraw() {
return stoneOperations[stone.shape].draw();
uint8_t res = 0;
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 stoneMoveDown() {
return stoneOperations[stone.shape].moveDown();
return move(e_MoveDown);
}
uint8_t stoneMoveLeft() {
return stoneOperations[stone.shape].moveLeft();
return move(e_MoveLeft);
}
uint8_t stoneMoveRight() {
return stoneOperations[stone.shape].moveRight();
return move(e_MoveRight);
}
uint8_t stoneRotateLeft() {
return stoneOperations[stone.shape].rotateLeft();
return move(e_RotateLeft);
}
uint8_t stoneRotateRight() {
return stoneOperations[stone.shape].rotateRight();
return move(e_RotateRight);
}

View File

@ -5,6 +5,7 @@
void shapesInit();
void stoneCreate();
uint8_t stoneIsValid();
uint8_t stoneDraw();
uint8_t stoneMoveDown();
uint8_t stoneMoveLeft();
@ -13,18 +14,4 @@ uint8_t stoneRotateLeft();
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 {
shape_t shape;
orientation_t orientation;
uint8_t x; // column
uint8_t y; // row
} stone_t;
extern stone_t stone;
#endif // _SHAPES_H_