test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 23 Jul 2013 12:46:22 -0700
changeset 7504 5a45a9536a65
parent 7478 0d02f6a323f5
child 7517 965d57022c01
permissions -rw-r--r--
Finished updating testoverlay2 for the new API and added it to the Visual Studio 2010 projects
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /********************************************************************************
    13  *                                                                              *
    14  * Test of the overlay used for moved pictures, test more closed to real life.  *
    15  * Running trojan moose :) Coded by Mike Gorchak.                               *
    16  *                                                                              *
    17  ********************************************************************************/
    18 
    19 #include <stdlib.h>
    20 #include <stdio.h>
    21 #include <string.h>
    22 
    23 #include "SDL.h"
    24 
    25 #define MOOSEPIC_W 64
    26 #define MOOSEPIC_H 88
    27 
    28 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    29 #define MOOSEFRAMES_COUNT 10
    30 
    31 SDL_Color MooseColors[84] = {
    32     {49, 49, 49}
    33     , {66, 24, 0}
    34     , {66, 33, 0}
    35     , {66, 66, 66}
    36     ,
    37     {66, 115, 49}
    38     , {74, 33, 0}
    39     , {74, 41, 16}
    40     , {82, 33, 8}
    41     ,
    42     {82, 41, 8}
    43     , {82, 49, 16}
    44     , {82, 82, 82}
    45     , {90, 41, 8}
    46     ,
    47     {90, 41, 16}
    48     , {90, 57, 24}
    49     , {99, 49, 16}
    50     , {99, 66, 24}
    51     ,
    52     {99, 66, 33}
    53     , {99, 74, 33}
    54     , {107, 57, 24}
    55     , {107, 82, 41}
    56     ,
    57     {115, 57, 33}
    58     , {115, 66, 33}
    59     , {115, 66, 41}
    60     , {115, 74, 0}
    61     ,
    62     {115, 90, 49}
    63     , {115, 115, 115}
    64     , {123, 82, 0}
    65     , {123, 99, 57}
    66     ,
    67     {132, 66, 41}
    68     , {132, 74, 41}
    69     , {132, 90, 8}
    70     , {132, 99, 33}
    71     ,
    72     {132, 99, 66}
    73     , {132, 107, 66}
    74     , {140, 74, 49}
    75     , {140, 99, 16}
    76     ,
    77     {140, 107, 74}
    78     , {140, 115, 74}
    79     , {148, 107, 24}
    80     , {148, 115, 82}
    81     ,
    82     {148, 123, 74}
    83     , {148, 123, 90}
    84     , {156, 115, 33}
    85     , {156, 115, 90}
    86     ,
    87     {156, 123, 82}
    88     , {156, 132, 82}
    89     , {156, 132, 99}
    90     , {156, 156, 156}
    91     ,
    92     {165, 123, 49}
    93     , {165, 123, 90}
    94     , {165, 132, 82}
    95     , {165, 132, 90}
    96     ,
    97     {165, 132, 99}
    98     , {165, 140, 90}
    99     , {173, 132, 57}
   100     , {173, 132, 99}
   101     ,
   102     {173, 140, 107}
   103     , {173, 140, 115}
   104     , {173, 148, 99}
   105     , {173, 173, 173}
   106     ,
   107     {181, 140, 74}
   108     , {181, 148, 115}
   109     , {181, 148, 123}
   110     , {181, 156, 107}
   111     ,
   112     {189, 148, 123}
   113     , {189, 156, 82}
   114     , {189, 156, 123}
   115     , {189, 156, 132}
   116     ,
   117     {189, 189, 189}
   118     , {198, 156, 123}
   119     , {198, 165, 132}
   120     , {206, 165, 99}
   121     ,
   122     {206, 165, 132}
   123     , {206, 173, 140}
   124     , {206, 206, 206}
   125     , {214, 173, 115}
   126     ,
   127     {214, 173, 140}
   128     , {222, 181, 148}
   129     , {222, 189, 132}
   130     , {222, 189, 156}
   131     ,
   132     {222, 222, 222}
   133     , {231, 198, 165}
   134     , {231, 231, 231}
   135     , {239, 206, 173}
   136 };
   137 
   138 
   139 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   140 static void
   141 quit(int rc)
   142 {
   143     SDL_Quit();
   144     exit(rc);
   145 }
   146 
   147 /* All RGB2YUV conversion code and some other parts of code has been taken from testoverlay.c */
   148 
   149 /* NOTE: These RGB conversion functions are not intended for speed,
   150          only as examples.
   151 */
   152 
   153 void
   154 RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
   155 {
   156     if (monochrome) {
   157 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   158         yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
   159         yuv[1] = 128;
   160         yuv[2] = 128;
   161 #else
   162         yuv[0] = (int)(0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
   163         yuv[1] = 128;
   164         yuv[2] = 128;
   165 #endif
   166     } else {
   167 #if 1                           /* these are the two formulas that I found on the FourCC site... */
   168         yuv[0] = (int)(0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]);
   169         yuv[1] = (int)((rgb[2] - yuv[0]) * 0.565 + 128);
   170         yuv[2] = (int)((rgb[0] - yuv[0]) * 0.713 + 128);
   171 #else
   172         yuv[0] = (0.257 * rgb[0]) + (0.504 * rgb[1]) + (0.098 * rgb[2]) + 16;
   173         yuv[1] = 128 - (0.148 * rgb[0]) - (0.291 * rgb[1]) + (0.439 * rgb[2]);
   174         yuv[2] = 128 + (0.439 * rgb[0]) - (0.368 * rgb[1]) - (0.071 * rgb[2]);
   175 #endif
   176     }
   177 
   178     if (luminance != 100) {
   179         yuv[0] = yuv[0] * luminance / 100;
   180         if (yuv[0] > 255)
   181             yuv[0] = 255;
   182     }
   183 }
   184 
   185 void
   186 ConvertRGBtoYV12(Uint8 *rgb, Uint8 *out, int w, int h,
   187                  int monochrome, int luminance)
   188 {
   189     int x, y;
   190     int yuv[3];
   191     Uint8 *op[3];
   192 
   193     op[0] = out;
   194     op[1] = op[0] + w*h;
   195     op[2] = op[1] + w*h/4;
   196     for (y = 0; y < h; ++y) {
   197         for (x = 0; x < w; ++x) {
   198             RGBtoYUV(rgb, yuv, monochrome, luminance);
   199             *(op[0]++) = yuv[0];
   200             if (x % 2 == 0 && y % 2 == 0) {
   201                 *(op[1]++) = yuv[2];
   202                 *(op[2]++) = yuv[1];
   203             }
   204             rgb += 3;
   205         }
   206     }
   207 }
   208 
   209 static void
   210 PrintUsage(char *argv0)
   211 {
   212     fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
   213     fprintf(stderr, "\n");
   214     fprintf(stderr, "Where 'arg' is any of the following options:\n");
   215     fprintf(stderr, "\n");
   216     fprintf(stderr, "    -fps <frames per second>\n");
   217     fprintf(stderr, "    -nodelay\n");
   218     fprintf(stderr, "    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   219     fprintf(stderr, "    -scale <scale factor> (initial scale of the overlay)\n");
   220     fprintf(stderr, "    -help (shows this help)\n");
   221     fprintf(stderr, "\n");
   222     fprintf(stderr,
   223             "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   224     fprintf(stderr, "\n");
   225 }
   226 
   227 int
   228 main(int argc, char **argv)
   229 {
   230     Uint8 *RawMooseData;
   231     SDL_RWops *handle;
   232     int window_w;
   233     int window_h;
   234     SDL_Window *window;
   235     SDL_Renderer *renderer;
   236     Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
   237     SDL_Texture *MooseTexture;
   238     SDL_Rect displayrect;
   239     SDL_Event event;
   240     int paused = 0;
   241     int i, j;
   242     int fps = 12;
   243     int fpsdelay;
   244     int nodelay = 0;
   245     Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
   246     int scale = 5;
   247     SDL_bool done = SDL_FALSE;
   248 
   249     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   250         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   251         return 3;
   252     }
   253 
   254     while (argc > 1) {
   255         if (strcmp(argv[1], "-fps") == 0) {
   256             if (argv[2]) {
   257                 fps = atoi(argv[2]);
   258                 if (fps == 0) {
   259                     fprintf(stderr,
   260                             "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   261                     quit(10);
   262                 }
   263                 if ((fps < 0) || (fps > 1000)) {
   264                     fprintf(stderr,
   265                             "The -fps option must be in range from 1 to 1000, default is 12.\n");
   266                     quit(10);
   267                 }
   268                 argv += 2;
   269                 argc -= 2;
   270             } else {
   271                 fprintf(stderr,
   272                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   273                 quit(10);
   274             }
   275         } else if (strcmp(argv[1], "-nodelay") == 0) {
   276             nodelay = 1;
   277             argv += 1;
   278             argc -= 1;
   279         } else if (strcmp(argv[1], "-scale") == 0) {
   280             if (argv[2]) {
   281                 scale = atoi(argv[2]);
   282                 if (scale == 0) {
   283                     fprintf(stderr,
   284                             "The -scale option requires an argument [from 1 to 50], default is 5.\n");
   285                     quit(10);
   286                 }
   287                 if ((scale < 0) || (scale > 50)) {
   288                     fprintf(stderr,
   289                             "The -scale option must be in range from 1 to 50, default is 5.\n");
   290                     quit(10);
   291                 }
   292                 argv += 2;
   293                 argc -= 2;
   294             } else {
   295                 fprintf(stderr,
   296                         "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
   297                 quit(10);
   298             }
   299         } else if ((strcmp(argv[1], "-help") == 0)
   300                    || (strcmp(argv[1], "-h") == 0)) {
   301             PrintUsage(argv[0]);
   302             quit(0);
   303         } else {
   304             fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
   305             quit(10);
   306         }
   307         break;
   308     }
   309 
   310     RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
   311     if (RawMooseData == NULL) {
   312         fprintf(stderr, "Can't allocate memory for movie !\n");
   313         free(RawMooseData);
   314         quit(1);
   315     }
   316 
   317     /* load the trojan moose images */
   318     handle = SDL_RWFromFile("moose.dat", "rb");
   319     if (handle == NULL) {
   320         fprintf(stderr, "Can't find the file moose.dat !\n");
   321         free(RawMooseData);
   322         quit(2);
   323     }
   324 
   325     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   326 
   327     SDL_RWclose(handle);
   328 
   329     /* Create the window and renderer */
   330     window_w = MOOSEPIC_W * scale;
   331     window_h = MOOSEPIC_H * scale;
   332     window = SDL_CreateWindow("Happy Moose",
   333                               SDL_WINDOWPOS_UNDEFINED,
   334                               SDL_WINDOWPOS_UNDEFINED,
   335                               window_w, window_h,
   336                               SDL_WINDOW_RESIZABLE);
   337     if (!window) {
   338         fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
   339         free(RawMooseData);
   340         quit(4);
   341     }
   342 
   343     renderer = SDL_CreateRenderer(window, -1, 0);
   344     if (!renderer) {
   345         fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
   346         free(RawMooseData);
   347         quit(4);
   348     }
   349 
   350     MooseTexture = SDL_CreateTexture(renderer, pixel_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   351     if (!MooseTexture) {
   352         fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
   353         free(RawMooseData);
   354         quit(5);
   355     }
   356     /* Uncomment this to check vertex color with a YUV texture */
   357     /*SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80);*/
   358 
   359     for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   360         Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
   361         Uint8 *rgb;
   362         Uint8 *frame;
   363 
   364         rgb = MooseFrameRGB;
   365         frame = RawMooseData + i * MOOSEFRAME_SIZE;
   366         for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
   367             rgb[0] = MooseColors[frame[j]].r;
   368             rgb[1] = MooseColors[frame[j]].g;
   369             rgb[2] = MooseColors[frame[j]].b;
   370             rgb += 3;
   371         }
   372         ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
   373     }
   374 
   375     free(RawMooseData);
   376 
   377     /* set the start frame */
   378     i = 0;
   379     if (nodelay) {
   380         fpsdelay = 0;
   381     } else {
   382         fpsdelay = 1000 / fps;
   383     }
   384 
   385     displayrect.x = 0;
   386     displayrect.y = 0;
   387     displayrect.w = window_w;
   388     displayrect.h = window_h;
   389 
   390     /* Ignore key up events, they don't even get filtered */
   391     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   392 
   393     /* Loop, waiting for QUIT or RESIZE */
   394     while (!done) {
   395         while (SDL_PollEvent(&event)) {
   396             switch (event.type) {
   397             case SDL_WINDOWEVENT:
   398                 if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   399                     SDL_RenderSetViewport(renderer, NULL);
   400                     displayrect.w = window_w = event.window.data1;
   401                     displayrect.h = window_h = event.window.data2;
   402                 }
   403                 break;
   404             case SDL_MOUSEBUTTONDOWN:
   405                 displayrect.x = event.button.x - window_w / 2;
   406                 displayrect.y = event.button.y - window_h / 2;
   407                 break;
   408             case SDL_MOUSEMOTION:
   409                 if (event.motion.state) {
   410                     displayrect.x = event.motion.x - window_w / 2;
   411                     displayrect.y = event.motion.y - window_h / 2;
   412                 }
   413                 break;
   414             case SDL_KEYDOWN:
   415                 if (event.key.keysym.sym == SDLK_SPACE) {
   416                     paused = !paused;
   417                     break;
   418                 }
   419                 if (event.key.keysym.sym != SDLK_ESCAPE) {
   420                     break;
   421                 }
   422             case SDL_QUIT:
   423                 done = SDL_TRUE;
   424                 break;
   425             }
   426         }
   427         SDL_Delay(fpsdelay);
   428 
   429         if (!paused) {
   430             i = (i + 1) % MOOSEFRAMES_COUNT;
   431 
   432             SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
   433         }
   434         SDL_RenderClear(renderer);
   435         SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
   436         SDL_RenderPresent(renderer);
   437     }
   438     SDL_DestroyRenderer(renderer);
   439     quit(0);
   440     return 0;
   441 }
   442 
   443 /* vi: set ts=4 sw=4 expandtab: */