feat: draw function
This commit is contained in:
parent
d5a89c35f1
commit
9efddf8a1a
55
game.c
55
game.c
|
@ -11,11 +11,14 @@
|
|||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
||||
#define CLAMP(x,a,b) (MIN(MAX(x,a),b))
|
||||
|
||||
int mouse_x = 0, mouse_y = 0;
|
||||
size_t mouse_x = 0, mouse_y = 0;
|
||||
int eyes_front = 0, eyes_back = 0;
|
||||
|
||||
struct bricks bricks = {0};
|
||||
|
||||
struct brick hand[5];
|
||||
size_t hand_count = 0;
|
||||
|
||||
void key_callback(int key, int scancode, int action, int mods) {
|
||||
if (action != GLFW_PRESS) return;
|
||||
switch (key) {
|
||||
|
@ -58,26 +61,48 @@ void init() {
|
|||
.back = {.x = 13, .y = 5, .val = 5},
|
||||
}
|
||||
);
|
||||
|
||||
hand_count = 3;
|
||||
hand[0] = (struct brick) {
|
||||
.front = {.x = 12, .y = 5, .val = 2},
|
||||
.back = {.x = 13, .y = 5, .val = 5},
|
||||
};
|
||||
hand[1] = (struct brick) {
|
||||
.front = {.x = 12, .y = 5, .val = 2},
|
||||
.back = {.x = 13, .y = 5, .val = 5},
|
||||
};
|
||||
hand[2] = (struct brick) {
|
||||
.front = {.x = 12, .y = 5, .val = 2},
|
||||
.back = {.x = 13, .y = 5, .val = 5},
|
||||
};
|
||||
}
|
||||
|
||||
void draw_image(decoded_image img) {
|
||||
for (size_t i = 0; i < img.buf_size; i++) img.buf[i] = i;
|
||||
void
|
||||
draw(
|
||||
struct image canvas, struct image texture,
|
||||
size_t xpos, size_t ypos
|
||||
) {
|
||||
for (size_t y = 0; y < texture.height; y++) {
|
||||
for (size_t x = 0; x < texture.width; x++) {
|
||||
canvas.buf[(ypos + y) * canvas.width + xpos + x] = texture.buf[y * texture.width + x];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void render(struct image canvas) {
|
||||
for (size_t i = 0; i < canvas.bufsize; i++) canvas.buf[i] = i;
|
||||
|
||||
// domino playground
|
||||
for (size_t i = 0; i < bricks.count; i++) {
|
||||
struct brick *b = &bricks.items.brick[i];
|
||||
|
||||
for (int y = 0; y < DOMINO_HEIGHT; y++) {
|
||||
for (int x = 0; x < DOMINO_WIDTH; x++) {
|
||||
img.buf[(b->front.y * EYE_SIZE + y) * img.width + b->front.x * EYE_SIZE + x] =
|
||||
(*(uint32_t*) &domino[b->back.val][b->front.val][y * DOMINO_WIDTH * BYTES_PER_PIXEL + x * BYTES_PER_PIXEL]);
|
||||
}
|
||||
}
|
||||
draw(canvas, domino[b->back.val][b->front.val], b->front.x * EYE_SIZE, b->front.y * EYE_SIZE);
|
||||
}
|
||||
|
||||
for (int y = 0; y < DOMINO_HEIGHT; y++) {
|
||||
for (int x = 0; x < DOMINO_WIDTH; x++) {
|
||||
img.buf[(CLAMP(mouse_y, 0, img.height) + y) * img.width + CLAMP(mouse_x, 0, img.width) + x] =
|
||||
(*(uint32_t*) &domino[eyes_front][eyes_back][y * DOMINO_WIDTH * BYTES_PER_PIXEL + x * BYTES_PER_PIXEL]);
|
||||
}
|
||||
for (size_t i = 0; i < hand_count; i++) {
|
||||
struct brick *b = &hand[i];
|
||||
draw(canvas, domino[b->back.val][b->front.val], (canvas.width - hand_count *(DOMINO_WIDTH + 4))/2 + i * (DOMINO_WIDTH + 4), canvas.height - DOMINO_WIDTH);
|
||||
}
|
||||
|
||||
draw(canvas,domino[eyes_front][eyes_back], CLAMP(mouse_x, 0, canvas.width), CLAMP(mouse_y, 0, canvas.height));
|
||||
}
|
||||
|
||||
|
|
14
game.h
14
game.h
|
@ -5,12 +5,14 @@
|
|||
#ifndef GAME_H
|
||||
#define GAME_H
|
||||
|
||||
typedef struct decoded_image {
|
||||
size_t width;
|
||||
size_t height;
|
||||
uint32_t *buf;
|
||||
size_t buf_size;
|
||||
} decoded_image;
|
||||
struct color {
|
||||
unsigned char r,g,b,a;
|
||||
};
|
||||
|
||||
struct image {
|
||||
size_t width, height, bufsize;
|
||||
union {uint32_t *buf; struct color *color;};
|
||||
};
|
||||
|
||||
#endif // GAME_H
|
||||
|
||||
|
|
8
main.c
8
main.c
|
@ -23,14 +23,14 @@ extern void key_callback(int key, int scancode, int action, int mods);
|
|||
extern void cursor_position_callback(int xpos, int ypos);
|
||||
extern void mouse_button_callback(int button, int action, int mods);
|
||||
extern void init();
|
||||
extern void draw_image(decoded_image img);
|
||||
extern void render(struct image img);
|
||||
|
||||
uint32_t buffer[256 * 240] = {0};
|
||||
|
||||
struct decoded_image canvas = {
|
||||
struct image canvas = {
|
||||
.width = 256,
|
||||
.height = 240,
|
||||
.buf_size = 256 * 240,
|
||||
.bufsize = 256 * 240,
|
||||
.buf = buffer,
|
||||
};
|
||||
|
||||
|
@ -109,7 +109,7 @@ void loop() {
|
|||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
draw_image(canvas);
|
||||
render(canvas);
|
||||
|
||||
// bind textures on corresponding texture units
|
||||
glActiveTexture(GL_TEXTURE);
|
||||
|
|
|
@ -13,15 +13,22 @@
|
|||
#define START_Y 2
|
||||
|
||||
void print_domino(unsigned char *data, int width, int n, int posx, int posy) {
|
||||
printf(" {");
|
||||
printf(" {.width = %d, .height = %d, .bufsize = %d, .color = (struct color[%d]) {",
|
||||
DOMINO_WIDTH,
|
||||
DOMINO_HEIGHT,
|
||||
DOMINO_WIDTH * DOMINO_HEIGHT,
|
||||
DOMINO_WIDTH * DOMINO_HEIGHT
|
||||
);
|
||||
for (int y = 0; y < DOMINO_HEIGHT; y++) {
|
||||
for (int x = 0; x < DOMINO_WIDTH; x++) {
|
||||
printf("{");
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%3d, ", data[(y + posy) * width * n + (x + posx) * n + i]);
|
||||
}
|
||||
printf("},");
|
||||
}
|
||||
}
|
||||
printf("},\n");
|
||||
printf("}},\n");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
|
@ -31,6 +38,8 @@ int main(int argc, char **argv) {
|
|||
unsigned char *data = stbi_load(argv[1], &x, &y, &n, 0);
|
||||
// n = n < 3 ? n : 3;
|
||||
|
||||
|
||||
printf("#include \"../game.h\"\n");
|
||||
printf("#define NUM_DOMINO_X %d\n", NUM_DOMINO_X );
|
||||
printf("#define NUM_DOMINO_Y %d\n", NUM_DOMINO_Y );
|
||||
printf("#define DOMINO_WIDTH %d\n", DOMINO_WIDTH );
|
||||
|
@ -38,10 +47,9 @@ int main(int argc, char **argv) {
|
|||
printf("#define EYE_SIZE %d\n", EYE_SIZE);
|
||||
printf("#define BYTES_PER_PIXEL %d\n", n);
|
||||
|
||||
printf("unsigned char domino[%d][%d][%d] = {\n",
|
||||
printf("struct image domino[%d][%d] = {\n",
|
||||
NUM_DOMINO_X,
|
||||
NUM_DOMINO_Y,
|
||||
DOMINO_WIDTH * DOMINO_HEIGHT * n
|
||||
NUM_DOMINO_Y
|
||||
);
|
||||
|
||||
for (int domino_x = 0; domino_x < NUM_DOMINO_X; domino_x++) {
|
||||
|
|
Loading…
Reference in a new issue