move and rotate of i

This commit is contained in:
Wolfgang Hottgenroth 2024-03-14 14:46:31 +01:00
parent 1959a3f578
commit a7ea698f9b
11 changed files with 376 additions and 35 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
$(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
$(CC) -o $@ $(LDFLAGS) $^
$(OBJDUMP) -D $(ARTIFACT).elf > $(ARTIFACT).txt

91
game-ctrl/buttons.c Normal file
View File

@ -0,0 +1,91 @@
#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 == 25) {
ledGreenToggle();
return 1;
}
// -----------------------
return 0;
}
static uint8_t buttonsMoveRightPressed() {
return 0;
}
static uint8_t buttonsRotateLeftPressed() {
return 0;
}
static uint8_t buttonsRotateRightPressed() {
// -----------------------
// TEST CODE
if (counter == 40) {
ledGreenToggle();
return 1;
}
// -----------------------
// -----------------------
// TEST CODE
if (counter == 80) {
ledGreenToggle();
return 1;
}
// -----------------------
return 0;
}
void buttonsExec(void *handle) {
if (stone.shape == e_ShapeInvalid) {
// 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
View File

@ -0,0 +1,7 @@
#ifndef _BUTTONS_H_
#define _BUTTONS_H_
void buttonsInit();
#endif // _BUTTONS_H_

View File

@ -53,3 +53,9 @@ uint8_t canvasIsRowFilled(uint8_t row) {
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);
}

View File

@ -19,6 +19,7 @@ void canvasClear();
void canvasSetAll(uint8_t color);
void canvasShow();
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);
uint8_t canvasIsRowFilled(uint8_t row);

View File

@ -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

View File

@ -62,6 +62,6 @@ void gameExec(void *handle) {
}
void gameInit() {
schAdd(gameExec, NULL, 0, 1000);
schAdd(gameExec, NULL, 0, 5000);
}

View File

@ -9,6 +9,8 @@
#include "displayDriver.h"
#include "canvas.h"
#include "game.h"
#include "buttons.h"
#include "shapes.h"
int main() {
@ -29,7 +31,9 @@ int main() {
displayDriverInit();
canvasInit();
shapesInit();
gameInit();
buttonsInit();
__enable_interrupt();

View File

@ -1,31 +1,280 @@
#include "shapes.h"
#include "shape_i.h"
#include "canvas.h"
#include "../rgb-driver/colors.h"
#define COLOR _cyan
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 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);
res = 1;
}
return res;
}
uint8_t moveDown_i() {
return 1;
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;
}
uint8_t moveLeft_i() {
return 1;
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;
}
uint8_t moveRight_i() {
return 1;
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;
}
uint8_t rotateLeft_i() {
return 1;
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;
}
return res;
}
uint8_t rotateRight_i() {
return 1;
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;
}
return res;
}

View File

@ -33,6 +33,10 @@ const stoneOperations_t stoneOperations[] = {
stone_t stone;
void shapesInit() {
stone.shape = e_ShapeInvalid;
}
void stoneCreate() {
stone.shape = e_I;
stone.orientation = e_0;

View File

@ -3,6 +3,7 @@
#include <stdint.h>
void shapesInit();
void stoneCreate();
uint8_t stoneDraw();
uint8_t stoneMoveDown();
@ -12,7 +13,7 @@ uint8_t stoneRotateLeft();
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_I=0, e_O, e_T, e_Z, e_S, e_L, e_J, e_ShapeInvalid } shape_t;
typedef enum { e_0, e_90, e_180, e_270 } orientation_t;
typedef struct {