ongoing work
This commit is contained in:
parent
416063dc0e
commit
38bbd5a2bb
249
test01.c
249
test01.c
@ -12,18 +12,32 @@ uint8_t canvas[CANVAS_WIDTH][CANVAS_HEIGHT];
|
|||||||
#define O_WIDTH 2
|
#define O_WIDTH 2
|
||||||
#define O_COLOR 1
|
#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 {
|
typedef struct {
|
||||||
t_shape shape;
|
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 x;
|
||||||
uint8_t y;
|
uint8_t y;
|
||||||
|
|
||||||
uint8_t rotation; // 0 = 12 o'clock, 1 = 3 o'clock, 2 = 6 o'clock, 3 = 9 o'clock
|
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 {
|
union {
|
||||||
uint8_t O[O_WIDTH][O_HEIGHT];
|
uint8_t O[O_WIDTH][O_HEIGHT];
|
||||||
|
uint8_t I[I_WIDTH][I_HEIGHT];
|
||||||
};
|
};
|
||||||
} t_object;
|
} t_object;
|
||||||
|
|
||||||
@ -47,15 +61,52 @@ void initObject(t_shape shape, uint8_t x, uint8_t y) {
|
|||||||
object.x = x;
|
object.x = x;
|
||||||
object.y = y;
|
object.y = y;
|
||||||
object.rotation = 0;
|
object.rotation = 0;
|
||||||
|
object.last_x = 0;
|
||||||
|
object.last_y = 0;
|
||||||
|
object.last_rotation = 0;
|
||||||
|
object.initial = 1;
|
||||||
|
|
||||||
switch (shape) {
|
switch (shape) {
|
||||||
case e_O:
|
case e_O:
|
||||||
|
object.width = O_WIDTH;
|
||||||
|
object.height = O_HEIGHT;
|
||||||
memset(object.O, O_COLOR, O_HEIGHT * O_WIDTH);
|
memset(object.O, O_COLOR, O_HEIGHT * O_WIDTH);
|
||||||
break;
|
break;
|
||||||
|
case e_I:
|
||||||
|
object.width = I_WIDTH;
|
||||||
|
object.height = I_HEIGHT;
|
||||||
|
memset(object.I, I_COLOR, I_HEIGHT * I_WIDTH);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void drawObject() {
|
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) {
|
switch (object.shape) {
|
||||||
case e_O:
|
case e_O:
|
||||||
switch (object.rotation) {
|
switch (object.rotation) {
|
||||||
@ -66,55 +117,81 @@ void drawObject() {
|
|||||||
canvas[object.x+1][object.y+1] = object.O[1][1];
|
canvas[object.x+1][object.y+1] = object.O[1][1];
|
||||||
break;
|
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) {
|
switch (object.shape) {
|
||||||
case e_O:
|
case e_O:
|
||||||
switch (object.rotation) {
|
switch (object.rotation) {
|
||||||
case 0:
|
case 0:
|
||||||
canvas[object.x][object.y] = 0;
|
res = (canvas[object.x][object.y] != 0) ||
|
||||||
canvas[object.x][object.y+1] = 0;
|
(canvas[object.x][object.y+1] != 0) ||
|
||||||
canvas[object.x+1][object.y] = 0;
|
(canvas[object.x+1][object.y] != 0) ||
|
||||||
canvas[object.x+1][object.y+1] = 0;
|
(canvas[object.x+1][object.y+1] != 0);
|
||||||
break;
|
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;
|
break;
|
||||||
case e_Left:
|
case e_I:
|
||||||
new_value = object.x - 1;
|
switch (object.rotation) {
|
||||||
if (new_value >= 0 && new_value < CANVAS_WIDTH-O_WIDTH) {
|
case 0:
|
||||||
object.x = new_value;
|
res = (canvas[object.x][object.y] != 0) ||
|
||||||
} else {
|
(canvas[object.x][object.y+1] != 0) ||
|
||||||
res = -1;
|
(canvas[object.x][object.y+2] != 0) ||
|
||||||
}
|
(canvas[object.x][object.y+3] != 0);
|
||||||
break;
|
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;
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
memset(canvas, 0, CANVAS_HEIGHT * CANVAS_WIDTH);
|
memset(canvas, 0, CANVAS_HEIGHT * CANVAS_WIDTH);
|
||||||
|
|
||||||
@ -126,39 +203,105 @@ int main(int argc, char *argv[]) {
|
|||||||
showCanvas();
|
showCanvas();
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
wipeObject();
|
moveObject(e_Down);
|
||||||
int err = moveObject(e_Down);
|
if (checkCanvasBordersForObject()) {
|
||||||
if (err != 0) {
|
|
||||||
printf("object would stick out canvas\n");
|
printf("object would stick out canvas\n");
|
||||||
|
revokeObjectMove();
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
drawObject();
|
|
||||||
showCanvas();
|
|
||||||
}
|
}
|
||||||
|
if (checkOtherObjectsTouchedForObject()) {
|
||||||
|
printf("object would touch other object\n");
|
||||||
|
revokeObjectMove();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
drawObject();
|
||||||
|
showCanvas();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
wipeObject();
|
moveObject(e_Right);
|
||||||
int err = moveObject(e_Right);
|
if (checkCanvasBordersForObject()) {
|
||||||
if (err != 0) {
|
|
||||||
printf("object would stick out canvas\n");
|
printf("object would stick out canvas\n");
|
||||||
|
revokeObjectMove();
|
||||||
break;
|
break;
|
||||||
} else {
|
|
||||||
drawObject();
|
|
||||||
showCanvas();
|
|
||||||
}
|
}
|
||||||
|
if (checkOtherObjectsTouchedForObject()) {
|
||||||
|
printf("object would touch other object\n");
|
||||||
|
revokeObjectMove();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
drawObject();
|
||||||
|
showCanvas();
|
||||||
}
|
}
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
wipeObject();
|
moveObject(e_Left);
|
||||||
int err = moveObject(e_Left);
|
if (checkCanvasBordersForObject()) {
|
||||||
if (err != 0) {
|
|
||||||
printf("object would stick out canvas\n");
|
printf("object would stick out canvas\n");
|
||||||
|
revokeObjectMove();
|
||||||
break;
|
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();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user