test/testoverlay2.c
changeset 5216 16d5e8e90e01
parent 4884 27ab20a36eba
child 5252 ec30f9d4c2a8
     1.1 --- a/test/testoverlay2.c	Mon Feb 07 00:36:42 2011 -0800
     1.2 +++ b/test/testoverlay2.c	Mon Feb 07 00:46:43 2011 -0800
     1.3 @@ -172,35 +172,27 @@
     1.4  }
     1.5  
     1.6  void
     1.7 -ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
     1.8 -                 int luminance)
     1.9 +ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h,
    1.10 +                 int monochrome, int luminance)
    1.11  {
    1.12      int x, y;
    1.13      int yuv[3];
    1.14 -    Uint8 *p, *op[3];
    1.15 +    Uint8 *op[3];
    1.16  
    1.17 -    SDL_LockSurface(s);
    1.18 -    SDL_LockYUVOverlay(o);
    1.19 -
    1.20 -    /* Convert */
    1.21 -    for (y = 0; y < s->h && y < o->h; y++) {
    1.22 -        p = ((Uint8 *) s->pixels) + s->pitch * y;
    1.23 -        op[0] = o->pixels[0] + o->pitches[0] * y;
    1.24 -        op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
    1.25 -        op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
    1.26 -        for (x = 0; x < s->w && x < o->w; x++) {
    1.27 -            RGBtoYUV(p, yuv, monochrome, luminance);
    1.28 +    op[0] = out;
    1.29 +    op[1] = op[0] + w*h;
    1.30 +    op[2] = op[1] + w*h/4;
    1.31 +    for (y = 0; y < h; ++y) {
    1.32 +        for (x = 0; x < w; ++x) {
    1.33 +            RGBtoYUV(rgb, yuv, monochrome, luminance);
    1.34              *(op[0]++) = yuv[0];
    1.35              if (x % 2 == 0 && y % 2 == 0) {
    1.36                  *(op[1]++) = yuv[2];
    1.37                  *(op[2]++) = yuv[1];
    1.38              }
    1.39 -            p += s->format->BytesPerPixel;
    1.40 +            rgb += 3;
    1.41          }
    1.42      }
    1.43 -
    1.44 -    SDL_UnlockYUVOverlay(o);
    1.45 -    SDL_UnlockSurface(s);
    1.46  }
    1.47  
    1.48  void
    1.49 @@ -339,12 +331,11 @@
    1.50      fprintf(stderr, "\n");
    1.51      fprintf(stderr, "Where 'arg' is any of the following options:\n");
    1.52      fprintf(stderr, "\n");
    1.53 -    fprintf(stderr, "	-fps <frames per second>\n");
    1.54 -    fprintf(stderr,
    1.55 -            "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
    1.56 -    fprintf(stderr,
    1.57 -            "	-scale <scale factor> (initial scale of the overlay)\n");
    1.58 -    fprintf(stderr, "	-help (shows this help)\n");
    1.59 +    fprintf(stderr, "    -fps <frames per second>\n");
    1.60 +    fprintf(stderr, "    -nodelay\n");
    1.61 +    fprintf(stderr, "    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
    1.62 +    fprintf(stderr, "    -scale <scale factor> (initial scale of the overlay)\n");
    1.63 +    fprintf(stderr, "    -help (shows this help)\n");
    1.64      fprintf(stderr, "\n");
    1.65      fprintf(stderr,
    1.66              "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
    1.67 @@ -356,19 +347,22 @@
    1.68  {
    1.69      Uint8 *RawMooseData;
    1.70      SDL_RWops *handle;
    1.71 -    SDL_Surface *screen;
    1.72 -    SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT];
    1.73 -    SDL_Overlay *overlay;
    1.74 -    SDL_Rect overlayrect;
    1.75 +    int window_w;
    1.76 +    int window_h;
    1.77 +    SDL_Window *window;
    1.78 +    SDL_Renderer *renderer;
    1.79 +    Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
    1.80 +    SDL_Texture *MooseTexture;
    1.81 +    SDL_Rect displayrect;
    1.82      SDL_Event event;
    1.83 -    Uint32 lastftick;
    1.84      int paused = 0;
    1.85 -    int resized = 0;
    1.86 -    int i;
    1.87 +    int i, j;
    1.88      int fps = 12;
    1.89      int fpsdelay;
    1.90 +    int nodelay = 0;
    1.91      int overlay_format = SDL_YUY2_OVERLAY;
    1.92      int scale = 5;
    1.93 +    SDL_bool done = SDL_FALSE;
    1.94  
    1.95      if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
    1.96          fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    1.97 @@ -396,6 +390,10 @@
    1.98                          "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
    1.99                  quit(10);
   1.100              }
   1.101 +        } else if (strcmp(argv[1], "-nodelay") == 0) {
   1.102 +            nodelay = 1;
   1.103 +            argv += 1;
   1.104 +            argc -= 1;
   1.105          } else if (strcmp(argv[1], "-format") == 0) {
   1.106              if (argv[2]) {
   1.107                  if (!strcmp(argv[2], "YV12"))
   1.108 @@ -471,122 +469,87 @@
   1.109  
   1.110      SDL_RWclose(handle);
   1.111  
   1.112 -    /* Set video mode */
   1.113 -    if ((screen =
   1.114 -         SDL_SetVideoMode(MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0,
   1.115 -                          SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) {
   1.116 -        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
   1.117 +    /* Create the window and renderer */
   1.118 +    window_w = MOOSEPIC_W * scale;
   1.119 +    window_h = MOOSEPIC_H * scale;
   1.120 +    window = SDL_CreateWindow("Happy Moose",
   1.121 +                              SDL_WINDOWPOS_UNDEFINED,
   1.122 +                              SDL_WINDOWPOS_UNDEFINED,
   1.123 +                              window_w, window_h,
   1.124 +                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
   1.125 +    if (!window) {
   1.126 +        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   1.127          free(RawMooseData);
   1.128          quit(4);
   1.129      }
   1.130  
   1.131 -    /* Set the window manager title bar */
   1.132 -    SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
   1.133 +    renderer = SDL_CreateRenderer(window, -1, 0);
   1.134 +    if (!renderer) {
   1.135 +        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   1.136 +        free(RawMooseData);
   1.137 +        quit(4);
   1.138 +    }
   1.139 +
   1.140 +    MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   1.141 +    if (!renderer) {
   1.142 +        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   1.143 +        free(RawMooseData);
   1.144 +        quit(5);
   1.145 +    }
   1.146  
   1.147      for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   1.148 -        MooseFrame[i] =
   1.149 -            SDL_CreateRGBSurfaceFrom(RawMooseData + i * MOOSEFRAME_SIZE,
   1.150 -                                     MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W,
   1.151 -                                     0, 0, 0, 0);
   1.152 -        if (MooseFrame[i] == NULL) {
   1.153 -            fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n",
   1.154 -                    SDL_GetError());
   1.155 -            free(RawMooseData);
   1.156 -            quit(5);
   1.157 +        Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
   1.158 +        Uint8 *rgb;
   1.159 +        Uint8 *frame;
   1.160 +
   1.161 +        rgb = MooseFrameRGB;
   1.162 +        frame = RawMooseData + i * MOOSEFRAME_SIZE;
   1.163 +        for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
   1.164 +            rgb[0] = MooseColors[frame[j]].r;
   1.165 +            rgb[1] = MooseColors[frame[j]].g;
   1.166 +            rgb[2] = MooseColors[frame[j]].b;
   1.167 +            rgb += 3;
   1.168          }
   1.169 -        SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
   1.170 -
   1.171 -        {
   1.172 -            SDL_Surface *newsurf;
   1.173 -            SDL_PixelFormat format;
   1.174 -
   1.175 -            format.palette = NULL;
   1.176 -            format.BitsPerPixel = 32;
   1.177 -            format.BytesPerPixel = 4;
   1.178 -#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   1.179 -            format.Rshift = 0;
   1.180 -            format.Gshift = 8;
   1.181 -            format.Bshift = 16;
   1.182 -#else
   1.183 -            format.Rshift = 24;
   1.184 -            format.Gshift = 16;
   1.185 -            format.Bshift = 8;
   1.186 -#endif
   1.187 -            format.Ashift = 0;
   1.188 -            format.Rmask = 0xff << format.Rshift;
   1.189 -            format.Gmask = 0xff << format.Gshift;
   1.190 -            format.Bmask = 0xff << format.Bshift;
   1.191 -            format.Amask = 0;
   1.192 -            format.Rloss = 0;
   1.193 -            format.Gloss = 0;
   1.194 -            format.Bloss = 0;
   1.195 -            format.Aloss = 8;
   1.196 -
   1.197 -            newsurf =
   1.198 -                SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
   1.199 -            if (!newsurf) {
   1.200 -                fprintf(stderr,
   1.201 -                        "Couldn't convert picture to 32bits RGB: %s\n",
   1.202 -                        SDL_GetError());
   1.203 -                quit(6);
   1.204 -            }
   1.205 -            SDL_FreeSurface(MooseFrame[i]);
   1.206 -            MooseFrame[i] = newsurf;
   1.207 -        }
   1.208 +        ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
   1.209      }
   1.210  
   1.211      free(RawMooseData);
   1.212  
   1.213 -    overlay =
   1.214 -        SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
   1.215 -    if (!overlay) {
   1.216 -        fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
   1.217 -        quit(7);
   1.218 +    /* set the start frame */
   1.219 +    i = 0;
   1.220 +    if (nodelay) {
   1.221 +        fpsdelay = 0;
   1.222 +    } else {
   1.223 +        fpsdelay = 1000 / fps;
   1.224      }
   1.225  
   1.226 -    printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
   1.227 -           overlay->planes, overlay->hw_overlay ? "hardware" : "software",
   1.228 -           overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
   1.229 -           SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
   1.230 -           SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
   1.231 -           SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
   1.232 -           SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
   1.233 -
   1.234 -    for (i = 0; i < overlay->planes; i++) {
   1.235 -        printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
   1.236 -    }
   1.237 -
   1.238 -    overlayrect.x = 0;
   1.239 -    overlayrect.y = 0;
   1.240 -    overlayrect.w = MOOSEPIC_W * scale;
   1.241 -    overlayrect.h = MOOSEPIC_H * scale;
   1.242 -
   1.243 -    /* set the start frame */
   1.244 -    i = 0;
   1.245 -    fpsdelay = 1000 / fps;
   1.246 +    displayrect.x = 0;
   1.247 +    displayrect.y = 0;
   1.248 +    displayrect.w = window_w;
   1.249 +    displayrect.h = window_h;
   1.250  
   1.251      /* Ignore key up events, they don't even get filtered */
   1.252      SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   1.253  
   1.254 -    lastftick = SDL_GetTicks();
   1.255 -
   1.256      /* Loop, waiting for QUIT or RESIZE */
   1.257 -    while (1) {
   1.258 -        if (SDL_PollEvent(&event)) {
   1.259 +    while (!done) {
   1.260 +        while (SDL_PollEvent(&event)) {
   1.261              switch (event.type) {
   1.262 -            case SDL_VIDEORESIZE:
   1.263 -                screen =
   1.264 -                    SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
   1.265 -                                     SDL_RESIZABLE | SDL_SWSURFACE);
   1.266 -                overlayrect.w = event.resize.w;
   1.267 -                overlayrect.h = event.resize.h;
   1.268 -                if (paused) {
   1.269 -                    resized = 1;
   1.270 +            case SDL_WINDOWEVENT:
   1.271 +                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   1.272 +                    displayrect.w = window_w = event.window.data1;
   1.273 +                    displayrect.w = window_h = event.window.data2;
   1.274                  }
   1.275                  break;
   1.276              case SDL_MOUSEBUTTONDOWN:
   1.277 -                overlayrect.x = event.button.x - overlayrect.w / 2;
   1.278 -                overlayrect.y = event.button.y - overlayrect.h / 2;
   1.279 +                displayrect.x = event.button.x - window_w / 2;
   1.280 +                displayrect.y = event.button.y - window_h / 2;
   1.281 +                break;
   1.282 +            case SDL_MOUSEMOTION:
   1.283 +                if (event.motion.state) {
   1.284 +                    displayrect.x = event.motion.x - window_w / 2;
   1.285 +                    displayrect.y = event.motion.y - window_h / 2;
   1.286 +                }
   1.287                  break;
   1.288              case SDL_KEYDOWN:
   1.289                  if (event.key.keysym.sym == SDLK_SPACE) {
   1.290 @@ -597,51 +560,24 @@
   1.291                      break;
   1.292                  }
   1.293              case SDL_QUIT:
   1.294 -                SDL_FreeYUVOverlay(overlay);
   1.295 -                for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   1.296 -                    SDL_FreeSurface(MooseFrame[i]);
   1.297 -                }
   1.298 -                quit(0);
   1.299 +                done = SDL_TRUE;
   1.300 +                break;
   1.301              }
   1.302          }
   1.303 +        SDL_Delay(fpsdelay);
   1.304  
   1.305 -        if ((!paused) || (resized)) {
   1.306 -            if (((SDL_GetTicks() - lastftick) > (Uint32)fpsdelay) || (resized)) {
   1.307 -                lastftick = SDL_GetTicks();
   1.308 +        if (!paused) {
   1.309 +            i = (i + 1) % MOOSEFRAMES_COUNT;
   1.310  
   1.311 -                switch (overlay_format) {
   1.312 -                case SDL_YUY2_OVERLAY:
   1.313 -                    ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
   1.314 -                    break;
   1.315 -                case SDL_YV12_OVERLAY:
   1.316 -                    ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
   1.317 -                    break;
   1.318 -                case SDL_UYVY_OVERLAY:
   1.319 -                    ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
   1.320 -                    break;
   1.321 -                case SDL_YVYU_OVERLAY:
   1.322 -                    ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
   1.323 -                    break;
   1.324 -                case SDL_IYUV_OVERLAY:
   1.325 -                    ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
   1.326 -                    break;
   1.327 -                }
   1.328 -
   1.329 -                SDL_DisplayYUVOverlay(overlay, &overlayrect);
   1.330 -                if (!resized) {
   1.331 -                    i++;
   1.332 -                    if (i == 10) {
   1.333 -                        i = 0;
   1.334 -                    }
   1.335 -                } else {
   1.336 -                    resized = 0;
   1.337 -                }
   1.338 -            }
   1.339 +            SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*2);
   1.340          }
   1.341 -        /* kind of timeslice to OS */
   1.342 -        SDL_Delay(1);
   1.343 +        SDL_RenderClear(renderer);
   1.344 +        SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
   1.345 +        SDL_RenderPresent(renderer);
   1.346      }
   1.347 -
   1.348 -    SDL_Quit();
   1.349 +    SDL_DestroyRenderer(renderer);
   1.350 +    quit(0);
   1.351      return 0;
   1.352  }
   1.353 +
   1.354 +/* vi: set ts=4 sw=4 expandtab: */