-
Notifications
You must be signed in to change notification settings - Fork 11
/
blit.c
183 lines (154 loc) · 4.2 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
PSP VSH 24bpp text bliter
*/
#include <libk/stdarg.h>
#include <libk/stdio.h>
#include <libk/string.h>
#include <psp2/types.h>
#include <psp2/display.h>
#include "blit.h"
#define ALPHA_BLEND 1
extern unsigned char msx[];
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
static int pwidth, pheight, bufferwidth, pixelformat;
static unsigned int* vram32;
static uint32_t fcolor = 0x00ffffff;
static uint32_t bcolor = 0xff000000;
#if ALPHA_BLEND
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
static uint32_t adjust_alpha(uint32_t col)
{
uint32_t alpha = col>>24;
uint8_t mul;
uint32_t c1,c2;
if(alpha==0) return col;
if(alpha==0xff) return col;
c1 = col & 0x00ff00ff;
c2 = col & 0x0000ff00;
mul = (uint8_t)(255-alpha);
c1 = ((c1*mul)>>8)&0x00ff00ff;
c2 = ((c2*mul)>>8)&0x0000ff00;
return (alpha<<24)|c1|c2;
}
#endif
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
//int blit_setup(int sx,int sy,const char *msg,int fg_col,int bg_col)
int blit_setup(void)
{
SceDisplayFrameBuf param;
param.size = sizeof(SceDisplayFrameBuf);
sceDisplayGetFrameBuf(¶m, SCE_DISPLAY_SETBUF_IMMEDIATE);
pwidth = param.width;
pheight = param.height;
vram32 = param.base;
bufferwidth = param.pitch;
pixelformat = param.pixelformat;
if( (bufferwidth==0) || (pixelformat!=0)) return -1;
fcolor = 0x00ffffff;
bcolor = 0xff000000;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// 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)
{
int x,y,p;
int offset;
char code;
unsigned char font;
uint32_t fg_col,bg_col;
#if ALPHA_BLEND
uint32_t col,c1,c2;
uint32_t alpha;
fg_col = adjust_alpha(fcolor);
bg_col = adjust_alpha(bcolor);
#else
fg_col = fcolor;
bg_col = bcolor;
#endif
//Kprintf("MODE %d WIDTH %d\n",pixelformat,bufferwidth);
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)
{
vram32[offset] = col;
vram32[offset + 1] = col;
vram32[offset + bufferwidth] = col;
vram32[offset + bufferwidth + 1] = col;
}
else if(alpha!=0xff)
{
c2 = vram32[offset];
c1 = c2 & 0x00ff00ff;
c2 = c2 & 0x0000ff00;
c1 = ((c1*alpha)>>8)&0x00ff00ff;
c2 = ((c2*alpha)>>8)&0x0000ff00;
uint32_t color = (col&0xffffff) + c1 + c2;
vram32[offset] = color;
vram32[offset + 1] = color;
vram32[offset + bufferwidth] = color;
vram32[offset + bufferwidth + 1] = color;
}
#else
uint32_t color = (font & 0x80) ? fg_col : bg_col;
vram32[offset] = color;
vram32[offset + 1] = color;
vram32[offset + bufferwidth] = color;
vram32[offset + bufferwidth + 1] = color;
#endif
font <<= 1;
offset+=2;
}
}
}
return x;
}
int blit_string_ctr(int sy,const char *msg)
{
int sx = (960 / 2) - (strlen(msg) * (16 / 2));
return blit_string(sx,sy,msg);
}
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);
return blit_string(sx, sy, string);
}
int blit_set_frame_buf(const SceDisplayFrameBuf *param)
{
pwidth = param->width;
pheight = param->height;
vram32 = param->base;
bufferwidth = param->pitch;
pixelformat = param->pixelformat;
if( (bufferwidth==0) || (pixelformat!=0)) return -1;
fcolor = 0x00ffffff;
bcolor = 0xff000000;
return 0;
}