forked from dots-tb/LOLIcon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblit.c
152 lines (121 loc) · 4.08 KB
/
blit.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
PSP VSH 24bpp text bliter
*/
#include <libk/stdarg.h>
#include <libk/stdio.h>
#include <libk/string.h>
#include "blit.h"
#define ALPHA_BLEND 1
#define printf ksceDebugPrintf
extern unsigned char msx[];
static int pwidth, pheight, pcenter, bufferwidth, pixelformat;
static unsigned int *vram32;
static uint32_t fcolor = COLOR_WHITE;
static uint32_t bcolor = COLOR_TRANSPARENT;
#if ALPHA_BLEND
static uint32_t adjust_alpha(uint32_t col) {
uint32_t alpha = col>>24, c1, c2;
uint8_t mul;
if(alpha == 0 || alpha == 0xff)
return col;
mul = (uint8_t)(255-alpha);
c1 = col & 0x00ff00ff;
c1 = ((c1 * mul) >> 8) & 0x00ff00ff;
c2 = col & 0x0000ff00;
c2 = ((c2 * mul) >> 8) & 0x0000ff00;
return (alpha<<24) | c1 | c2;
}
#endif
/////////////////////////////////////////////////////////////////////////////
// blit text
/////////////////////////////////////////////////////////////////////////////
void blit_set_color(int fg_col, int bg_col) {
fcolor = fg_col;
bcolor = bg_col;
}
/////////////////////////////////////////////////////////////////////////////
// blit text
/////////////////////////////////////////////////////////////////////////////
int blit_string(int sx, int sy, const char *msg) {
uint32_t fg_col, bg_col;
int x, y, p, offset;
unsigned char font;
char code;
#if ALPHA_BLEND
uint32_t col, c1, c2, alpha;
fg_col = adjust_alpha(fcolor);
bg_col = adjust_alpha(bcolor);
#else
fg_col = fcolor;
bg_col = bcolor;
#endif
if(bufferwidth == 0 || pixelformat != 0)
return -1;
for(x=0; msg[x] && x<(pwidth/16); ++x) {
code = msg[x] & 0x7f; // 7bit ANK
for(y=0; y<8; ++y) {
offset = (sy + (y * 2)) * bufferwidth + sx + x * 16;
font = y>=7 ? 0x00 : msx[ code*8 + y ];
for(p=0; p<8; ++p) {
#if ALPHA_BLEND
col = (font & 0x80) ? fg_col : bg_col;
alpha = col>>24;
if(alpha == 0) {
ksceKernelMemcpyKernelToUser((uintptr_t)(&vram32[offset]), &col, sizeof(col));
ksceKernelMemcpyKernelToUser((uintptr_t)(&vram32[offset + 1]), &col, sizeof(col));
ksceKernelMemcpyKernelToUser((uintptr_t)(&vram32[offset + bufferwidth]), &col, sizeof(col));
ksceKernelMemcpyKernelToUser((uintptr_t)(&vram32[offset + bufferwidth + 1]), &col, sizeof(col));
} else if(alpha != 0xff) {
ksceKernelMemcpyUserToKernel(&c2, (uintptr_t)(vram32[offset]), sizeof(unsigned int));
c1 = c2 & 0x00ff00ff;
c2 = c2 & 0x0000ff00;
c1 = ((c1*alpha)>>8)&0x00ff00ff;
c2 = ((c2*alpha)>>8)&0x0000ff00;
uint32_t color = (col&0xffffff) + c1 + c2;
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset), &color, sizeof(uint32_t));
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + 1), &color, sizeof(uint32_t));
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + bufferwidth), &color, sizeof(uint32_t));
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + bufferwidth + 1), &color, sizeof(uint32_t));
}
#else
uint32_t color = (font & 0x80) ? fg_col : bg_col;
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + 1), &color, sizeof(uint32_t));
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + bufferwidth), &color, sizeof(uint32_t));
ksceKernelMemcpyKernelToUser((uintptr_t)(vram32 + offset + bufferwidth + 1), &color, sizeof(uint32_t));
#endif
font <<= 1;
offset += 2;
}
}
}
return x;
}
int blit_string_ctr(int sy, const char *msg) {
int sx = pcenter - (strlen(msg) * 8);
return blit_string(sx,sy,msg);
}
int blit_string_center_x(int offset) {
return pcenter-(offset*8);
}
int blit_stringf(int sx, int sy, const char *msg, ...) {
va_list list;
char string[512];
va_start(list, msg);
vsnprintf(string, 512, msg, list);
va_end(list);
// correct value is (sx/960.0f)*pwidth but needs more adjustments
return blit_string(sx, (sy/544.0f)*pheight, string);
}
int blit_set_frame_buf(const SceDisplayFrameBuf *param) {
pwidth = param->width;
pheight = param->height;
pcenter = pwidth/2;
vram32 = param->base;
bufferwidth = param->pitch;
pixelformat = param->pixelformat;
if (bufferwidth == 0 || pixelformat != 0)
return -1;
fcolor = COLOR_WHITE;
bcolor = COLOR_TRANSPARENT;
return 0;
}