test/testshape.c
changeset 8102 c86987834681
parent 7720 f9a649383362
child 8149 681eb46b8ac4
     1.1 --- a/test/testshape.c	Wed Jan 08 01:07:57 2014 -0500
     1.2 +++ b/test/testshape.c	Wed Jan 08 14:18:11 2014 -0500
     1.3 @@ -19,12 +19,11 @@
     1.4  #define SHAPED_WINDOW_Y 150
     1.5  #define SHAPED_WINDOW_DIMENSION 640
     1.6  
     1.7 -#define TICK_INTERVAL 1000/10
     1.8 -
     1.9  typedef struct LoadedPicture {
    1.10      SDL_Surface *surface;
    1.11      SDL_Texture *texture;
    1.12      SDL_WindowShapeMode mode;
    1.13 +    const char* name;
    1.14  } LoadedPicture;
    1.15  
    1.16  void render(SDL_Renderer *renderer,SDL_Texture *texture,SDL_Rect texture_dimensions)
    1.17 @@ -39,17 +38,6 @@
    1.18      SDL_RenderPresent(renderer);
    1.19  }
    1.20  
    1.21 -static Uint32 next_time;
    1.22 -
    1.23 -Uint32 time_left()
    1.24 -{
    1.25 -    Uint32 now = SDL_GetTicks();
    1.26 -    if(next_time <= now)
    1.27 -        return 0;
    1.28 -    else
    1.29 -        return next_time - now;
    1.30 -}
    1.31 -
    1.32  int main(int argc,char** argv)
    1.33  {
    1.34      Uint8 num_pictures;
    1.35 @@ -87,18 +75,19 @@
    1.36          pictures[i].surface = NULL;
    1.37      for(i=0;i<num_pictures;i++) {
    1.38          pictures[i].surface = SDL_LoadBMP(argv[i+1]);
    1.39 +        pictures[i].name = argv[i+1];
    1.40          if(pictures[i].surface == NULL) {
    1.41              j = 0;
    1.42              for(j=0;j<num_pictures;j++)
    1.43                  SDL_FreeSurface(pictures[j].surface);
    1.44              SDL_free(pictures);
    1.45              SDL_VideoQuit();
    1.46 -            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file.");
    1.47 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Could not load surface from named bitmap file: %s", argv[i+1]);
    1.48              exit(-3);
    1.49          }
    1.50  
    1.51          format = pictures[i].surface->format;
    1.52 -        if(format->Amask != 0) {
    1.53 +        if(SDL_ISPIXELFORMAT_ALPHA(format->format)) {
    1.54              pictures[i].mode.mode = ShapeModeBinarizeAlpha;
    1.55              pictures[i].mode.parameters.binarizationCutoff = 255;
    1.56          }
    1.57 @@ -108,7 +97,11 @@
    1.58          }
    1.59      }
    1.60  
    1.61 -    window = SDL_CreateShapedWindow("SDL_Shape test",SHAPED_WINDOW_X,SHAPED_WINDOW_Y,SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,SDL_WINDOW_RESIZABLE);
    1.62 +    window = SDL_CreateShapedWindow("SDL_Shape test",
    1.63 +        SHAPED_WINDOW_X, SHAPED_WINDOW_Y,
    1.64 +        SHAPED_WINDOW_DIMENSION,SHAPED_WINDOW_DIMENSION,
    1.65 +        0);
    1.66 +    SDL_SetWindowPosition(window, SHAPED_WINDOW_X, SHAPED_WINDOW_Y);
    1.67      if(window == NULL) {
    1.68          for(i=0;i<num_pictures;i++)
    1.69              SDL_FreeSurface(pictures[i].surface);
    1.70 @@ -157,23 +150,26 @@
    1.71      texture_dimensions.w = 0;
    1.72      texture_dimensions.x = 0;
    1.73      texture_dimensions.y = 0;
    1.74 +    SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
    1.75      SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
    1.76      SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    1.77      SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
    1.78 -    next_time = SDL_GetTicks() + TICK_INTERVAL;
    1.79      while(should_exit == 0) {
    1.80          event_pending = SDL_PollEvent(&event);
    1.81          if(event_pending == 1) {
    1.82              if(event.type == SDL_KEYDOWN) {
    1.83                  button_down = 1;
    1.84 -                if(event.key.keysym.sym == SDLK_ESCAPE)
    1.85 +                if(event.key.keysym.sym == SDLK_ESCAPE) {
    1.86                      should_exit = 1;
    1.87 +                    break;
    1.88 +                }
    1.89              }
    1.90              if(button_down && event.type == SDL_KEYUP) {
    1.91                  button_down = 0;
    1.92                  current_picture += 1;
    1.93                  if(current_picture >= num_pictures)
    1.94                      current_picture = 0;
    1.95 +                SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Changing to shaped bmp: %s", pictures[current_picture].name);
    1.96                  SDL_QueryTexture(pictures[current_picture].texture,(Uint32 *)&pixelFormat,(int *)&access,&texture_dimensions.w,&texture_dimensions.h);
    1.97                  SDL_SetWindowSize(window,texture_dimensions.w,texture_dimensions.h);
    1.98                  SDL_SetWindowShape(window,pictures[current_picture].surface,&pictures[current_picture].mode);
    1.99 @@ -183,8 +179,7 @@
   1.100              event_pending = 0;
   1.101          }
   1.102          render(renderer,pictures[current_picture].texture,texture_dimensions);
   1.103 -        SDL_Delay(time_left());
   1.104 -        next_time += TICK_INTERVAL;
   1.105 +        SDL_Delay(10);
   1.106      }
   1.107  
   1.108      /* Free the textures. */