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)
     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                 SDL_FreeSurface(pictures[j].surface);
    94             SDL_free(pictures);
    95             SDL_VideoQuit();
    96             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file.");
    97             exit(-3);
    98         }
    99 
   100         format = pictures[i].surface->format;
   101         if(format->Amask != 0) {
   102             pictures[i].mode.mode = ShapeModeBinarizeAlpha;
   103             pictures[i].mode.parameters.binarizationCutoff = 255;
   104         }
   105         else {
   106             pictures[i].mode.mode = ShapeModeColorKey;
   107             pictures[i].mode.parameters.colorKey = black;
   108         }
   109     }
   110 
   111     window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
   112     if(window == NULL) {
   113         for(i=0;i<num_pictures;i++)
   114             SDL_FreeSurface(pictures[i].surface);
   115         SDL_free(pictures);
   116         SDL_VideoQuit();
   117         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create shaped window for SDL_Shape.");
   118         exit(-4);
   119     }
   120     renderer = SDL_CreateRenderer(window,-1,0);
   121     if (!renderer) {
   122         SDL_DestroyWindow(window);
   123         for(i=0;i<num_pictures;i++)
   124             SDL_FreeSurface(pictures[i].surface);
   125         SDL_free(pictures);
   126         SDL_VideoQuit();
   127         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create rendering context for SDL_Shape window.");
   128         exit(-5);
   129     }
   130 
   131     for(i=0;i<num_pictures;i++)
   132         pictures[i].texture = NULL;
   133     for(i=0;i<num_pictures;i++) {
   134         pictures[i].texture = SDL_CreateTextureFromSurface(renderer,pictures[i].surface);
   135         if(pictures[i].texture == NULL) {
   136             j = 0;
   137             for(j=0;j<num_pictures;i++)
   138                 if(pictures[i].texture != NULL)
   139                     SDL_DestroyTexture(pictures[i].texture);
   140             for(i=0;i<num_pictures;i++)
   141                 SDL_FreeSurface(pictures[i].surface);
   142             SDL_free(pictures);
   143             SDL_DestroyRenderer(renderer);
   144             SDL_DestroyWindow(window);
   145             SDL_VideoQuit();
   146             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not create texture for SDL_shape.");
   147             exit(-6);
   148         }
   149     }
   150 
   151     event_pending = 0;
   152     should_exit = 0;
   153     event_pending = SDL_PollEvent(&event);
   154     current_picture = 0;
   155     button_down = 0;
   156     texture_dimensions.h = 0;
   157     texture_dimensions.w = 0;
   158     texture_dimensions.x = 0;
   159     texture_dimensions.y = 0;
   160     SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   161     SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   162     SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   163     next_time = SDL_GetTicks() + TICK_INTERVAL;
   164     while(should_exit == 0) {
   165         event_pending = SDL_PollEvent(&event);
   166         if(event_pending == 1) {
   167             if(event.type == SDL_KEYDOWN) {
   168                 button_down = 1;
   169                 if(event.key.keysym.sym == SDLK_ESCAPE)
   170                     should_exit = 1;
   171             }
   172             if(button_down && event.type == SDL_KEYUP) {
   173                 button_down = 0;
   174                 current_picture += 1;
   175                 if(current_picture >= num_pictures)
   176                     current_picture = 0;
   177                 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   178                 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   179                 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   180             }
   181             if(event.type == SDL_QUIT)
   182                 should_exit = 1;
   183             event_pending = 0;
   184         }
   185         render(renderer,pictures[current_picture].texture,texture_dimensions);
   186         SDL_Delay(time_left());
   187         next_time += TICK_INTERVAL;
   188     }
   189 
   190     /* Free the textures. */
   191     for(i=0;i<num_pictures;i++)
   192         SDL_DestroyTexture(pictures[i].texture);
   193     SDL_DestroyRenderer(renderer);
   194     /* Destroy the window. */
   195     SDL_DestroyWindow(window);
   196     /* Free the original surfaces backing the textures. */
   197     for(i=0;i<num_pictures;i++)
   198         SDL_FreeSurface(pictures[i].surface);
   199     SDL_free(pictures);
   200     /* Call SDL_VideoQuit() before quitting. */
   201     SDL_VideoQuit();
   202 
   203     return 0;
   204 }
   205 
   206 /* vi: set ts=4 sw=4 expandtab: */