/[projet1]/public/pc/emulators/caloric/src/sdl.c
Defence Force logotype

Contents of /public/pc/emulators/caloric/src/sdl.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 147 - (show annotations)
Sun Aug 23 11:20:36 2009 UTC (10 years, 1 month ago) by Jede
File MIME type: text/plain
File size: 17216 byte(s)
Changement de zoom par l'interface
1 /*
2 * sdl.c - SDL interface for Euphoric
3 * (c) 2007 Jean-Yves Lamoureux
4 */
5
6 /*
7 This file is copyright Jean-Yves Lamoureux 2007.
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of version 2 of the GNU General Public License as published by the
11 Free Software Foundation.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License along with
18 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 Place, Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22 #include "config.h"
23 #if HAVE_SDL
24 #include <ctype.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <stdarg.h>
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <time.h>
34 #include <unistd.h>
35
36 #include <SDL/SDL.h>
37 #include <SDL/SDL_timer.h>
38 #include <SDL_ttf.h>
39
40 #include "caloric.h"
41 #include "hosttraps.h"
42 #include "stats.h"
43 #include "dsp.h"
44
45 extern volatile unsigned char Tick;
46 static long palette[8]={0,0xFF0000,0x00FF00,0xFFFF00,0x0000FF,0xFF00FF,0x00FFFF,0xFFFFFF};
47 extern int zoom;
48 int zoomChange ;
49 char windowChanging = 0;
50 uint8_t scanlineEmulation = 0;
51 uint8_t oldEffect = 0;
52 uint8_t setfullScreen = 0, fullScreen = 0;
53 int count_frame=0;
54 time_t previoustime;
55
56
57 typedef struct {
58 int red;
59 int blue;
60
61 int green;
62 } oriccolor;
63 oriccolor oriccolors[8];
64
65 SDL_Surface *screen;
66 int border=20;
67
68 void display_frame (void);
69
70 int sdl_start()
71 {
72 char title[1024];
73 Uint32 flags = SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE|SDL_INIT_TIMER;
74 Uint32 flags_video= SDL_HWPALETTE|SDL_HWACCEL|SDL_HWSURFACE|SDL_RLEACCEL; // Correcting full screen
75 zoomChange = zoom;
76 SDL_Rect **modes;
77 int i;
78 windowChanging=1;
79
80
81 /* Init video blah */
82 /*Get some infos from video card and resolution*/
83
84 //zoom=2;
85 if(fullScreen) {
86 flags |= SDL_FULLSCREEN;
87 flags_video ^= SDL_FULLSCREEN;
88 // zoom=1; // We set zoom, we don't care zoom>1 if it's in full screen
89 fprintf(stderr, "Full!\n");
90 }
91 if(audio_method == AM_SDL)
92 flags |= SDL_INIT_AUDIO;
93 if (SDL_Init(flags) < 0) {
94 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
95 return 0;
96 }
97
98 const SDL_VideoInfo *pVideoInfo = SDL_GetVideoInfo();
99
100 if (!pVideoInfo) {
101 printf("\n\t SDL_GetVideoInfo return NULL\n");
102 }
103 #if HAVE_DEBUG_MODE
104 else {
105
106
107 printf("\thardware surfaces : %d\n", pVideoInfo->hw_available);
108 printf("\twindow manager available : %d\n", pVideoInfo->wm_available);
109 printf("\thardware to hardware blits : %d\n", pVideoInfo->blit_hw);
110 printf("\thardware to hardware colorkey blits : %d\n", pVideoInfo->blit_hw_CC);
111 printf("\thardware to hardware alpha blits : %d\n", pVideoInfo->blit_hw_A);
112 printf("\tsoftware to hardware blits : %d\n", pVideoInfo->blit_sw);
113 printf("\tsoftware to hardware colorkey blits : %d\n", pVideoInfo->blit_sw_CC);
114 printf("\tsoftware to hardware alpha blits : %d\n", pVideoInfo->blit_sw_A);
115 printf("\tcolor fills accelerated : %d\n", pVideoInfo->blit_fill);
116 printf("\tTotal video memory Kilobytes : %d\n", pVideoInfo->video_mem);
117 }
118
119 #endif
120 modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE|SDL_HWACCEL);
121
122 /* Check is there are any modes available */
123 if(modes == (SDL_Rect **)0){
124 printf("No modes available!\n");
125 exit(-1);
126 }
127
128 /* Check if or resolution is restricted */
129 #if HAVE_DEBUG_MODE
130
131 if(modes == (SDL_Rect **)-1){
132 printf("All resolutions available.\n");
133 }
134 else{
135 /* Print valid modes */
136 printf("Available Modes\n");
137 for(i=0;modes[i];++i)
138 printf(" %d x %d\n", modes[i]->w, modes[i]->h);
139
140 }
141
142 #endif
143
144 // calculate the best full screen mode
145 //zoom=modes[0]->h/VIDEO_HEIGHT;
146
147 /* 320x240x32 */
148
149 screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom+border, 8, flags_video); //
150 if ( screen == NULL ) {
151 fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());
152 return 0;
153 }
154
155 sprintf(title, "Caloric %s", VERSION);
156 SDL_WM_SetCaption(title, NULL);
157
158 windowChanging=0;
159 return 1;
160 }
161
162 void sdl_end()
163 {
164 windowChanging=1;
165 SDL_FreeSurface(screen);
166 screen = NULL;
167 SDL_QuitSubSystem(SDL_INIT_VIDEO);
168 SDL_Quit();
169 windowChanging=0;
170 }
171
172 void sdl_display (void)
173 {
174 windowChanging=1;
175 SDL_Flip(screen);
176 windowChanging=0;
177 }
178
179
180
181 void Reset_Screen (void)
182 {
183
184 //initialised=1;
185 display_frame ();
186 }
187
188 #define GET_R(a) ((a&0x00FF0000)>>16)&0xFF
189 #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF
190 #define GET_B(a) (a&0x000000FF)&0xFF
191 #define RGB(r, g, b) (r<<16) | (g<<8) | (b)
192
193 /********************************************************/
194 /* BEGIN Jede Method*/
195 /********************************************************/
196
197 void initSDL_display()
198 {
199
200
201
202 oriccolors[0].red=0;
203 oriccolors[0].green=0;
204 oriccolors[0].blue=0;
205
206
207 oriccolors[1].red=255;
208 oriccolors[1].green=0;
209 oriccolors[1].blue=0;
210
211
212 oriccolors[2].red=0;
213 oriccolors[2].green=255;
214 oriccolors[2].blue=0;
215
216 oriccolors[3].red=255;
217 oriccolors[3].green=255;
218 oriccolors[3].blue=0;
219
220 oriccolors[4].red=0;
221 oriccolors[4].green=0;
222 oriccolors[4].blue=255;
223
224 oriccolors[5].red=255;
225 oriccolors[5].green=0;
226 oriccolors[5].blue=255;
227
228 oriccolors[6].red=0;
229 oriccolors[6].green=255;
230 oriccolors[6].blue=255;
231
232 oriccolors[7].red=255;
233 oriccolors[7].green=255;
234 oriccolors[7].blue=255;
235 /*
236 if(TTF_Init() == -1)
237 {
238 fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
239 exit(EXIT_FAILURE);
240 }
241 */
242 }
243
244 void DrawRect(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B, int zoom)
245 {
246 Uint32 color = SDL_MapRGB(screen->format, R, G, B);
247 SDL_Rect position;
248 // gdImageFilledRectangle(screen,x,y,x+zoom-1,y+zoom-1,color);
249
250 //Uint32 color=0;
251 position.x = x;
252 position.y = y;
253 position.w = zoom;
254 position.h = zoom;
255 SDL_FillRect(screen,&position, color);
256
257
258 /*
259 switch (screen->format->BytesPerPixel)
260 {
261 case 1: // Assuming 8-bpp
262 {
263 Uint8 *bufp;
264 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
265 *bufp = color;
266 }
267 break;
268 case 2: // Probably 15-bpp or 16-bpp
269 {
270 Uint16 *bufp;
271 bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
272 *bufp = color;
273 }
274 break;
275 case 3: // Slow 24-bpp mode, usually not used
276 {
277 Uint8 *bufp;
278 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
279 if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
280 {
281 bufp[0] = color;
282 bufp[1] = color >> 8;
283 bufp[2] = color >> 16;
284 } else {
285 bufp[2] = color;
286 bufp[1] = color >> 8;
287 bufp[0] = color >> 16;
288 }
289 }
290 break;
291 case 4: // Probably 32-bpp
292 {
293 Uint32 *bufp;
294 bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
295 *bufp = color;
296 }
297 break;*/
298 }
299
300
301
302
303 //
304
305 void DrawPixel(SDL_Surface *screen, int x, int y,
306 Uint8 R, Uint8 G, Uint8 B)
307 {
308
309 Uint32 color = SDL_MapRGB(screen->format, R, G, B);
310 switch (screen->format->BytesPerPixel)
311 {
312 case 1: // Assuming 8-bpp
313 {
314 Uint8 *bufp;
315 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
316 *bufp = color;
317 }
318 break;
319 case 2: // Probably 15-bpp or 16-bpp
320 {
321 Uint16 *bufp;
322 bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
323 *bufp = color;
324 }
325 break;
326 case 3: // Slow 24-bpp mode, usually not used
327 {
328 Uint8 *bufp;
329 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
330 if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
331 {
332 bufp[0] = color;
333 bufp[1] = color >> 8;
334 bufp[2] = color >> 16;
335 } else {
336 bufp[2] = color;
337 bufp[1] = color >> 8;
338 bufp[0] = color >> 16;
339 }
340 }
341 break;
342 case 4: // Probably 32-bpp
343 {
344 Uint32 *bufp;
345 bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
346 *bufp = color;
347 }
348 break;
349 }
350 }
351
352
353 void lockscreenSDL_Display(SDL_Surface *screen)
354 {
355 if ( SDL_MUSTLOCK(screen) )
356 {
357 if ( SDL_LockSurface(screen) < 0 )
358 {
359 return;
360 }
361 }
362 }
363
364 void unlockscreenSDL_Display(SDL_Surface *screen)
365 {
366 if ( SDL_MUSTLOCK(screen) )
367 {
368 SDL_UnlockSurface(screen);
369 }
370 }
371
372 void displaySDL_stats()
373 {
374 SDL_Surface *texte = NULL, *fond = NULL;
375 /*
376 time_t timenow;
377 time_t seconds;
378 char mychar[30]="";
379 SDL_Color couleurRouge = {255, 0, 0};
380 SDL_Color couleurNoire = {0, 0, 0};
381 TTF_Font *police = NULL;
382 SDL_Rect position;
383 Uint32 color=0;
384 position.x = 0;
385 position.y = 0;
386 position.w = VIDEO_WIDTH*zoom;
387 position.h = border;
388 SDL_FillRect(screen,&position, color);
389
390
391 police = TTF_OpenFont("verdana.ttf", 8);
392
393 if (police==NULL)
394 {
395 fprintf(stderr,"Can't find the ttf!!!!\n");
396 exit(12);
397 }
398 time (&timenow);
399 seconds = timenow - previoustime;
400 sprintf(&mychar, "%f /s\0", (float) count_frame/ seconds);
401 //fprintf(stderr,"%d\n",stats.frames);
402 //printf(
403 texte = TTF_RenderText_Solid(police, mychar , couleurRouge);
404 SDL_BlitSurface(texte, NULL, screen, &position);*/
405
406 //fprintf (fp, "seconds %lu", (unsigned long) seconds);
407
408 //fprintf (fp, ", frames %lu", stats.frames);
409 //if (seconds != 0)
410 //fprintf (fp, " (%.1f/s)", (double) stats.frames / seconds);
411 /*
412 fprintf (fp, ", scanlines %lu", stats.scanlines);
413 if (stats.frames != 0)
414 fprintf (fp, " (%.1f/f)", (double) stats.scanlines / stats.frames);
415
416 fprintf (fp, ", polls %lu", stats.polls);
417 if (seconds != 0)
418 fprintf (fp, " (%.1f/s)", (double) stats.polls / seconds);
419
420 TTF_CloseFont(police);
421 previoustime=timenow;
422 */
423 }
424
425 void displaySDL_run(void)
426 {
427 int x=0;
428 int y=0;
429 int calcul=0;
430 int move_x=0;
431 int move_y=0;
432 render_frame ();
433 if (frametouched) // if frame is modified let's go !
434 {
435 lockscreenSDL_Display(screen);
436 if (zoom==1)
437 {
438 for (y=0;y<VIDEO_HEIGHT;y++)
439 {
440 if (buf[y * (VIDEO_WIDTH + 1) + VIDEO_WIDTH]==1)
441 for (x=0;x<VIDEO_WIDTH;x++)
442 {
443 DrawPixel(screen, x, y+border,oriccolors[buf[calcul+x]].red, oriccolors[buf[calcul+x]].green, oriccolors[buf[calcul+x]].blue);
444 }
445 calcul+=VIDEO_WIDTH+1;
446 }
447 }
448 else
449 {
450 for (y=0;y<VIDEO_HEIGHT;y++)
451 {
452 if (buf[y * (VIDEO_WIDTH + 1) + VIDEO_WIDTH]==1)
453 {
454 for (x=0;x<VIDEO_WIDTH;x++)
455 {
456 DrawRect(screen, move_x, move_y+border,oriccolors[buf[calcul+x]].red, oriccolors[buf[calcul+x]].green, oriccolors[buf[calcul+x]].blue,zoom);
457 move_x+=zoom;
458 }
459 }
460 move_x=0;
461 calcul+=VIDEO_WIDTH+1;
462 move_y+=zoom;
463 }
464 }
465 unlockscreenSDL_Display(screen);
466 count_frame++;
467 displaySDL_stats();
468 SDL_Flip(screen);
469
470
471 }
472
473 SDL_Delay(20);
474 //usleep(20000);
475 return;
476 }
477
478
479 /********************************************************/
480 /* END Jede Method*/
481 /********************************************************/
482
483 void display_frame (void)
484 {
485 int x, y;
486 int i = 0;
487 int j = 0;
488 uint8_t *src;
489 uint32_t *dst;
490 static uint32_t *tmp = NULL;
491 static uint32_t *tmpback;
492 uint8_t *bs;
493
494 /*
495 if(zoomChange != zoom) {
496 sdl_end();
497 zoom = zoomChange;
498 sdl_start();
499 }
500 */
501 if(setfullScreen != fullScreen) {
502 fullScreen = setfullScreen;
503
504 sdl_end();
505
506 sdl_start();
507 render_frame_init();
508
509
510 }
511 // comment this 3 lines to use jylam method and uncoment from render_frame() to SDL_Delay(20); included
512 displaySDL_run();
513 poll_keyboard();
514 return;
515
516
517
518
519 /* Xeuphoric uses 1 byte at the end of each line,
520 indicating a state change.
521 We don't need that anymore */
522 /*
523 render_frame ();
524 if(scanlineEmulation == 0) {
525 src = (uint8_t *)buf;
526 dst = (uint32_t *)screen->pixels;
527
528
529 oldEffect = 0;
530 if(zoom == 1) {
531 for(y=0; y<VIDEO_HEIGHT; y++) {
532 for(x=0; x<VIDEO_WIDTH; x++) {
533 *dst = palette[*src];
534 dst++;
535 src++;
536 }
537 src++;
538 }
539 } else {
540
541 for(y=0; y<VIDEO_HEIGHT; y++) {
542 for(i = 0; i < zoom; i++) {
543 bs = src;
544 for(x=0; x<VIDEO_WIDTH; x++) {
545 for(j = 0; j < zoom; j++) {
546 *dst = palette[*src];
547 dst++;
548 }
549 src++;
550 }
551 src = bs;
552 }
553 src+=(VIDEO_WIDTH+1);
554 }
555 }
556 } else {
557 // Scanline emulation
558 if(oldEffect == 0) {
559 if(tmp != NULL) {
560 free(tmp);
561 }
562 sdl_end();
563 zoom = 2;
564 sdl_start();
565 oldEffect = 1;
566 if(tmp == NULL) {
567 tmp = (uint32_t*)malloc((VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2) * 4);
568 tmpback = tmp;
569 }
570 } else {
571 }
572
573 src = (uint8_t *)buf;
574 dst = (uint32_t *)screen->pixels;
575
576
577 // Double the screen, making 1 scanline of 2 black
578 for(y=0; y<VIDEO_HEIGHT; y++) {
579 for(x=0; x<VIDEO_WIDTH; x++) {
580 for(j = 0; j < zoom; j++) {
581 *dst = palette[*src];
582 dst++;
583 }
584 src++;
585 }
586 dst+=(VIDEO_WIDTH*2);
587 src++;
588 }
589
590 // Blur midline
591 dst = (uint32_t *)screen->pixels;
592 dst+=VIDEO_WIDTH*2;
593 for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {
594 for(x=0; x<VIDEO_WIDTH*2; x++) {
595 uint8_t r1 = GET_R(*(dst-(VIDEO_WIDTH*2)));
596 uint8_t g1 = GET_G(*(dst-(VIDEO_WIDTH*2)));
597 uint8_t b1 = GET_B(*(dst-(VIDEO_WIDTH*2)));
598 uint8_t r2 = GET_R(*(dst+(VIDEO_WIDTH*2)));
599 uint8_t g2 = GET_G(*(dst+(VIDEO_WIDTH*2)));
600 uint8_t b2 = GET_B(*(dst+(VIDEO_WIDTH*2)));
601 uint8_t r = (r1+r2)/3;
602 uint8_t g = (g1+g2)/3;
603 uint8_t b = (b1+b2)/3;
604
605 *dst = RGB(r, g, b);
606 dst++;
607 }
608 dst+=(VIDEO_WIDTH*2);
609 }
610
611 // Blend at right
612 dst = (uint32_t *)screen->pixels;
613 memcpy(tmp, dst, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
614 for(y=VIDEO_WIDTH*2; y < ((VIDEO_HEIGHT*2) * (VIDEO_WIDTH*2))- (VIDEO_WIDTH*2); y++) {
615 uint8_t r1 = GET_R(tmp[y]);
616 uint8_t g1 = GET_G(tmp[y]);
617 uint8_t b1 = GET_B(tmp[y]);
618 uint8_t r2 = GET_R(tmp[y+1]);
619 uint8_t g2 = GET_G(tmp[y+1]);
620 uint8_t b2 = GET_B(tmp[y+1]);
621 uint8_t r = (r1+r2)/2;
622 uint8_t g = (g1+g2)/2;
623 uint8_t b = (b1+b2)/2;
624 tmp[y] = RGB(r, g, b);
625 }
626
627
628 dst++;
629 tmp++;
630 for(y=1; y < (VIDEO_HEIGHT*2); y++) {
631 uint8_t decal = rand()%3;
632 for(x=decal; x<(VIDEO_WIDTH*2); x++) {
633 uint8_t r1 = GET_R(*(dst));
634 uint8_t g1 = GET_G(*(dst));
635 uint8_t b1 = GET_B(*(dst));
636 uint32_t v1 = (r1+g1)/2;
637 uint8_t r2 = GET_R(*(tmp-decal));
638 uint8_t g2 = GET_G(*(tmp-decal));
639 uint8_t b2 = GET_B(*(tmp-decal));
640 uint32_t v2 = (r2+g2)/2;
641 uint8_t r;
642 uint8_t g;
643 uint8_t b;
644
645 if(v2>v1) {
646 r = (r1+r2)/2;
647 g = (g1+g2)/2;
648 b = (b1+b2)/2;
649 } else {
650 r = r1;
651 g = g1;
652 b = b1;
653 }
654 *tmp = RGB(r, g, b);
655 dst+=1;
656 tmp+=1;
657 }
658 }
659 tmp = tmpback;
660 memcpy(screen->pixels, tmp, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
661
662 }
663 sdl_display();
664 poll_keyboard();
665 SDL_Delay(20); // max(1/50 Hz, 1/60 Hz) = 20 ms
666 */
667 }
668 #else
669 #error You must have SDL to compile Xeuphoric
670 #endif

  ViewVC Help
Powered by ViewVC 1.1.26