test/testshape.c
changeset 8102 c86987834681
parent 7720 f9a649383362
child 8149 681eb46b8ac4
equal deleted inserted replaced
8101:6983d05a5973 8102:c86987834681
    17 
    17 
    18 #define SHAPED_WINDOW_X 150
    18 #define SHAPED_WINDOW_X 150
    19 #define SHAPED_WINDOW_Y 150
    19 #define SHAPED_WINDOW_Y 150
    20 #define SHAPED_WINDOW_DIMENSION 640
    20 #define SHAPED_WINDOW_DIMENSION 640
    21 
    21 
    22 #define TICK_INTERVAL 1000/10
       
    23 
       
    24 typedef struct LoadedPicture {
    22 typedef struct LoadedPicture {
    25     SDL_Surface *surface;
    23     SDL_Surface *surface;
    26     SDL_Texture *texture;
    24     SDL_Texture *texture;
    27     SDL_WindowShapeMode mode;
    25     SDL_WindowShapeMode mode;
       
    26     const char* name;
    28 } LoadedPicture;
    27 } LoadedPicture;
    29 
    28 
    30 void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
    29 void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
    31 {
    30 {
    32     /* Clear render-target to blue. */
    31     /* Clear render-target to blue. */
    35 
    34 
    36     /* Render the texture. */
    35     /* Render the texture. */
    37     SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
    36     SDL_RenderCopy(renderer,texture,&texture_dimensions,&texture_dimensions);
    38 
    37 
    39     SDL_RenderPresent(renderer);
    38     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 }
    39 }
    52 
    40 
    53 int main(int argc,char** argv)
    41 int main(int argc,char** argv)
    54 {
    42 {
    55     Uint8 num_pictures;
    43     Uint8 num_pictures;
    85     pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
    73     pictures = (LoadedPicture *)SDL_malloc(sizeof(LoadedPicture)*num_pictures);
    86     for(i=0;i<num_pictures;i++)
    74     for(i=0;i<num_pictures;i++)
    87         pictures[i].surface = NULL;
    75         pictures[i].surface = NULL;
    88     for(i=0;i<num_pictures;i++) {
    76     for(i=0;i<num_pictures;i++) {
    89         pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    77         pictures[i].surface = SDL_LoadBMP(argv[i+1]);
       
    78         pictures[i].name = argv[i+1];
    90         if(pictures[i].surface == NULL) {
    79         if(pictures[i].surface == NULL) {
    91             j = 0;
    80             j = 0;
    92             for(j=0;j<num_pictures;j++)
    81             for(j=0;j<num_pictures;j++)
    93                 SDL_FreeSurface(pictures[j].surface);
    82                 SDL_FreeSurface(pictures[j].surface);
    94             SDL_free(pictures);
    83             SDL_free(pictures);
    95             SDL_VideoQuit();
    84             SDL_VideoQuit();
    96             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file.");
    85             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
    97             exit(-3);
    86             exit(-3);
    98         }
    87         }
    99 
    88 
   100         format = pictures[i].surface->format;
    89         format = pictures[i].surface->format;
   101         if(format->Amask != 0) {
    90         if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
   102             pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    91             pictures[i].mode.mode = ShapeModeBinarizeAlpha;
   103             pictures[i].mode.parameters.binarizationCutoff = 255;
    92             pictures[i].mode.parameters.binarizationCutoff = 255;
   104         }
    93         }
   105         else {
    94         else {
   106             pictures[i].mode.mode = ShapeModeColorKey;
    95             pictures[i].mode.mode = ShapeModeColorKey;
   107             pictures[i].mode.parameters.colorKey = black;
    96             pictures[i].mode.parameters.colorKey = black;
   108         }
    97         }
   109     }
    98     }
   110 
    99 
   111     window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
   100     window = SDL_CreateShapedWindow("SDL_Shape test",
       
   101         SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
       
   102         SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
       
   103         0);
       
   104     SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
   112     if(window == NULL) {
   105     if(window == NULL) {
   113         for(i=0;i<num_pictures;i++)
   106         for(i=0;i<num_pictures;i++)
   114             SDL_FreeSurface(pictures[i].surface);
   107             SDL_FreeSurface(pictures[i].surface);
   115         SDL_free(pictures);
   108         SDL_free(pictures);
   116         SDL_VideoQuit();
   109         SDL_VideoQuit();
   155     button_down = 0;
   148     button_down = 0;
   156     texture_dimensions.h = 0;
   149     texture_dimensions.h = 0;
   157     texture_dimensions.w = 0;
   150     texture_dimensions.w = 0;
   158     texture_dimensions.x = 0;
   151     texture_dimensions.x = 0;
   159     texture_dimensions.y = 0;
   152     texture_dimensions.y = 0;
       
   153     SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
   160     SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   154     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);
   155     SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   162     SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   156     SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   163     next_time = SDL_GetTicks() + TICK_INTERVAL;
       
   164     while(should_exit == 0) {
   157     while(should_exit == 0) {
   165         event_pending = SDL_PollEvent(&event);
   158         event_pending = SDL_PollEvent(&event);
   166         if(event_pending == 1) {
   159         if(event_pending == 1) {
   167             if(event.type == SDL_KEYDOWN) {
   160             if(event.type == SDL_KEYDOWN) {
   168                 button_down = 1;
   161                 button_down = 1;
   169                 if(event.key.keysym.sym == SDLK_ESCAPE)
   162                 if(event.key.keysym.sym == SDLK_ESCAPE) {
   170                     should_exit = 1;
   163                     should_exit = 1;
       
   164                     break;
       
   165                 }
   171             }
   166             }
   172             if(button_down && event.type == SDL_KEYUP) {
   167             if(button_down && event.type == SDL_KEYUP) {
   173                 button_down = 0;
   168                 button_down = 0;
   174                 current_picture += 1;
   169                 current_picture += 1;
   175                 if(current_picture >= num_pictures)
   170                 if(current_picture >= num_pictures)
   176                     current_picture = 0;
   171                     current_picture = 0;
       
   172                 SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
   177                 SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
   173                 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);
   174                 SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
   179                 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   175                 SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
   180             }
   176             }
   181             if(event.type == SDL_QUIT)
   177             if(event.type == SDL_QUIT)
   182                 should_exit = 1;
   178                 should_exit = 1;
   183             event_pending = 0;
   179             event_pending = 0;
   184         }
   180         }
   185         render(renderer,pictures[current_picture].texture,texture_dimensions);
   181         render(renderer,pictures[current_picture].texture,texture_dimensions);
   186         SDL_Delay(time_left());
   182         SDL_Delay(10);
   187         next_time += TICK_INTERVAL;
       
   188     }
   183     }
   189 
   184 
   190     /* Free the textures. */
   185     /* Free the textures. */
   191     for(i=0;i<num_pictures;i++)
   186     for(i=0;i<num_pictures;i++)
   192         SDL_DestroyTexture(pictures[i].texture);
   187         SDL_DestroyTexture(pictures[i].texture);