1 
Retro_SetColors(SDL_Surface * surface,SDL_Color * colors,int firstcolor,int ncolors)2 int Retro_SetColors(SDL_Surface *surface, SDL_Color *colors, int firstcolor, int ncolors)
3 {
4    return 0;
5 }
6 
Retro_SetPalette(SDL_Surface * surface,int flags,const SDL_Color * colors,int firstcolor,int ncolors)7 int Retro_SetPalette(SDL_Surface * surface, int flags, const SDL_Color * colors,int firstcolor, int ncolors)
8 {
9 
10    SDL_Palette * palette= surface->format->palette;
11 
12    int status = 0;
13 
14    /* Verify the parameters */
15    if (!palette)
16       return -1;
17    if (ncolors > (palette->ncolors - firstcolor))
18    {
19       ncolors = (palette->ncolors - firstcolor);
20       status = -1;
21    }
22 
23    if (colors != (palette->colors + firstcolor))
24       memcpy(palette->colors + firstcolor, colors,
25             ncolors * sizeof(*colors));
26    ++palette->version;
27    if (!palette->version)
28       palette->version = 1;
29 
30    return status;
31 }
32 
33 
Retro_MapRGB(SDL_PixelFormat * a,int r,int g,int b)34 unsigned int Retro_MapRGB(SDL_PixelFormat *a, int r, int g, int b)
35 {
36    if( a->BitsPerPixel == 16 )
37       return RGB565( (r)>>3, (g)>>3, (b)>>3);
38    else if( a->BitsPerPixel == 32 )
39       return r<<16|g<<8|b;
40    else if( a->BitsPerPixel == 8 )
41       return 0;//TODO closest match in lut
42    else return 0;
43 }
44 
45 static const char *cross[] = {
46    "X                               ",
47    "XX                              ",
48    "X.X                             ",
49    "X..X                            ",
50    "X...X                           ",
51    "X....X                          ",
52    "X.....X                         ",
53    "X......X                        ",
54    "X.......X                       ",
55    "X........X                      ",
56    "X.....XXXXX                     ",
57    "X..X..X                         ",
58    "X.X X..X                        ",
59    "XX  X..X                        ",
60    "X    X..X                       ",
61    "     X..X                       ",
62    "      X..X                      ",
63    "      X..X                      ",
64    "       XX                       ",
65    "                                ",
66 };
67 
draw_cross(SDL_Surface * surf,int x,int y)68 void draw_cross(SDL_Surface * surf,int x,int y)
69 {
70    int i,j,idx;
71    int dx=32,dy=20;
72    unsigned  color;
73 
74    for(j=y;j<y+dy;j++){
75       idx=0;
76       for(i=x;i<x+dx;i++){
77          if(cross[j-y][idx]=='.')SDL_DrawPixel(surf,i,j,SDL_MapRGB(surf->format, 255, 255, 255));
78          else if(cross[j-y][idx]=='X')SDL_DrawPixel(surf,i,j,SDL_MapRGB(surf->format, 0, 0, 0));
79          idx++;
80       }
81    }
82 
83 
84 }
85 
Retro_Fillrect(SDL_Surface * surf,SDL_Rect * rect,unsigned int col)86 void Retro_Fillrect(SDL_Surface * surf,SDL_Rect *rect,unsigned int col)
87 {
88    if(rect==NULL)SDL_DrawFRect(surf,surf->clip_rect.x,surf->clip_rect.y,surf->clip_rect.w ,surf->clip_rect.h,col);
89    else SDL_DrawFRect(surf,rect->x,rect->y,rect->w ,rect->h,col);
90 }
91 
Retro_FreeSurface(SDL_Surface * surf)92 void Retro_FreeSurface(SDL_Surface *surf )
93 {
94 
95    printf("free surf format palette color\n");
96    if(surf->format->palette->colors)
97       free(surf->format->palette->colors);
98 
99    printf("free surf format palette \n");
100    if(surf->format->palette)
101       free(surf->format->palette);
102    printf("free surf format  \n");
103    if(surf->format)
104       free(surf->format);
105    printf("free surf pixel  \n");
106    if(surf->pixels)
107       free(surf->pixels);
108    printf("free surf  \n");
109    if(surf)
110       free(surf);
111 
112 }
113 
Retro_BlitSurface(SDL_Surface * ss,SDL_Rect * sr,SDL_Surface * ds,SDL_Rect * dr)114 void Retro_BlitSurface(SDL_Surface *ss,SDL_Rect* sr,SDL_Surface *ds,SDL_Rect* dr)
115 {
116    SDL_Rect src,dst;
117    int x,y,w;
118    unsigned char *pout = NULL;
119    unsigned char *pin  = NULL;
120    int sBPP=ss->format->BytesPerPixel;
121    int dBPP=ds->format->BytesPerPixel;
122 
123    if(sr==NULL){
124       src.x=0;
125       src.y=0;
126       src.w=ss->clip_rect.w;
127       src.h=ss->clip_rect.h;
128    }
129    else {
130       src.x=sr->x;
131       src.y=sr->y;
132       src.w=sr->w;
133       src.h=sr->h;
134    }
135 
136    if(dr==NULL){
137       dst.x=0;
138       dst.y=0;
139       dst.w=ds->clip_rect.w;
140       dst.h=ds->clip_rect.h;
141    }
142    else {
143       dst.x=dr->x;
144       dst.y=dr->y;
145       dst.w=dr->w;
146       dst.h=dr->h;
147    }
148    //printf("s(%d,%d,%d,%d) d(%d,%d,%d,%d)\n",src.x,src.y,src.w,src.h,dst.x,dst.y,dst.w,dst.h);
149    pout=(unsigned char *)ds->pixels+(dst.x*dBPP+dst.y*ds->w*dBPP);
150    pin =(unsigned char *)ss->pixels+(src.x*sBPP+src.y*ss->w*sBPP);
151 #if 1
152    //COLORKEY
153    if(ss->flags&SDL_SRCCOLORKEY)
154    {
155       unsigned int key=ss->format->colorkey;
156       unsigned int res;
157 
158       for(y=0;y<src.h;y++)
159       {
160          for(x=0;x<src.w;x++)
161          {
162             if(sBPP==4)res=retro_unaligned32 (pin);
163             else if(sBPP==2)res=retro_unaligned16 (pin);
164             else  res=*pin;
165             //FIXME 16 to 32 , 8 to 16, 8 to 32 & 32 to 16 .
166             if(sBPP==1 && dBPP==1)
167             {
168                if(key!=*pin)*pout=*pin;
169                pout++;pin++;
170             }
171             else if(sBPP==1 && dBPP==4)
172             {
173                unsigned int mcoul=ss->format->palette->colors[*pin].r<<16|\
174                                   ss->format->palette->colors[*pin].g<<8|\
175                                   ss->format->palette->colors[*pin].b;
176                //printf("c:%x- ",mcoul);
177                retro_unaligned32 (pout) = mcoul;
178                pout += 4;
179                pin++;
180             }
181             else
182             {
183                for(w=0;w<dBPP;w++)
184                {
185                   if(res!=key)
186                      *pout=*pin;
187                   pout++;pin++;
188                }
189             }
190 
191          }
192          pin +=(ss->w-src.w)*sBPP;
193          pout+=(ds->w-src.w)*dBPP;
194       }
195       return;
196    }
197 #endif
198    for(y=0;y<src.h;y++)
199    {
200       for(x=0;x<src.w;x++)
201       {
202 
203          //FIXME 16 to 32 , 8 to 16, 8 to 32 & 32 to 16 .
204          if(sBPP==1 && dBPP==1)
205          {
206             *pout=*pin;
207             pout++;pin++;
208          }
209          else
210          {
211             if(sBPP==1 && dBPP==4)
212             {
213                unsigned int mcoul=ss->format->palette->colors[*pin].r<<16|\
214                                   ss->format->palette->colors[*pin].g<<8|\
215                                   ss->format->palette->colors[*pin].b;
216                //printf("c:%x- ",mcoul);
217                retro_unaligned32 (pout) = mcoul;
218                pout += 4;
219                pin++;
220             }
221             else
222             {
223                for(w=0;w<dBPP;w++)
224                {
225                   *pout=*pin;
226                   pout++;pin++;
227                }
228             }
229          }
230       }
231       pin +=(ss->w-src.w)*sBPP;
232       pout+=(ds->w-src.w)*dBPP;
233    }
234 }
235 
Retro_SetColorKey(SDL_Surface * surface,Uint32 flag,Uint32 key)236 int Retro_SetColorKey(SDL_Surface *surface, Uint32 flag, Uint32 key)
237 {
238    if(flag==SDL_SRCCOLORKEY)
239    {
240       surface->format->colorkey=key;
241       surface->flags|=SDL_SRCCOLORKEY;
242    }
243 
244    return 0;
245 }
246 
247 
Retro_CreateRGBSurface(int w,int h,int d,int rm,int rg,int rb,int ra)248 SDL_Surface *Retro_CreateRGBSurface( int w,int h, int d, int rm,int rg,int rb,int ra)
249 {
250    SDL_Surface *bitmp = (SDL_Surface *) calloc(1, sizeof(*bitmp));
251 
252    if (bitmp == NULL)
253    {
254       printf("tex surface failed");
255       return NULL;
256    }
257 
258    bitmp->format = calloc(1,sizeof(*bitmp->format));
259    if (bitmp->format == NULL)
260    {
261       printf("tex format failed");
262       return NULL;
263    }
264 
265    bitmp->format->palette = calloc(1,sizeof(*bitmp->format->palette));
266    if (bitmp->format->palette == NULL)
267    {
268       printf("tex format palette failed");
269       return NULL;
270    }
271    printf("create palette\n");
272    bitmp->format->palette->ncolors=256;
273    bitmp->format->palette->colors=malloc(1024);
274    bitmp->format->palette->version=0;
275    bitmp->format->palette->refcount=0;
276    memset(bitmp->format->palette->colors,0x0,1024);
277    printf("fill surf %d \n",bitmp->format->palette->colors[255].r);
278    if(d==16)
279    {
280       bitmp->format->BitsPerPixel = 16;
281       bitmp->format->BytesPerPixel = 2;
282       bitmp->format->Rloss=3;
283       bitmp->format->Gloss=3;
284       bitmp->format->Bloss=3;
285       bitmp->format->Aloss=0;
286       bitmp->format->Rshift=11;
287       bitmp->format->Gshift=6;
288       bitmp->format->Bshift=0;
289       bitmp->format->Ashift=0;
290       bitmp->format->Rmask=0x0000F800;
291       bitmp->format->Gmask=0x000007E0;
292       bitmp->format->Bmask=0x0000001F;
293       bitmp->format->Amask=0x00000000;
294       bitmp->format->colorkey=0;
295       bitmp->format->alpha=0;
296       //bitmp->format->palette = NULL;
297 
298       bitmp->flags=0;
299       bitmp->w=w;
300       bitmp->h=h;
301       bitmp->pitch=/*retro*/w*2;
302       bitmp->pixels=malloc(sizeof(unsigned char)*h*w*2);//  (unsigned char *)&Retro_Screen[0];
303       if (!bitmp->pixels)
304       {
305          printf("failed alloc pixels\n");
306          Retro_FreeSurface(bitmp);
307          return NULL;
308       }
309       memset(bitmp->pixels,0, h*w*2);
310    }
311    else if(d==32)
312    {
313       bitmp->format->BitsPerPixel = 32;
314       bitmp->format->BytesPerPixel = 4;
315       bitmp->format->Rloss=0;
316       bitmp->format->Gloss=0;
317       bitmp->format->Bloss=0;
318       bitmp->format->Aloss=0;
319       bitmp->format->Rshift=16;
320       bitmp->format->Gshift=8;
321       bitmp->format->Bshift=0;
322       bitmp->format->Ashift=24;
323       bitmp->format->Rmask=0x00ff0000;
324       bitmp->format->Gmask=0x0000ff00;
325       bitmp->format->Bmask=0x000000ff;
326       bitmp->format->Amask=0xff000000;
327       bitmp->format->colorkey=0;
328       bitmp->format->alpha=0;
329       //bitmp->format->palette = NULL;
330 
331       bitmp->flags=0;
332       bitmp->w=w;
333       bitmp->h=h;
334       bitmp->pitch=w*4;
335       bitmp->pixels=malloc(sizeof(unsigned char)*h*w*4);//  (unsigned char *)&Retro_Screen[0];
336       if (!bitmp->pixels)
337       {
338          printf("failed alloc pixels\n");
339          Retro_FreeSurface(bitmp);
340          return NULL;
341       }
342       memset(bitmp->pixels,0, h*w*4);
343    }
344    else
345    {
346       bitmp->format->BitsPerPixel = 8;
347       bitmp->format->BytesPerPixel = 1;
348       bitmp->format->Rloss=0;
349       bitmp->format->Gloss=0;
350       bitmp->format->Bloss=0;
351       bitmp->format->Aloss=0;
352       bitmp->format->Rshift=0;
353       bitmp->format->Gshift=0;
354       bitmp->format->Bshift=0;
355       bitmp->format->Ashift=0;
356       bitmp->format->Rmask=0;
357       bitmp->format->Gmask=0;
358       bitmp->format->Bmask=0;
359       bitmp->format->Amask=0;
360       bitmp->format->colorkey=0;
361       bitmp->format->alpha=0;
362       bitmp->flags=0;
363       bitmp->w=w;
364       bitmp->h=h;
365       bitmp->pitch=w*1;
366       bitmp->pixels=malloc(sizeof(unsigned char)*h*w*1);//  (unsigned char *)&Retro_Screen[0];
367       if (!bitmp->pixels)
368       {
369          printf("failed alloc pixels\n");
370          Retro_FreeSurface(bitmp);
371          return NULL;
372       }
373       memset(bitmp->pixels,0, h*w*1);
374    }
375 
376    bitmp->clip_rect.x=0;
377    bitmp->clip_rect.y=0;
378    bitmp->clip_rect.w=w;
379    bitmp->clip_rect.h=h;
380 
381    //printf("fin prepare tex:%dx%dx%d\n",bitmp->w,bitmp->h,bitmp->format->BytesPerPixel);
382    return bitmp;
383 }
384 
Retro_SetVideoMode(int w,int h,int b)385 SDL_Surface *Retro_SetVideoMode(int w,int h,int b)
386 {
387    SDL_Surface *bitmp;
388 
389    bitmp = (SDL_Surface *) calloc(1, sizeof(*bitmp));
390    if (bitmp == NULL)
391    {
392       printf("tex surface failed");
393       return NULL;
394    }
395 
396    bitmp->format = calloc(1,sizeof(*bitmp->format));
397    if (bitmp->format == NULL)
398    {
399       printf("tex format failed");
400       return NULL;
401    }
402 
403 
404    bitmp->format->palette = calloc(1,sizeof(*bitmp->format->palette));
405    if (bitmp->format->palette == NULL)
406    {
407       printf("tex format palette failed");
408       return NULL;
409    }
410    printf("create palette\n");
411    bitmp->format->palette->ncolors=256;
412    bitmp->format->palette->colors=malloc(1024);
413    bitmp->format->palette->version=0;
414    bitmp->format->palette->refcount=0;
415    memset(bitmp->format->palette->colors,0x0,1024);
416 
417    if(b==16)
418    {
419       bitmp->format->BitsPerPixel = 16;
420       bitmp->format->BytesPerPixel = 2;
421       bitmp->format->Rloss=3;
422       bitmp->format->Gloss=3;
423       bitmp->format->Bloss=3;
424       bitmp->format->Aloss=0;
425       bitmp->format->Rshift=11;
426       bitmp->format->Gshift=6;
427       bitmp->format->Bshift=0;
428       bitmp->format->Ashift=0;
429       bitmp->format->Rmask=0x0000F800;
430       bitmp->format->Gmask=0x000007E0;
431       bitmp->format->Bmask=0x0000001F;
432       bitmp->format->Amask=0x00000000;
433       bitmp->format->colorkey=0;
434       bitmp->format->alpha=0;
435       //bitmp->format->palette = NULL;
436 
437       bitmp->flags=0;
438       bitmp->w=w;
439       bitmp->h=h;
440       bitmp->pitch=retrow*2;
441       bitmp->pixels=(unsigned char *)&Retro_Screen[0];
442       bitmp->clip_rect.x=0;
443       bitmp->clip_rect.y=0;
444       bitmp->clip_rect.w=w;
445       bitmp->clip_rect.h=h;
446    }
447    else
448    {
449       bitmp->format->BitsPerPixel = 32;
450       bitmp->format->BytesPerPixel = 4;
451       bitmp->format->Rloss=0;
452       bitmp->format->Gloss=0;
453       bitmp->format->Bloss=0;
454       bitmp->format->Aloss=0;
455       bitmp->format->Rshift=16;
456       bitmp->format->Gshift=8;
457       bitmp->format->Bshift=0;
458       bitmp->format->Ashift=24;
459       bitmp->format->Rmask=0x00ff0000;
460       bitmp->format->Gmask=0x0000ff00;
461       bitmp->format->Bmask=0x000000ff;
462       bitmp->format->Amask=0xff000000;
463       bitmp->format->colorkey=0;
464       bitmp->format->alpha=0;
465       //bitmp->format->palette = NULL;
466       bitmp->flags=0;
467       bitmp->w=w;
468       bitmp->h=h;
469       bitmp->pitch=retrow*4;
470       bitmp->pixels=(unsigned char *)&Retro_Screen[0];
471       bitmp->clip_rect.x=0;
472       bitmp->clip_rect.y=0;
473       bitmp->clip_rect.w=w;
474       bitmp->clip_rect.h=h;
475    }
476 
477    printf("SetvideoMode fin prepare tex:%dx%dx%d pitch:%d\n",bitmp->w,bitmp->h,bitmp->format->BytesPerPixel,bitmp->pitch);
478    return bitmp;
479 }
480 
Retro_GetRGB(int coul,SDL_PixelFormat * format,int * r,int * g,int * b)481 void Retro_GetRGB(int coul,SDL_PixelFormat *format, int *r,int *g,int *b)
482 {
483    if(format->BitsPerPixel==16)
484    {
485 #define R5 ((coul>>11)&0x1F)
486 #define G6 ((coul>>5 )&0x3F)
487 #define B5 ((coul    )&0x1F)
488 
489       *r = ( R5 * 527 + 23 ) >> 6;
490       *g = ( G6 * 259 + 33 ) >> 6;
491       *b = ( B5 * 527 + 23 ) >> 6;
492    }
493    else
494    {
495 #define R ((coul>>16)&0xff)
496 #define G ((coul>>8 )&0xff)
497 #define B ((coul    )&0xff)
498       *r = R;
499       *g = G;
500       *b = B;
501    }
502 }
503