initial
This commit is contained in:
commit
416063dc0e
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
*.o
|
||||
main
|
25
Makefile
Normal file
25
Makefile
Normal file
@ -0,0 +1,25 @@
|
||||
CC=gcc
|
||||
|
||||
COMMON=-Wall -std=gnu99
|
||||
CFLAGS=$(COMMON)
|
||||
|
||||
LDFLAGS=
|
||||
|
||||
main: test01.o
|
||||
$(CC) -o $@ $(LDFLAGS) $^
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
.S.o:
|
||||
$(CC) $(ASFLAGS) -c $<
|
||||
|
||||
|
||||
.PHONY: all
|
||||
all: main
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm -f *.o *.elf *.map *.txt main
|
||||
|
||||
|
165
test01.c
Normal file
165
test01.c
Normal file
@ -0,0 +1,165 @@
|
||||
#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
|
||||
|
||||
typedef enum {e_O} t_shape;
|
||||
|
||||
typedef struct {
|
||||
t_shape shape;
|
||||
|
||||
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
|
||||
|
||||
union {
|
||||
uint8_t O[O_WIDTH][O_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]);
|
||||
}
|
||||
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;
|
||||
|
||||
switch (shape) {
|
||||
case e_O:
|
||||
memset(object.O, O_COLOR, O_HEIGHT * O_WIDTH);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void drawObject() {
|
||||
switch (object.shape) {
|
||||
case e_O:
|
||||
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+1][object.y] = object.O[1][0];
|
||||
canvas[object.x+1][object.y+1] = object.O[1][1];
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void wipeObject() {
|
||||
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;
|
||||
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;
|
||||
}
|
||||
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) {
|
||||
wipeObject();
|
||||
int err = moveObject(e_Down);
|
||||
if (err != 0) {
|
||||
printf("object would stick out canvas\n");
|
||||
break;
|
||||
} else {
|
||||
drawObject();
|
||||
showCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
wipeObject();
|
||||
int err = moveObject(e_Right);
|
||||
if (err != 0) {
|
||||
printf("object would stick out canvas\n");
|
||||
break;
|
||||
} else {
|
||||
drawObject();
|
||||
showCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
while (1) {
|
||||
wipeObject();
|
||||
int err = moveObject(e_Left);
|
||||
if (err != 0) {
|
||||
printf("object would stick out canvas\n");
|
||||
break;
|
||||
} else {
|
||||
drawObject();
|
||||
showCanvas();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user