showimage.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 15 Feb 2013 14:47:06 -0800
changeset 347 ad5034cad524
parent 320 aa5d55b11751
child 363 985dbf1d5892
permissions -rw-r--r--
Updated copyright year
     1 /*
     2   showimage:  A test application for the SDL image loading library.
     3   Copyright (C) 1997-2013 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].unused);
    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 
    64     /* Check command line usage */
    65     if ( ! argv[1] ) {
    66         fprintf(stderr, "Usage: %s [-fullscreen] <image_file> ...\n", argv[0]);
    67         return(1);
    68     }
    69 
    70     flags = SDL_WINDOW_HIDDEN;
    71     for ( i=1; argv[i]; ++i ) {
    72         if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    73             SDL_ShowCursor(0);
    74             flags |= SDL_WINDOW_FULLSCREEN;
    75         }
    76     }
    77     
    78     if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
    79         fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
    80         return(2);
    81     }
    82 
    83     for ( i=1; argv[i]; ++i ) {
    84         if ( strcmp(argv[i], "-fullscreen") == 0 ) {
    85             continue;
    86         }
    87 
    88         /* Open the image file */
    89         texture = IMG_LoadTexture(renderer, argv[i]);
    90         if (!texture) {
    91             fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
    92             continue;
    93         }
    94         SDL_QueryTexture(texture, NULL, NULL, &w, &h);
    95 
    96         /* Show the window */
    97         SDL_SetWindowTitle(window, argv[i]);
    98         SDL_SetWindowSize(window, w, h);
    99         SDL_ShowWindow(window);
   100 
   101         /* Draw a background pattern in case the image has transparency */
   102         draw_background(renderer, w, h);
   103 
   104         /* Display the image */
   105         SDL_RenderCopy(renderer, texture, NULL, NULL);
   106         SDL_RenderPresent(renderer);
   107 
   108         done = 0;
   109         while ( ! done ) {
   110             if ( SDL_PollEvent(&event) ) {
   111                 switch (event.type) {
   112                     case SDL_KEYUP:
   113                         switch (event.key.keysym.sym) {
   114                             case SDLK_LEFT:
   115                                 if ( i > 1 ) {
   116                                     i -= 2;
   117                                     done = 1;
   118                                 }
   119                                 break;
   120                             case SDLK_RIGHT:
   121                                 if ( argv[i+1] ) {
   122                                     done = 1;
   123                                 }
   124                                 break;
   125                             case SDLK_ESCAPE:
   126                             case SDLK_q:
   127                                 argv[i+1] = NULL;
   128                             /* Drop through to done */
   129                             case SDLK_SPACE:
   130                             case SDLK_TAB:
   131                             done = 1;
   132                             break;
   133                             default:
   134                             break;
   135                         }
   136                         break;
   137                     case SDL_MOUSEBUTTONDOWN:
   138                         done = 1;
   139                         break;
   140                     case SDL_QUIT:
   141                         argv[i+1] = NULL;
   142                         done = 1;
   143                         break;
   144                     default:
   145                         break;
   146                 }
   147             } else {
   148                 SDL_Delay(10);
   149             }
   150         }
   151         SDL_DestroyTexture(texture);
   152     }
   153 
   154     /* We're done! */
   155     SDL_Quit();
   156     return(0);
   157 }