domino-dungeon/draw.c
2025-10-18 17:32:37 +02:00

40 lines
1.3 KiB
C

#include "characters.h"
#include "draw.h"
void draw_image( struct image canvas, struct image texture, size_t xpos, size_t ypos, bool vertical) {
for (size_t y = 0; y < texture.height; y++) {
for (size_t x = 0; x < texture.width; x++) {
int canvas_y = (vertical ? x : y) + ypos;
int canvas_x = (vertical ? y : x) + xpos;
if (canvas_x < 0 || canvas_x >= (int) canvas.width - 1 || canvas_y < 0 || canvas_y >= (int) canvas.height - 1) continue;
canvas.buf[canvas_y * canvas.width + canvas_x] = texture.buf[y * texture.width + x];
}
}
}
void draw_glyph( struct image canvas, uint32_t glyph, size_t xpos, size_t ypos, struct color color) {
struct image texture = {
.width = 5,
.height = 6,
.bufsize = 5 * 6,
.buf = (uint32_t[30]) {0}
};
for (size_t y = 0; y < texture.height; y++) {
for (size_t x = 0; x < texture.width; x++) {
if ((glyph >> ((texture.height - y - 1) * texture.width + (texture.width - x - 1))) & 1) {
texture.color[y * texture.width + x] = color;
}
}
}
draw_image(canvas, texture, xpos, ypos, 0);
}
void draw_text(struct image canvas, char *string, size_t xpos, size_t ypos, struct color color) {
for (size_t i = 0; string[i]; i++) {
uint32_t glyph = characters[string[i] - ' '];
draw_glyph(canvas, glyph, xpos + i * 5, ypos, color);
}
}