Compare commits
4 Commits
1959a3f578
...
game_ctrl_
Author | SHA1 | Date | |
---|---|---|---|
007548efbb | |||
14f6018f5a | |||
f4b614bf0f | |||
a7ea698f9b |
@ -11,7 +11,7 @@ CFLAGS=-Wall -mmcu=$(MCU) -std=gnu99 -I $(TOOLCHAIN_PREFIX)/include -O1 -g0
|
|||||||
|
|
||||||
LDFLAGS=-mmcu=$(MCU) -L $(TOOLCHAIN_PREFIX)/include
|
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
|
$(ARTIFACT).elf: main.o led.o time.o PontCoopScheduler.o displayDriver.o canvas.o shapes.o game.o buttons.o
|
||||||
$(CC) -o $@ $(LDFLAGS) $^
|
$(CC) -o $@ $(LDFLAGS) $^
|
||||||
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt
|
||||||
|
|
||||||
|
98
game-ctrl/buttons.c
Normal file
98
game-ctrl/buttons.c
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
#include "stddef.h"
|
||||||
|
#include "stdint.h"
|
||||||
|
|
||||||
|
#include "buttons.h"
|
||||||
|
#include "PontCoopScheduler.h"
|
||||||
|
#include "shapes.h"
|
||||||
|
#include "canvas.h"
|
||||||
|
#include "led.h"
|
||||||
|
|
||||||
|
|
||||||
|
// TEST CODE
|
||||||
|
uint16_t counter;
|
||||||
|
|
||||||
|
|
||||||
|
static uint8_t buttonsMoveLeftPressed() {
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
//if (counter == 95) {
|
||||||
|
// ledGreenToggle();
|
||||||
|
// return 1;
|
||||||
|
//}
|
||||||
|
// -----------------------
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsMoveRightPressed() {
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
if (counter == 95) {
|
||||||
|
ledGreenToggle();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// -----------------------
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsRotateLeftPressed() {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t buttonsRotateRightPressed() {
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
if (counter == 35) {
|
||||||
|
ledGreenToggle();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// -----------------------
|
||||||
|
// -----------------------
|
||||||
|
// TEST CODE
|
||||||
|
if (counter == 45) {
|
||||||
|
ledGreenToggle();
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
// -----------------------
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonsExec(void *handle) {
|
||||||
|
if (! stoneIsValid()) {
|
||||||
|
// don't do anything, the stone has not been initialized
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TEST CODE
|
||||||
|
counter++;
|
||||||
|
|
||||||
|
uint8_t buttonPressed = 0;
|
||||||
|
|
||||||
|
if (buttonsMoveLeftPressed()) {
|
||||||
|
stoneMoveLeft();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsMoveRightPressed()) {
|
||||||
|
stoneMoveRight();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsRotateLeftPressed()) {
|
||||||
|
stoneRotateLeft();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
if (buttonsRotateRightPressed()) {
|
||||||
|
stoneRotateRight();
|
||||||
|
buttonPressed = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (buttonPressed == 1) {
|
||||||
|
canvasShow();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void buttonsInit() {
|
||||||
|
// TEST CODE
|
||||||
|
counter = 0;
|
||||||
|
|
||||||
|
schAdd(buttonsExec, NULL, 0, 100);
|
||||||
|
}
|
||||||
|
|
7
game-ctrl/buttons.h
Normal file
7
game-ctrl/buttons.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#ifndef _BUTTONS_H_
|
||||||
|
#define _BUTTONS_H_
|
||||||
|
|
||||||
|
void buttonsInit();
|
||||||
|
|
||||||
|
|
||||||
|
#endif // _BUTTONS_H_
|
@ -53,3 +53,9 @@ uint8_t canvasIsRowFilled(uint8_t row) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint8_t canvasIsPixelFree(uint8_t column, uint8_t row) {
|
||||||
|
return (*((canvas.canvas) + (row * canvas.width + column)) == 0) &&
|
||||||
|
(column < canvas.width) &&
|
||||||
|
(row < canvas.height);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@ void canvasClear();
|
|||||||
void canvasSetAll(uint8_t color);
|
void canvasSetAll(uint8_t color);
|
||||||
void canvasShow();
|
void canvasShow();
|
||||||
void canvasSetPixel(uint8_t column, uint8_t row, uint8_t color);
|
void canvasSetPixel(uint8_t column, uint8_t row, uint8_t color);
|
||||||
|
uint8_t canvasIsPixelFree(uint8_t column, uint8_t row);
|
||||||
void canvasWipeRow(uint8_t row);
|
void canvasWipeRow(uint8_t row);
|
||||||
uint8_t canvasIsRowFilled(uint8_t row);
|
uint8_t canvasIsRowFilled(uint8_t row);
|
||||||
|
|
||||||
|
@ -1,22 +0,0 @@
|
|||||||
#ifndef _COLORS_H_
|
|
||||||
#define _COLORS_H_
|
|
||||||
|
|
||||||
|
|
||||||
#define _off 0x00
|
|
||||||
#define _blue 0x01
|
|
||||||
#define _green 0x02
|
|
||||||
#define _orange 0x03
|
|
||||||
#define _rose 0x04
|
|
||||||
#define _magenta 0x05
|
|
||||||
#define _violet 0x06
|
|
||||||
#define _azure 0x07
|
|
||||||
#define _cyan 0x08
|
|
||||||
#define _springgreen 0x09
|
|
||||||
#define _chartreuse 0x0a
|
|
||||||
#define _yellow 0x0b
|
|
||||||
#define _white 0x0c
|
|
||||||
#define _red 0x0d
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
|
@ -9,6 +9,8 @@
|
|||||||
#include "displayDriver.h"
|
#include "displayDriver.h"
|
||||||
#include "canvas.h"
|
#include "canvas.h"
|
||||||
#include "game.h"
|
#include "game.h"
|
||||||
|
#include "buttons.h"
|
||||||
|
#include "shapes.h"
|
||||||
|
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
@ -29,7 +31,9 @@ int main() {
|
|||||||
displayDriverInit();
|
displayDriverInit();
|
||||||
canvasInit();
|
canvasInit();
|
||||||
|
|
||||||
|
shapesInit();
|
||||||
gameInit();
|
gameInit();
|
||||||
|
buttonsInit();
|
||||||
|
|
||||||
__enable_interrupt();
|
__enable_interrupt();
|
||||||
|
|
||||||
|
@ -1,31 +0,0 @@
|
|||||||
#include "shapes.h"
|
|
||||||
#include "shape_i.h"
|
|
||||||
#include "canvas.h"
|
|
||||||
|
|
||||||
uint8_t draw_i() {
|
|
||||||
canvasSetPixel(stone.x, stone.y, 0x01);
|
|
||||||
canvasSetPixel(stone.x, stone.y+1, 0x01);
|
|
||||||
canvasSetPixel(stone.x, stone.y+2, 0x01);
|
|
||||||
canvasSetPixel(stone.x, stone.y+3, 0x01);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t moveDown_i() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t moveLeft_i() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t moveRight_i() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t rotateLeft_i() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t rotateRight_i() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -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;
|
|
||||||
}
|
|
@ -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_
|
|
@ -2,67 +2,221 @@
|
|||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#include "shapes.h"
|
#include "shapes.h"
|
||||||
#include "shape_i.h"
|
#include "canvas.h"
|
||||||
#include "shape_o.h"
|
#include "../rgb-driver/colors.h"
|
||||||
#include "shape_t.h"
|
|
||||||
#include "shape_z.h"
|
|
||||||
#include "shape_s.h"
|
|
||||||
#include "shape_l.h"
|
|
||||||
#include "shape_j.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 {
|
typedef struct {
|
||||||
uint8_t (* draw)();
|
shape_t shape;
|
||||||
uint8_t (*moveDown)();
|
orientation_t orientation;
|
||||||
uint8_t (*moveLeft)();
|
uint8_t x; // column
|
||||||
uint8_t (*moveRight)();
|
uint8_t y; // row
|
||||||
uint8_t (*rotateLeft)();
|
} stone_t;
|
||||||
uint8_t (*rotateRight)();
|
|
||||||
} stoneOperations_t;
|
|
||||||
|
|
||||||
const stoneOperations_t stoneOperations[] = {
|
typedef struct {
|
||||||
{ .draw = draw_i, .moveDown = moveDown_i, .moveLeft = moveLeft_i, .moveRight = moveRight_i, .rotateLeft = rotateLeft_i, .rotateRight = rotateRight_i },
|
uint8_t x;
|
||||||
{ .draw = draw_o, .moveDown = moveDown_o, .moveLeft = moveLeft_o, .moveRight = moveRight_o, .rotateLeft = rotateLeft_o, .rotateRight = rotateRight_o },
|
uint8_t y;
|
||||||
{ .draw = draw_t, .moveDown = moveDown_t, .moveLeft = moveLeft_t, .moveRight = moveRight_t, .rotateLeft = rotateLeft_t, .rotateRight = rotateRight_t },
|
} pixel_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 },
|
typedef struct {
|
||||||
{ .draw = draw_l, .moveDown = moveDown_l, .moveLeft = moveLeft_l, .moveRight = moveRight_l, .rotateLeft = rotateLeft_l, .rotateRight = rotateRight_l },
|
int8_t x;
|
||||||
{ .draw = draw_j, .moveDown = moveDown_j, .moveLeft = moveLeft_j, .moveRight = moveRight_j, .rotateLeft = rotateLeft_j, .rotateRight = rotateRight_j }
|
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;
|
||||||
|
uint8_t nullRotation;
|
||||||
|
pixel_t draw[4];
|
||||||
|
motion_t motion[5][4];
|
||||||
|
} motionTable_t;
|
||||||
|
|
||||||
|
const motionTable_t motions[2] = { // 2 = number of implemented stones
|
||||||
|
{ // I
|
||||||
|
.color = _cyan,
|
||||||
|
.nullRotation = 0,
|
||||||
|
.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 = {-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 = { {-1, 0}, {-1, 0}, {-1, 0}, {-1, 0} }, .reset = { { 3, 0}, { 3, 0}, { 3, 0}, { 3, 0} }, .offset = {-1, 0} }, // 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 = { { 1,-1}, { 1, 1}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-1} }, // 90
|
||||||
|
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 180
|
||||||
|
{ .set = { { 1,-1}, { 1, 1}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-1} }, // 270
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{ // O
|
||||||
|
.color = _yellow,
|
||||||
|
.nullRotation = 1,
|
||||||
|
.draw = { { 0, 0}, { 0, 1}, { 1, 0}, { 1, 1} },
|
||||||
|
.motion = {
|
||||||
|
{
|
||||||
|
// move down
|
||||||
|
{ .set = { { 0, 2}, { 1, 2}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 1, 0}, { 1, 0}, { 1, 0} }, .offset = { 0, 1} }, // 0
|
||||||
|
{ .set = { { 0, 2}, { 1, 2}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 1, 0}, { 1, 0}, { 1, 0} }, .offset = { 0, 1} }, // 90
|
||||||
|
{ .set = { { 0, 2}, { 1, 2}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 1, 0}, { 1, 0}, { 1, 0} }, .offset = { 0, 1} }, // 180
|
||||||
|
{ .set = { { 0, 2}, { 1, 2}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 1, 0}, { 1, 0}, { 1, 0} }, .offset = { 0, 1} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// move left
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 1}, {-1, 1} }, .reset = { { 1, 0}, { 1, 1}, { 1, 1}, { 1, 1} }, .offset = {-1, 0} }, // 0
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 1}, {-1, 1} }, .reset = { { 1, 0}, { 1, 1}, { 1, 1}, { 1, 1} }, .offset = {-1, 0} }, // 90
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 1}, {-1, 1} }, .reset = { { 1, 0}, { 1, 1}, { 1, 1}, { 1, 1} }, .offset = {-1, 0} }, // 180
|
||||||
|
{ .set = { {-1, 0}, {-1, 1}, {-1, 1}, {-1, 1} }, .reset = { { 1, 0}, { 1, 1}, { 1, 1}, { 1, 1} }, .offset = {-1, 0} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// move right
|
||||||
|
{ .set = { { 2, 0}, { 2, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 1}, { 0, 1}, { 0, 1} }, .offset = { 1, 0} }, // 0
|
||||||
|
{ .set = { { 2, 0}, { 2, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 1}, { 0, 1}, { 0, 1} }, .offset = { 1, 0} }, // 90
|
||||||
|
{ .set = { { 2, 0}, { 2, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 1}, { 0, 1}, { 0, 1} }, .offset = { 1, 0} }, // 180
|
||||||
|
{ .set = { { 2, 0}, { 2, 1}, { 2, 1}, { 2, 1} }, .reset = { { 0, 0}, { 0, 1}, { 0, 1}, { 0, 1} }, .offset = { 1, 0} }, // 270
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// rotate left
|
||||||
|
{ .set = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 0} }, // 0
|
||||||
|
{ .set = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 0} }, // 90
|
||||||
|
{ .set = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 0} }, // 180
|
||||||
|
{ .set = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .reset = { { 0, 0}, { 0, 0}, { 0, 0}, { 0, 0} }, .offset = { 0, 0} }, // 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 = { { 1,-1}, { 1, 1}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-1} }, // 90
|
||||||
|
{ .set = { {-2, 1}, {-1, 1}, { 1, 1}, { 1, 1} }, .reset = { { 0, 0}, { 0, 2}, { 0, 3}, { 0, 3} }, .offset = {-2, 1} }, // 180
|
||||||
|
{ .set = { { 1,-1}, { 1, 1}, { 1, 2}, { 1, 2} }, .reset = { { 0, 0}, { 2, 0}, { 3, 0}, { 3, 0} }, .offset = { 1,-1} }, // 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;
|
stone_t stone;
|
||||||
|
|
||||||
|
void shapesInit() {
|
||||||
|
stone.shape = e_ShapeInvalid;
|
||||||
|
}
|
||||||
|
|
||||||
void stoneCreate() {
|
void stoneCreate() {
|
||||||
stone.shape = e_I;
|
static uint8_t cnt = 0;
|
||||||
|
stone.shape = ((shape_t[]){ e_I, e_O, e_T, e_Z, e_S, e_L, e_J })[cnt];
|
||||||
|
cnt++;
|
||||||
|
if (cnt > 1) {
|
||||||
|
cnt = 0;
|
||||||
|
}
|
||||||
stone.orientation = e_0;
|
stone.orientation = e_0;
|
||||||
stone.x = 5;
|
stone.x = 5;
|
||||||
stone.y = 0;
|
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
|
// all of them return 1 in case of success and 0 in case of error
|
||||||
|
static uint8_t move(direction_t direction) {
|
||||||
|
if (motions[stone.shape].nullRotation && (direction == e_RotateLeft || direction == e_RotateRight)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
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];
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
uint8_t stoneDraw() {
|
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() {
|
uint8_t stoneMoveDown() {
|
||||||
return stoneOperations[stone.shape].moveDown();
|
return move(e_MoveDown);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t stoneMoveLeft() {
|
uint8_t stoneMoveLeft() {
|
||||||
return stoneOperations[stone.shape].moveLeft();
|
return move(e_MoveLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t stoneMoveRight() {
|
uint8_t stoneMoveRight() {
|
||||||
return stoneOperations[stone.shape].moveRight();
|
return move(e_MoveRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t stoneRotateLeft() {
|
uint8_t stoneRotateLeft() {
|
||||||
return stoneOperations[stone.shape].rotateLeft();
|
return move(e_RotateLeft);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint8_t stoneRotateRight() {
|
uint8_t stoneRotateRight() {
|
||||||
return stoneOperations[stone.shape].rotateRight();
|
return move(e_RotateRight);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,7 +3,9 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
|
void shapesInit();
|
||||||
void stoneCreate();
|
void stoneCreate();
|
||||||
|
uint8_t stoneIsValid();
|
||||||
uint8_t stoneDraw();
|
uint8_t stoneDraw();
|
||||||
uint8_t stoneMoveDown();
|
uint8_t stoneMoveDown();
|
||||||
uint8_t stoneMoveLeft();
|
uint8_t stoneMoveLeft();
|
||||||
@ -12,17 +14,4 @@ uint8_t stoneRotateLeft();
|
|||||||
uint8_t stoneRotateRight();
|
uint8_t stoneRotateRight();
|
||||||
|
|
||||||
|
|
||||||
typedef enum { e_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeEnd } shape_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_
|
#endif // _SHAPES_H_
|
||||||
|
Reference in New Issue
Block a user