Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h>
- #include <SDL/SDL_rotozoom.h>
- void displayImage(const char *, SDL_Surface *);
- int main(int argc, char **argv) {
- if (argc <= 1) {
- printf("At least one argument is required.\n");
- exit(1);
- }
- //Set up screen
- //putenv("SDL_NOMOUSE=1"); //Don't even try to grab the mouse when using fbcon
- if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
- printf("SDL_Init: %s\n", SDL_GetError());
- exit(1);
- }
- atexit(SDL_Quit);
- SDL_Surface *screen = SDL_SetVideoMode(1024, 768, 32, SDL_DOUBLEBUF | SDL_FULLSCREEN);
- if (screen == NULL) {
- printf("SDL_SetVideoMode: %s\n", SDL_GetError());
- exit(1);
- }
- char driver[16];
- SDL_VideoDriverName((char *)&driver, sizeof(driver));
- printf("Got screen: %ix%i-%i via %s\n", screen->w, screen->h, screen->format->BitsPerPixel, driver);
- SDL_ShowCursor(SDL_DISABLE);
- //Set up image library
- IMG_Init(IMG_INIT_JPG | IMG_INIT_PNG | IMG_INIT_TIF);
- atexit(IMG_Quit);
- //Wait for kill or mouse click
- #define EVTLOOP_WAIT 200
- #define IMAGE_INTERVAL 30000
- SDL_Event event;
- unsigned long ticks = IMAGE_INTERVAL; //Force immediate render
- unsigned int imgindex = 1;
- do {
- ticks += EVTLOOP_WAIT;
- if (ticks >= IMAGE_INTERVAL) {
- ticks = 0;
- if (++imgindex >= argc)
- imgindex = 1;
- displayImage(argv[imgindex], screen);
- }
- SDL_Delay(EVTLOOP_WAIT);
- SDL_PollEvent(&event);
- } while (event.type != SDL_QUIT && event.type != SDL_MOUSEBUTTONUP);
- }
- void displayImage(const char *file, SDL_Surface *screen) {
- SDL_Surface *img = IMG_Load(file);
- if (img == NULL) {
- printf("IMG_Load: %s\n", IMG_GetError());
- exit(1);
- }
- printf("%s: %ix%i", file, img->w, img->h);
- //Scale to fullscreen, preserve aspect
- double s_aspect = (double)screen->w / screen->h;
- double i_aspect = (double)img->w / img->h;
- double scale;
- if (i_aspect > s_aspect) {
- //Scale to screen width
- scale = (double)screen->w / img->w;
- } else {
- // " " height
- scale = (double)screen->h / img->h;
- }
- SDL_Surface *scaled = zoomSurface(img, scale, scale, SMOOTHING_ON);
- printf(" scaled to %ix%i\n", scaled->w, scaled->h);
- SDL_FreeSurface(img);
- //Display
- SDL_FillRect(screen, NULL, 0x000000);
- SDL_Rect destrect = {(screen->w - scaled->w) / 2, (screen->h - scaled->h) / 2};
- if (SDL_BlitSurface(scaled, NULL, screen, &destrect) != 0) {
- printf("SDL_BlitSurface: %s\n", SDL_GetError());
- exit(1);
- }
- SDL_FreeSurface(scaled);
- SDL_Flip(screen);
- }
Add Comment
Please, Sign In to add comment