test/testshape.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 14 Aug 2013 23:30:10 -0700
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 7663 53fe1b64eb2d
permissions -rw-r--r--
Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
     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     /* Enable standard application logging */
    72     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    73 
    74     if(argc < 2) {
    75 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "SDL_Shape requires at least one bitmap file as argument.");
    76         exit(-1);
    77     }
    78 
    79     if(SDL_VideoInit(NULL) == -1) {
    80         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not initialize SDL video.");
    81         exit(-2);
    82     }
    83 
    84     num_pictures = argc - 1;
    85     pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
    86     for(i=0;i<num_pictures;i++)
    87         pictures[i].surface = NULL;
    88     for(i=0;i<num_pictures;i++) {
    89         pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    90         if(pictures[i].surface == NULL) {
    91             j = 0;
    92             for(j=0;j<num_pictures;j++)
    93                 if(pictures[j].surface != NULL)
    94                     SDL_FreeSurface(pictures[j].surface);
    95             SDL_free(pictures);
    96             SDL_VideoQuit();
    97             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file.");
    98             exit(-3);
    99         }
   100 
   101         format = pictures[i].surface->format;
   102         if(format->Amask != 0) {
   103             pictures[i].mode.mode = ShapeModeBinarizeAlpha;
   104             pictures[i].mode.parameters.binarizationCutoff = 255;
   105         }
   106         else {
   107             pictures[i].mode.mode = ShapeModeColorKey;
   108             pictures[i].mode.parameters.colorKey = black;
   109         }
   110     }
   111 
   112     window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
   113     if(window == NULL) {
   114         for(i=0;i<num_pictures;i++)
   115             SDL_FreeSurface(pictures[i].surface);
   116         SDL_free(pictures);
   117         SDL_VideoQuit();
   118         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
   119         exit(-4);
   120     }
   121     renderer = SDL_CreateRenderer(window,-1,0);
   122     if (!renderer) {
   123         SDL_DestroyWindow(window);
   124         for(i=0;i<num_pictures;i++)
   125             SDL_FreeSurface(pictures[i].surface);
   126         SDL_free(pictures);
   127         SDL_VideoQuit();
   128         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
   129         exit(-5);
   130     }
   131 
   132     for(i=0;i<num_pictures;i++)
   133         pictures[i].texture = NULL;
   134     for(i=0;i<num_pictures;i++) {
   135         pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
   136         if(pictures[i].texture == NULL) {
   137             j = 0;
   138             for(j=0;j<num_pictures;i++)
   139                 if(pictures[i].texture != NULL)
   140                     SDL_DestroyTexture(pictures[i].texture);
   141             for(i=0;i<num_pictures;i++)
   142                 SDL_FreeSurface(pictures[i].surface);
   143             SDL_free(pictures);
   144             SDL_DestroyRenderer(renderer);
   145             SDL_DestroyWindow(window);
   146             SDL_VideoQuit();
   147             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
   148             exit(-6);
   149         }
   150     }
   151 
   152     event_pending = 0;
   153     should_exit = 0;
   154     event_pending = SDL_PollEvent(&event);
   155     current_picture = 0;
   156     button_down = 0;
   157     texture_dimensions.h = 0;
   158     texture_dimensions.w = 0;
   159     texture_dimensions.x = 0;
   160     texture_dimensions.y = 0;
   161     SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   162     SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   163     SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   164     next_time = SDL_GetTicks() + TICK_INTERVAL;
   165     while(should_exit == 0) {
   166         event_pending = SDL_PollEvent(&event);
   167         if(event_pending == 1) {
   168             if(event.type == SDL_KEYDOWN) {
   169                 button_down = 1;
   170                 if(event.key.keysym.sym == SDLK_ESCAPE)
   171                     should_exit = 1;
   172             }
   173             if(button_down && event.type == SDL_KEYUP) {
   174                 button_down = 0;
   175                 current_picture += 1;
   176                 if(current_picture >= num_pictures)
   177                     current_picture = 0;
   178                 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   179                 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   180                 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   181             }
   182             if(event.type == SDL_QUIT)
   183                 should_exit = 1;
   184             event_pending = 0;
   185         }
   186         render(renderer,pictures[current_picture].texture,texture_dimensions);
   187         SDL_Delay(time_left());
   188         next_time += TICK_INTERVAL;
   189     }
   190 
   191     //Free the textures.
   192     for(i=0;i<num_pictures;i++)
   193         SDL_DestroyTexture(pictures[i].texture);
   194     SDL_DestroyRenderer(renderer);
   195     //Destroy the window.
   196     SDL_DestroyWindow(window);
   197     //Free the original surfaces backing the textures.
   198     for(i=0;i<num_pictures;i++)
   199         SDL_FreeSurface(pictures[i].surface);
   200     SDL_free(pictures);
   201     //Call SDL_VideoQuit() before quitting.
   202     SDL_VideoQuit();
   203 
   204     return 0;
   205 }
   206 
   207 /* vi: set ts=4 sw=4 expandtab: */