From 9efddf8a1a0f4a2e5b17563718346be84486653d Mon Sep 17 00:00:00 2001 From: orangerot Date: Tue, 14 Oct 2025 16:32:45 +0200 Subject: feat: draw function --- game.c | 55 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 15 deletions(-) (limited to 'game.c') diff --git a/game.c b/game.c index 56f2a4a..7c788fe 100644 --- a/game.c +++ b/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( + 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 draw_image(decoded_image img) { - for (size_t i = 0; i < img.buf_size; i++) img.buf[i] = i; +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)); } + -- cgit v1.2.3