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