Files
milsko/src/draw.c
NishiOwO 3cb4492952 dont shift coord
git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@54 b9cfdab3-6d41-4d17-bbe4-086880011989
2025-09-29 05:24:32 +00:00

142 lines
2.9 KiB
C

/* $Id$ */
#include <Mw/Milsko.h>
static int hex(const char* txt, int len) {
int i;
int r = 0;
for(i = 0; i < len; i++) {
char c = txt[i];
int n = 0;
if('a' <= c && c <= 'f') {
n = c - 'a' + 10;
} else if('A' <= c && c <= 'F') {
n = c - 'A' + 10;
} else if('0' <= c && c <= '9') {
n = c - '0';
}
r = r << 4;
r |= n;
}
return r;
}
MwLLColor MwParseColor(MwWidget handle, const char* text) {
int r = 0;
int g = 0;
int b = 0;
if(text[0] == '#' && strlen(text) == 4) {
r = hex(text + 1, 1);
g = hex(text + 2, 1);
b = hex(text + 3, 1);
r |= r << 4;
g |= g << 4;
b |= b << 4;
} else if(text[0] == '#' && strlen(text) == 7) {
r = hex(text + 1, 2);
g = hex(text + 3, 2);
b = hex(text + 5, 2);
}
return MwLLAllocColor(handle->lowlevel, r, g, b);
}
void MwDrawRect(MwWidget handle, MwRect* rect, MwLLColor color) {
MwPoint p[4];
p[0].x = rect->x;
p[0].y = rect->y;
p[1].x = rect->x + rect->width;
p[1].y = rect->y;
p[2].x = rect->x + rect->width;
p[2].y = rect->y + rect->height;
p[3].x = rect->x;
p[3].y = rect->y + rect->height;
MwLLPolygon(handle->lowlevel, p, 4, color);
}
void MwDrawFrame(MwWidget handle, MwRect* rect, MwLLColor color, int invert) {
MwPoint p[6];
const int diff = 128;
const int border = 2;
MwLLColor darker = MwLLAllocColor(handle->lowlevel, color->red - diff, color->green - diff, color->blue - diff);
MwLLColor lighter = MwLLAllocColor(handle->lowlevel, color->red + diff, color->green + diff, color->blue + diff);
p[0].x = 0;
p[0].y = 0;
p[1].x = 0 + rect->width;
p[1].y = 0;
p[2].x = 0 + rect->width - border;
p[2].y = 0 + border;
p[3].x = 0 + border;
p[3].y = 0 + border;
p[4].x = 0 + border;
p[4].y = 0 + rect->height - border;
p[5].x = 0;
p[5].y = 0 + rect->height;
MwLLPolygon(handle->lowlevel, p, 6, invert ? darker : lighter);
p[0].x = 0 + rect->width;
p[0].y = 0;
p[1].x = 0 + rect->width - border;
p[1].y = 0 + border;
p[2].x = 0 + rect->width - border;
p[2].y = 0 + rect->height - border;
p[3].x = 0 + border;
p[3].y = 0 + rect->height - border;
p[4].x = 0;
p[4].y = 0 + rect->height;
p[5].x = 0 + rect->width;
p[5].y = 0 + rect->height;
MwLLPolygon(handle->lowlevel, p, 6, invert ? lighter : darker);
MwLLFreeColor(lighter);
MwLLFreeColor(darker);
rect->x += border;
rect->y += border;
rect->width -= border * 2;
rect->height -= border * 2;
}
void MwDrawText(MwWidget handle, MwPoint* point, const char* text, MwLLColor color) {
int i, x, y, sx, sy, sc = 1;
MwRect r;
sx = point->x - strlen(text) * 8 * sc / 2;
sy = point->y - 16 * sc / 2;
for(i = 0; text[i] != 0; i++) {
for(y = 0; y < 16; y++) {
for(x = 0; x < 8; x++) {
r.x = sx + x * sc;
r.y = sy + y * sc;
r.width = sc;
r.height = sc;
if(MwFontData[(unsigned char)text[i]].data[y] & (1 << (7 - x))) {
MwDrawRect(handle, &r, color);
}
}
}
sx += 8 * sc;
}
}