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