Updated testoverlay2 to use the SDL 2D rendering API
authorSam Lantinga <slouken@libsdl.org>
Mon, 07 Feb 2011 00:46:43 -0800
changeset 521616d5e8e90e01
parent 5215 42e0e2f91c55
child 5217 9c0593fa27d6
Updated testoverlay2 to use the SDL 2D rendering API
include/SDL_render.h
test/testoverlay2.c
     1.1 --- a/include/SDL_render.h	Mon Feb 07 00:36:42 2011 -0800
     1.2 +++ b/include/SDL_render.h	Mon Feb 07 00:46:43 2011 -0800
     1.3 @@ -192,6 +192,7 @@
     1.4   *          of range.
     1.5   *  
     1.6   *  \sa SDL_QueryTexture()
     1.7 + *  \sa SDL_UpdateTexture()
     1.8   *  \sa SDL_DestroyTexture()
     1.9   */
    1.10  extern DECLSPEC SDL_Texture * SDLCALL SDL_CreateTexture(SDL_Renderer * renderer,                                                        Uint32 format,
     2.1 --- a/test/testoverlay2.c	Mon Feb 07 00:36:42 2011 -0800
     2.2 +++ b/test/testoverlay2.c	Mon Feb 07 00:46:43 2011 -0800
     2.3 @@ -172,35 +172,27 @@
     2.4  }
     2.5  
     2.6  void
     2.7 -ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
     2.8 -                 int luminance)
     2.9 +ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h,
    2.10 +                 int monochrome, int luminance)
    2.11  {
    2.12      int x, y;
    2.13      int yuv[3];
    2.14 -    Uint8 *p, *op[3];
    2.15 +    Uint8 *op[3];
    2.16  
    2.17 -    SDL_LockSurface(s);
    2.18 -    SDL_LockYUVOverlay(o);
    2.19 -
    2.20 -    /* Convert */
    2.21 -    for (y = 0; y < s->h && y < o->h; y++) {
    2.22 -        p = ((Uint8 *) s->pixels) + s->pitch * y;
    2.23 -        op[0] = o->pixels[0] + o->pitches[0] * y;
    2.24 -        op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
    2.25 -        op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
    2.26 -        for (x = 0; x < s->w && x < o->w; x++) {
    2.27 -            RGBtoYUV(p, yuv, monochrome, luminance);
    2.28 +    op[0] = out;
    2.29 +    op[1] = op[0] + w*h;
    2.30 +    op[2] = op[1] + w*h/4;
    2.31 +    for (y = 0; y < h; ++y) {
    2.32 +        for (x = 0; x < w; ++x) {
    2.33 +            RGBtoYUV(rgb, yuv, monochrome, luminance);
    2.34              *(op[0]++) = yuv[0];
    2.35              if (x % 2 == 0 && y % 2 == 0) {
    2.36                  *(op[1]++) = yuv[2];
    2.37                  *(op[2]++) = yuv[1];
    2.38              }
    2.39 -            p += s->format->BytesPerPixel;
    2.40 +            rgb += 3;
    2.41          }
    2.42      }
    2.43 -
    2.44 -    SDL_UnlockYUVOverlay(o);
    2.45 -    SDL_UnlockSurface(s);
    2.46  }
    2.47  
    2.48  void
    2.49 @@ -339,12 +331,11 @@
    2.50      fprintf(stderr, "\n");
    2.51      fprintf(stderr, "Where 'arg' is any of the following options:\n");
    2.52      fprintf(stderr, "\n");
    2.53 -    fprintf(stderr, "	-fps <frames per second>\n");
    2.54 -    fprintf(stderr,
    2.55 -            "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
    2.56 -    fprintf(stderr,
    2.57 -            "	-scale <scale factor> (initial scale of the overlay)\n");
    2.58 -    fprintf(stderr, "	-help (shows this help)\n");
    2.59 +    fprintf(stderr, "    -fps <frames per second>\n");
    2.60 +    fprintf(stderr, "    -nodelay\n");
    2.61 +    fprintf(stderr, "    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
    2.62 +    fprintf(stderr, "    -scale <scale factor> (initial scale of the overlay)\n");
    2.63 +    fprintf(stderr, "    -help (shows this help)\n");
    2.64      fprintf(stderr, "\n");
    2.65      fprintf(stderr,
    2.66              "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
    2.67 @@ -356,19 +347,22 @@
    2.68  {
    2.69      Uint8 *RawMooseData;
    2.70      SDL_RWops *handle;
    2.71 -    SDL_Surface *screen;
    2.72 -    SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT];
    2.73 -    SDL_Overlay *overlay;
    2.74 -    SDL_Rect overlayrect;
    2.75 +    int window_w;
    2.76 +    int window_h;
    2.77 +    SDL_Window *window;
    2.78 +    SDL_Renderer *renderer;
    2.79 +    Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
    2.80 +    SDL_Texture *MooseTexture;
    2.81 +    SDL_Rect displayrect;
    2.82      SDL_Event event;
    2.83 -    Uint32 lastftick;
    2.84      int paused = 0;
    2.85 -    int resized = 0;
    2.86 -    int i;
    2.87 +    int i, j;
    2.88      int fps = 12;
    2.89      int fpsdelay;
    2.90 +    int nodelay = 0;
    2.91      int overlay_format = SDL_YUY2_OVERLAY;
    2.92      int scale = 5;
    2.93 +    SDL_bool done = SDL_FALSE;
    2.94  
    2.95      if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
    2.96          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    2.97 @@ -396,6 +390,10 @@
    2.98                          "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
    2.99                  quit(10);
   2.100              }
   2.101 +        } else if (strcmp(argv[1], "-nodelay") == 0) {
   2.102 +            nodelay = 1;
   2.103 +            argv += 1;
   2.104 +            argc -= 1;
   2.105          } else if (strcmp(argv[1], "-format") == 0) {
   2.106              if (argv[2]) {
   2.107                  if (!strcmp(argv[2], "YV12"))
   2.108 @@ -471,122 +469,87 @@
   2.109  
   2.110      SDL_RWclose(handle);
   2.111  
   2.112 -    /* Set video mode */
   2.113 -    if ((screen =
   2.114 -         SDL_SetVideoMode(MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0,
   2.115 -                          SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) {
   2.116 -        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
   2.117 +    /* Create the window and renderer */
   2.118 +    window_w = MOOSEPIC_W * scale;
   2.119 +    window_h = MOOSEPIC_H * scale;
   2.120 +    window = SDL_CreateWindow("Happy Moose",
   2.121 +                              SDL_WINDOWPOS_UNDEFINED,
   2.122 +                              SDL_WINDOWPOS_UNDEFINED,
   2.123 +                              window_w, window_h,
   2.124 +                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   2.125 +    if (!window) {
   2.126 +        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   2.127          free(RawMooseData);
   2.128          quit(4);
   2.129      }
   2.130  
   2.131 -    /* Set the window manager title bar */
   2.132 -    SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
   2.133 +    renderer = SDL_CreateRenderer(window, -1, 0);
   2.134 +    if (!renderer) {
   2.135 +        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   2.136 +        free(RawMooseData);
   2.137 +        quit(4);
   2.138 +    }
   2.139 +
   2.140 +    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   2.141 +    if (!renderer) {
   2.142 +        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   2.143 +        free(RawMooseData);
   2.144 +        quit(5);
   2.145 +    }
   2.146  
   2.147      for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   2.148 -        MooseFrame[i] =
   2.149 -            SDL_CreateRGBSurfaceFrom(RawMooseData + i * MOOSEFRAME_SIZE,
   2.150 -                                     MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W,
   2.151 -                                     0, 0, 0, 0);
   2.152 -        if (MooseFrame[i] == NULL) {
   2.153 -            fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n",
   2.154 -                    SDL_GetError());
   2.155 -            free(RawMooseData);
   2.156 -            quit(5);
   2.157 +        Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
   2.158 +        Uint8 *rgb;
   2.159 +        Uint8 *frame;
   2.160 +
   2.161 +        rgb = MooseFrameRGB;
   2.162 +        frame = RawMooseData + i * MOOSEFRAME_SIZE;
   2.163 +        for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
   2.164 +            rgb[0] = MooseColors[frame[j]].r;
   2.165 +            rgb[1] = MooseColors[frame[j]].g;
   2.166 +            rgb[2] = MooseColors[frame[j]].b;
   2.167 +            rgb += 3;
   2.168          }
   2.169 -        SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
   2.170 -
   2.171 -        {
   2.172 -            SDL_Surface *newsurf;
   2.173 -            SDL_PixelFormat format;
   2.174 -
   2.175 -            format.palette = NULL;
   2.176 -            format.BitsPerPixel = 32;
   2.177 -            format.BytesPerPixel = 4;
   2.178 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   2.179 -            format.Rshift = 0;
   2.180 -            format.Gshift = 8;
   2.181 -            format.Bshift = 16;
   2.182 -#else
   2.183 -            format.Rshift = 24;
   2.184 -            format.Gshift = 16;
   2.185 -            format.Bshift = 8;
   2.186 -#endif
   2.187 -            format.Ashift = 0;
   2.188 -            format.Rmask = 0xff << format.Rshift;
   2.189 -            format.Gmask = 0xff << format.Gshift;
   2.190 -            format.Bmask = 0xff << format.Bshift;
   2.191 -            format.Amask = 0;
   2.192 -            format.Rloss = 0;
   2.193 -            format.Gloss = 0;
   2.194 -            format.Bloss = 0;
   2.195 -            format.Aloss = 8;
   2.196 -
   2.197 -            newsurf =
   2.198 -                SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
   2.199 -            if (!newsurf) {
   2.200 -                fprintf(stderr,
   2.201 -                        "Couldn't convert picture to 32bits RGB: %s\n",
   2.202 -                        SDL_GetError());
   2.203 -                quit(6);
   2.204 -            }
   2.205 -            SDL_FreeSurface(MooseFrame[i]);
   2.206 -            MooseFrame[i] = newsurf;
   2.207 -        }
   2.208 +        ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
   2.209      }
   2.210  
   2.211      free(RawMooseData);
   2.212  
   2.213 -    overlay =
   2.214 -        SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   2.215 -    if (!overlay) {
   2.216 -        fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   2.217 -        quit(7);
   2.218 +    /* set the start frame */
   2.219 +    i = 0;
   2.220 +    if (nodelay) {
   2.221 +        fpsdelay = 0;
   2.222 +    } else {
   2.223 +        fpsdelay = 1000 / fps;
   2.224      }
   2.225  
   2.226 -    printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
   2.227 -           overlay->planes, overlay->hw_overlay ? "hardware" : "software",
   2.228 -           overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
   2.229 -           SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
   2.230 -           SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
   2.231 -           SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
   2.232 -           SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
   2.233 -
   2.234 -    for (i = 0; i < overlay->planes; i++) {
   2.235 -        printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   2.236 -    }
   2.237 -
   2.238 -    overlayrect.x = 0;
   2.239 -    overlayrect.y = 0;
   2.240 -    overlayrect.w = MOOSEPIC_W * scale;
   2.241 -    overlayrect.h = MOOSEPIC_H * scale;
   2.242 -
   2.243 -    /* set the start frame */
   2.244 -    i = 0;
   2.245 -    fpsdelay = 1000 / fps;
   2.246 +    displayrect.x = 0;
   2.247 +    displayrect.y = 0;
   2.248 +    displayrect.w = window_w;
   2.249 +    displayrect.h = window_h;
   2.250  
   2.251      /* Ignore key up events, they don't even get filtered */
   2.252      SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   2.253  
   2.254 -    lastftick = SDL_GetTicks();
   2.255 -
   2.256      /* Loop, waiting for QUIT or RESIZE */
   2.257 -    while (1) {
   2.258 -        if (SDL_PollEvent(&event)) {
   2.259 +    while (!done) {
   2.260 +        while (SDL_PollEvent(&event)) {
   2.261              switch (event.type) {
   2.262 -            case SDL_VIDEORESIZE:
   2.263 -                screen =
   2.264 -                    SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
   2.265 -                                     SDL_RESIZABLE | SDL_SWSURFACE);
   2.266 -                overlayrect.w = event.resize.w;
   2.267 -                overlayrect.h = event.resize.h;
   2.268 -                if (paused) {
   2.269 -                    resized = 1;
   2.270 +            case SDL_WINDOWEVENT:
   2.271 +                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   2.272 +                    displayrect.w = window_w = event.window.data1;
   2.273 +                    displayrect.w = window_h = event.window.data2;
   2.274                  }
   2.275                  break;
   2.276              case SDL_MOUSEBUTTONDOWN:
   2.277 -                overlayrect.x = event.button.x - overlayrect.w / 2;
   2.278 -                overlayrect.y = event.button.y - overlayrect.h / 2;
   2.279 +                displayrect.x = event.button.x - window_w / 2;
   2.280 +                displayrect.y = event.button.y - window_h / 2;
   2.281 +                break;
   2.282 +            case SDL_MOUSEMOTION:
   2.283 +                if (event.motion.state) {
   2.284 +                    displayrect.x = event.motion.x - window_w / 2;
   2.285 +                    displayrect.y = event.motion.y - window_h / 2;
   2.286 +                }
   2.287                  break;
   2.288              case SDL_KEYDOWN:
   2.289                  if (event.key.keysym.sym == SDLK_SPACE) {
   2.290 @@ -597,51 +560,24 @@
   2.291                      break;
   2.292                  }
   2.293              case SDL_QUIT:
   2.294 -                SDL_FreeYUVOverlay(overlay);
   2.295 -                for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   2.296 -                    SDL_FreeSurface(MooseFrame[i]);
   2.297 -                }
   2.298 -                quit(0);
   2.299 +                done = SDL_TRUE;
   2.300 +                break;
   2.301              }
   2.302          }
   2.303 +        SDL_Delay(fpsdelay);
   2.304  
   2.305 -        if ((!paused) || (resized)) {
   2.306 -            if (((SDL_GetTicks() - lastftick) > (Uint32)fpsdelay) || (resized)) {
   2.307 -                lastftick = SDL_GetTicks();
   2.308 +        if (!paused) {
   2.309 +            i = (i + 1) % MOOSEFRAMES_COUNT;
   2.310  
   2.311 -                switch (overlay_format) {
   2.312 -                case SDL_YUY2_OVERLAY:
   2.313 -                    ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
   2.314 -                    break;
   2.315 -                case SDL_YV12_OVERLAY:
   2.316 -                    ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
   2.317 -                    break;
   2.318 -                case SDL_UYVY_OVERLAY:
   2.319 -                    ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
   2.320 -                    break;
   2.321 -                case SDL_YVYU_OVERLAY:
   2.322 -                    ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
   2.323 -                    break;
   2.324 -                case SDL_IYUV_OVERLAY:
   2.325 -                    ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
   2.326 -                    break;
   2.327 -                }
   2.328 -
   2.329 -                SDL_DisplayYUVOverlay(overlay, &overlayrect);
   2.330 -                if (!resized) {
   2.331 -                    i++;
   2.332 -                    if (i == 10) {
   2.333 -                        i = 0;
   2.334 -                    }
   2.335 -                } else {
   2.336 -                    resized = 0;
   2.337 -                }
   2.338 -            }
   2.339 +            SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*2);
   2.340          }
   2.341 -        /* kind of timeslice to OS */
   2.342 -        SDL_Delay(1);
   2.343 +        SDL_RenderClear(renderer);
   2.344 +        SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
   2.345 +        SDL_RenderPresent(renderer);
   2.346      }
   2.347 -
   2.348 -    SDL_Quit();
   2.349 +    SDL_DestroyRenderer(renderer);
   2.350 +    quit(0);
   2.351      return 0;
   2.352  }
   2.353 +
   2.354 +/* vi: set ts=4 sw=4 expandtab: */