test/testshape.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 19 Feb 2015 23:52:10 -0500
changeset 9370 795d4866e4db
parent 8696 6bb798ddf205
child 9619 b94b6d0bff0f
permissions -rw-r--r--
Mac OS X: Better way to stop duplicate joystick reports (thanks, Kyungdahm!).

Fixes Bugzilla #2822.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
eligottlieb@4799
    12
#include <stdlib.h>
eligottlieb@4799
    13
#include <math.h>
eligottlieb@4816
    14
#include <stdio.h>
eligottlieb@4816
    15
#include "SDL.h"
eligottlieb@4816
    16
#include "SDL_shape.h"
eligottlieb@4799
    17
eligottlieb@4799
    18
#define SHAPED_WINDOW_X 150
eligottlieb@4799
    19
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
    20
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    21
eligottlieb@4808
    22
typedef struct LoadedPicture {
slouken@5147
    23
    SDL_Surface *surface;
slouken@5147
    24
    SDL_Texture *texture;
slouken@5147
    25
    SDL_WindowShapeMode mode;
urkle@8102
    26
    const char* name;
eligottlieb@4808
    27
} LoadedPicture;
eligottlieb@4808
    28
slouken@5147
    29
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
slouken@5147
    30
{
gabomdq@7678
    31
    /* Clear render-target to blue. */
slouken@5147
    32
    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
slouken@5147
    33
    SDL_RenderClear(renderer);
slouken@7191
    34
gabomdq@7678
    35
    /* Render the texture. */
slouken@5147
    36
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
slouken@7191
    37
slouken@5147
    38
    SDL_RenderPresent(renderer);
eligottlieb@4804
    39
}
eligottlieb@4804
    40
slouken@5147
    41
int main(int argc,char** argv)
slouken@5147
    42
{
slouken@5147
    43
    Uint8 num_pictures;
slouken@5147
    44
    LoadedPicture* pictures;
slouken@5147
    45
    int i, j;
slouken@5147
    46
    SDL_PixelFormat* format = NULL;
slouken@5147
    47
    SDL_Window *window;
slouken@5147
    48
    SDL_Renderer *renderer;
slouken@5147
    49
    SDL_Color black = {0,0,0,0xff};
slouken@5147
    50
    SDL_Event event;
slouken@5147
    51
    int event_pending = 0;
slouken@5147
    52
    int should_exit = 0;
slouken@5147
    53
    unsigned int current_picture;
slouken@5147
    54
    int button_down;
slouken@5147
    55
    Uint32 pixelFormat = 0;
slouken@5147
    56
    int access = 0;
philipp@8696
    57
    SDL_Rect texture_dimensions;
eligottlieb@4816
    58
aschiffler@7639
    59
    /* Enable standard application logging */
aschiffler@7639
    60
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    61
slouken@5147
    62
    if(argc < 2) {
aschiffler@7639
    63
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
slouken@5147
    64
        exit(-1);
slouken@5032
    65
    }
slouken@7191
    66
slouken@5147
    67
    if(SDL_VideoInit(NULL) == -1) {
aschiffler@7639
    68
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
slouken@5147
    69
        exit(-2);
slouken@5147
    70
    }
slouken@7191
    71
slouken@5147
    72
    num_pictures = argc - 1;
philipp@7479
    73
    pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
slouken@5147
    74
    for(i=0;i<num_pictures;i++)
slouken@5147
    75
        pictures[i].surface = NULL;
slouken@5147
    76
    for(i=0;i<num_pictures;i++) {
slouken@5147
    77
        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
urkle@8102
    78
        pictures[i].name = argv[i+1];
slouken@5147
    79
        if(pictures[i].surface == NULL) {
slouken@5147
    80
            j = 0;
slouken@5147
    81
            for(j=0;j<num_pictures;j++)
slouken@7720
    82
                SDL_FreeSurface(pictures[j].surface);
philipp@7479
    83
            SDL_free(pictures);
slouken@5147
    84
            SDL_VideoQuit();
urkle@8102
    85
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
slouken@5147
    86
            exit(-3);
slouken@5147
    87
        }
eligottlieb@4816
    88
slouken@5147
    89
        format = pictures[i].surface->format;
urkle@8102
    90
        if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
slouken@5147
    91
            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
slouken@5147
    92
            pictures[i].mode.parameters.binarizationCutoff = 255;
slouken@5147
    93
        }
slouken@5147
    94
        else {
slouken@5147
    95
            pictures[i].mode.mode = ShapeModeColorKey;
slouken@5147
    96
            pictures[i].mode.parameters.colorKey = black;
slouken@5147
    97
        }
slouken@5147
    98
    }
slouken@7191
    99
urkle@8102
   100
    window = SDL_CreateShapedWindow("SDL_Shape test",
urkle@8102
   101
        SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
urkle@8102
   102
        SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
urkle@8102
   103
        0);
urkle@8102
   104
    SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
slouken@5147
   105
    if(window == NULL) {
slouken@5147
   106
        for(i=0;i<num_pictures;i++)
slouken@5147
   107
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   108
        SDL_free(pictures);
slouken@5147
   109
        SDL_VideoQuit();
aschiffler@7639
   110
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
slouken@5147
   111
        exit(-4);
slouken@5147
   112
    }
slouken@5147
   113
    renderer = SDL_CreateRenderer(window,-1,0);
slouken@5147
   114
    if (!renderer) {
slouken@5147
   115
        SDL_DestroyWindow(window);
slouken@5147
   116
        for(i=0;i<num_pictures;i++)
slouken@5147
   117
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   118
        SDL_free(pictures);
slouken@5147
   119
        SDL_VideoQuit();
aschiffler@7639
   120
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
slouken@5147
   121
        exit(-5);
slouken@5147
   122
    }
slouken@7191
   123
slouken@5147
   124
    for(i=0;i<num_pictures;i++)
slouken@5147
   125
        pictures[i].texture = NULL;
slouken@5147
   126
    for(i=0;i<num_pictures;i++) {
slouken@5158
   127
        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
slouken@5147
   128
        if(pictures[i].texture == NULL) {
slouken@5147
   129
            j = 0;
slouken@5147
   130
            for(j=0;j<num_pictures;i++)
slouken@5147
   131
                if(pictures[i].texture != NULL)
slouken@5147
   132
                    SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   133
            for(i=0;i<num_pictures;i++)
slouken@5147
   134
                SDL_FreeSurface(pictures[i].surface);
philipp@7479
   135
            SDL_free(pictures);
slouken@5147
   136
            SDL_DestroyRenderer(renderer);
slouken@5147
   137
            SDL_DestroyWindow(window);
slouken@5147
   138
            SDL_VideoQuit();
aschiffler@7639
   139
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
slouken@5147
   140
            exit(-6);
slouken@5147
   141
        }
slouken@5147
   142
    }
slouken@7191
   143
slouken@5147
   144
    event_pending = 0;
slouken@5147
   145
    should_exit = 0;
slouken@5147
   146
    event_pending = SDL_PollEvent(&event);
slouken@5147
   147
    current_picture = 0;
slouken@5147
   148
    button_down = 0;
slouken@5147
   149
    texture_dimensions.h = 0;
slouken@5147
   150
    texture_dimensions.w = 0;
slouken@5147
   151
    texture_dimensions.x = 0;
slouken@5147
   152
    texture_dimensions.y = 0;
urkle@8102
   153
    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
slouken@5147
   154
    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   155
    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   156
    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   157
    while(should_exit == 0) {
slouken@5147
   158
        event_pending = SDL_PollEvent(&event);
slouken@5147
   159
        if(event_pending == 1) {
slouken@5147
   160
            if(event.type == SDL_KEYDOWN) {
slouken@5147
   161
                button_down = 1;
urkle@8102
   162
                if(event.key.keysym.sym == SDLK_ESCAPE) {
slouken@5147
   163
                    should_exit = 1;
urkle@8102
   164
                    break;
urkle@8102
   165
                }
slouken@5147
   166
            }
slouken@5147
   167
            if(button_down && event.type == SDL_KEYUP) {
slouken@5147
   168
                button_down = 0;
slouken@5147
   169
                current_picture += 1;
slouken@5147
   170
                if(current_picture >= num_pictures)
slouken@5147
   171
                    current_picture = 0;
urkle@8102
   172
                SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
slouken@5147
   173
                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   174
                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   175
                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   176
            }
slouken@5147
   177
            if(event.type == SDL_QUIT)
slouken@5147
   178
                should_exit = 1;
slouken@5147
   179
            event_pending = 0;
slouken@5147
   180
        }
slouken@5147
   181
        render(renderer,pictures[current_picture].texture,texture_dimensions);
urkle@8102
   182
        SDL_Delay(10);
slouken@5147
   183
    }
slouken@7191
   184
gabomdq@7678
   185
    /* Free the textures. */
slouken@5147
   186
    for(i=0;i<num_pictures;i++)
slouken@5147
   187
        SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   188
    SDL_DestroyRenderer(renderer);
gabomdq@7678
   189
    /* Destroy the window. */
slouken@5147
   190
    SDL_DestroyWindow(window);
gabomdq@7678
   191
    /* Free the original surfaces backing the textures. */
slouken@5147
   192
    for(i=0;i<num_pictures;i++)
slouken@5147
   193
        SDL_FreeSurface(pictures[i].surface);
philipp@7479
   194
    SDL_free(pictures);
gabomdq@7678
   195
    /* Call SDL_VideoQuit() before quitting. */
slouken@5147
   196
    SDL_VideoQuit();
eligottlieb@4816
   197
slouken@5147
   198
    return 0;
eligottlieb@4799
   199
}
slouken@5147
   200
slouken@5147
   201
/* vi: set ts=4 sw=4 expandtab: */