test/testshape.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 01 Feb 2011 19:19:43 -0800
changeset 5147 ad50b3db78bd
parent 5142 c8e049de174c
child 5158 b3ccd1947786
permissions -rw-r--r--
The rendering functions take a context so it's clear what window they're drawing to. This also potentially opens to the door to multi-threaded rendering in the future.
eligottlieb@4799
     1
#include <stdlib.h>
eligottlieb@4799
     2
#include <math.h>
eligottlieb@4816
     3
#include <stdio.h>
eligottlieb@4816
     4
#include "SDL.h"
eligottlieb@4816
     5
#include "SDL_shape.h"
eligottlieb@4799
     6
eligottlieb@4799
     7
#define SHAPED_WINDOW_X 150
eligottlieb@4799
     8
#define SHAPED_WINDOW_Y 150
eligottlieb@4799
     9
#define SHAPED_WINDOW_DIMENSION 640
eligottlieb@4799
    10
egottlieb@4849
    11
#define TICK_INTERVAL 1000/10
eligottlieb@4805
    12
eligottlieb@4808
    13
typedef struct LoadedPicture {
slouken@5147
    14
    SDL_Surface *surface;
slouken@5147
    15
    SDL_Texture *texture;
slouken@5147
    16
    SDL_WindowShapeMode mode;
eligottlieb@4808
    17
} LoadedPicture;
eligottlieb@4808
    18
slouken@5147
    19
void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
slouken@5147
    20
{
slouken@5147
    21
    //Clear render-target to blue.
slouken@5147
    22
    SDL_SetRenderDrawColor(renderer,0x00,0x00,0xff,0xff);
slouken@5147
    23
    SDL_RenderClear(renderer);
slouken@5147
    24
    
slouken@5147
    25
    //Render the texture.
slouken@5147
    26
    SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
slouken@5147
    27
    
slouken@5147
    28
    SDL_RenderPresent(renderer);
eligottlieb@4804
    29
}
eligottlieb@4804
    30
eligottlieb@4805
    31
static Uint32 next_time;
eligottlieb@4805
    32
slouken@5147
    33
Uint32 time_left()
slouken@5147
    34
{
egottlieb@4849
    35
    Uint32 now = SDL_GetTicks();
egottlieb@4849
    36
    if(next_time <= now)
egottlieb@4849
    37
        return 0;
slouken@5147
    38
    else
egottlieb@4849
    39
        return next_time - now;
eligottlieb@4805
    40
}
eligottlieb@4805
    41
slouken@5147
    42
int main(int argc,char** argv)
slouken@5147
    43
{
slouken@5147
    44
    Uint8 num_pictures;
slouken@5147
    45
    LoadedPicture* pictures;
slouken@5147
    46
    int i, j;
slouken@5147
    47
    SDL_PixelFormat* format = NULL;
slouken@5147
    48
    SDL_Window *window;
slouken@5147
    49
    SDL_Renderer *renderer;
slouken@5147
    50
    SDL_Color black = {0,0,0,0xff};
slouken@5147
    51
    SDL_Event event;
slouken@5147
    52
    int event_pending = 0;
slouken@5147
    53
    int should_exit = 0;
slouken@5147
    54
    unsigned int current_picture;
slouken@5147
    55
    int button_down;
slouken@5147
    56
    Uint32 pixelFormat = 0;
slouken@5147
    57
    int access = 0;
slouken@5147
    58
    SDL_Rect texture_dimensions;;
eligottlieb@4816
    59
slouken@5147
    60
    if(argc < 2) {
slouken@5147
    61
        printf("SDL_Shape requires at least one bitmap file as argument.\n");
slouken@5147
    62
        exit(-1);
slouken@5032
    63
    }
slouken@5147
    64
    
slouken@5147
    65
    if(SDL_VideoInit(NULL) == -1) {
slouken@5147
    66
        printf("Could not initialize SDL video.\n");
slouken@5147
    67
        exit(-2);
slouken@5147
    68
    }
slouken@5147
    69
    
slouken@5147
    70
    num_pictures = argc - 1;
slouken@5147
    71
    pictures = (LoadedPicture *)malloc(sizeof(LoadedPicture)*num_pictures);
slouken@5147
    72
    for(i=0;i<num_pictures;i++)
slouken@5147
    73
        pictures[i].surface = NULL;
slouken@5147
    74
    for(i=0;i<num_pictures;i++) {
slouken@5147
    75
        pictures[i].surface = SDL_LoadBMP(argv[i+1]);
slouken@5147
    76
        if(pictures[i].surface == NULL) {
slouken@5147
    77
            j = 0;
slouken@5147
    78
            for(j=0;j<num_pictures;j++)
slouken@5147
    79
                if(pictures[j].surface != NULL)
slouken@5147
    80
                    SDL_FreeSurface(pictures[j].surface);
slouken@5147
    81
            free(pictures);
slouken@5147
    82
            SDL_VideoQuit();
slouken@5147
    83
            printf("Could not load surface from named bitmap file.\n");
slouken@5147
    84
            exit(-3);
slouken@5147
    85
        }
eligottlieb@4816
    86
slouken@5147
    87
        format = pictures[i].surface->format;
slouken@5147
    88
        if(format->Amask != 0) {
slouken@5147
    89
            pictures[i].mode.mode = ShapeModeBinarizeAlpha;
slouken@5147
    90
            pictures[i].mode.parameters.binarizationCutoff = 255;
slouken@5147
    91
        }
slouken@5147
    92
        else {
slouken@5147
    93
            pictures[i].mode.mode = ShapeModeColorKey;
slouken@5147
    94
            pictures[i].mode.parameters.colorKey = black;
slouken@5147
    95
        }
slouken@5147
    96
    }
slouken@5147
    97
    
slouken@5147
    98
    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
slouken@5147
    99
    if(window == NULL) {
slouken@5147
   100
        for(i=0;i<num_pictures;i++)
slouken@5147
   101
            SDL_FreeSurface(pictures[i].surface);
slouken@5147
   102
        free(pictures);
slouken@5147
   103
        SDL_VideoQuit();
slouken@5147
   104
        printf("Could not create shaped window for SDL_Shape.\n");
slouken@5147
   105
        exit(-4);
slouken@5147
   106
    }
slouken@5147
   107
    renderer = SDL_CreateRenderer(window,-1,0);
slouken@5147
   108
    if (!renderer) {
slouken@5147
   109
        SDL_DestroyWindow(window);
slouken@5147
   110
        for(i=0;i<num_pictures;i++)
slouken@5147
   111
            SDL_FreeSurface(pictures[i].surface);
slouken@5147
   112
        free(pictures);
slouken@5147
   113
        SDL_VideoQuit();
slouken@5147
   114
        printf("Could not create rendering context for SDL_Shape window.\n");
slouken@5147
   115
        exit(-5);
slouken@5147
   116
    }
slouken@5147
   117
    
slouken@5147
   118
    for(i=0;i<num_pictures;i++)
slouken@5147
   119
        pictures[i].texture = NULL;
slouken@5147
   120
    for(i=0;i<num_pictures;i++) {
slouken@5147
   121
        pictures[i].texture = SDL_CreateTextureFromSurface(renderer,0,pictures[i].surface);
slouken@5147
   122
        if(pictures[i].texture == NULL) {
slouken@5147
   123
            j = 0;
slouken@5147
   124
            for(j=0;j<num_pictures;i++)
slouken@5147
   125
                if(pictures[i].texture != NULL)
slouken@5147
   126
                    SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   127
            for(i=0;i<num_pictures;i++)
slouken@5147
   128
                SDL_FreeSurface(pictures[i].surface);
slouken@5147
   129
            free(pictures);
slouken@5147
   130
            SDL_DestroyRenderer(renderer);
slouken@5147
   131
            SDL_DestroyWindow(window);
slouken@5147
   132
            SDL_VideoQuit();
slouken@5147
   133
            printf("Could not create texture for SDL_shape.\n");
slouken@5147
   134
            exit(-6);
slouken@5147
   135
        }
slouken@5147
   136
    }
slouken@5147
   137
    
slouken@5147
   138
    event_pending = 0;
slouken@5147
   139
    should_exit = 0;
slouken@5147
   140
    event_pending = SDL_PollEvent(&event);
slouken@5147
   141
    current_picture = 0;
slouken@5147
   142
    button_down = 0;
slouken@5147
   143
    texture_dimensions.h = 0;
slouken@5147
   144
    texture_dimensions.w = 0;
slouken@5147
   145
    texture_dimensions.x = 0;
slouken@5147
   146
    texture_dimensions.y = 0;
slouken@5147
   147
    SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   148
    SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   149
    SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   150
    next_time = SDL_GetTicks() + TICK_INTERVAL;
slouken@5147
   151
    while(should_exit == 0) {
slouken@5147
   152
        event_pending = SDL_PollEvent(&event);
slouken@5147
   153
        if(event_pending == 1) {
slouken@5147
   154
            if(event.type == SDL_KEYDOWN) {
slouken@5147
   155
                button_down = 1;
slouken@5147
   156
                if(event.key.keysym.sym == SDLK_ESCAPE)
slouken@5147
   157
                    should_exit = 1;
slouken@5147
   158
            }
slouken@5147
   159
            if(button_down && event.type == SDL_KEYUP) {
slouken@5147
   160
                button_down = 0;
slouken@5147
   161
                current_picture += 1;
slouken@5147
   162
                if(current_picture >= num_pictures)
slouken@5147
   163
                    current_picture = 0;
slouken@5147
   164
                SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
slouken@5147
   165
                SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
slouken@5147
   166
                SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
slouken@5147
   167
            }
slouken@5147
   168
            if(event.type == SDL_QUIT)
slouken@5147
   169
                should_exit = 1;
slouken@5147
   170
            event_pending = 0;
slouken@5147
   171
        }
slouken@5147
   172
        render(renderer,pictures[current_picture].texture,texture_dimensions);
slouken@5147
   173
        SDL_Delay(time_left());
slouken@5147
   174
        next_time += TICK_INTERVAL;
slouken@5147
   175
    }
slouken@5147
   176
    
slouken@5147
   177
    //Free the textures.
slouken@5147
   178
    for(i=0;i<num_pictures;i++)
slouken@5147
   179
        SDL_DestroyTexture(pictures[i].texture);
slouken@5147
   180
    SDL_DestroyRenderer(renderer);
slouken@5147
   181
    //Destroy the window.
slouken@5147
   182
    SDL_DestroyWindow(window);
slouken@5147
   183
    //Free the original surfaces backing the textures.
slouken@5147
   184
    for(i=0;i<num_pictures;i++)
slouken@5147
   185
        SDL_FreeSurface(pictures[i].surface);
slouken@5147
   186
    free(pictures);
slouken@5147
   187
    //Call SDL_VideoQuit() before quitting.
slouken@5147
   188
    SDL_VideoQuit();
eligottlieb@4816
   189
slouken@5147
   190
    return 0;
eligottlieb@4799
   191
}
slouken@5147
   192
slouken@5147
   193
/* vi: set ts=4 sw=4 expandtab: */