forked from PSP-Archive/OldskOOl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pg.c
399 lines (349 loc) · 7.98 KB
/
pg.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
// primitive graphics for Hello World PSP
#include "pg.h"
#include "font.c"
//system call
void pspDisplayWaitVblankStart();
void pspDisplaySetMode(long,long,long);
void pspDisplaySetFrameBuf(char *topaddr,long linesize,long pixelsize,long);
#define CMAX_X 60
#define CMAX_Y 38
#define CMAX2_X 30
#define CMAX2_Y 19
#define CMAX4_X 15
#define CMAX4_Y 9
//variables
char *pg_vramtop=(char *)0x04000000;
long pg_screenmode;
long pg_showframe;
long pg_drawframe;
void pgWaitVn(unsigned long count)
{
for (; count>0; --count) {
sceDisplayWaitVblankStart();
}
}
void pgWaitV()
{
sceDisplayWaitVblankStart();
}
char *pgGetVramAddr(unsigned long x,unsigned long y)
{
return pg_vramtop+(pg_drawframe?0x44000:0)+x*2+y*512*2+0x40000000;
}
char *pgGetVramAddrLog(unsigned long x,unsigned long y)
{
return pg_vramtop+(pg_drawframe?0:0x44000)+x*2+y*512*2+0x40000000;
}
void pgInit()
{
sceDisplaySetMode(0,SCREEN_WIDTH,SCREEN_HEIGHT);
pgScreenFrame(0,0);
}
void pgFillvram(unsigned long color)
{
unsigned char *vptr0; //pointer to vram
unsigned long i;
vptr0=pgGetVramAddr(0,0);
for (i=0; i<0x44000/2; i++) {
*(unsigned short *)vptr0=color;
vptr0+=2;
}
}
void pgScreenFrame(long mode,long frame)
{
pg_screenmode=mode;
frame=(frame?1:0);
pg_showframe=frame;
if (mode==0) {
//screen off
pg_drawframe=frame;
sceDisplaySetFrameBuf(0,0,0,1);
} else if (mode==1) {
//show/draw same
pg_drawframe=frame;
sceDisplaySetFrameBuf(pg_vramtop+(pg_showframe?0x44000:0),512,1,1);
} else if (mode==2) {
//show/draw different
pg_drawframe=(frame?0:1);
sceDisplaySetFrameBuf(pg_vramtop+(pg_showframe?0x44000:0),512,1,1);
}
}
void pgScreenFlip()
{
pg_showframe=(pg_showframe?0:1);
pg_drawframe=(pg_drawframe?0:1);
sceDisplaySetFrameBuf(pg_vramtop+(pg_showframe?0x44000:0),512,1,0);
}
void pgScreenFlipV()
{
pgWaitV();
pgScreenFlip();
}
void PutPixel(unsigned long x, unsigned long y, unsigned short color)
{
unsigned char *vptr; //pointer to vram
if(x>479)
x=479;
if(x<0)
x=0;
if(y>271)
y=271;
if(y<0)
y=0;
vptr=pgGetVramAddr(x,y);
*(unsigned short *)vptr=color;
}
void PutGfx(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short *d)
{
unsigned char *vptr0; //pointer to vram
unsigned char *vptr; //pointer to vram
unsigned long xx,yy,mx,my;
const unsigned short *dd;
vptr0=pgGetVramAddr(x,y);
for (yy=0; yy<h; yy++) {
for (my=0; my<mag; my++) {
vptr=vptr0;
dd=d;
for (xx=0; xx<w; xx++) {
for (mx=0; mx<mag; mx++) {
*(unsigned short *)vptr=*dd;
vptr+=2;
}
dd++;
}
vptr0+=512*2;
}
d+=w;
}
}
void PutGfxTrans(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short *d)
{
unsigned char *vptr0; //pointer to vram
unsigned char *vptr; //pointer to vram
unsigned long xx,yy,mx,my;
const unsigned short *dd;
vptr0=pgGetVramAddr(x,y);
for (yy=0; yy<h; yy++) {
for (my=0; my<mag; my++) {
vptr=vptr0;
dd=d;
for (xx=0; xx<w; xx++) {
for (mx=0; mx<mag; mx++) {
if(*dd!=0){
*(unsigned short *)vptr=*dd;
}
vptr+=2;
}
dd++;
}
vptr0+=512*2;
}
d+=w;
}
}
void FilledRec(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short color)
{
unsigned char *vptr0; //pointer to vram
unsigned char *vptr; //pointer to vram
unsigned long xx,yy,mx,my;
vptr0=pgGetVramAddr(x,y);
for (yy=0; yy<h; yy++) {
for (my=0; my<mag; my++) {
vptr=vptr0;
for (xx=0; xx<w; xx++) {
for (mx=0; mx<mag; mx++) {
*(unsigned short *)vptr=color;
vptr+=2;
}
}
vptr0+=512*2;
}
}
}
//**-------------------
void pgPrint(unsigned long x,unsigned long y,unsigned long color,const char *str)
{
while (*str!=0 && x<CMAX_X && y<CMAX_Y) {
pgPutChar(x*8,y*8,color,0,*str,1,0,1);
str++;
x++;
if (x>=CMAX_X) {
x=0;
y++;
}
}
}
void pgPutChar(unsigned long x,unsigned long y,unsigned long color,unsigned long bgcolor,unsigned char ch,char drawfg,char drawbg,char mag)
{
unsigned char *vptr0; //pointer to vram
unsigned char *vptr; //pointer to vram
const unsigned char *cfont; //pointer to font
unsigned long cx,cy;
unsigned long b;
char mx,my;
if (ch>255) return;
cfont=font+ch*8;
vptr0=pgGetVramAddr(x,y);
for (cy=0; cy<8; cy++) {
for (my=0; my<mag; my++) {
vptr=vptr0;
b=0x80;
for (cx=0; cx<8; cx++) {
for (mx=0; mx<mag; mx++) {
if ((*cfont&b)!=0) {
if (drawfg) *(unsigned short *)vptr=color;
} else {
if (drawbg) *(unsigned short *)vptr=bgcolor;
}
vptr+=1*2;
}
b=b>>1;
}
vptr0+=512*2;
}
cfont++;
}
}
void cls()
{
pgFillvram(0);
pgScreenFlip();
pgFillvram(0);
}
// generate 7 transcendental values with one function
// p = function selector, #define the following:
// SIN 1 COS 2 TAN 3 EXP 4 SINH 5 COSH 6 TANH 7
// n = convergence cutoff, n = 3 is typical,
// higher n gives higher precision, but slower execution
//
float trigfunc(int p, int n, float x)
{
int k;
float r, s, t;
if (p <= 3)
{
r = - x * x; // trig
}
else
{
r = x * x; // hyperbolic
}
s = 4 * n + 2;
for (k = n; k > 0; k--)
{
s = 4 * k - 2 + r/s;
}
switch (p % 4)
{
case 0 : t = (s + x)/(s - x); // exp
break;
case 1 : t = 2 * x * s/(s * s - r); // sin, sinh
break;
case 2 : t = (s * s + r)/(s * s - r); // cos, cosh
break;
case 3 : t = 2 * x * s/(s * s + r); // tan, tanh
break;
}
return (t);
}
unsigned _rand(unsigned long MAX)
{
Seed1 = (Seed1 + 46906481) ^ Seed2;
Seed2 = Seed1 ^ ( ((Seed2<<3) | (Seed2 >> 29)) + 103995407);
return (Seed1 - Seed2)%MAX;
}
pgLine(float x1, float y1, float x2, float y2, unsigned long index)
{
float indice;
float dx,dy;
float x,y;
float m;
if(x1==x2)
x2++;
if(x1>x2)
indice=1;
else
indice=0;
if (!indice)
{
dx=x2-x1;
dy=y2-y1;
m=(float)dy/dx;
if(m<=1&&m>=-1)
{
for(x=0; x<dx; x++)
{
y=m*x+y1;
PutPixel(x+x1, y, index);
}
}
else
{
if(dx==1&&y2>y1)
{
for(;y1<y2;y1++)
PutPixel(x1,y1,index);
}
else
if(dx==1&&y2<y1)
{
for(;y1>y2;y1--)
PutPixel(x1,y1,index);
}
else
{
m=(float)dx/dy;
if(m>0)
{
for(y=0;y<=dy;y++)
{
x=m*y+x1;
PutPixel(x,y+y1,index);
}
}
else
{
for(y=0;y>dy;y--)
{
x=m*y+x1;
PutPixel(x,y+y1,index);
}
}
}
}
}
if (indice==1)
{
dx=x1-x2;
dy=y1-y2;
m=(float)dy/dx;
if(m<=1&&m>=-1)
{
for(x=0;x<dx;x++)
{
y=y1-m*x;
PutPixel(x1-x, y, index);
}
}
else
{
m=(float)dx/dy;
if(m>0)
{
for(y=0;y<dy;y++)
{
x=x1-m*y;
PutPixel(x,y1-y,index);
}
}
else
{
for(y=0;y>dy;y--)
{
x=x1-m*y;
PutPixel(x,y1-y,index);
}
}
}
}
}