showimage.c
author Ozkan Sezer <sezeroz@gmail.com>
Thu, 18 Oct 2018 11:57:19 +0300
changeset 612 ca95d0e31aec
parent 575 36e9e2255178
child 619 6f43400b7d77
permissions -rw-r--r--
use less ancient versions of autofoo scripts
     1 /*
     2   showimage:  A test application for the SDL image loading library.
     3   Copyright (C) 1997-2018 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 ( SDL_strcmp(argv[i], "-fullscreen") == 0 ) {
    74             SDL_ShowCursor(0);
    75             flags |= SDL_WINDOW_FULLSCREEN;
    76         }
    77     }
    78 
    79     if (SDL_Init(SDL_INIT_VIDEO) == -1) {
    80         fprintf(stderr, "SDL_Init(SDL_INIT_VIDEO) failed: %s\n", SDL_GetError());
    81         return(2);
    82     }
    83 
    84     if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
    85         fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
    86         return(2);
    87     }
    88 
    89     for ( i=1; argv[i]; ++i ) {
    90         if ( SDL_strcmp(argv[i], "-fullscreen") == 0 ) {
    91             continue;
    92         }
    93 
    94         if ( SDL_strcmp(argv[i], "-save") == 0 && argv[i+1] ) {
    95             ++i;
    96             saveFile = argv[i];
    97             continue;
    98         }
    99 
   100         /* Open the image file */
   101         texture = IMG_LoadTexture(renderer, argv[i]);
   102         if (!texture) {
   103             fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
   104             continue;
   105         }
   106         SDL_QueryTexture(texture, NULL, NULL, &w, &h);
   107 
   108         /* Save the image file, if desired */
   109         if ( saveFile ) {
   110             SDL_Surface *surface = IMG_Load(argv[i]);
   111             if (surface) {
   112                 int result;
   113                 const char *ext = SDL_strrchr(saveFile, '.');
   114                 if ( ext && SDL_strcasecmp(ext, ".bmp") == 0 ) {
   115                     result = SDL_SaveBMP(surface, saveFile);
   116                 } else if ( ext && SDL_strcasecmp(ext, ".jpg") == 0 ) {
   117                     result = IMG_SaveJPG(surface, saveFile, 90);
   118                 } else {
   119                     result = IMG_SavePNG(surface, saveFile);
   120                 }
   121                 if ( result < 0 ) {
   122                     fprintf(stderr, "Couldn't save %s: %s\n", saveFile, SDL_GetError());
   123                 }
   124             } else {
   125                 fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
   126             }
   127         }
   128 
   129         /* Show the window */
   130         SDL_SetWindowTitle(window, argv[i]);
   131         SDL_SetWindowSize(window, w, h);
   132         SDL_ShowWindow(window);
   133 
   134         done = 0;
   135         while ( ! done ) {
   136             while ( SDL_PollEvent(&event) ) {
   137                 switch (event.type) {
   138                     case SDL_KEYUP:
   139                         switch (event.key.keysym.sym) {
   140                             case SDLK_LEFT:
   141                                 if ( i > 1 ) {
   142                                     i -= 2;
   143                                     done = 1;
   144                                 }
   145                                 break;
   146                             case SDLK_RIGHT:
   147                                 if ( argv[i+1] ) {
   148                                     done = 1;
   149                                 }
   150                                 break;
   151                             case SDLK_ESCAPE:
   152                             case SDLK_q:
   153                                 argv[i+1] = NULL;
   154                             /* Drop through to done */
   155                             case SDLK_SPACE:
   156                             case SDLK_TAB:
   157                             done = 1;
   158                             break;
   159                             default:
   160                             break;
   161                         }
   162                         break;
   163                     case SDL_MOUSEBUTTONDOWN:
   164                         done = 1;
   165                         break;
   166                     case SDL_QUIT:
   167                         argv[i+1] = NULL;
   168                         done = 1;
   169                         break;
   170                     default:
   171                         break;
   172                 }
   173             }
   174             /* Draw a background pattern in case the image has transparency */
   175             draw_background(renderer, w, h);
   176 
   177             /* Display the image */
   178             SDL_RenderCopy(renderer, texture, NULL, NULL);
   179             SDL_RenderPresent(renderer);
   180 
   181             SDL_Delay(100);
   182         }
   183         SDL_DestroyTexture(texture);
   184     }
   185 
   186 
   187     SDL_DestroyRenderer(renderer);
   188     SDL_DestroyWindow(window);
   189 
   190     /* We're done! */
   191     SDL_Quit();
   192     return(0);
   193 }