1
2 #include "RSDL_wrapper.h"
3 #include <stdio.h>
4 #include <string.h>
5
6 #ifdef _3DS
7 void* linearMemAlign(size_t size, size_t alignment);
8 void linearFree(void* mem);
9 #endif
10
11 static const char *cross[] = {
12 "X ",
13 "XX ",
14 "X.X ",
15 "X..X ",
16 "X...X ",
17 "X....X ",
18 "X.....X ",
19 "X......X ",
20 "X.......X ",
21 "X........X ",
22 "X.....XXXXX ",
23 "X..X..X ",
24 "X.X X..X ",
25 "XX X..X ",
26 "X X..X ",
27 " X..X ",
28 " X..X ",
29 " X..X ",
30 " XX ",
31 " ",
32 };
33
34 #ifdef M16B
DrawPointBmp(unsigned short int * buffer,int x,int y,unsigned short int color,int rwidth,int rheight)35 void DrawPointBmp(unsigned short int *buffer,int x, int y, unsigned short int color,int rwidth,int rheight)
36 #else
37 void DrawPointBmp(unsigned int *buffer,int x, int y, unsigned int color,int rwidth,int rheight)
38 #endif
39
40 {
41 int idx;
42
43 idx=x+y*rwidth;
44 if(idx>=0 && idx<rwidth*rheight)
45 buffer[idx]=color;
46 }
47
draw_cross(RSDL_Surface * surface,int x,int y)48 void draw_cross(RSDL_Surface *surface,int x,int y) {
49
50 int i,j,idx;
51 int dx=32,dy=20;
52 #ifdef M16B
53 unsigned short int col=0xffff;
54 #else
55 unsigned int col=0xffffffff;
56 #endif
57 int w=surface->w;
58 int h=surface->h;
59
60 for(j=y;j<y+dy;j++){
61 idx=0;
62 for(i=x;i<x+dx;i++){
63
64 if(cross[j-y][idx]=='.')DrawPointBmp(surface->pixels,i,j,col,w,h);
65 else if(cross[j-y][idx]=='X')DrawPointBmp(surface->pixels,i,j,0,w,h);
66 idx++;
67 }
68 }
69 }
70
Retro_MapRGB(RSDL_PixelFormat * a,int r,int g,int b)71 unsigned int Retro_MapRGB(RSDL_PixelFormat *a, int r, int g, int b){
72
73 return (r >> a->Rloss) << a->Rshift
74 | (g >> a->Gloss) << a->Gshift
75 | (b >> a->Bloss) << a->Bshift
76 | a->Amask;
77 }
78
Retro_MapRGBA(RSDL_PixelFormat * a,int r,int g,int b,int alpha)79 unsigned int Retro_MapRGBA(RSDL_PixelFormat *a, int r, int g, int b,int alpha){
80
81 return (r >> a->Rloss) << a->Rshift
82 | (g >> a->Gloss) << a->Gshift
83 | (b >> a->Bloss) << a->Bshift
84 | ((alpha >> a->Aloss) << a->Ashift & a->Amask);
85 }
86
87 static __inline__
RSDL_IntersectRect(const RSDL_Rect * A,const RSDL_Rect * B,RSDL_Rect * intersection)88 RSDL_bool RSDL_IntersectRect(const RSDL_Rect *A, const RSDL_Rect *B, RSDL_Rect *intersection)
89 {
90 int Amin, Amax, Bmin, Bmax;
91
92 /* Horizontal intersection */
93 Amin = A->x;
94 Amax = Amin + A->w;
95 Bmin = B->x;
96 Bmax = Bmin + B->w;
97 if(Bmin > Amin)
98 Amin = Bmin;
99 intersection->x = Amin;
100 if(Bmax < Amax)
101 Amax = Bmax;
102 intersection->w = Amax - Amin > 0 ? Amax - Amin : 0;
103
104 /* Vertical intersection */
105 Amin = A->y;
106 Amax = Amin + A->h;
107 Bmin = B->y;
108 Bmax = Bmin + B->h;
109 if(Bmin > Amin)
110 Amin = Bmin;
111 intersection->y = Amin;
112 if(Bmax < Amax)
113 Amax = Bmax;
114 intersection->h = Amax - Amin > 0 ? Amax - Amin : 0;
115
116 return (intersection->w && intersection->h);
117 }
118 /*
119 * Set the clipping rectangle for a blittable surface
120 */
RSDL_SetClipRect(RSDL_Surface * surface,const RSDL_Rect * rect)121 RSDL_bool RSDL_SetClipRect(RSDL_Surface *surface, const RSDL_Rect *rect)
122 {
123 RSDL_Rect full_rect;
124
125 /* Don't do anything if there's no surface to act on */
126 if ( ! surface ) {
127 return RSDL_FALSE;
128 }
129
130 /* Set up the full surface rectangle */
131 full_rect.x = 0;
132 full_rect.y = 0;
133 full_rect.w = surface->w;
134 full_rect.h = surface->h;
135
136 /* Set the clipping rectangle */
137 if ( ! rect ) {
138 surface->clip_rect = full_rect;
139 return 1;
140 }
141 return RSDL_IntersectRect(rect, &full_rect, &surface->clip_rect);
142 }
143
RSDL_GetClipRect(RSDL_Surface * surface,RSDL_Rect * rect)144 void RSDL_GetClipRect(RSDL_Surface *surface, RSDL_Rect *rect)
145 {
146 if ( surface && rect ) {
147 *rect = surface->clip_rect;
148 }
149 }
150
151
Retro_FreeSurface(RSDL_Surface * surf)152 void Retro_FreeSurface(RSDL_Surface *surf )
153 {
154 if (!surf)
155 return;
156
157 //printf("free surf format palette color\n");
158
159 if(surf->format->palette->colors)
160 free(surf->format->palette->colors);
161
162 //printf("free surf format palette \n");
163 if(surf->format->palette)
164 free(surf->format->palette);
165 //printf("free surf format \n");
166 if(surf->format)
167 free(surf->format);
168 //printf("free surf pixel \n");
169
170 #ifdef _3DS
171 if(surf->pixels)
172 linearFree(surf->pixels);
173 #else
174 if(surf->pixels)
175 free(surf->pixels);
176 #endif
177 }
178
179
Retro_CreateRGBSurface32(int w,int h,int d,int rm,int rg,int rb,int ra)180 RSDL_Surface *Retro_CreateRGBSurface32( int w,int h, int d, int rm,int rg,int rb,int ra)
181 {
182 //printf("s(%d,%d,%d) (%x,%x,%x,%x)\n",w,h,d,rm,rg,rb,ra);
183
184 RSDL_Surface *bitmp;
185
186 bitmp = (RSDL_Surface *) calloc(1, sizeof(*bitmp));
187 if (bitmp == NULL)
188 {
189 printf("tex surface failed");
190 return NULL;
191 }
192
193 bitmp->format = (RSDL_PixelFormat *) calloc(1,sizeof(*bitmp->format));
194 if (bitmp->format == NULL)
195 {
196 printf("tex format failed");
197 return NULL;
198 }
199
200 bitmp->format->palette =(RSDL_Palette *) calloc(1,sizeof(*bitmp->format->palette));
201 if (bitmp->format->palette == NULL)
202 {
203 printf("tex format palette failed");
204 return NULL;
205 }
206
207 printf("create surface XR8G8B8 libretro\n");
208 //FIXME: why pal for 32 bits surface ?
209 bitmp->format->palette->ncolors=256;
210 bitmp->format->palette->colors=(RSDL_Color *)malloc(1024);
211 bitmp->format->palette->version=0;
212 bitmp->format->palette->refcount=0;
213 memset(bitmp->format->palette->colors,0x0,1024);
214
215 bitmp->format->BitsPerPixel = 32;
216 bitmp->format->BytesPerPixel = 4;
217 bitmp->format->Rloss=0;
218 bitmp->format->Gloss=0;
219 bitmp->format->Bloss=0;
220 bitmp->format->Aloss=0;
221
222 bitmp->format->Rshift=16;
223 bitmp->format->Gshift=8;
224 bitmp->format->Bshift=0;
225 bitmp->format->Ashift=24;
226
227 bitmp->format->Rmask=0x00ff0000;
228 bitmp->format->Gmask=0x0000ff00;
229 bitmp->format->Bmask=0x000000ff;
230 bitmp->format->Amask=0xff000000;
231
232 /*
233 bitmp->format->Rshift=16;
234 bitmp->format->Gshift=8;
235 bitmp->format->Bshift=0;
236 bitmp->format->Ashift=24;
237
238 bitmp->format->Rmask=0x00ff0000;
239 bitmp->format->Gmask=0x0000ff00;
240 bitmp->format->Bmask=0x000000ff;
241 bitmp->format->Amask=0xff000000;
242 */
243
244 bitmp->format->colorkey=0;
245 bitmp->format->alpha=255;//0;
246 //bitmp->format->palette = NULL;
247
248 bitmp->flags=0;
249 bitmp->w=w;
250 bitmp->h=h;
251 bitmp->pitch=w*4;
252
253 #ifdef _3DS
254 bitmp->pixels=linearMemAlign(sizeof(unsigned char)*h*w*4, 0x80);
255 #else
256 bitmp->pixels=malloc(sizeof(unsigned char)*h*w*4);
257 #endif
258 if (!bitmp->pixels) {
259 printf("failed alloc pixels\n");
260 Retro_FreeSurface(bitmp);
261 return NULL;
262 }
263 memset(bitmp->pixels,0, h*w*4);
264
265 bitmp->clip_rect.x=0;
266 bitmp->clip_rect.y=0;
267 bitmp->clip_rect.w=w;
268 bitmp->clip_rect.h=h;
269
270 //printf("fin prepare tex:%dx%dx%d\n",bitmp->w,bitmp->h,bitmp->format->BytesPerPixel);
271 return bitmp;
272
273 }
274
Retro_CreateRGBSurface16(int w,int h,int d,int rm,int rg,int rb,int ra)275 RSDL_Surface *Retro_CreateRGBSurface16( int w,int h, int d, int rm,int rg,int rb,int ra)
276 {
277 //printf("s(%d,%d,%d) (%x,%x,%x,%x)\n",w,h,d,rm,rg,rb,ra);
278
279 RSDL_Surface *bitmp;
280
281 bitmp = (RSDL_Surface *) calloc(1, sizeof(*bitmp));
282 if (bitmp == NULL)
283 {
284 printf("tex surface failed");
285 return NULL;
286 }
287
288 bitmp->format =(RSDL_PixelFormat *) calloc(1,sizeof(*bitmp->format));
289 if (bitmp->format == NULL)
290 {
291 printf("tex format failed");
292 return NULL;
293 }
294
295 bitmp->format->palette = (RSDL_Palette *)calloc(1,sizeof(*bitmp->format->palette));
296 if (bitmp->format->palette == NULL)
297 {
298 printf("tex format palette failed");
299 return NULL;
300 }
301
302 printf("create surface RGB565 libretro\n");
303 //FIXME: why pal for 32 bits surface ?
304 bitmp->format->palette->ncolors=256;
305 bitmp->format->palette->colors=(RSDL_Color *)malloc(256*2);
306 bitmp->format->palette->version=0;
307 bitmp->format->palette->refcount=0;
308 memset(bitmp->format->palette->colors,0x0,256*2);
309
310 bitmp->format->BitsPerPixel = 16;
311 bitmp->format->BytesPerPixel = 2;
312 bitmp->format->Rloss=3;
313 bitmp->format->Gloss=2;
314 bitmp->format->Bloss=3;
315 bitmp->format->Aloss=0;
316
317 bitmp->format->Rshift=11;
318 bitmp->format->Gshift=5;
319 bitmp->format->Bshift=0;
320 bitmp->format->Ashift=0;
321
322 bitmp->format->Rmask=0x0000F800;
323 bitmp->format->Gmask=0x000007E0;
324 bitmp->format->Bmask=0x0000001F;
325 bitmp->format->Amask=0x00000000;
326
327 /*
328 bitmp->format->Rshift=16;
329 bitmp->format->Gshift=8;
330 bitmp->format->Bshift=0;
331 bitmp->format->Ashift=24;
332
333 bitmp->format->Rmask=0x00ff0000;
334 bitmp->format->Gmask=0x0000ff00;
335 bitmp->format->Bmask=0x000000ff;
336 bitmp->format->Amask=0xff000000;
337 */
338
339 bitmp->format->colorkey=0;
340 bitmp->format->alpha=255;//0;
341 //bitmp->format->palette = NULL;
342
343 bitmp->flags=0;
344 bitmp->w=w;
345 bitmp->h=h;
346 bitmp->pitch=w*2;
347 #ifdef _3DS
348 bitmp->pixels=linearMemAlign(sizeof(unsigned char)*h*w*2, 0x80);
349 #else
350 bitmp->pixels=malloc(sizeof(unsigned char)*h*w*2);
351 #endif
352 if (!bitmp->pixels) {
353 printf("failed alloc pixels\n");
354 Retro_FreeSurface(bitmp);
355 return NULL;
356 }
357 memset(bitmp->pixels,0, h*w*2);
358
359 bitmp->clip_rect.x=0;
360 bitmp->clip_rect.y=0;
361 bitmp->clip_rect.w=w;
362 bitmp->clip_rect.h=h;
363
364 //printf("fin prepare tex:%dx%dx%d\n",bitmp->w,bitmp->h,bitmp->format->BytesPerPixel);
365 return bitmp;
366
367 }
368 #include "font2.i"
369 #ifdef M16B
Retro_Draw_string(RSDL_Surface * surface,signed short int x,signed short int y,const char * string,unsigned short maxstrlen,unsigned short xscale,unsigned short yscale,unsigned short fg,unsigned short bg)370 void Retro_Draw_string(RSDL_Surface *surface, signed short int x, signed short int y, const char *string,unsigned short maxstrlen,unsigned short xscale, unsigned short yscale, unsigned short fg, unsigned short bg)
371 #else
372 void Retro_Draw_string(RSDL_Surface *surface, signed short int x, signed short int y, const char *string,unsigned short maxstrlen,unsigned short xscale, unsigned short yscale, unsigned fg, unsigned bg)
373 #endif
374 {
375 int k,strlen;
376 unsigned char *linesurf;
377
378 int col, bit;
379 unsigned char b;
380
381 int xrepeat, yrepeat;
382 #ifdef M16B
383 signed short int ypixel;
384 unsigned short *yptr;
385
386 unsigned short*mbuffer=(unsigned short*)surface->pixels;
387 #else
388 signed int ypixel;
389 unsigned *yptr;
390
391 unsigned *mbuffer=(unsigned*)surface->pixels;
392 #endif
393
394
395 #define VIRTUAL_WIDTH surface->w
396
397 if ((surface->clip_rect.w==0) || (surface->clip_rect.h==0)) {
398 return;
399 }
400
401
402 #define charWidthLocal 8
403 #define charHeightLocal 8
404
405 Sint16 left, right, top, bottom;
406 Sint16 x1, y1, x2, y2;
407
408 left = surface->clip_rect.x;
409 x2 = x + charWidthLocal;
410 if (x2<left) {
411 return;
412 }
413 right = surface->clip_rect.x + surface->clip_rect.w - 1;
414 x1 = x;
415 if (x1>right) {
416 return;
417 }
418 top = surface->clip_rect.y;
419 y2 = y + charHeightLocal;
420 if (y2<top) {
421 return;
422 }
423 bottom = surface->clip_rect.y + surface->clip_rect.h - 1;
424 y1 = y;
425 if (y1>bottom) {
426 return;
427 }
428
429
430 if(string==NULL)return;
431 for(strlen = 0; strlen<maxstrlen && string[strlen]; strlen++) {}
432
433
434 int surfw=strlen * 7 * xscale;
435 int surfh=8 * yscale;
436
437 #ifdef M16B
438 linesurf =(unsigned char *)malloc(sizeof(unsigned short)*surfw*surfh );
439 yptr = (unsigned short *)&linesurf[0];
440
441 #else
442 linesurf =(unsigned char *)malloc(sizeof(unsigned )*surfw*surfh );
443 yptr = (unsigned *)&linesurf[0];
444
445 #endif
446 // yptr = (unsigned *)&linesurf[0];
447
448
449 for(ypixel = 0; ypixel<8; ypixel++) {
450
451 for(col=0; col<strlen; col++) {
452
453 b = font_array[(string[col]^0x80)*8 + ypixel];
454
455 for(bit=0; bit<7; bit++, yptr++) {
456 *yptr = (b & (1<<(7-bit))) ? fg : bg;
457 for(xrepeat = 1; xrepeat < xscale; xrepeat++, yptr++)
458 yptr[1] = *yptr;
459 }
460 }
461
462 for(yrepeat = 1; yrepeat < yscale; yrepeat++)
463 for(xrepeat = 0; xrepeat<surfw; xrepeat++, yptr++)
464 *yptr = yptr[-surfw];
465
466 }
467
468 #ifdef M16B
469 yptr = (unsigned short*)&linesurf[0];
470 #else
471 yptr = (unsigned *)&linesurf[0];
472 #endif
473
474 for(yrepeat = y; yrepeat < y+ surfh; yrepeat++)
475 for(xrepeat = x; xrepeat< x+surfw; xrepeat++,yptr++)
476 if(*yptr!=0 && (xrepeat+yrepeat*VIRTUAL_WIDTH) < surface->w*surface->h )mbuffer[xrepeat+yrepeat*VIRTUAL_WIDTH] = *yptr;
477
478 free(linesurf);
479
480 }
481 #ifdef M16B
Retro_Draw_char(RSDL_Surface * surface,signed short int x,signed short int y,char string,unsigned short xscale,unsigned short yscale,unsigned short fg,unsigned short bg)482 void Retro_Draw_char(RSDL_Surface *surface, signed short int x, signed short int y, char string,unsigned short xscale, unsigned short yscale, unsigned short fg, unsigned short bg)
483 #else
484 void Retro_Draw_char(RSDL_Surface *surface, signed short int x, signed short int y, char string,unsigned short xscale, unsigned short yscale, unsigned fg, unsigned bg)
485 #endif
486 {
487 int k,strlen;
488 unsigned char *linesurf;
489 // signed int ypixel;
490 int col, bit;
491 unsigned char b;
492
493 int xrepeat, yrepeat;
494
495 #ifdef M16B
496 signed short int ypixel;
497 unsigned short *yptr;
498
499 unsigned short*mbuffer=(unsigned short*)surface->pixels;
500 #else
501 signed int ypixel;
502 unsigned *yptr;
503
504 unsigned *mbuffer=(unsigned*)surface->pixels;
505 #endif
506 // unsigned *yptr;
507
508 // unsigned *mbuffer=(unsigned*)surface->pixels;
509
510 #define VIRTUAL_WIDTH surface->w
511
512 if ((surface->clip_rect.w==0) || (surface->clip_rect.h==0)) {
513 return;
514 }
515
516
517 #define charWidthLocal 7*xscale
518 #define charHeightLocal 8*yscale
519
520 Sint16 left, right, top, bottom;
521 Sint16 x1, y1, x2, y2;
522
523 left = surface->clip_rect.x;
524 x2 = x + charWidthLocal;
525 if (x2<left) {
526 return;
527 }
528 right = surface->clip_rect.x + surface->clip_rect.w - 1;
529 x1 = x;
530 if (x1>right) {
531 return;
532 }
533 top = surface->clip_rect.y;
534 y2 = y + charHeightLocal;
535 if (y2<top) {
536 return;
537 }
538 bottom = surface->clip_rect.y + surface->clip_rect.h - 1;
539 y1 = y;
540 if (y1>bottom) {
541 return;
542 }
543
544
545 strlen = 1;
546
547 int surfw=strlen * charWidthLocal;
548 int surfh=charHeightLocal;
549
550 #ifdef M16B
551 linesurf =(unsigned char *)malloc(sizeof(unsigned short)*surfw*surfh );
552 yptr = (unsigned short *)&linesurf[0];
553
554 #else
555 linesurf =(unsigned char *)malloc(sizeof(unsigned )*surfw*surfh );
556 yptr = (unsigned *)&linesurf[0];
557
558 #endif
559 // linesurf =(unsigned char *)malloc(sizeof(unsigned )*surfw*surfh );
560 // yptr = (unsigned *)&linesurf[0];
561
562
563 for(ypixel = 0; ypixel<8; ypixel++) {
564
565 //for(col=0; col<strlen; col++) {
566
567 b = font_array[(string^0x80)*8 + ypixel];
568
569 for(bit=0; bit<7; bit++, yptr++) {
570 *yptr = (b & (1<<(7-bit))) ? fg : bg;
571 for(xrepeat = 1; xrepeat < xscale; xrepeat++, yptr++)
572 yptr[1] = *yptr;
573 }
574 //}
575
576 for(yrepeat = 1; yrepeat < yscale; yrepeat++)
577 for(xrepeat = 0; xrepeat<surfw; xrepeat++, yptr++)
578 *yptr = yptr[-surfw];
579
580 }
581
582
583 #ifdef M16B
584 yptr = (unsigned short*)&linesurf[0];
585 #else
586 yptr = (unsigned *)&linesurf[0];
587 #endif
588 // yptr = (unsigned *)&linesurf[0];
589
590 for(yrepeat = y; yrepeat < y+ surfh; yrepeat++)
591 for(xrepeat = x; xrepeat< x+surfw; xrepeat++,yptr++)
592 if(*yptr!=0 && (xrepeat+yrepeat*VIRTUAL_WIDTH) < surface->w*surface->h )mbuffer[xrepeat+yrepeat*VIRTUAL_WIDTH] = *yptr;
593
594 free(linesurf);
595
596 }
597
598 #include "3x5_font.h"
599
600 #define CHAR_WIDTH 3
601 #define CHAR_HEIGHT 7
602
print(RSDL_Surface * buffer,int x,int y,unsigned couleur,unsigned char c)603 void print(RSDL_Surface *buffer,int x, int y, unsigned couleur,unsigned char c)
604 {
605
606 #ifdef M16B
607 unsigned short *mbuffer=(unsigned short*)buffer->pixels;
608 #else
609 unsigned *mbuffer=(unsigned*)buffer->pixels;
610 #endif
611
612 int i,j;
613
614 int w=buffer->w;
615 int h=buffer->h;
616
617 c = c & 0x7F;
618 if (c < ' ') {
619 c = 0;
620 } else {
621 c -= ' ';
622 }
623
624 unsigned char *chr = font35[c];
625
626 for (j=0; j<CHAR_WIDTH; j++) {
627 for (i=0; i<CHAR_HEIGHT; i++) {
628 if (chr[j] & (1<<i))
629 {
630 int idx=x+j+((y+i)*w);
631 mbuffer[idx]=couleur;
632 }
633 }
634 }
635
636 }
637