From 38bbd5a2bb59e5f04c3cc95b772637fe575114b3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Sat, 2 Mar 2024 18:22:48 +0100 Subject: [PATCH] ongoing work --- test01.c | 249 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 196 insertions(+), 53 deletions(-) diff --git a/test01.c b/test01.c index 05a3d10..d7ebc83 100644 --- a/test01.c +++ b/test01.c @@ -12,18 +12,32 @@ uint8_t canvas[CANVAS_WIDTH][CANVAS_HEIGHT]; #define O_WIDTH 2 #define O_COLOR 1 -typedef enum {e_O} t_shape; +#define I_HEIGHT 4 +#define I_WIDTH 1 +#define I_COLOR 1 + +typedef enum {e_O, e_I} t_shape; typedef struct { t_shape shape; + // flip width and height when rotating + uint8_t width; + uint8_t height; + + uint8_t initial; // 1 = initial appearing, no wipe necessary when drawing + uint8_t x; uint8_t y; - uint8_t rotation; // 0 = 12 o'clock, 1 = 3 o'clock, 2 = 6 o'clock, 3 = 9 o'clock + uint8_t last_x; + uint8_t last_y; + uint8_t last_rotation; + union { uint8_t O[O_WIDTH][O_HEIGHT]; + uint8_t I[I_WIDTH][I_HEIGHT]; }; } t_object; @@ -47,15 +61,52 @@ void initObject(t_shape shape, uint8_t x, uint8_t y) { object.x = x; object.y = y; object.rotation = 0; + object.last_x = 0; + object.last_y = 0; + object.last_rotation = 0; + object.initial = 1; switch (shape) { case e_O: + object.width = O_WIDTH; + object.height = O_HEIGHT; memset(object.O, O_COLOR, O_HEIGHT * O_WIDTH); break; + case e_I: + object.width = I_WIDTH; + object.height = I_HEIGHT; + memset(object.I, I_COLOR, I_HEIGHT * I_WIDTH); + break; } } void drawObject() { + if (object.initial == 0) { + // wipe + switch (object.shape) { + case e_O: + switch (object.last_rotation) { + case 0: + canvas[object.last_x][object.last_y] = 0; + canvas[object.last_x][object.last_y+1] = 0; + canvas[object.last_x+1][object.last_y] = 0; + canvas[object.last_x+1][object.last_y+1] = 0; + break; + } + break; + case e_I: + switch (object.last_rotation) { + case 0: + canvas[object.last_x][object.last_y] = 0; + canvas[object.last_x][object.last_y+1] = 0; + canvas[object.last_x][object.last_y+2] = 0; + canvas[object.last_x][object.last_y+3] = 0; + break; + } + break; + } + } + // draw switch (object.shape) { case e_O: switch (object.rotation) { @@ -66,55 +117,81 @@ void drawObject() { canvas[object.x+1][object.y+1] = object.O[1][1]; break; } + break; + case e_I: + switch (object.rotation) { + case 0: + canvas[object.x][object.y] = object.O[0][0]; + canvas[object.x][object.y+1] = object.O[0][1]; + canvas[object.x][object.y+2] = object.O[0][2]; + canvas[object.x][object.y+3] = object.O[0][3]; + break; + } + break; + } + // first draw has been done, next time, wipe is necessary + object.initial = 0; +} + +void moveObject(t_direction direction) { + object.last_x = object.x; + object.last_y = object.y; + object.last_rotation = object.rotation; + + switch (direction) { + case e_Down: + object.y = object.y + 1; + break; + case e_Left: + object.x = object.x - 1; + break; + case e_Right: + object.x = object.x + 1; + break; } } -void wipeObject() { +void revokeObjectMove() { + object.x = object.last_x; + object.y = object.last_y; + object.rotation = object.last_rotation; +} + +int checkCanvasBordersForObject() { + // return true if borders are violated + return ((object.y > CANVAS_HEIGHT - object.height) || (object.x > CANVAS_WIDTH - object.width)); +} + +int checkOtherObjectsTouchedForObject() { + // return true if another object is touched + int res = 0; + switch (object.shape) { case e_O: switch (object.rotation) { case 0: - canvas[object.x][object.y] = 0; - canvas[object.x][object.y+1] = 0; - canvas[object.x+1][object.y] = 0; - canvas[object.x+1][object.y+1] = 0; + res = (canvas[object.x][object.y] != 0) || + (canvas[object.x][object.y+1] != 0) || + (canvas[object.x+1][object.y] != 0) || + (canvas[object.x+1][object.y+1] != 0); break; } - } -} - -int moveObject(t_direction direction) { - int res = 0; - uint8_t new_value; - switch (direction) { - case e_Down: - new_value = object.y + 1; - if (new_value >= 0 && new_value <= CANVAS_HEIGHT-O_HEIGHT) { - object.y = new_value; - } else { - res = -1; - } break; - case e_Left: - new_value = object.x - 1; - if (new_value >= 0 && new_value < CANVAS_WIDTH-O_WIDTH) { - object.x = new_value; - } else { - res = -1; - } - break; - case e_Right: - new_value = object.x + 1; - if (new_value >= 0 && new_value <= CANVAS_WIDTH-O_WIDTH) { - object.x = new_value; - } else { - res = -1; + case e_I: + switch (object.rotation) { + case 0: + res = (canvas[object.x][object.y] != 0) || + (canvas[object.x][object.y+1] != 0) || + (canvas[object.x][object.y+2] != 0) || + (canvas[object.x][object.y+3] != 0); + break; } break; } return res; } + int main(int argc, char *argv[]) { memset(canvas, 0, CANVAS_HEIGHT * CANVAS_WIDTH); @@ -126,40 +203,106 @@ int main(int argc, char *argv[]) { showCanvas(); while (1) { - wipeObject(); - int err = moveObject(e_Down); - if (err != 0) { + moveObject(e_Down); + if (checkCanvasBordersForObject()) { printf("object would stick out canvas\n"); + revokeObjectMove(); break; - } else { - drawObject(); - showCanvas(); } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); } while (1) { - wipeObject(); - int err = moveObject(e_Right); - if (err != 0) { + moveObject(e_Right); + if (checkCanvasBordersForObject()) { printf("object would stick out canvas\n"); + revokeObjectMove(); break; - } else { - drawObject(); - showCanvas(); } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); } while (1) { - wipeObject(); - int err = moveObject(e_Left); - if (err != 0) { + moveObject(e_Left); + if (checkCanvasBordersForObject()) { printf("object would stick out canvas\n"); + revokeObjectMove(); break; - } else { - drawObject(); - showCanvas(); } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); + } + + + initObject(e_I, 1, 0); + + drawObject(); + showCanvas(); + + while (1) { + moveObject(e_Down); + if (checkCanvasBordersForObject()) { + printf("object would stick out canvas\n"); + revokeObjectMove(); + break; + } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); } + while (1) { + moveObject(e_Right); + if (checkCanvasBordersForObject()) { + printf("object would stick out canvas\n"); + revokeObjectMove(); + break; + } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); + } + + while (1) { + moveObject(e_Left); + if (checkCanvasBordersForObject()) { + printf("object would stick out canvas\n"); + revokeObjectMove(); + break; + } + if (checkOtherObjectsTouchedForObject()) { + printf("object would touch other object\n"); + revokeObjectMove(); + break; + } + drawObject(); + showCanvas(); + } + }