Added screenshot support for tests using the common framework.
authorSam Lantinga <slouken@libsdl.org>
Thu, 10 Mar 2011 00:58:45 -0800
changeset 54639cdb6f1d791a
parent 5462 6d43a04618fc
child 5464 bec064e902cf
Added screenshot support for tests using the common framework.
test/common.c
     1.1 --- a/test/common.c	Wed Mar 09 18:26:35 2011 -0800
     1.2 +++ b/test/common.c	Thu Mar 10 00:58:45 2011 -0800
     1.3 @@ -977,6 +977,41 @@
     1.4      fprintf(stderr, "\n");
     1.5  }
     1.6  
     1.7 +static void
     1.8 +ScreenShot(SDL_Renderer *renderer)
     1.9 +{
    1.10 +    SDL_Rect viewport;
    1.11 +    SDL_Surface *surface;
    1.12 +
    1.13 +    if (!renderer) {
    1.14 +        return;
    1.15 +    }
    1.16 +
    1.17 +    SDL_RenderGetViewport(renderer, &viewport);
    1.18 +    surface = SDL_CreateRGBSurface(0, viewport.w, viewport.h, 24,
    1.19 +#if SDL_BYTEORDER == SDL_LIL_ENDIAN
    1.20 +                    0x00FF0000, 0x0000FF00, 0x000000FF,
    1.21 +#else
    1.22 +                    0x000000FF, 0x0000FF00, 0x00FF0000,
    1.23 +#endif
    1.24 +                    0x00000000);
    1.25 +    if (!surface) {
    1.26 +        fprintf(stderr, "Couldn't create surface: %s\n", SDL_GetError());
    1.27 +        return;
    1.28 +    }
    1.29 +
    1.30 +    if (SDL_RenderReadPixels(renderer, NULL, surface->format->format,
    1.31 +                             surface->pixels, surface->pitch) < 0) {
    1.32 +        fprintf(stderr, "Couldn't read screen: %s\n", SDL_GetError());
    1.33 +        return;
    1.34 +    }
    1.35 +
    1.36 +    if (SDL_SaveBMP(surface, "screenshot.bmp") < 0) {
    1.37 +        fprintf(stderr, "Couldn't save screenshot.bmp: %s\n", SDL_GetError());
    1.38 +        return;
    1.39 +    }
    1.40 +}
    1.41 +
    1.42  void
    1.43  CommonEvent(CommonState * state, SDL_Event * event, int *done)
    1.44  {
    1.45 @@ -991,9 +1026,9 @@
    1.46          switch (event->window.event) {
    1.47          case SDL_WINDOWEVENT_CLOSE:
    1.48  			{
    1.49 -				SDL_Window *pWindow = SDL_GetWindowFromID(event->window.windowID);
    1.50 -				if ( pWindow ) {
    1.51 -					SDL_DestroyWindow( pWindow );
    1.52 +                SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
    1.53 +                if (window) {
    1.54 +					SDL_DestroyWindow(window);
    1.55  				}
    1.56  			}
    1.57              break;
    1.58 @@ -1002,6 +1037,17 @@
    1.59      case SDL_KEYDOWN:
    1.60          switch (event->key.keysym.sym) {
    1.61              /* Add hotkeys here */
    1.62 +        case SDLK_PRINTSCREEN: {
    1.63 +                SDL_Window *window = SDL_GetWindowFromID(event->key.windowID);
    1.64 +                if (window) {
    1.65 +                    for (i = 0; i < state->num_windows; ++i) {
    1.66 +                        if (window == state->windows[i]) {
    1.67 +                            ScreenShot(state->renderers[i]);
    1.68 +                        }
    1.69 +                    }
    1.70 +                }
    1.71 +            }
    1.72 +            break;
    1.73          case SDLK_c:
    1.74              if (event->key.keysym.mod & KMOD_CTRL) {
    1.75                  /* Ctrl-C copy awesome text! */