test/testshape.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 29 Aug 2013 08:29:51 -0700
changeset 7720 f9a649383362
parent 7678 286c42d7c5ed
child 8102 c86987834681
permissions -rw-r--r--
Christoph Mallon: Remove pointless if (x) before SDL_FreeSurface(x)
slouken@5535
     1
/*
slouken@7517
     2
  Copyright (C) 1997-2013 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
egottlieb@4849
    22
#define TICK_INTERVAL 1000/10
eligottlieb@4805
    23
eligottlieb@4808
    24
typedef struct LoadedPicture {
slouken@5147
    25
    SDL_Surface *surface;
slouken@5147
    26
    SDL_Texture *texture;
slouken@5147
    27
    SDL_WindowShapeMode mode;
eligottlieb@4808
    28
} LoadedPicture;
eligottlieb@4808
    29
slouken@5147
    30
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
slouken@5147
    31
{
gabomdq@7678
    32
    /* Clear render-target to blue. */
slouken@5147
    33
    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
slouken@5147
    34
    SDL_RenderClear(renderer);
slouken@7191
    35
gabomdq@7678
    36
    /* Render the texture. */
slouken@5147
    37
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
slouken@7191
    38
slouken@5147
    39
    SDL_RenderPresent(renderer);
eligottlieb@4804
    40
}
eligottlieb@4804
    41
eligottlieb@4805
    42
static Uint32 next_time;
eligottlieb@4805
    43
slouken@5147
    44
Uint32 time_left()
slouken@5147
    45
{
egottlieb@4849
    46
    Uint32 now = SDL_GetTicks();
egottlieb@4849
    47
    if(next_time <= now)
egottlieb@4849
    48
        return 0;
slouken@5147
    49
    else
egottlieb@4849
    50
        return next_time - now;
eligottlieb@4805
    51
}
eligottlieb@4805
    52
slouken@5147
    53
int main(int argc,char** argv)
slouken@5147
    54
{
slouken@5147
    55
    Uint8 num_pictures;
slouken@5147
    56
    LoadedPicture* pictures;
slouken@5147
    57
    int i, j;
slouken@5147
    58
    SDL_PixelFormat* format = NULL;
slouken@5147
    59
    SDL_Window *window;
slouken@5147
    60
    SDL_Renderer *renderer;
slouken@5147
    61
    SDL_Color black = {0,0,0,0xff};
slouken@5147
    62
    SDL_Event event;
slouken@5147
    63
    int event_pending = 0;
slouken@5147
    64
    int should_exit = 0;
slouken@5147
    65
    unsigned int current_picture;
slouken@5147
    66
    int button_down;
slouken@5147
    67
    Uint32 pixelFormat = 0;
slouken@5147
    68
    int access = 0;
slouken@5147
    69
    SDL_Rect texture_dimensions;;
eligottlieb@4816
    70
aschiffler@7639
    71
    /* Enable standard application logging */
aschiffler@7639
    72
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    73
slouken@5147
    74
    if(argc < 2) {
aschiffler@7639
    75
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
slouken@5147
    76
        exit(-1);
slouken@5032
    77
    }
slouken@7191
    78
slouken@5147
    79
    if(SDL_VideoInit(NULL) == -1) {
aschiffler@7639
    80
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
slouken@5147
    81
        exit(-2);
slouken@5147
    82
    }
slouken@7191
    83
slouken@5147
    84
    num_pictures = argc - 1;
philipp@7479
    85
    pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
slouken@5147
    86
    for(i=0;i<num_pictures;i++)
slouken@5147
    87
        pictures[i].surface = NULL;
slouken@5147
    88
    for(i=0;i<num_pictures;i++) {
slouken@5147
    89
        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
slouken@5147
    90
        if(pictures[i].surface == NULL) {
slouken@5147
    91
            j = 0;
slouken@5147
    92
            for(j=0;j<num_pictures;j++)
slouken@7720
    93
                SDL_FreeSurface(pictures[j].surface);
philipp@7479
    94
            SDL_free(pictures);
slouken@5147
    95
            SDL_VideoQuit();
aschiffler@7639
    96
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file.");
slouken@5147
    97
            exit(-3);
slouken@5147
    98
        }
eligottlieb@4816
    99
slouken@5147
   100
        format = pictures[i].surface->format;
slouken@5147
   101
        if(format->Amask != 0) {
slouken@5147
   102
            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
slouken@5147
   103
            pictures[i].mode.parameters.binarizationCutoff = 255;
slouken@5147
   104
        }
slouken@5147
   105
        else {
slouken@5147
   106
            pictures[i].mode.mode = ShapeModeColorKey;
slouken@5147
   107
            pictures[i].mode.parameters.colorKey = black;
slouken@5147
   108
        }
slouken@5147
   109
    }
slouken@7191
   110
philipp@7478
   111
    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
slouken@5147
   112
    if(window == NULL) {
slouken@5147
   113
        for(i=0;i<num_pictures;i++)
slouken@5147
   114
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   115
        SDL_free(pictures);
slouken@5147
   116
        SDL_VideoQuit();
aschiffler@7639
   117
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
slouken@5147
   118
        exit(-4);
slouken@5147
   119
    }
slouken@5147
   120
    renderer = SDL_CreateRenderer(window,-1,0);
slouken@5147
   121
    if (!renderer) {
slouken@5147
   122
        SDL_DestroyWindow(window);
slouken@5147
   123
        for(i=0;i<num_pictures;i++)
slouken@5147
   124
            SDL_FreeSurface(pictures[i].surface);
philipp@7479
   125
        SDL_free(pictures);
slouken@5147
   126
        SDL_VideoQuit();
aschiffler@7639
   127
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
slouken@5147
   128
        exit(-5);
slouken@5147
   129
    }
slouken@7191
   130
slouken@5147
   131
    for(i=0;i<num_pictures;i++)
slouken@5147
   132
        pictures[i].texture = NULL;
slouken@5147
   133
    for(i=0;i<num_pictures;i++) {
slouken@5158
   134
        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
slouken@5147
   135
        if(pictures[i].texture == NULL) {
slouken@5147
   136
            j = 0;
slouken@5147
   137
            for(j=0;j<num_pictures;i++)
slouken@5147
   138
                if(pictures[i].texture != NULL)
slouken@5147
   139
                    SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   140
            for(i=0;i<num_pictures;i++)
slouken@5147
   141
                SDL_FreeSurface(pictures[i].surface);
philipp@7479
   142
            SDL_free(pictures);
slouken@5147
   143
            SDL_DestroyRenderer(renderer);
slouken@5147
   144
            SDL_DestroyWindow(window);
slouken@5147
   145
            SDL_VideoQuit();
aschiffler@7639
   146
            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
slouken@5147
   147
            exit(-6);
slouken@5147
   148
        }
slouken@5147
   149
    }
slouken@7191
   150
slouken@5147
   151
    event_pending = 0;
slouken@5147
   152
    should_exit = 0;
slouken@5147
   153
    event_pending = SDL_PollEvent(&event);
slouken@5147
   154
    current_picture = 0;
slouken@5147
   155
    button_down = 0;
slouken@5147
   156
    texture_dimensions.h = 0;
slouken@5147
   157
    texture_dimensions.w = 0;
slouken@5147
   158
    texture_dimensions.x = 0;
slouken@5147
   159
    texture_dimensions.y = 0;
slouken@5147
   160
    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   161
    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   162
    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   163
    next_time = SDL_GetTicks() + TICK_INTERVAL;
slouken@5147
   164
    while(should_exit == 0) {
slouken@5147
   165
        event_pending = SDL_PollEvent(&event);
slouken@5147
   166
        if(event_pending == 1) {
slouken@5147
   167
            if(event.type == SDL_KEYDOWN) {
slouken@5147
   168
                button_down = 1;
slouken@5147
   169
                if(event.key.keysym.sym == SDLK_ESCAPE)
slouken@5147
   170
                    should_exit = 1;
slouken@5147
   171
            }
slouken@5147
   172
            if(button_down && event.type == SDL_KEYUP) {
slouken@5147
   173
                button_down = 0;
slouken@5147
   174
                current_picture += 1;
slouken@5147
   175
                if(current_picture >= num_pictures)
slouken@5147
   176
                    current_picture = 0;
slouken@5147
   177
                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   178
                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   179
                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   180
            }
slouken@5147
   181
            if(event.type == SDL_QUIT)
slouken@5147
   182
                should_exit = 1;
slouken@5147
   183
            event_pending = 0;
slouken@5147
   184
        }
slouken@5147
   185
        render(renderer,pictures[current_picture].texture,texture_dimensions);
slouken@5147
   186
        SDL_Delay(time_left());
slouken@5147
   187
        next_time += TICK_INTERVAL;
slouken@5147
   188
    }
slouken@7191
   189
gabomdq@7678
   190
    /* Free the textures. */
slouken@5147
   191
    for(i=0;i<num_pictures;i++)
slouken@5147
   192
        SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   193
    SDL_DestroyRenderer(renderer);
gabomdq@7678
   194
    /* Destroy the window. */
slouken@5147
   195
    SDL_DestroyWindow(window);
gabomdq@7678
   196
    /* Free the original surfaces backing the textures. */
slouken@5147
   197
    for(i=0;i<num_pictures;i++)
slouken@5147
   198
        SDL_FreeSurface(pictures[i].surface);
philipp@7479
   199
    SDL_free(pictures);
gabomdq@7678
   200
    /* Call SDL_VideoQuit() before quitting. */
slouken@5147
   201
    SDL_VideoQuit();
eligottlieb@4816
   202
slouken@5147
   203
    return 0;
eligottlieb@4799
   204
}
slouken@5147
   205
slouken@5147
   206
/* vi: set ts=4 sw=4 expandtab: */