/[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 82 - (show annotations)
Wed May 27 20:46:43 2009 UTC (10 years, 10 months ago) by Jede
File MIME type: text/plain
File size: 14773 byte(s)
rendering more readable.

Zoom is disabled but it could be done easily
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 int green;
61 } oriccolor;
62 oriccolor oriccolors[8];
63
64 SDL_Surface *screen;
65 int border=20;
66
67 void display_frame (void);
68
69 int sdl_start()
70 {
71 char title[1024];
72 Uint32 flags = SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE|SDL_INIT_TIMER;
73 Uint32 flags_video= SDL_HWPALETTE; // Correcting full screen
74 zoomChange = zoom;
75 SDL_Rect **modes;
76 int i;
77 windowChanging=1;
78
79
80 /* Init video blah */
81 /*Get some infos from video card and resolution*/
82
83 zoom=1;
84 if(fullScreen) {
85 flags |= SDL_FULLSCREEN;
86 flags_video ^= SDL_FULLSCREEN;
87 // zoom=1; // We set zoom, we don't care zoom>1 if it's in full screen
88 fprintf(stderr, "Full!\n");
89 }
90 if(audio_method == AM_SDL)
91 flags |= SDL_INIT_AUDIO;
92 if (SDL_Init(flags) < 0) {
93 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
94 return 0;
95 }
96
97 const SDL_VideoInfo *pVideoInfo = SDL_GetVideoInfo();
98
99 if (!pVideoInfo) {
100 printf("\n\t SDL_GetVideoInfo return NULL\n");
101 }
102 #if HAVE_DEBUG_MODE
103 else {
104
105
106 printf("\thardware surfaces : %d\n", pVideoInfo->hw_available);
107 printf("\twindow manager available : %d\n", pVideoInfo->wm_available);
108 printf("\thardware to hardware blits : %d\n", pVideoInfo->blit_hw);
109 printf("\thardware to hardware colorkey blits : %d\n", pVideoInfo->blit_hw_CC);
110 printf("\thardware to hardware alpha blits : %d\n", pVideoInfo->blit_hw_A);
111 printf("\tsoftware to hardware blits : %d\n", pVideoInfo->blit_sw);
112 printf("\tsoftware to hardware colorkey blits : %d\n", pVideoInfo->blit_sw_CC);
113 printf("\tsoftware to hardware alpha blits : %d\n", pVideoInfo->blit_sw_A);
114 printf("\tcolor fills accelerated : %d\n", pVideoInfo->blit_fill);
115 printf("\tTotal video memory Kilobytes : %d\n", pVideoInfo->video_mem);
116 }
117
118 #endif
119 modes=SDL_ListModes(NULL, SDL_FULLSCREEN|SDL_HWSURFACE);
120
121 /* Check is there are any modes available */
122 if(modes == (SDL_Rect **)0){
123 printf("No modes available!\n");
124 exit(-1);
125 }
126
127 /* Check if or resolution is restricted */
128 #if HAVE_DEBUG_MODE
129
130 if(modes == (SDL_Rect **)-1){
131 printf("All resolutions available.\n");
132 }
133 else{
134 /* Print valid modes */
135 printf("Available Modes\n");
136 for(i=0;modes[i];++i)
137 printf(" %d x %d\n", modes[i]->w, modes[i]->h);
138 }
139
140 #endif
141
142 /* 320x240x32 */
143 screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom+border, 32, flags_video); //
144 if ( screen == NULL ) {
145 fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());
146 return 0;
147 }
148
149 sprintf(title, "Caloric %s", VERSION);
150 SDL_WM_SetCaption(title, NULL);
151
152 windowChanging=0;
153 return 1;
154 }
155
156 void sdl_end()
157 {
158 windowChanging=1;
159 SDL_FreeSurface(screen);
160 screen = NULL;
161 SDL_QuitSubSystem(SDL_INIT_VIDEO);
162 SDL_Quit();
163 windowChanging=0;
164 }
165
166 void sdl_display (void)
167 {
168 windowChanging=1;
169 SDL_Flip(screen);
170 windowChanging=0;
171 }
172
173 void Reset_Screen (void)
174 {
175 display_frame ();
176 }
177
178 #define GET_R(a) ((a&0x00FF0000)>>16)&0xFF
179 #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF
180 #define GET_B(a) (a&0x000000FF)&0xFF
181 #define RGB(r, g, b) (r<<16) | (g<<8) | (b)
182
183 /********************************************************/
184 /* BEGIN Jede Method*/
185 /********************************************************/
186
187 void initSDL_display()
188 {
189
190
191
192 oriccolors[0].red=0;
193 oriccolors[0].green=0;
194 oriccolors[0].blue=0;
195
196
197 oriccolors[1].red=255;
198 oriccolors[1].green=0;
199 oriccolors[1].blue=0;
200
201
202 oriccolors[2].red=0;
203 oriccolors[2].green=255;
204 oriccolors[2].blue=0;
205
206 oriccolors[3].red=255;
207 oriccolors[3].green=255;
208 oriccolors[3].blue=0;
209
210 oriccolors[4].red=0;
211 oriccolors[4].green=0;
212 oriccolors[4].blue=255;
213
214 oriccolors[5].red=255;
215 oriccolors[5].green=0;
216 oriccolors[5].blue=255;
217
218 oriccolors[6].red=0;
219 oriccolors[6].green=255;
220 oriccolors[6].blue=255;
221
222 oriccolors[7].red=255;
223 oriccolors[7].green=255;
224 oriccolors[7].blue=255;
225
226 if(TTF_Init() == -1)
227 {
228 fprintf(stderr, "Erreur d'initialisation de TTF_Init : %s\n", TTF_GetError());
229 exit(EXIT_FAILURE);
230 }
231
232 }
233
234 void DrawPixel(SDL_Surface *screen, int x, int y,
235 Uint8 R, Uint8 G, Uint8 B)
236 {
237
238 Uint32 color = SDL_MapRGB(screen->format, R, G, B);
239 switch (screen->format->BytesPerPixel)
240 {
241 case 1: // Assuming 8-bpp
242 {
243 Uint8 *bufp;
244 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
245 *bufp = color;
246 }
247 break;
248 case 2: // Probably 15-bpp or 16-bpp
249 {
250 Uint16 *bufp;
251 bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
252 *bufp = color;
253 }
254 break;
255 case 3: // Slow 24-bpp mode, usually not used
256 {
257 Uint8 *bufp;
258 bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
259 if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
260 {
261 bufp[0] = color;
262 bufp[1] = color >> 8;
263 bufp[2] = color >> 16;
264 } else {
265 bufp[2] = color;
266 bufp[1] = color >> 8;
267 bufp[0] = color >> 16;
268 }
269 }
270 break;
271 case 4: // Probably 32-bpp
272 {
273 Uint32 *bufp;
274 bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
275 *bufp = color;
276 }
277 break;
278 }
279 }
280
281
282 void lockscreenSDL_Display(SDL_Surface *screen)
283 {
284 if ( SDL_MUSTLOCK(screen) )
285 {
286 if ( SDL_LockSurface(screen) < 0 )
287 {
288 return;
289 }
290 }
291 }
292
293 void unlockscreenSDL_Display(SDL_Surface *screen)
294 {
295 if ( SDL_MUSTLOCK(screen) )
296 {
297 SDL_UnlockSurface(screen);
298 }
299 }
300
301 void displaySDL_stats()
302 {
303 SDL_Surface *texte = NULL, *fond = NULL;
304
305 time_t timenow;
306 time_t seconds;
307 char mychar[30]="";
308 SDL_Color couleurRouge = {255, 0, 0};
309 SDL_Color couleurNoire = {0, 0, 0};
310 TTF_Font *police = NULL;
311 SDL_Rect position;
312 Uint32 color=0;
313 position.x = 0;
314 position.y = 0;
315 position.w = VIDEO_WIDTH*zoom;
316 position.h = border;
317 SDL_FillRect(screen,&position, color);
318
319
320 police = TTF_OpenFont("verdana.ttf", 8);
321
322 if (police==NULL)
323 {
324 fprintf(stderr,"Can't find the ttf!!!!\n");
325 exit(12);
326 }
327 time (&timenow);
328 seconds = timenow - previoustime;/*
329 sprintf(&mychar, "%f /s\0", (float) count_frame/ seconds);
330 //fprintf(stderr,"%d\n",stats.frames);
331 //printf(
332 texte = TTF_RenderText_Solid(police, mychar , couleurRouge);
333 SDL_BlitSurface(texte, NULL, screen, &position);*/
334
335 //fprintf (fp, "seconds %lu", (unsigned long) seconds);
336
337 //fprintf (fp, ", frames %lu", stats.frames);
338 //if (seconds != 0)
339 //fprintf (fp, " (%.1f/s)", (double) stats.frames / seconds);
340 /*
341 fprintf (fp, ", scanlines %lu", stats.scanlines);
342 if (stats.frames != 0)
343 fprintf (fp, " (%.1f/f)", (double) stats.scanlines / stats.frames);
344
345 fprintf (fp, ", polls %lu", stats.polls);
346 if (seconds != 0)
347 fprintf (fp, " (%.1f/s)", (double) stats.polls / seconds);
348 */
349 TTF_CloseFont(police);
350 previoustime=timenow;
351
352 }
353
354 void displaySDL_run(void)
355 {
356
357 int x=0;
358 int y=0;
359 int calcul=0;
360 render_frame ();
361
362 //if (frametouched)
363 {
364 lockscreenSDL_Display(screen);
365 //xeuphoricSDLDisplay_lockscreen(screen);
366
367
368 for (y=0;y<VIDEO_HEIGHT;y++)
369 {
370 //err("=>%d\n",buf[240]);
371 //if (buf[240*(y+1)]==0)
372 {
373 for (x=0;x<VIDEO_WIDTH;x++)
374 {
375 DrawPixel(screen, x, y+border,oriccolors[buf[calcul+x]].red, oriccolors[buf[calcul+x]].green, oriccolors[buf[calcul+x]].blue);
376
377 //calcul++;
378 }
379 }
380 //else
381 calcul+=VIDEO_WIDTH+1;
382 //calcul++;
383 }
384 unlockscreenSDL_Display(screen);
385 count_frame++;
386 displaySDL_stats();
387 SDL_Flip(screen);
388
389
390 }
391
392 SDL_Delay(20);
393 //usleep(20000);
394 return;
395 }
396
397
398 /********************************************************/
399 /* END Jede Method*/
400 /********************************************************/
401
402 void display_frame (void)
403 {
404 int x, y;
405 int i = 0;
406 int j = 0;
407 uint8_t *src;
408 uint32_t *dst;
409 static uint32_t *tmp = NULL;
410 static uint32_t *tmpback;
411 uint8_t *bs;
412
413 /*
414 if(zoomChange != zoom) {
415 sdl_end();
416 zoom = zoomChange;
417 sdl_start();
418 }
419 */
420 if(setfullScreen != fullScreen) {
421 fullScreen = setfullScreen;
422 sdl_end();
423 sdl_start();
424 }
425 // comment this 3 lines to use jylam method and uncoment from render_frame() to SDL_Delay(20); included
426 displaySDL_run();
427 poll_keyboard();
428 return;
429
430
431
432
433 /* Xeuphoric uses 1 byte at the end of each line,
434 indicating a state change.
435 We don't need that anymore */
436 /*
437 render_frame ();
438 if(scanlineEmulation == 0) {
439 src = (uint8_t *)buf;
440 dst = (uint32_t *)screen->pixels;
441
442
443 oldEffect = 0;
444 if(zoom == 1) {
445 for(y=0; y<VIDEO_HEIGHT; y++) {
446 for(x=0; x<VIDEO_WIDTH; x++) {
447 *dst = palette[*src];
448 dst++;
449 src++;
450 }
451 src++;
452 }
453 } else {
454
455 for(y=0; y<VIDEO_HEIGHT; y++) {
456 for(i = 0; i < zoom; i++) {
457 bs = src;
458 for(x=0; x<VIDEO_WIDTH; x++) {
459 for(j = 0; j < zoom; j++) {
460 *dst = palette[*src];
461 dst++;
462 }
463 src++;
464 }
465 src = bs;
466 }
467 src+=(VIDEO_WIDTH+1);
468 }
469 }
470 } else {
471 // Scanline emulation
472 if(oldEffect == 0) {
473 if(tmp != NULL) {
474 free(tmp);
475 }
476 sdl_end();
477 zoom = 2;
478 sdl_start();
479 oldEffect = 1;
480 if(tmp == NULL) {
481 tmp = (uint32_t*)malloc((VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2) * 4);
482 tmpback = tmp;
483 }
484 } else {
485 }
486
487 src = (uint8_t *)buf;
488 dst = (uint32_t *)screen->pixels;
489
490
491 // Double the screen, making 1 scanline of 2 black
492 for(y=0; y<VIDEO_HEIGHT; y++) {
493 for(x=0; x<VIDEO_WIDTH; x++) {
494 for(j = 0; j < zoom; j++) {
495 *dst = palette[*src];
496 dst++;
497 }
498 src++;
499 }
500 dst+=(VIDEO_WIDTH*2);
501 src++;
502 }
503
504 // Blur midline
505 dst = (uint32_t *)screen->pixels;
506 dst+=VIDEO_WIDTH*2;
507 for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {
508 for(x=0; x<VIDEO_WIDTH*2; x++) {
509 uint8_t r1 = GET_R(*(dst-(VIDEO_WIDTH*2)));
510 uint8_t g1 = GET_G(*(dst-(VIDEO_WIDTH*2)));
511 uint8_t b1 = GET_B(*(dst-(VIDEO_WIDTH*2)));
512 uint8_t r2 = GET_R(*(dst+(VIDEO_WIDTH*2)));
513 uint8_t g2 = GET_G(*(dst+(VIDEO_WIDTH*2)));
514 uint8_t b2 = GET_B(*(dst+(VIDEO_WIDTH*2)));
515 uint8_t r = (r1+r2)/3;
516 uint8_t g = (g1+g2)/3;
517 uint8_t b = (b1+b2)/3;
518
519 *dst = RGB(r, g, b);
520 dst++;
521 }
522 dst+=(VIDEO_WIDTH*2);
523 }
524
525 // Blend at right
526 dst = (uint32_t *)screen->pixels;
527 memcpy(tmp, dst, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
528 for(y=VIDEO_WIDTH*2; y < ((VIDEO_HEIGHT*2) * (VIDEO_WIDTH*2))- (VIDEO_WIDTH*2); y++) {
529 uint8_t r1 = GET_R(tmp[y]);
530 uint8_t g1 = GET_G(tmp[y]);
531 uint8_t b1 = GET_B(tmp[y]);
532 uint8_t r2 = GET_R(tmp[y+1]);
533 uint8_t g2 = GET_G(tmp[y+1]);
534 uint8_t b2 = GET_B(tmp[y+1]);
535 uint8_t r = (r1+r2)/2;
536 uint8_t g = (g1+g2)/2;
537 uint8_t b = (b1+b2)/2;
538 tmp[y] = RGB(r, g, b);
539 }
540
541
542 dst++;
543 tmp++;
544 for(y=1; y < (VIDEO_HEIGHT*2); y++) {
545 uint8_t decal = rand()%3;
546 for(x=decal; x<(VIDEO_WIDTH*2); x++) {
547 uint8_t r1 = GET_R(*(dst));
548 uint8_t g1 = GET_G(*(dst));
549 uint8_t b1 = GET_B(*(dst));
550 uint32_t v1 = (r1+g1)/2;
551 uint8_t r2 = GET_R(*(tmp-decal));
552 uint8_t g2 = GET_G(*(tmp-decal));
553 uint8_t b2 = GET_B(*(tmp-decal));
554 uint32_t v2 = (r2+g2)/2;
555 uint8_t r;
556 uint8_t g;
557 uint8_t b;
558
559 if(v2>v1) {
560 r = (r1+r2)/2;
561 g = (g1+g2)/2;
562 b = (b1+b2)/2;
563 } else {
564 r = r1;
565 g = g1;
566 b = b1;
567 }
568 *tmp = RGB(r, g, b);
569 dst+=1;
570 tmp+=1;
571 }
572 }
573 tmp = tmpback;
574 memcpy(screen->pixels, tmp, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);
575
576 }
577 sdl_display();
578 poll_keyboard();
579 SDL_Delay(20); // max(1/50 Hz, 1/60 Hz) = 20 ms
580 */
581 }
582 #else
583 #error You must have SDL to compile Xeuphoric
584 #endif

  ViewVC Help
Powered by ViewVC 1.1.26