showimage.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 07 Jun 2013 21:12:10 -0700
changeset 403 45133777418d
parent 391 cf211e9ff224
child 486 7bb8af91e887
permissions -rw-r--r--
Run an update loop to refresh the display if the window is obscured for some reason.
This fixes black screen on Linux in VMware
slouken@0
     1
/*
slouken@280
     2
  showimage:  A test application for the SDL image loading library.
slouken@347
     3
  Copyright (C) 1997-2013 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@363
    46
            SDL_SetRenderDrawColor(renderer, col[i].r, col[i].g, col[i].b, col[i].a);
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@391
    63
    const char *saveFile = NULL;
slouken@0
    64
slouken@320
    65
    /* Check command line usage */
slouken@320
    66
    if ( ! argv[1] ) {
slouken@391
    67
        fprintf(stderr, "Usage: %s [-fullscreen] [-save file.png] <image_file> ...\n", argv[0]);
slouken@320
    68
        return(1);
slouken@320
    69
    }
slouken@0
    70
slouken@366
    71
    flags = SDL_WINDOW_HIDDEN;
slouken@320
    72
    for ( i=1; argv[i]; ++i ) {
slouken@320
    73
        if ( strcmp(argv[i], "-fullscreen") == 0 ) {
slouken@320
    74
            SDL_ShowCursor(0);
slouken@320
    75
            flags |= SDL_WINDOW_FULLSCREEN;
slouken@320
    76
        }
slouken@320
    77
    }
slouken@368
    78
slouken@320
    79
    if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
slouken@320
    80
        fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
slouken@320
    81
        return(2);
slouken@320
    82
    }
slouken@0
    83
slouken@320
    84
    for ( i=1; argv[i]; ++i ) {
slouken@320
    85
        if ( strcmp(argv[i], "-fullscreen") == 0 ) {
slouken@320
    86
            continue;
slouken@320
    87
        }
slouken@187
    88
slouken@391
    89
        if ( strcmp(argv[i], "-save") == 0 && argv[i+1] ) {
slouken@391
    90
            ++i;
slouken@391
    91
            saveFile = argv[i];
slouken@391
    92
            continue;
slouken@391
    93
        }
slouken@391
    94
slouken@320
    95
        /* Open the image file */
slouken@320
    96
        texture = IMG_LoadTexture(renderer, argv[i]);
slouken@320
    97
        if (!texture) {
slouken@320
    98
            fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
slouken@320
    99
            continue;
slouken@320
   100
        }
slouken@320
   101
        SDL_QueryTexture(texture, NULL, NULL, &w, &h);
slouken@0
   102
slouken@391
   103
        /* Save the image file, if desired */
slouken@391
   104
        if ( saveFile ) {
slouken@391
   105
            SDL_Surface *surface = IMG_Load(argv[i]);
slouken@391
   106
            if (surface) {
slouken@391
   107
                if ( IMG_SavePNG(surface, saveFile) < 0 ) {
slouken@391
   108
                    fprintf(stderr, "Couldn't save %s: %s\n", saveFile, SDL_GetError());
slouken@391
   109
                }
slouken@391
   110
            } else {
slouken@391
   111
                fprintf(stderr, "Couldn't load %s: %s\n", argv[i], SDL_GetError());
slouken@391
   112
            }
slouken@391
   113
        }
slouken@391
   114
slouken@320
   115
        /* Show the window */
slouken@320
   116
        SDL_SetWindowTitle(window, argv[i]);
slouken@320
   117
        SDL_SetWindowSize(window, w, h);
slouken@320
   118
        SDL_ShowWindow(window);
slouken@17
   119
slouken@320
   120
        done = 0;
slouken@320
   121
        while ( ! done ) {
slouken@403
   122
            while ( SDL_PollEvent(&event) ) {
slouken@320
   123
                switch (event.type) {
slouken@320
   124
                    case SDL_KEYUP:
slouken@320
   125
                        switch (event.key.keysym.sym) {
slouken@320
   126
                            case SDLK_LEFT:
slouken@320
   127
                                if ( i > 1 ) {
slouken@320
   128
                                    i -= 2;
slouken@320
   129
                                    done = 1;
slouken@320
   130
                                }
slouken@320
   131
                                break;
slouken@320
   132
                            case SDLK_RIGHT:
slouken@320
   133
                                if ( argv[i+1] ) {
slouken@320
   134
                                    done = 1;
slouken@320
   135
                                }
slouken@320
   136
                                break;
slouken@320
   137
                            case SDLK_ESCAPE:
slouken@320
   138
                            case SDLK_q:
slouken@320
   139
                                argv[i+1] = NULL;
slouken@320
   140
                            /* Drop through to done */
slouken@320
   141
                            case SDLK_SPACE:
slouken@320
   142
                            case SDLK_TAB:
slouken@320
   143
                            done = 1;
slouken@320
   144
                            break;
slouken@320
   145
                            default:
slouken@320
   146
                            break;
slouken@320
   147
                        }
slouken@320
   148
                        break;
slouken@320
   149
                    case SDL_MOUSEBUTTONDOWN:
slouken@320
   150
                        done = 1;
slouken@320
   151
                        break;
slouken@320
   152
                    case SDL_QUIT:
slouken@320
   153
                        argv[i+1] = NULL;
slouken@320
   154
                        done = 1;
slouken@320
   155
                        break;
slouken@320
   156
                    default:
slouken@320
   157
                        break;
slouken@320
   158
                }
slouken@320
   159
            }
slouken@403
   160
            /* Draw a background pattern in case the image has transparency */
slouken@403
   161
            draw_background(renderer, w, h);
slouken@403
   162
slouken@403
   163
            /* Display the image */
slouken@403
   164
            SDL_RenderCopy(renderer, texture, NULL, NULL);
slouken@403
   165
            SDL_RenderPresent(renderer);
slouken@403
   166
slouken@403
   167
            SDL_Delay(100);
slouken@320
   168
        }
slouken@320
   169
        SDL_DestroyTexture(texture);
slouken@320
   170
    }
slouken@17
   171
slouken@320
   172
    /* We're done! */
slouken@320
   173
    SDL_Quit();
slouken@320
   174
    return(0);
slouken@0
   175
}