showimage.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 01 Jan 2017 18:50:51 -0800
changeset 496 6332f9425dcc
parent 486 7bb8af91e887
child 500 28967769bc2e
permissions -rw-r--r--
Updated copyright for 2017
     1 /*
     2   showimage:  A test application for the SDL image loading library.
     3   Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
     4 
     5   This software is provided 'as-is', without any express or implied
     6   warranty.  In no event will the authors be held liable for any damages
     7   arising from the use of this software.
     8 
     9   Permission is granted to anyone to use this software for any purpose,
    10   including commercial applications, and to alter it and redistribute it
    11   freely, subject to the following restrictions:
    12 
    13   1. The origin of this software must not be misrepresented; you must not
    14      claim that you wrote the original software. If you use this software
    15      in a product, an acknowledgment in the product documentation would be
    16      appreciated but is not required.
    17   2. Altered source versions must be plainly marked as such, and must not be
    18      misrepresented as being the original software.
    19   3. This notice may not be removed or altered from any source distribution.
    20 */
    21 
    22 #include <stdlib.h>
    23 #include <stdio.h>
    24 #include <string.h>
    25 
    26 #include "SDL.h"
    27 #include "SDL_image.h"
    28 
    29 
    30 /* Draw a Gimpish background pattern to show transparency in the image */
    31 static void draw_background(SDL_Renderer *renderer, int w, int h)
    32 {
    33     SDL_Color col[2] = {
    34         { 0x66, 0x66, 0x66, 0xff },
    35         { 0x99, 0x99, 0x99, 0xff },
    36     };
    37     int i, x, y;
    38     SDL_Rect rect;
    39 
    40     rect.w = 8;
    41     rect.h = 8;
    42     for (y = 0; y < h; y += rect.h) {
    43         for (x = 0; x < w; x += rect.w) {
    44             /* use an 8x8 checkerboard pattern */
    45             i = (((x ^ y) >> 3) & 1);
    46             SDL_SetRenderDrawColor(renderer, col[i].r, col[i].g, col[i].b, col[i].a);
    47 
    48             rect.x = x;
    49             rect.y = y;
    50             SDL_RenderFillRect(renderer, &rect);
    51         }
    52     }
    53 }
    54 
    55 int main(int argc, char *argv[])
    56 {
    57     SDL_Window *window;
    58     SDL_Renderer *renderer;
    59     SDL_Texture *texture;
    60     Uint32 flags;
    61     int i, w, h, done;
    62     SDL_Event event;
    63     const char *saveFile = NULL;
    64 
    65     /* Check command line usage */
    66     if ( ! argv[1] ) {
    67         fprintf(stderr, "Usage: %s [-fullscreen] [-save file.png] <image_file> ...\n", argv[0]);
    68         return(1);
    69     }
    70 
    71     flags = SDL_WINDOW_HIDDEN;
    72     for ( i=1; argv[i]; ++i ) {
    73         if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    74             SDL_ShowCursor(0);
    75             flags |= SDL_WINDOW_FULLSCREEN;
    76         }
    77     }
    78 
    79     if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
    80         fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
    81         return(2);
    82     }
    83 
    84     for ( i=1; argv[i]; ++i ) {
    85         if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    86             continue;
    87         }
    88 
    89         if ( strcmp(argv[i], "-save") == 0 && argv[i+1] ) {
    90             ++i;
    91             saveFile = argv[i];
    92             continue;
    93         }
    94 
    95         /* Open the image file */
    96         texture = IMG_LoadTexture(renderer, argv[i]);
    97         if (!texture) {
    98             fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
    99             continue;
   100         }
   101         SDL_QueryTexture(texture, NULL, NULL, &w, &h);
   102 
   103         /* Save the image file, if desired */
   104         if ( saveFile ) {
   105             SDL_Surface *surface = IMG_Load(argv[i]);
   106             if (surface) {
   107                 if ( IMG_SavePNG(surface, saveFile) < 0 ) {
   108                     fprintf(stderr, "Couldn't save %s: %s\n", saveFile, SDL_GetError());
   109                 }
   110             } else {
   111                 fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
   112             }
   113         }
   114 
   115         /* Show the window */
   116         SDL_SetWindowTitle(window, argv[i]);
   117         SDL_SetWindowSize(window, w, h);
   118         SDL_ShowWindow(window);
   119 
   120         done = 0;
   121         while ( ! done ) {
   122             while ( SDL_PollEvent(&event) ) {
   123                 switch (event.type) {
   124                     case SDL_KEYUP:
   125                         switch (event.key.keysym.sym) {
   126                             case SDLK_LEFT:
   127                                 if ( i > 1 ) {
   128                                     i -= 2;
   129                                     done = 1;
   130                                 }
   131                                 break;
   132                             case SDLK_RIGHT:
   133                                 if ( argv[i+1] ) {
   134                                     done = 1;
   135                                 }
   136                                 break;
   137                             case SDLK_ESCAPE:
   138                             case SDLK_q:
   139                                 argv[i+1] = NULL;
   140                             /* Drop through to done */
   141                             case SDLK_SPACE:
   142                             case SDLK_TAB:
   143                             done = 1;
   144                             break;
   145                             default:
   146                             break;
   147                         }
   148                         break;
   149                     case SDL_MOUSEBUTTONDOWN:
   150                         done = 1;
   151                         break;
   152                     case SDL_QUIT:
   153                         argv[i+1] = NULL;
   154                         done = 1;
   155                         break;
   156                     default:
   157                         break;
   158                 }
   159             }
   160             /* Draw a background pattern in case the image has transparency */
   161             draw_background(renderer, w, h);
   162 
   163             /* Display the image */
   164             SDL_RenderCopy(renderer, texture, NULL, NULL);
   165             SDL_RenderPresent(renderer);
   166 
   167             SDL_Delay(100);
   168         }
   169         SDL_DestroyTexture(texture);
   170     }
   171 
   172     /* We're done! */
   173     SDL_Quit();
   174     return(0);
   175 }