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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 81 by Jede, Sun May 17 18:19:15 2009 UTC revision 82 by Jede, Wed May 27 20:46:43 2009 UTC
# Line 35  Line 35 
35    
36  #include <SDL/SDL.h>  #include <SDL/SDL.h>
37  #include <SDL/SDL_timer.h>  #include <SDL/SDL_timer.h>
38    #include <SDL_ttf.h>
39    
40  #include "caloric.h"  #include "caloric.h"
41  #include "hosttraps.h"  #include "hosttraps.h"
# Line 49  char windowChanging = 0; Line 50  char windowChanging = 0;
50  uint8_t scanlineEmulation = 0;  uint8_t scanlineEmulation = 0;
51  uint8_t oldEffect = 0;  uint8_t oldEffect = 0;
52  uint8_t setfullScreen = 0, fullScreen = 0;  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;  SDL_Surface *screen;
65       int border=20;
66    
67  void display_frame (void);  void display_frame (void);
68    
# Line 64  int sdl_start() Line 74  int sdl_start()
74      zoomChange = zoom;      zoomChange = zoom;
75      SDL_Rect **modes;      SDL_Rect **modes;
76      int i;      int i;
77      windowChanging=1;      windowChanging=1;
78    
79    
80      /* Init video blah */      /* Init video blah */
81      /*Get some infos from video card and resolution*/      /*Get some infos from video card and resolution*/
82    
83     zoom=1;
   
     /*  
     typedef struct{  
   Uint32 hw_available:1;  
   Uint32 wm_available:1;  
   Uint32 blit_hw:1;  
   Uint32 blit_hw_CC:1;  
   Uint32 blit_hw_A:1;  
   Uint32 blit_sw:1;  
   Uint32 blit_sw_CC:1;  
   Uint32 blit_sw_A:1;  
   Uint32 blit_fill;  
   Uint32 video_mem;  
   SDL_PixelFormat *vfmt;  
 } SDL_VideoInfo;  
   
 Structure Data  
   
 hw_available    Is it possible to create hardware surfaces?  
 wm_available    Is there a window manager available  
 blit_hw Are hardware to hardware blits accelerated?  
 blit_hw_CC      Are hardware to hardware colorkey blits accelerated?  
 blit_hw_A       Are hardware to hardware alpha blits accelerated?  
 blit_sw Are software to hardware blits accelerated?  
 blit_sw_CC      Are software to hardware colorkey blits accelerated?  
 blit_sw_A       Are software to hardware alpha blits accelerated?  
 blit_fill       Are color fills accelerated?  
 video_mem       Total amount of video memory in Kilobytes  
 vfmt    Pixel format of the video device  
     */  
    // printf("Can we start a hardware surface : %d\n",vid_info->video_mem);  
 zoom=2;  
84      if(fullScreen) {      if(fullScreen) {
85          //flags |= SDL_FULLSCREEN;          flags |= SDL_FULLSCREEN;
86          flags_video ^= SDL_FULLSCREEN;          flags_video ^= SDL_FULLSCREEN;
87          zoom=2; // We set zoom, we don't care zoom>1 if it's in full screen         // zoom=1; // We set zoom, we don't care zoom>1 if it's in full screen
88          fprintf(stderr, "Full!\n");          fprintf(stderr, "Full!\n");
89      }      }
90      if(audio_method == AM_SDL)      if(audio_method == AM_SDL)
# Line 146  if(modes == (SDL_Rect **)0){ Line 125  if(modes == (SDL_Rect **)0){
125  }  }
126    
127  /* Check if or resolution is restricted */  /* Check if or resolution is restricted */
128    #if HAVE_DEBUG_MODE
129    
130  if(modes == (SDL_Rect **)-1){  if(modes == (SDL_Rect **)-1){
131    printf("All resolutions available.\n");    printf("All resolutions available.\n");
132  }  }
# Line 156  else{ Line 137  else{
137      printf("  %d x %d\n", modes[i]->w, modes[i]->h);      printf("  %d x %d\n", modes[i]->w, modes[i]->h);
138  }  }
139    
140    #endif
141    
142      /* 320x240x32 */      /* 320x240x32 */
143      screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, 32, flags_video); //      screen = SDL_SetVideoMode(VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom+border, 32, flags_video); //
144      if ( screen == NULL ) {      if ( screen == NULL ) {
145          fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());          fprintf(stderr, "Unable to set %dx%dx32 video: %s\n", VIDEO_WIDTH*zoom, VIDEO_HEIGHT*zoom, SDL_GetError());
146          return 0;          return 0;
# Line 198  void Reset_Screen (void) Line 179  void Reset_Screen (void)
179  #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF  #define GET_G(a) ((a&0x0000FF00)>>8)&0xFF
180  #define GET_B(a) (a&0x000000FF)&0xFF  #define GET_B(a) (a&0x000000FF)&0xFF
181  #define RGB(r, g, b) (r<<16) | (g<<8) | (b)  #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)  void display_frame (void)
403  {  {
# Line 208  void display_frame (void) Line 408  void display_frame (void)
408      uint32_t *dst;      uint32_t *dst;
409      static uint32_t *tmp = NULL;      static uint32_t *tmp = NULL;
410      static uint32_t *tmpback;      static uint32_t *tmpback;
411      uint8_t *bs;      uint8_t *bs;
412    
413  /*  /*
414      if(zoomChange != zoom) {      if(zoomChange != zoom) {
415          sdl_end();          sdl_end();
# Line 218  void display_frame (void) Line 419  void display_frame (void)
419      */      */
420      if(setfullScreen != fullScreen) {      if(setfullScreen != fullScreen) {
421          fullScreen = setfullScreen;          fullScreen = setfullScreen;
         /*  
         flags ^= SDL_FULLSCREEN;  
         screen = SDL_SetVideoMode(..., flags);  
         SDL_Surface *SDL_SetVideoMode  (int width, int height, int bpp, Uint32 flags);  
         */  
422          sdl_end();          sdl_end();
423          sdl_start();          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    
     render_frame ();  
431    
432    
433      /* Xeuphoric uses 1 byte at the end of each line,      /* Xeuphoric uses 1 byte at the end of each line,
434         indicating a state change.         indicating a state change.
435         We don't need that anymore */         We don't need that anymore */
436           /*
437        render_frame ();
438      if(scanlineEmulation == 0) {      if(scanlineEmulation == 0) {
439          src = (uint8_t *)buf;          src = (uint8_t *)buf;
440          dst = (uint32_t *)screen->pixels;          dst = (uint32_t *)screen->pixels;
# Line 267  void display_frame (void) Line 468  void display_frame (void)
468              }              }
469          }          }
470      } else {      } else {
471          /* Scanline emulation */          // Scanline emulation
472          if(oldEffect == 0) {          if(oldEffect == 0) {
473              if(tmp != NULL) {              if(tmp != NULL) {
474                  free(tmp);                  free(tmp);
# Line 287  void display_frame (void) Line 488  void display_frame (void)
488          dst = (uint32_t *)screen->pixels;          dst = (uint32_t *)screen->pixels;
489    
490    
491          /* Double the screen, making 1 scanline of 2 black */          // Double the screen, making 1 scanline of 2 black
492          for(y=0; y<VIDEO_HEIGHT; y++) {          for(y=0; y<VIDEO_HEIGHT; y++) {
493              for(x=0; x<VIDEO_WIDTH; x++) {              for(x=0; x<VIDEO_WIDTH; x++) {
494                  for(j = 0; j < zoom; j++) {                  for(j = 0; j < zoom; j++) {
# Line 300  void display_frame (void) Line 501  void display_frame (void)
501              src++;              src++;
502          }          }
503    
504          /* Blur midline */          // Blur midline
505          dst = (uint32_t *)screen->pixels;          dst = (uint32_t *)screen->pixels;
506          dst+=VIDEO_WIDTH*2;          dst+=VIDEO_WIDTH*2;
507          for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {          for(y=1; y < (VIDEO_HEIGHT*2)-1; y+=2) {
# Line 321  void display_frame (void) Line 522  void display_frame (void)
522              dst+=(VIDEO_WIDTH*2);              dst+=(VIDEO_WIDTH*2);
523          }          }
524    
525          /* Blend at right  */          // Blend at right
526          dst = (uint32_t *)screen->pixels;          dst = (uint32_t *)screen->pixels;
527          memcpy(tmp, dst, (VIDEO_HEIGHT*2)*(VIDEO_WIDTH*2)*4);          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++) {          for(y=VIDEO_WIDTH*2; y < ((VIDEO_HEIGHT*2) * (VIDEO_WIDTH*2))- (VIDEO_WIDTH*2); y++) {
# Line 375  void display_frame (void) Line 576  void display_frame (void)
576      }      }
577      sdl_display();      sdl_display();
578      poll_keyboard();      poll_keyboard();
579      SDL_Delay(20);    /* max(1/50 Hz, 1/60 Hz) = 20 ms */      SDL_Delay(20);    // max(1/50 Hz, 1/60 Hz) = 20 ms
580        */
581  }  }
582  #else  #else
583  #error You must have SDL to compile Xeuphoric  #error You must have SDL to compile Xeuphoric

Legend:
Removed from v.81  
changed lines
  Added in v.82

  ViewVC Help
Powered by ViewVC 1.1.26