tari

SDL_slideshow.c

Jul 18th, 2010
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.49 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #include <SDL/SDL.h>
  5. #include <SDL/SDL_image.h>
  6. #include <SDL/SDL_rotozoom.h>
  7.  
  8. void displayImage(const char *, SDL_Surface *);
  9.  
  10. int main(int argc, char **argv) {
  11.     if (argc <= 1) {
  12.         printf("At least one argument is required.\n");
  13.         exit(1);
  14.     }
  15.     //Set up screen
  16.     //putenv("SDL_NOMOUSE=1");  //Don't even try to grab the mouse when using fbcon
  17.     if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
  18.         printf("SDL_Init: %s\n", SDL_GetError());
  19.         exit(1);
  20.     }
  21.     atexit(SDL_Quit);
  22.     SDL_Surface *screen = SDL_SetVideoMode(1024, 768, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
  23.     if (screen == NULL) {
  24.         printf("SDL_SetVideoMode: %s\n", SDL_GetError());
  25.         exit(1);
  26.     }
  27.     char driver[16];
  28.     SDL_VideoDriverName((char *)&driver, sizeof(driver));
  29.     printf("Got screen: %ix%i-%i via %s\n", screen->w, screen->h, screen->format->BitsPerPixel, driver);
  30.     SDL_ShowCursor(SDL_DISABLE);
  31.  
  32.     //Set up image library
  33.     IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF);
  34.     atexit(IMG_Quit);
  35.  
  36.  
  37.     //Wait for kill or mouse click
  38. #define EVTLOOP_WAIT 200
  39. #define IMAGE_INTERVAL 30000
  40.     SDL_Event event;
  41.     unsigned long ticks = IMAGE_INTERVAL;   //Force immediate render
  42.     unsigned int imgindex = 1;
  43.     do {
  44.         ticks += EVTLOOP_WAIT;
  45.         if (ticks >= IMAGE_INTERVAL) {
  46.             ticks = 0;
  47.             if (++imgindex >= argc)
  48.                 imgindex = 1;
  49.             displayImage(argv[imgindex], screen);
  50.         }
  51.  
  52.         SDL_Delay(EVTLOOP_WAIT);
  53.         SDL_PollEvent(&event);
  54.     } while (event.type != SDL_QUIT && event.type != SDL_MOUSEBUTTONUP);
  55. }
  56.  
  57. void displayImage(const char *file, SDL_Surface *screen) {
  58.     SDL_Surface *img = IMG_Load(file);
  59.     if (img == NULL) {
  60.         printf("IMG_Load: %s\n", IMG_GetError());
  61.         exit(1);
  62.     }
  63.     printf("%s: %ix%i", file, img->w, img->h);
  64.     //Scale to fullscreen, preserve aspect
  65.     double s_aspect = (double)screen->w / screen->h;
  66.     double i_aspect = (double)img->w / img->h;
  67.     double scale;
  68.     if (i_aspect > s_aspect) {
  69.         //Scale to screen width
  70.         scale = (double)screen->w / img->w;
  71.     } else {
  72.         // " " height
  73.         scale = (double)screen->h / img->h;
  74.     }
  75.     SDL_Surface *scaled = zoomSurface(img, scale, scale, SMOOTHING_ON);
  76.     printf(" scaled to %ix%i\n", scaled->w, scaled->h);
  77.     SDL_FreeSurface(img);
  78.  
  79.     //Display
  80.     SDL_FillRect(screen, NULL, 0x000000);
  81.     SDL_Rect destrect = {(screen->w - scaled->w) / 2, (screen->h - scaled->h) / 2};
  82.     if (SDL_BlitSurface(scaled, NULL, screen, &destrect) != 0) {
  83.         printf("SDL_BlitSurface: %s\n", SDL_GetError());
  84.         exit(1);
  85.     }
  86.     SDL_FreeSurface(scaled);
  87.     SDL_Flip(screen);
  88. }
Add Comment
Please, Sign In to add comment