Clarified that the clip rectangle is defined relative to the viewport, and added a clip test to testviewport.c
authorSam Lantinga <slouken@libsdl.org>
Sun, 08 Mar 2020 21:02:40 -0700
changeset 135995cfc87d218e1
parent 13598 663f2daa3e4f
child 13600 e42055c6d8b5
Clarified that the clip rectangle is defined relative to the viewport, and added a clip test to testviewport.c
include/SDL_render.h
test/testviewport.c
     1.1 --- a/include/SDL_render.h	Sun Mar 08 19:23:21 2020 -0700
     1.2 +++ b/include/SDL_render.h	Sun Mar 08 21:02:40 2020 -0700
     1.3 @@ -618,8 +618,8 @@
     1.4   *  \brief Set the clip rectangle for the current target.
     1.5   *
     1.6   *  \param renderer The renderer for which clip rectangle should be set.
     1.7 - *  \param rect   A pointer to the rectangle to set as the clip rectangle, or
     1.8 - *                NULL to disable clipping.
     1.9 + *  \param rect   A pointer to the rectangle to set as the clip rectangle,
    1.10 + *                relative to the viewport, or NULL to disable clipping.
    1.11   *
    1.12   *  \return 0 on success, or -1 on error
    1.13   *
     2.1 --- a/test/testviewport.c	Sun Mar 08 19:23:21 2020 -0700
     2.2 +++ b/test/testviewport.c	Sun Mar 08 21:02:40 2020 -0700
     2.3 @@ -25,12 +25,14 @@
     2.4  
     2.5  static SDLTest_CommonState *state;
     2.6  
     2.7 -SDL_Rect viewport;
     2.8 -int done, j;
     2.9 -SDL_bool use_target = SDL_FALSE;
    2.10 +static SDL_Rect viewport;
    2.11 +static int done, j;
    2.12 +static SDL_bool use_target = SDL_FALSE;
    2.13  #ifdef __EMSCRIPTEN__
    2.14 -Uint32 wait_start;
    2.15 +static Uint32 wait_start;
    2.16  #endif
    2.17 +static SDL_Texture *sprite;
    2.18 +static int sprite_w, sprite_h;
    2.19  
    2.20  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    2.21  static void
    2.22 @@ -40,6 +42,55 @@
    2.23      exit(rc);
    2.24  }
    2.25  
    2.26 +int
    2.27 +LoadSprite(char *file, SDL_Renderer *renderer)
    2.28 +{
    2.29 +    SDL_Surface *temp;
    2.30 +
    2.31 +    /* Load the sprite image */
    2.32 +    temp = SDL_LoadBMP(file);
    2.33 +    if (temp == NULL) {
    2.34 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
    2.35 +        return (-1);
    2.36 +    }
    2.37 +    sprite_w = temp->w;
    2.38 +    sprite_h = temp->h;
    2.39 +
    2.40 +    /* Set transparent pixel as the pixel at (0,0) */
    2.41 +    if (temp->format->palette) {
    2.42 +        SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    2.43 +    } else {
    2.44 +        switch (temp->format->BitsPerPixel) {
    2.45 +        case 15:
    2.46 +            SDL_SetColorKey(temp, SDL_TRUE,
    2.47 +                            (*(Uint16 *) temp->pixels) & 0x00007FFF);
    2.48 +            break;
    2.49 +        case 16:
    2.50 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    2.51 +            break;
    2.52 +        case 24:
    2.53 +            SDL_SetColorKey(temp, SDL_TRUE,
    2.54 +                            (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    2.55 +            break;
    2.56 +        case 32:
    2.57 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    2.58 +            break;
    2.59 +        }
    2.60 +    }
    2.61 +
    2.62 +    /* Create textures from the image */
    2.63 +    sprite = SDL_CreateTextureFromSurface(renderer, temp);
    2.64 +    if (!sprite) {
    2.65 +        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
    2.66 +        SDL_FreeSurface(temp);
    2.67 +        return (-1);
    2.68 +    }
    2.69 +    SDL_FreeSurface(temp);
    2.70 +
    2.71 +    /* We're ready to roll. :) */
    2.72 +    return (0);
    2.73 +}
    2.74 +
    2.75  void
    2.76  DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
    2.77  {    
    2.78 @@ -53,11 +104,11 @@
    2.79      SDL_RenderClear(renderer);
    2.80  
    2.81      /* Test inside points */
    2.82 -    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    2.83 -    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
    2.84 -    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
    2.85 -    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
    2.86 -    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
    2.87 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
    2.88 +    SDL_RenderDrawPoint(renderer, viewport.h/2 + 20, viewport.w/2);
    2.89 +    SDL_RenderDrawPoint(renderer, viewport.h/2 - 20, viewport.w/2);
    2.90 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 20);
    2.91 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 20);
    2.92  
    2.93      /* Test horizontal and vertical lines */
    2.94      SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
    2.95 @@ -68,17 +119,15 @@
    2.96  
    2.97      /* Test diagonal lines */
    2.98      SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
    2.99 -    SDL_RenderDrawLine(renderer, 0, 0,
   2.100 -                       viewport.w-1, viewport.h-1);
   2.101 -    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
   2.102 -                       0, viewport.h-1);                      
   2.103 +    SDL_RenderDrawLine(renderer, 0, 0, viewport.w-1, viewport.h-1);
   2.104 +    SDL_RenderDrawLine(renderer, viewport.w-1, 0, 0, viewport.h-1);                      
   2.105  
   2.106      /* Test outside points */
   2.107 -    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
   2.108 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
   2.109      SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
   2.110      SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
   2.111 -    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
   2.112 -    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
   2.113 +    SDL_RenderDrawPoint(renderer, viewport.h/2, viewport.w/2 - viewport.w);
   2.114 +    SDL_RenderDrawPoint(renderer, viewport.h/2, viewport.w/2 + viewport.w);
   2.115  
   2.116      /* Add a box at the top */
   2.117      rect.w = 8;
   2.118 @@ -86,6 +135,14 @@
   2.119      rect.x = (viewport.w - rect.w) / 2;
   2.120      rect.y = 0;
   2.121      SDL_RenderFillRect(renderer, &rect);
   2.122 +
   2.123 +    /* Add a clip rect and fill it with the sprite */
   2.124 +    SDL_QueryTexture(sprite, NULL, NULL, &rect.w, &rect.h);
   2.125 +    rect.x = (viewport.w - rect.w) / 2;
   2.126 +    rect.y = (viewport.h - rect.h) / 2;
   2.127 +    SDL_RenderSetClipRect(renderer, &rect);
   2.128 +    SDL_RenderCopy(renderer, sprite, NULL, &rect);
   2.129 +    SDL_RenderSetClipRect(renderer, NULL);
   2.130  }
   2.131  
   2.132  void
   2.133 @@ -149,6 +206,7 @@
   2.134          return 1;
   2.135      }
   2.136  
   2.137 +
   2.138      for (i = 1; i < argc;) {
   2.139          int consumed;
   2.140  
   2.141 @@ -171,6 +229,10 @@
   2.142          quit(2);
   2.143      }
   2.144  
   2.145 +    if (LoadSprite("icon.bmp", state->renderers[0]) < 0) {
   2.146 +        quit(2);
   2.147 +    }
   2.148 +
   2.149      if (use_target) {
   2.150          int w, h;
   2.151