graphic-test-01/test01.c

315 lines
7.1 KiB
C

#include <stdio.h>
#include <stdint.h>
#include <string.h>
#define CANVAS_HEIGHT 20
#define CANVAS_WIDTH 10
uint8_t canvas[CANVAS_WIDTH][CANVAS_HEIGHT];
#define O_HEIGHT 2
#define O_WIDTH 2
#define O_COLOR 1
#define I_HEIGHT 4
#define I_WIDTH 1
#define I_COLOR 2
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;
t_object object;
typedef enum {e_Down, e_Left, e_Right} t_direction;
void showCanvas() {
for (uint8_t y = 0; y < CANVAS_HEIGHT; y++) {
for (uint8_t x = 0; x < CANVAS_WIDTH; x++) {
printf("%02x ", canvas[x][y]);
canvas[x][y] &= ~0x80;
}
printf("\n");
}
printf("\n");
}
void initObject(t_shape shape, uint8_t x, uint8_t y) {
object.shape = shape;
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
// the bit7 set marks changed pixels
switch (object.shape) {
case e_O:
switch (object.rotation) {
case 0:
canvas[object.x][object.y] = object.O[0][0] | 0x80;
canvas[object.x][object.y+1] = object.O[0][1] | 0x80;
canvas[object.x+1][object.y] = object.O[1][0] | 0x80;
canvas[object.x+1][object.y+1] = object.O[1][1] | 0x80;
break;
}
break;
case e_I:
switch (object.rotation) {
case 0:
canvas[object.x][object.y] = object.O[0][0] | 0x80;
canvas[object.x][object.y+1] = object.O[0][1] | 0x80;
canvas[object.x][object.y+2] = object.O[0][2] | 0x80;
canvas[object.x][object.y+3] = object.O[0][3] | 0x80;
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 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 0;
// return true if another object is touched
int res = 0;
// BROKEN! That doesn't work, pixels of the moving object would also be considered.
// The direction must be considered, only the first pixels in the moving direction
// must be checked.
switch (object.shape) {
case e_O:
switch (object.rotation) {
case 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;
}
break;
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);
showCanvas();
initObject(e_O, 2, 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();
}
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();
}
}