diff --git a/Makefile b/Makefile index 83acf0e..08d4b54 100644 --- a/Makefile +++ b/Makefile @@ -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 +$(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 $(CC) -o $@ $(LDFLAGS) $^ $(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt diff --git a/game.c b/game.c new file mode 100644 index 0000000..5894f69 --- /dev/null +++ b/game.c @@ -0,0 +1,67 @@ +#include "stddef.h" +#include "stdint.h" + +#include "game.h" +#include "PontCoopScheduler.h" +#include "shapes.h" +#include "canvas.h" + + +typedef enum { e_idle, e_start, e_newStone, e_down, e_gameOver, e_delay } state_t; + +void gameExec(void *handle) { + static state_t state = e_start; + static uint8_t delay; + + switch (state) { + case e_idle: + break; + + case e_start: + canvasClear(); + state = e_newStone; + break; + + case e_newStone: + stoneCreate(); + if (stoneDraw()) { + state = e_down; + } else { + state = e_gameOver; + } + break; + + case e_down: + if (! stoneMoveDown()) { + state = e_newStone; + } + break; + + case e_gameOver: + for (uint8_t c = 0; c < canvas.width; c++) { + canvasSetPixel(c, 0, 0x0d); + canvasSetPixel(c, canvas.height-1, 0x0d); + } + for (uint8_t r = 0; r < canvas.height; r++) { + canvasSetPixel(0, r, 0x0d); + canvasSetPixel(canvas.width-1, r, 0x0d); + } + delay = 10; + state = e_delay; + break; + + case e_delay: + delay--; + if (delay == 0) { + state = e_start; + } + break; + } + + canvasShow(); +} + +void gameInit() { + schAdd(gameExec, NULL, 0, 1000); +} + diff --git a/game.h b/game.h new file mode 100644 index 0000000..a474984 --- /dev/null +++ b/game.h @@ -0,0 +1,7 @@ +#ifndef _GAME_H_ +#define _GAME_H_ + +void gameInit(); + + +#endif // _GAME_H_ diff --git a/main.c b/main.c index 101ddeb..9116076 100644 --- a/main.c +++ b/main.c @@ -8,7 +8,7 @@ #include "led.h" #include "displayDriver.h" #include "canvas.h" -#include "shapes.h" +#include "game.h" int main() { @@ -29,7 +29,7 @@ int main() { displayDriverInit(); canvasInit(); - shapesInit(); + gameInit(); __enable_interrupt(); diff --git a/shape_i.c b/shape_i.c index 5e65414..ed4d346 100644 --- a/shape_i.c +++ b/shape_i.c @@ -1,26 +1,31 @@ #include "shapes.h" #include "shape_i.h" +#include "canvas.h" uint8_t draw_i() { - return 0; + 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 0; + return 1; } uint8_t moveLeft_i() { - return 0; + return 1; } uint8_t moveRight_i() { - return 0; + return 1; } uint8_t rotateLeft_i() { - return 0; + return 1; } uint8_t rotateRight_i() { - return 0; + return 1; } diff --git a/shapes.c b/shapes.c index bf396ee..f5b0bd8 100644 --- a/shapes.c +++ b/shapes.c @@ -11,15 +11,6 @@ #include "shape_j.h" -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; typedef struct { uint8_t (* draw)(); @@ -42,37 +33,38 @@ const stoneOperations_t stoneOperations[] = { stone_t stone; -uint8_t draw() { +void stoneCreate() { + stone.shape = e_I; + stone.orientation = e_0; + stone.x = 5; + stone.y = 0; +} + +// all of them return 1 in case of success and 0 in case of error +uint8_t stoneDraw() { return stoneOperations[stone.shape].draw(); } -uint8_t moveDown() { +uint8_t stoneMoveDown() { return stoneOperations[stone.shape].moveDown(); } -uint8_t moveLeft() { +uint8_t stoneMoveLeft() { return stoneOperations[stone.shape].moveLeft(); } -uint8_t moveRight() { +uint8_t stoneMoveRight() { return stoneOperations[stone.shape].moveRight(); } -uint8_t rotateLeft() { +uint8_t stoneRotateLeft() { return stoneOperations[stone.shape].rotateLeft(); } -uint8_t rotateRight() { +uint8_t stoneRotateRight() { return stoneOperations[stone.shape].rotateRight(); } -void shapesExec(void *handle) { - draw(); -} - -void shapesInit() { - shapesExec(NULL); -} diff --git a/shapes.h b/shapes.h index 1861010..bc1dbd0 100644 --- a/shapes.h +++ b/shapes.h @@ -1,11 +1,28 @@ #ifndef _SHAPES_H_ #define _SHAPES_H_ +#include + +void stoneCreate(); +uint8_t stoneDraw(); +uint8_t stoneMoveDown(); +uint8_t stoneMoveLeft(); +uint8_t stoneMoveRight(); +uint8_t stoneRotateLeft(); +uint8_t stoneRotateRight(); -void shapesInit(); -void shapesExec(void *handle); +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_