-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdraw.c
138 lines (118 loc) · 3.65 KB
/
draw.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
forcelang - language override plugin
Written 2018/2019 by xdaniel - github.com/xdanieldzd
*/
#include "globals.h"
#include "font.h"
#define SETPIXELBLEND(_rx, _ry, color) \
{ \
colorRgba *op = (colorRgba *)frameBufParam.base + (_ry * frameBufParam.pitch) + _rx; \
colorRgba np = { .rgba.r = ((uint16_t)op->rgba.r + color.rgba.r) >> 1, ((uint16_t)op->rgba.g + color.rgba.g) >> 1, ((uint16_t)op->rgba.b + color.rgba.b) >> 1, ((uint16_t)op->rgba.a + color.rgba.a) >> 1 }; \
*op = np; \
}
const colorRgba colorBlackTransparent = { .rgba.r = 0x00, .rgba.g = 0x00, .rgba.b = 0x00, .rgba.a = 0x80 };
const colorRgba colorWhite = { .packed = 0xFFFFFFFF };
const colorRgba colorGreen = { .rgba.r = 0xB0, .rgba.g = 0xFF, .rgba.b = 0xB0, .rgba.a = 0xFF };
const colorRgba colorNone = (colorRgba){ .packed = 0x00000000 };
SceDisplayFrameBuf frameBufParam;
void drawUpdate(const SceDisplayFrameBuf *pParam)
{
memcpy(&frameBufParam, pParam, sizeof(SceDisplayFrameBuf));
}
void drawRectangleLP(int x, int y, int width, int height, colorRgba color)
{
if (color.rgba.a == 0) return;
if (color.rgba.a == 0xFF)
{
for (int ry = y; ry < y + height; ry++)
{
memset((uint32_t *)frameBufParam.base + (ry * frameBufParam.pitch) + x, color.packed, sizeof(uint32_t) * width);
}
}
if (color.rgba.a < 0xFF)
{
for (int ry = y; ry < y + height; ry += 2)
{
for (int rx = x; rx < x + width; rx += 2)
{
SETPIXELBLEND((rx ), (ry ), color);
SETPIXELBLEND((rx + 1), (ry ), color);
SETPIXELBLEND((rx ), (ry + 1), color);
SETPIXELBLEND((rx + 1), (ry + 1), color);
}
}
}
}
void drawCharacter(int x, int y, int zoom, char ch, colorRgba color)
{
uint32_t *fb = frameBufParam.base;
uint8_t *bitmap = font_8x16_data[0];
if (ch >= 0x00 && ch < 0x80)
{
bitmap = font_8x16_data[(int)ch];
}
for (int by = 0; by < (16 << zoom); by++)
{
for (int bx = 0; bx < (8 << zoom); bx++)
{
if (bitmap[by >> zoom] >> (bx >> zoom) & 0x01)
{
fb[((y + by) * frameBufParam.pitch) + x + bx] = color.packed;
}
}
}
}
void drawStringInternal(int x, int y, int zoom, colorRgba foreColor, colorRgba backColor, const char *str)
{
int width = 0;
int height = 1;
int lineLen = 0;
int i = 0;
while(str[i] != '\0')
{
if (str[i] == '\n')
{
if (lineLen > width) width = lineLen;
lineLen = 0;
height++;
}
else
{
lineLen += (font_8x16_widths[(int)str[i]] + 1);
}
i++;
}
if (lineLen > width) width = lineLen;
width <<= zoom;
int offset = 0;
if (backColor.rgba.a != 0)
{
drawRectangleLP(x, y, width + 8, ((height << zoom) * 16) + 8 + (((height - 1) << zoom) * 4), backColor);
offset = 4;
}
int sx = x + offset, sy = y + offset;
i = 0;
while(str[i] != '\0')
{
if (str[i] == '\n')
{
sx = x + offset;
sy += (20 << zoom);
}
else
{
drawCharacter(sx, sy, zoom, str[i], foreColor);
sx += ((font_8x16_widths[(int)str[i]] + 1) << zoom);
}
i++;
}
}
void drawString(int x, int y, int zoom, colorRgba foreColor, colorRgba backColor, const char *format, ...)
{
char buffer[256] = "";
va_list args;
va_start(args, format);
vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
drawStringInternal(x, y, zoom, foreColor, backColor, buffer);
}