test/testshape.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Sat, 20 Jul 2013 21:47:16 +0200
changeset 7478 0d02f6a323f5
parent 7191 75360622e65f
child 7479 ef5b0ca53305
permissions -rw-r--r--
Removed not needed SDL_WINDOW_SHOWN from test programs.
See bug #1667.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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
{
slouken@5147
    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
slouken@5147
    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
slouken@5147
    71
    if(argc < 2) {
slouken@5147
    72
        printf("SDL_Shape requires at least one bitmap file as argument.\n");
slouken@5147
    73
        exit(-1);
slouken@5032
    74
    }
slouken@7191
    75
slouken@5147
    76
    if(SDL_VideoInit(NULL) == -1) {
slouken@5147
    77
        printf("Could not initialize SDL video.\n");
slouken@5147
    78
        exit(-2);
slouken@5147
    79
    }
slouken@7191
    80
slouken@5147
    81
    num_pictures = argc - 1;
slouken@5147
    82
    pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
slouken@5147
    83
    for(i=0;i<num_pictures;i++)
slouken@5147
    84
        pictures[i].surface = NULL;
slouken@5147
    85
    for(i=0;i<num_pictures;i++) {
slouken@5147
    86
        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
slouken@5147
    87
        if(pictures[i].surface == NULL) {
slouken@5147
    88
            j = 0;
slouken@5147
    89
            for(j=0;j<num_pictures;j++)
slouken@5147
    90
                if(pictures[j].surface != NULL)
slouken@5147
    91
                    SDL_FreeSurface(pictures[j].surface);
slouken@5147
    92
            free(pictures);
slouken@5147
    93
            SDL_VideoQuit();
slouken@5147
    94
            printf("Could not load surface from named bitmap file.\n");
slouken@5147
    95
            exit(-3);
slouken@5147
    96
        }
eligottlieb@4816
    97
slouken@5147
    98
        format = pictures[i].surface->format;
slouken@5147
    99
        if(format->Amask != 0) {
slouken@5147
   100
            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
slouken@5147
   101
            pictures[i].mode.parameters.binarizationCutoff = 255;
slouken@5147
   102
        }
slouken@5147
   103
        else {
slouken@5147
   104
            pictures[i].mode.mode = ShapeModeColorKey;
slouken@5147
   105
            pictures[i].mode.parameters.colorKey = black;
slouken@5147
   106
        }
slouken@5147
   107
    }
slouken@7191
   108
philipp@7478
   109
    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
slouken@5147
   110
    if(window == NULL) {
slouken@5147
   111
        for(i=0;i<num_pictures;i++)
slouken@5147
   112
            SDL_FreeSurface(pictures[i].surface);
slouken@5147
   113
        free(pictures);
slouken@5147
   114
        SDL_VideoQuit();
slouken@5147
   115
        printf("Could not create shaped window for SDL_Shape.\n");
slouken@5147
   116
        exit(-4);
slouken@5147
   117
    }
slouken@5147
   118
    renderer = SDL_CreateRenderer(window,-1,0);
slouken@5147
   119
    if (!renderer) {
slouken@5147
   120
        SDL_DestroyWindow(window);
slouken@5147
   121
        for(i=0;i<num_pictures;i++)
slouken@5147
   122
            SDL_FreeSurface(pictures[i].surface);
slouken@5147
   123
        free(pictures);
slouken@5147
   124
        SDL_VideoQuit();
slouken@5147
   125
        printf("Could not create rendering context for SDL_Shape window.\n");
slouken@5147
   126
        exit(-5);
slouken@5147
   127
    }
slouken@7191
   128
slouken@5147
   129
    for(i=0;i<num_pictures;i++)
slouken@5147
   130
        pictures[i].texture = NULL;
slouken@5147
   131
    for(i=0;i<num_pictures;i++) {
slouken@5158
   132
        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
slouken@5147
   133
        if(pictures[i].texture == NULL) {
slouken@5147
   134
            j = 0;
slouken@5147
   135
            for(j=0;j<num_pictures;i++)
slouken@5147
   136
                if(pictures[i].texture != NULL)
slouken@5147
   137
                    SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   138
            for(i=0;i<num_pictures;i++)
slouken@5147
   139
                SDL_FreeSurface(pictures[i].surface);
slouken@5147
   140
            free(pictures);
slouken@5147
   141
            SDL_DestroyRenderer(renderer);
slouken@5147
   142
            SDL_DestroyWindow(window);
slouken@5147
   143
            SDL_VideoQuit();
slouken@5147
   144
            printf("Could not create texture for SDL_shape.\n");
slouken@5147
   145
            exit(-6);
slouken@5147
   146
        }
slouken@5147
   147
    }
slouken@7191
   148
slouken@5147
   149
    event_pending = 0;
slouken@5147
   150
    should_exit = 0;
slouken@5147
   151
    event_pending = SDL_PollEvent(&event);
slouken@5147
   152
    current_picture = 0;
slouken@5147
   153
    button_down = 0;
slouken@5147
   154
    texture_dimensions.h = 0;
slouken@5147
   155
    texture_dimensions.w = 0;
slouken@5147
   156
    texture_dimensions.x = 0;
slouken@5147
   157
    texture_dimensions.y = 0;
slouken@5147
   158
    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   159
    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   160
    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   161
    next_time = SDL_GetTicks() + TICK_INTERVAL;
slouken@5147
   162
    while(should_exit == 0) {
slouken@5147
   163
        event_pending = SDL_PollEvent(&event);
slouken@5147
   164
        if(event_pending == 1) {
slouken@5147
   165
            if(event.type == SDL_KEYDOWN) {
slouken@5147
   166
                button_down = 1;
slouken@5147
   167
                if(event.key.keysym.sym == SDLK_ESCAPE)
slouken@5147
   168
                    should_exit = 1;
slouken@5147
   169
            }
slouken@5147
   170
            if(button_down && event.type == SDL_KEYUP) {
slouken@5147
   171
                button_down = 0;
slouken@5147
   172
                current_picture += 1;
slouken@5147
   173
                if(current_picture >= num_pictures)
slouken@5147
   174
                    current_picture = 0;
slouken@5147
   175
                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   176
                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   177
                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   178
            }
slouken@5147
   179
            if(event.type == SDL_QUIT)
slouken@5147
   180
                should_exit = 1;
slouken@5147
   181
            event_pending = 0;
slouken@5147
   182
        }
slouken@5147
   183
        render(renderer,pictures[current_picture].texture,texture_dimensions);
slouken@5147
   184
        SDL_Delay(time_left());
slouken@5147
   185
        next_time += TICK_INTERVAL;
slouken@5147
   186
    }
slouken@7191
   187
slouken@5147
   188
    //Free the textures.
slouken@5147
   189
    for(i=0;i<num_pictures;i++)
slouken@5147
   190
        SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   191
    SDL_DestroyRenderer(renderer);
slouken@5147
   192
    //Destroy the window.
slouken@5147
   193
    SDL_DestroyWindow(window);
slouken@5147
   194
    //Free the original surfaces backing the textures.
slouken@5147
   195
    for(i=0;i<num_pictures;i++)
slouken@5147
   196
        SDL_FreeSurface(pictures[i].surface);
slouken@5147
   197
    free(pictures);
slouken@5147
   198
    //Call SDL_VideoQuit() before quitting.
slouken@5147
   199
    SDL_VideoQuit();
eligottlieb@4816
   200
slouken@5147
   201
    return 0;
eligottlieb@4799
   202
}
slouken@5147
   203
slouken@5147
   204
/* vi: set ts=4 sw=4 expandtab: */