/[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 89 - (show annotations)
Thu May 28 16:32:59 2009 UTC (10 years, 8 months ago) by Jede
File MIME type: text/plain
File size: 17151 byte(s)
Comment 

On each end of line, we have a flag, if it's equal to 1 the line has changed ! It's now running quicker ! (zoom *2)
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 #endif
142
143 /* 320x240x32 */
144 screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom+border, 8, flags_video); //
145 if ( screen == NULL ) {
146 fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());
147 return 0;
148 }
149
150 sprintf(title, "Caloric %s", VERSION);
151 SDL_WM_SetCaption(title, NULL);
152
153 windowChanging=0;
154 return 1;
155 }
156
157 void sdl_end()
158 {
159 windowChanging=1;
160 SDL_FreeSurface(screen);
161 screen = NULL;
162 SDL_QuitSubSystem(SDL_INIT_VIDEO);
163 SDL_Quit();
164 windowChanging=0;
165 }
166
167 void sdl_display (void)
168 {
169 windowChanging=1;
170 SDL_Flip(screen);
171 windowChanging=0;
172 }
173
174 void Reset_Screen (void)
175 {
176 display_frame ();
177 }
178
179 #define GET_R(a) ((a&0x00FF0000)>>16)&0xFF
180 #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF
181 #define GET_B(a) (a&0x000000FF)&0xFF
182 #define RGB(r, g, b) (r<<16) | (g<<8) | (b)
183
184 /********************************************************/
185 /* BEGIN Jede Method*/
186 /********************************************************/
187
188 void initSDL_display()
189 {
190
191
192
193 oriccolors[0].red=0;
194 oriccolors[0].green=0;
195 oriccolors[0].blue=0;
196
197
198 oriccolors[1].red=255;
199 oriccolors[1].green=0;
200 oriccolors[1].blue=0;
201
202
203 oriccolors[2].red=0;
204 oriccolors[2].green=255;
205 oriccolors[2].blue=0;
206
207 oriccolors[3].red=255;
208 oriccolors[3].green=255;
209 oriccolors[3].blue=0;
210
211 oriccolors[4].red=0;
212 oriccolors[4].green=0;
213 oriccolors[4].blue=255;
214
215 oriccolors[5].red=255;
216 oriccolors[5].green=0;
217 oriccolors[5].blue=255;
218
219 oriccolors[6].red=0;
220 oriccolors[6].green=255;
221 oriccolors[6].blue=255;
222
223 oriccolors[7].red=255;
224 oriccolors[7].green=255;
225 oriccolors[7].blue=255;
226 /*
227 if(TTF_Init() == -1)
228 {
229 fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
230 exit(EXIT_FAILURE);
231 }
232 */
233 }
234
235 void DrawRect(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B, int zoom)
236 {
237 Uint32 color = SDL_MapRGB(screen->format, R, G, B);
238 SDL_Rect position;
239 // gdImageFilledRectangle(screen,x,y,x+zoom-1,y+zoom-1,color);
240
241 //Uint32 color=0;
242 position.x = x;
243 position.y = y;
244 position.w = zoom;
245 position.h = zoom;
246 SDL_FillRect(screen,&position, color);
247
248
249 /*
250 switch (screen->format->BytesPerPixel)
251 {
252 case 1: // Assuming 8-bpp
253 {
254 Uint8 *bufp;
255 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
256 *bufp = color;
257 }
258 break;
259 case 2: // Probably 15-bpp or 16-bpp
260 {
261 Uint16 *bufp;
262 bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
263 *bufp = color;
264 }
265 break;
266 case 3: // Slow 24-bpp mode, usually not used
267 {
268 Uint8 *bufp;
269 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
270 if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
271 {
272 bufp[0] = color;
273 bufp[1] = color >> 8;
274 bufp[2] = color >> 16;
275 } else {
276 bufp[2] = color;
277 bufp[1] = color >> 8;
278 bufp[0] = color >> 16;
279 }
280 }
281 break;
282 case 4: // Probably 32-bpp
283 {
284 Uint32 *bufp;
285 bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
286 *bufp = color;
287 }
288 break;*/
289 }
290
291
292
293
294 //
295
296 void DrawPixel(SDL_Surface *screen, int x, int y,
297 Uint8 R, Uint8 G, Uint8 B)
298 {
299
300 Uint32 color = SDL_MapRGB(screen->format, R, G, B);
301 switch (screen->format->BytesPerPixel)
302 {
303 case 1: // Assuming 8-bpp
304 {
305 Uint8 *bufp;
306 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
307 *bufp = color;
308 }
309 break;
310 case 2: // Probably 15-bpp or 16-bpp
311 {
312 Uint16 *bufp;
313 bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
314 *bufp = color;
315 }
316 break;
317 case 3: // Slow 24-bpp mode, usually not used
318 {
319 Uint8 *bufp;
320 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
321 if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
322 {
323 bufp[0] = color;
324 bufp[1] = color >> 8;
325 bufp[2] = color >> 16;
326 } else {
327 bufp[2] = color;
328 bufp[1] = color >> 8;
329 bufp[0] = color >> 16;
330 }
331 }
332 break;
333 case 4: // Probably 32-bpp
334 {
335 Uint32 *bufp;
336 bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
337 *bufp = color;
338 }
339 break;
340 }
341 }
342
343
344 void lockscreenSDL_Display(SDL_Surface *screen)
345 {
346 if ( SDL_MUSTLOCK(screen) )
347 {
348 if ( SDL_LockSurface(screen) < 0 )
349 {
350 return;
351 }
352 }
353 }
354
355 void unlockscreenSDL_Display(SDL_Surface *screen)
356 {
357 if ( SDL_MUSTLOCK(screen) )
358 {
359 SDL_UnlockSurface(screen);
360 }
361 }
362
363 void displaySDL_stats()
364 {
365 SDL_Surface *texte = NULL, *fond = NULL;
366 /*
367 time_t timenow;
368 time_t seconds;
369 char mychar[30]="";
370 SDL_Color couleurRouge = {255, 0, 0};
371 SDL_Color couleurNoire = {0, 0, 0};
372 TTF_Font *police = NULL;
373 SDL_Rect position;
374 Uint32 color=0;
375 position.x = 0;
376 position.y = 0;
377 position.w = VIDEO_WIDTH*zoom;
378 position.h = border;
379 SDL_FillRect(screen,&position, color);
380
381
382 police = TTF_OpenFont("verdana.ttf", 8);
383
384 if (police==NULL)
385 {
386 fprintf(stderr,"Can't find the ttf!!!!\n");
387 exit(12);
388 }
389 time (&timenow);
390 seconds = timenow - previoustime;
391 sprintf(&mychar, "%f /s\0", (float) count_frame/ seconds);
392 //fprintf(stderr,"%d\n",stats.frames);
393 //printf(
394 texte = TTF_RenderText_Solid(police, mychar , couleurRouge);
395 SDL_BlitSurface(texte, NULL, screen, &position);*/
396
397 //fprintf (fp, "seconds %lu", (unsigned long) seconds);
398
399 //fprintf (fp, ", frames %lu", stats.frames);
400 //if (seconds != 0)
401 //fprintf (fp, " (%.1f/s)", (double) stats.frames / seconds);
402 /*
403 fprintf (fp, ", scanlines %lu", stats.scanlines);
404 if (stats.frames != 0)
405 fprintf (fp, " (%.1f/f)", (double) stats.scanlines / stats.frames);
406
407 fprintf (fp, ", polls %lu", stats.polls);
408 if (seconds != 0)
409 fprintf (fp, " (%.1f/s)", (double) stats.polls / seconds);
410
411 TTF_CloseFont(police);
412 previoustime=timenow;
413 */
414 }
415
416 void displaySDL_run(void)
417 {
418
419 int x=0;
420 int y=0;
421 int calcul=0;
422 int move_x=0;
423 int move_y=0;
424 render_frame ();
425 zoom=2;
426 if (frametouched) // if frame is modified let's go !
427 {
428 lockscreenSDL_Display(screen);
429 if (zoom==1)
430 {
431 for (y=0;y<VIDEO_HEIGHT;y++)
432 {
433 if (buf[y * (VIDEO_WIDTH + 1) + VIDEO_WIDTH]==1)
434 for (x=0;x<VIDEO_WIDTH;x++)
435 {
436 DrawPixel(screen, x, y+border,oriccolors[buf[calcul+x]].red, oriccolors[buf[calcul+x]].green, oriccolors[buf[calcul+x]].blue);
437 }
438 calcul+=VIDEO_WIDTH+1;
439 }
440 }
441 else
442 {
443 for (y=0;y<VIDEO_HEIGHT;y++)
444 {
445 if (buf[y * (VIDEO_WIDTH + 1) + VIDEO_WIDTH]==1)
446 {
447 for (x=0;x<VIDEO_WIDTH;x++)
448 {
449 //DrawRect(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B, zoom)
450 DrawRect(screen, move_x, move_y+border,oriccolors[buf[calcul+x]].red, oriccolors[buf[calcul+x]].green, oriccolors[buf[calcul+x]].blue,zoom);
451 move_x+=zoom;
452 }
453 }
454 move_x=0;
455 calcul+=VIDEO_WIDTH+1;
456 move_y+=zoom;
457 }
458 }
459 unlockscreenSDL_Display(screen);
460 count_frame++;
461 displaySDL_stats();
462 SDL_Flip(screen);
463
464
465 }
466
467 SDL_Delay(20);
468 //usleep(20000);
469 return;
470 }
471
472
473 /********************************************************/
474 /* END Jede Method*/
475 /********************************************************/
476
477 void display_frame (void)
478 {
479 int x, y;
480 int i = 0;
481 int j = 0;
482 uint8_t *src;
483 uint32_t *dst;
484 static uint32_t *tmp = NULL;
485 static uint32_t *tmpback;
486 uint8_t *bs;
487
488 /*
489 if(zoomChange != zoom) {
490 sdl_end();
491 zoom = zoomChange;
492 sdl_start();
493 }
494 */
495 if(setfullScreen != fullScreen) {
496 fullScreen = setfullScreen;
497 sdl_end();
498 sdl_start();
499 }
500 // comment this 3 lines to use jylam method and uncoment from render_frame() to SDL_Delay(20); included
501 displaySDL_run();
502 poll_keyboard();
503 return;
504
505
506
507
508 /* Xeuphoric uses 1 byte at the end of each line,
509 indicating a state change.
510 We don't need that anymore */
511 /*
512 render_frame ();
513 if(scanlineEmulation == 0) {
514 src = (uint8_t *)buf;
515 dst = (uint32_t *)screen->pixels;
516
517
518 oldEffect = 0;
519 if(zoom == 1) {
520 for(y=0; y<VIDEO_HEIGHT; y++) {
521 for(x=0; x<VIDEO_WIDTH; x++) {
522 *dst = palette[*src];
523 dst++;
524 src++;
525 }
526 src++;
527 }
528 } else {
529
530 for(y=0; y<VIDEO_HEIGHT; y++) {
531 for(i = 0; i < zoom; i++) {
532 bs = src;
533 for(x=0; x<VIDEO_WIDTH; x++) {
534 for(j = 0; j < zoom; j++) {
535 *dst = palette[*src];
536 dst++;
537 }
538 src++;
539 }
540 src = bs;
541 }
542 src+=(VIDEO_WIDTH+1);
543 }
544 }
545 } else {
546 // Scanline emulation
547 if(oldEffect == 0) {
548 if(tmp != NULL) {
549 free(tmp);
550 }
551 sdl_end();
552 zoom = 2;
553 sdl_start();
554 oldEffect = 1;
555 if(tmp == NULL) {
556 tmp = (uint32_t*)malloc((VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2) * 4);
557 tmpback = tmp;
558 }
559 } else {
560 }
561
562 src = (uint8_t *)buf;
563 dst = (uint32_t *)screen->pixels;
564
565
566 // Double the screen, making 1 scanline of 2 black
567 for(y=0; y<VIDEO_HEIGHT; y++) {
568 for(x=0; x<VIDEO_WIDTH; x++) {
569 for(j = 0; j < zoom; j++) {
570 *dst = palette[*src];
571 dst++;
572 }
573 src++;
574 }
575 dst+=(VIDEO_WIDTH*2);
576 src++;
577 }
578
579 // Blur midline
580 dst = (uint32_t *)screen->pixels;
581 dst+=VIDEO_WIDTH*2;
582 for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {
583 for(x=0; x<VIDEO_WIDTH*2; x++) {
584 uint8_t r1 = GET_R(*(dst-(VIDEO_WIDTH*2)));
585 uint8_t g1 = GET_G(*(dst-(VIDEO_WIDTH*2)));
586 uint8_t b1 = GET_B(*(dst-(VIDEO_WIDTH*2)));
587 uint8_t r2 = GET_R(*(dst+(VIDEO_WIDTH*2)));
588 uint8_t g2 = GET_G(*(dst+(VIDEO_WIDTH*2)));
589 uint8_t b2 = GET_B(*(dst+(VIDEO_WIDTH*2)));
590 uint8_t r = (r1+r2)/3;
591 uint8_t g = (g1+g2)/3;
592 uint8_t b = (b1+b2)/3;
593
594 *dst = RGB(r, g, b);
595 dst++;
596 }
597 dst+=(VIDEO_WIDTH*2);
598 }
599
600 // Blend at right
601 dst = (uint32_t *)screen->pixels;
602 memcpy(tmp, dst, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
603 for(y=VIDEO_WIDTH*2; y < ((VIDEO_HEIGHT*2) * (VIDEO_WIDTH*2))- (VIDEO_WIDTH*2); y++) {
604 uint8_t r1 = GET_R(tmp[y]);
605 uint8_t g1 = GET_G(tmp[y]);
606 uint8_t b1 = GET_B(tmp[y]);
607 uint8_t r2 = GET_R(tmp[y+1]);
608 uint8_t g2 = GET_G(tmp[y+1]);
609 uint8_t b2 = GET_B(tmp[y+1]);
610 uint8_t r = (r1+r2)/2;
611 uint8_t g = (g1+g2)/2;
612 uint8_t b = (b1+b2)/2;
613 tmp[y] = RGB(r, g, b);
614 }
615
616
617 dst++;
618 tmp++;
619 for(y=1; y < (VIDEO_HEIGHT*2); y++) {
620 uint8_t decal = rand()%3;
621 for(x=decal; x<(VIDEO_WIDTH*2); x++) {
622 uint8_t r1 = GET_R(*(dst));
623 uint8_t g1 = GET_G(*(dst));
624 uint8_t b1 = GET_B(*(dst));
625 uint32_t v1 = (r1+g1)/2;
626 uint8_t r2 = GET_R(*(tmp-decal));
627 uint8_t g2 = GET_G(*(tmp-decal));
628 uint8_t b2 = GET_B(*(tmp-decal));
629 uint32_t v2 = (r2+g2)/2;
630 uint8_t r;
631 uint8_t g;
632 uint8_t b;
633
634 if(v2>v1) {
635 r = (r1+r2)/2;
636 g = (g1+g2)/2;
637 b = (b1+b2)/2;
638 } else {
639 r = r1;
640 g = g1;
641 b = b1;
642 }
643 *tmp = RGB(r, g, b);
644 dst+=1;
645 tmp+=1;
646 }
647 }
648 tmp = tmpback;
649 memcpy(screen->pixels, tmp, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
650
651 }
652 sdl_display();
653 poll_keyboard();
654 SDL_Delay(20); // max(1/50 Hz, 1/60 Hz) = 20 ms
655 */
656 }
657 #else
658 #error You must have SDL to compile Xeuphoric
659 #endif

  ViewVC Help
Powered by ViewVC 1.1.26