1 // primitive graphics for Hello World sce
2 
3 #include "pg.h"
4 #include "font.c"
5 
6 #include <math.h>
7 
8 #define pg_vramtop ((unsigned char *)0x04000000)
9 long pg_screenmode;
10 long pg_showframe;
11 long pg_drawframe=0;
12 
pgWaitVn(unsigned long count)13 void pgWaitVn(unsigned long count)
14 {
15 	for (; count>0; --count) {
16 		sceDisplayWaitVblankStart();
17 	}
18 }
19 
20 
pgWaitV()21 void pgWaitV()
22 {
23 	sceDisplayWaitVblankStart();
24 }
25 
26 
pgGetVramAddr(unsigned long x,unsigned long y)27 unsigned char *pgGetVramAddr(unsigned long x,unsigned long y)
28 {
29 	return pg_vramtop+pg_drawframe*FRAMESIZE+x*PIXELSIZE*2+y*LINESIZE*2+0x40000000;
30 }
31 
32 
pgPrint(unsigned long x,unsigned long y,unsigned long color,const char * str,int bg)33 void pgPrint(unsigned long x,unsigned long y,unsigned long color,const char *str,int bg)
34 {
35 	while (*str!=0 && x<CMAX_X && y<CMAX_Y) {
36 		pgPutChar(x*8,y*8,color,0,*str,1,bg,1);
37 		str++;
38 		x++;
39 		if (x>=CMAX_X) {
40 			x=0;
41 			y++;
42 		}
43 	}
44 }
45 
pgPrintCenter(unsigned long y,unsigned long color,const char * str,int bg)46 void pgPrintCenter(unsigned long y,unsigned long color,const char *str,int bg)
47 {
48 	unsigned long x=(480-strlen(str)*8)>>4;
49 	while (*str!=0 && x<CMAX_X && y<CMAX_Y) {
50 		pgPutChar(x*8,y*8,color,0,*str,1,bg,1);
51 		str++;
52 		x++;
53 		if (x>=CMAX_X) {
54 			x=0;
55 			y++;
56 		}
57 	}
58 }
59 
pgDrawFrame(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2,unsigned long color)60 void pgDrawFrame(unsigned long x1, unsigned long y1, unsigned long x2, unsigned long y2, unsigned long color)
61 {
62 	unsigned char *vptr0;		//pointer to vram
63 	unsigned long i;
64 
65 	vptr0=pgGetVramAddr(0,0);
66 	for(i=x1; i<=x2; i++){
67 		((unsigned short *)vptr0)[i*PIXELSIZE + y1*LINESIZE] = color;
68 		((unsigned short *)vptr0)[i*PIXELSIZE + y2*LINESIZE] = color;
69 	}
70 	for(i=y1; i<=y2; i++){
71 		((unsigned short *)vptr0)[x1*PIXELSIZE + i*LINESIZE] = color;
72 		((unsigned short *)vptr0)[x2*PIXELSIZE + i*LINESIZE] = color;
73 	}
74 }
75 
pgFillBox(unsigned long x1,unsigned long y1,unsigned long x2,unsigned long y2,unsigned long color)76 void pgFillBox(unsigned long x1, unsigned long y1, unsigned long x2, unsigned long y2, unsigned long color)
77 {
78 	unsigned char *vptr0;		//pointer to vram
79 	unsigned long i, j;
80 
81 	vptr0=pgGetVramAddr(0,0);
82 	for(i=y1; i<=y2; i++){
83 		for(j=x1; j<=x2; j++){
84 			((unsigned short *)vptr0)[j*PIXELSIZE + i*LINESIZE] = color;
85 		}
86 	}
87 }
88 
pgFillvram(unsigned long color)89 void pgFillvram(unsigned long color)
90 {
91 
92 	unsigned char *vptr0;		//pointer to vram
93 	unsigned long i;
94 
95 	vptr0=pgGetVramAddr(0,0);
96 	for (i=0; i<FRAMESIZE/2; i++) {
97 		*(unsigned short *)vptr0=color;
98 		vptr0+=PIXELSIZE*2;
99 	}
100 
101 }
102 
pgBitBlt(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short * d)103 void pgBitBlt(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short *d)
104 {
105 	unsigned char *vptr0;		//pointer to vram
106 	unsigned char *vptr;		//pointer to vram
107 	unsigned long xx,yy,mx,my;
108 	const unsigned short *dd;
109 
110 	vptr0=pgGetVramAddr(x,y);
111 	for (yy=0; yy<h; yy++) {
112 		for (my=0; my<mag; my++) {
113 			vptr=vptr0;
114 			dd=d;
115 			for (xx=0; xx<w; xx++) {
116 				for (mx=0; mx<mag; mx++) {
117 					*(unsigned short *)vptr=*dd;
118 					vptr+=PIXELSIZE*2;
119 				}
120 				dd++;
121 			}
122 			vptr0+=LINESIZE*2;
123 		}
124 		d+=w;
125 	}
126 
127 }
128 
129 //�悭�킩��Ȃ�x2 - LCK
pgBitBltN2(unsigned long x,unsigned long y,unsigned long h,unsigned long * d)130 void pgBitBltN2(unsigned long x,unsigned long y,unsigned long h,unsigned long *d)
131 {
132 	unsigned long *v0;		//pointer to vram
133 	unsigned long xx,yy;
134 	unsigned long dx,d0,d1;
135 
136 	v0=(unsigned long *)pgGetVramAddr(x,y);
137 	for (yy=h; yy>0; --yy) {
138 		for (xx=80; xx>0; --xx) {
139 			dx=*(d++);
140 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
141 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
142 			v0[LINESIZE/2]=d0;
143 			v0[LINESIZE/2+1]=d1;
144 			*(v0++)=d0;
145 			*(v0++)=d1;
146 		}
147 		v0+=(LINESIZE-160);
148 		d+=8;
149 	}
150 }
151 
152 //by z-rwt
pgBitBltStScan(unsigned long x,unsigned long y,unsigned long h,unsigned long * d)153 void pgBitBltStScan(unsigned long x,unsigned long y,unsigned long h,unsigned long *d)
154 {
155 	unsigned long *v0;		//pointer to vram
156 	unsigned long xx,yy;
157 	unsigned long dx,d0,d1;
158 
159 	v0=(unsigned long *)pgGetVramAddr(x,y);
160 	for (yy=h; yy>0; --yy) {
161 		for (xx=80; xx>0; --xx) {
162 			dx=*(d++);
163 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
164 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
165 			*(v0++)=d0;
166 			*(v0++)=d1;
167 		}
168 		v0+=(LINESIZE-160);
169 		d+=8;
170 	}
171 }
172 
pgBitBltSt2wotop(unsigned long x,unsigned long y,unsigned long h,unsigned long * d)173 void pgBitBltSt2wotop(unsigned long x,unsigned long y,unsigned long h,unsigned long *d)
174 {
175 	unsigned long *v0;		//pointer to vram
176 	unsigned long xx,yy;
177 	unsigned long dx,d0,d1;
178 
179 	v0=(unsigned long *)pgGetVramAddr(x,y);
180 	for (yy=0; yy<16; yy++){
181 		for (xx=80; xx>0; --xx) {
182 			dx=*(d++);
183 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
184 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
185 			*(v0++)=d0;
186 			*(v0++)=d1;
187 		}
188 		v0+=(LINESIZE/2-160);
189 		d+=8;
190 	}
191 	for (; yy<h; yy++) {
192 		for (xx=80; xx>0; --xx) {
193 			dx=*(d++);
194 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
195 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
196 			v0[LINESIZE/2]=d0;
197 			v0[LINESIZE/2+1]=d1;
198 			*(v0++)=d0;
199 			*(v0++)=d1;
200 		}
201 		v0+=(LINESIZE-160);
202 		d+=8;
203 	}
204 }
205 
pgBitBltSt2wobot(unsigned long x,unsigned long y,unsigned long h,unsigned long * d)206 void pgBitBltSt2wobot(unsigned long x,unsigned long y,unsigned long h,unsigned long *d)
207 {
208 	unsigned long *v0;		//pointer to vram
209 	unsigned long xx,yy;
210 	unsigned long dx,d0,d1;
211 
212 	v0=(unsigned long *)pgGetVramAddr(x,y);
213 	for (yy=0; yy<h-16; yy++){
214 		for (xx=80; xx>0; --xx) {
215 			dx=*(d++);
216 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
217 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
218 			v0[LINESIZE/2]=d0;
219 			v0[LINESIZE/2+1]=d1;
220 			*(v0++)=d0;
221 			*(v0++)=d1;
222 		}
223 		v0+=(LINESIZE-160);
224 		d+=8;
225 	}
226 	for (; yy<h; yy++) {
227 		for (xx=80; xx>0; --xx) {
228 			dx=*(d++);
229 			d0=( (dx&0x0000ffff)|((dx&0x0000ffff)<<16) );
230 			d1=( (dx&0xffff0000)|((dx&0xffff0000)>>16) );
231 			*(v0++)=d0;
232 			*(v0++)=d1;
233 		}
234 		v0+=(LINESIZE/2-160);
235 		d+=8;
236 	}
237 }
238 
pgPutChar(unsigned long x,unsigned long y,unsigned long color,unsigned long bgcolor,unsigned char ch,char drawfg,char drawbg,char mag)239 void pgPutChar(unsigned long x,unsigned long y,unsigned long color,unsigned long bgcolor,unsigned char ch,char drawfg,char drawbg,char mag)
240 {
241 	unsigned char *vptr0;		//pointer to vram
242 	unsigned char *vptr;		//pointer to vram
243 	const unsigned char *cfont;		//pointer to font
244 	unsigned long cx,cy;
245 	unsigned long b;
246 	char mx,my;
247 
248 	cfont=font+ch*8;
249 	vptr0=pgGetVramAddr(x,y);
250 	for (cy=0; cy<8; cy++) {
251 		for (my=0; my<mag; my++) {
252 			vptr=vptr0;
253 			b=0x80;
254 			for (cx=0; cx<8; cx++) {
255 				for (mx=0; mx<mag; mx++) {
256 					if ((*cfont&b)!=0) {
257 						if (drawfg) *(unsigned short *)vptr=color;
258 					} else {
259 						if (drawbg) *(unsigned short *)vptr=bgcolor;
260 					}
261 					vptr+=PIXELSIZE*2;
262 				}
263 				b=b>>1;
264 			}
265 			vptr0+=LINESIZE*2;
266 		}
267 		cfont++;
268 	}
269 }
270 
pgScreenFrame(long mode,long frame)271 void pgScreenFrame(long mode,long frame)
272 {
273 	pg_screenmode=mode;
274 	frame=(frame?1:0);
275 	pg_showframe=frame;
276 	if (mode==0) {
277 		//screen off
278 		pg_drawframe=frame;
279 		sceDisplaySetFrameBuf(0,0,0,1);
280 	} else if (mode==1) {
281 		//show/draw same
282 		pg_drawframe=frame;
283 		sceDisplaySetFrameBuf(pg_vramtop+(pg_showframe?FRAMESIZE:0),LINESIZE,PIXELSIZE,1);
284 	} else if (mode==2) {
285 		//show/draw different
286 		pg_drawframe=(frame?0:1);
287 		sceDisplaySetFrameBuf(pg_vramtop+(pg_showframe?FRAMESIZE:0),LINESIZE,PIXELSIZE,1);
288 	}
289 }
290 
pgScreenFlip()291 void pgScreenFlip()
292 {
293 	pg_showframe=pg_drawframe;
294 	pg_drawframe++;
295 	pg_drawframe&=1;
296 	sceDisplaySetFrameBuf(pg_vramtop+pg_showframe*FRAMESIZE,LINESIZE,PIXELSIZE,0);
297 }
298 
299 
pgScreenFlipV()300 void pgScreenFlipV()
301 {
302 	pgWaitV();
303 	pgScreenFlip();
304 }
305 /******************************************************************************/
306 
307 static unsigned Seed1;
308 static unsigned Seed2;
309 
_rand(unsigned long MAX)310 unsigned _rand(unsigned long MAX)
311 {
312 	Seed1 = (Seed1 + 46906481) ^ Seed2;
313 	Seed2 = Seed1 ^ ( ((Seed2<<3) | (Seed2 >> 29)) + 103995407);
314 
315 	return (Seed1 - Seed2)%MAX;
316 }
317 
PutGfx(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short * d)318 void PutGfx(unsigned long x,unsigned long y,unsigned long w,unsigned long h,unsigned long mag,const unsigned short *d)
319 {
320 	unsigned char *vptr0;		//pointer to vram
321 	unsigned char *vptr;		//pointer to vram
322 	unsigned long xx,yy,mx,my;
323 	const unsigned short *dd;
324 
325 	vptr0=pgGetVramAddr(x,y);
326 	for (yy=0; yy<h; yy++) {
327 		for (my=0; my<mag; my++) {
328 			vptr=vptr0;
329 			dd=d;
330 			for (xx=0; xx<w; xx++) {
331 				for (mx=0; mx<mag; mx++) {
332 					*(unsigned short *)vptr=*dd;
333 					vptr+=2;
334 				}
335 				dd++;
336 			}
337 			vptr0+=512*2;
338 		}
339 		d+=w;
340 	}
341 
342 }
343 
344