test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 27 Jan 2018 12:05:26 -0800
changeset 11836 a42b155124a3
parent 11834 b359333bdc52
child 12503 806492103856
permissions -rw-r--r--
Fixed bug 4065 - SDL_wave.c fail to compile in armeabi with ndk r16b

Sylvain

armeabi is almost deprecated for android-ndk higher that r13b.
either it doesn't compile (ICE), or it executes wrongly (using long long for instance).

android people advices to use armeabi-v7a (or use r13b).
     1 /*
     2   Copyright (C) 1997-2018 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 #ifdef __EMSCRIPTEN__
    20 #include <emscripten/emscripten.h>
    21 #endif
    22 
    23 #include "SDL.h"
    24 
    25 #include "testyuv_cvt.h"
    26 
    27 #define MOOSEPIC_W 64
    28 #define MOOSEPIC_H 88
    29 
    30 #define MOOSEFRAME_SIZE (MOOSEPIC_W * MOOSEPIC_H)
    31 #define MOOSEFRAMES_COUNT 10
    32 
    33 SDL_Color MooseColors[84] = {
    34     {49, 49, 49, SDL_ALPHA_OPAQUE}
    35     , {66, 24, 0, SDL_ALPHA_OPAQUE}
    36     , {66, 33, 0, SDL_ALPHA_OPAQUE}
    37     , {66, 66, 66, SDL_ALPHA_OPAQUE}
    38     ,
    39     {66, 115, 49, SDL_ALPHA_OPAQUE}
    40     , {74, 33, 0, SDL_ALPHA_OPAQUE}
    41     , {74, 41, 16, SDL_ALPHA_OPAQUE}
    42     , {82, 33, 8, SDL_ALPHA_OPAQUE}
    43     ,
    44     {82, 41, 8, SDL_ALPHA_OPAQUE}
    45     , {82, 49, 16, SDL_ALPHA_OPAQUE}
    46     , {82, 82, 82, SDL_ALPHA_OPAQUE}
    47     , {90, 41, 8, SDL_ALPHA_OPAQUE}
    48     ,
    49     {90, 41, 16, SDL_ALPHA_OPAQUE}
    50     , {90, 57, 24, SDL_ALPHA_OPAQUE}
    51     , {99, 49, 16, SDL_ALPHA_OPAQUE}
    52     , {99, 66, 24, SDL_ALPHA_OPAQUE}
    53     ,
    54     {99, 66, 33, SDL_ALPHA_OPAQUE}
    55     , {99, 74, 33, SDL_ALPHA_OPAQUE}
    56     , {107, 57, 24, SDL_ALPHA_OPAQUE}
    57     , {107, 82, 41, SDL_ALPHA_OPAQUE}
    58     ,
    59     {115, 57, 33, SDL_ALPHA_OPAQUE}
    60     , {115, 66, 33, SDL_ALPHA_OPAQUE}
    61     , {115, 66, 41, SDL_ALPHA_OPAQUE}
    62     , {115, 74, 0, SDL_ALPHA_OPAQUE}
    63     ,
    64     {115, 90, 49, SDL_ALPHA_OPAQUE}
    65     , {115, 115, 115, SDL_ALPHA_OPAQUE}
    66     , {123, 82, 0, SDL_ALPHA_OPAQUE}
    67     , {123, 99, 57, SDL_ALPHA_OPAQUE}
    68     ,
    69     {132, 66, 41, SDL_ALPHA_OPAQUE}
    70     , {132, 74, 41, SDL_ALPHA_OPAQUE}
    71     , {132, 90, 8, SDL_ALPHA_OPAQUE}
    72     , {132, 99, 33, SDL_ALPHA_OPAQUE}
    73     ,
    74     {132, 99, 66, SDL_ALPHA_OPAQUE}
    75     , {132, 107, 66, SDL_ALPHA_OPAQUE}
    76     , {140, 74, 49, SDL_ALPHA_OPAQUE}
    77     , {140, 99, 16, SDL_ALPHA_OPAQUE}
    78     ,
    79     {140, 107, 74, SDL_ALPHA_OPAQUE}
    80     , {140, 115, 74, SDL_ALPHA_OPAQUE}
    81     , {148, 107, 24, SDL_ALPHA_OPAQUE}
    82     , {148, 115, 82, SDL_ALPHA_OPAQUE}
    83     ,
    84     {148, 123, 74, SDL_ALPHA_OPAQUE}
    85     , {148, 123, 90, SDL_ALPHA_OPAQUE}
    86     , {156, 115, 33, SDL_ALPHA_OPAQUE}
    87     , {156, 115, 90, SDL_ALPHA_OPAQUE}
    88     ,
    89     {156, 123, 82, SDL_ALPHA_OPAQUE}
    90     , {156, 132, 82, SDL_ALPHA_OPAQUE}
    91     , {156, 132, 99, SDL_ALPHA_OPAQUE}
    92     , {156, 156, 156, SDL_ALPHA_OPAQUE}
    93     ,
    94     {165, 123, 49, SDL_ALPHA_OPAQUE}
    95     , {165, 123, 90, SDL_ALPHA_OPAQUE}
    96     , {165, 132, 82, SDL_ALPHA_OPAQUE}
    97     , {165, 132, 90, SDL_ALPHA_OPAQUE}
    98     ,
    99     {165, 132, 99, SDL_ALPHA_OPAQUE}
   100     , {165, 140, 90, SDL_ALPHA_OPAQUE}
   101     , {173, 132, 57, SDL_ALPHA_OPAQUE}
   102     , {173, 132, 99, SDL_ALPHA_OPAQUE}
   103     ,
   104     {173, 140, 107, SDL_ALPHA_OPAQUE}
   105     , {173, 140, 115, SDL_ALPHA_OPAQUE}
   106     , {173, 148, 99, SDL_ALPHA_OPAQUE}
   107     , {173, 173, 173, SDL_ALPHA_OPAQUE}
   108     ,
   109     {181, 140, 74, SDL_ALPHA_OPAQUE}
   110     , {181, 148, 115, SDL_ALPHA_OPAQUE}
   111     , {181, 148, 123, SDL_ALPHA_OPAQUE}
   112     , {181, 156, 107, SDL_ALPHA_OPAQUE}
   113     ,
   114     {189, 148, 123, SDL_ALPHA_OPAQUE}
   115     , {189, 156, 82, SDL_ALPHA_OPAQUE}
   116     , {189, 156, 123, SDL_ALPHA_OPAQUE}
   117     , {189, 156, 132, SDL_ALPHA_OPAQUE}
   118     ,
   119     {189, 189, 189, SDL_ALPHA_OPAQUE}
   120     , {198, 156, 123, SDL_ALPHA_OPAQUE}
   121     , {198, 165, 132, SDL_ALPHA_OPAQUE}
   122     , {206, 165, 99, SDL_ALPHA_OPAQUE}
   123     ,
   124     {206, 165, 132, SDL_ALPHA_OPAQUE}
   125     , {206, 173, 140, SDL_ALPHA_OPAQUE}
   126     , {206, 206, 206, SDL_ALPHA_OPAQUE}
   127     , {214, 173, 115, SDL_ALPHA_OPAQUE}
   128     ,
   129     {214, 173, 140, SDL_ALPHA_OPAQUE}
   130     , {222, 181, 148, SDL_ALPHA_OPAQUE}
   131     , {222, 189, 132, SDL_ALPHA_OPAQUE}
   132     , {222, 189, 156, SDL_ALPHA_OPAQUE}
   133     ,
   134     {222, 222, 222, SDL_ALPHA_OPAQUE}
   135     , {231, 198, 165, SDL_ALPHA_OPAQUE}
   136     , {231, 231, 231, SDL_ALPHA_OPAQUE}
   137     , {239, 206, 173, SDL_ALPHA_OPAQUE}
   138 };
   139 
   140 Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
   141 SDL_Texture *MooseTexture;
   142 SDL_Rect displayrect;
   143 int window_w;
   144 int window_h;
   145 SDL_Window *window;
   146 SDL_Renderer *renderer;
   147 int paused = 0;
   148 int i;
   149 SDL_bool done = SDL_FALSE;
   150 static int fpsdelay;
   151 
   152 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
   153 static void
   154 quit(int rc)
   155 {
   156     SDL_Quit();
   157     exit(rc);
   158 }
   159 
   160 static void
   161 PrintUsage(char *argv0)
   162 {
   163     SDL_Log("Usage: %s [arg] [arg] [arg] ...\n", argv0);
   164     SDL_Log("\n");
   165     SDL_Log("Where 'arg' is any of the following options:\n");
   166     SDL_Log("\n");
   167     SDL_Log("    -fps <frames per second>\n");
   168     SDL_Log("    -nodelay\n");
   169     SDL_Log("    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
   170     SDL_Log("    -scale <scale factor> (initial scale of the overlay)\n");
   171     SDL_Log("    -help (shows this help)\n");
   172     SDL_Log("\n");
   173     SDL_Log("Press ESC to exit, or SPACE to freeze the movie while application running.\n");
   174     SDL_Log("\n");
   175 }
   176 
   177 void
   178 loop()
   179 {
   180     SDL_Event event;
   181 
   182     while (SDL_PollEvent(&event)) {
   183         switch (event.type) {
   184         case SDL_WINDOWEVENT:
   185             if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
   186                 SDL_RenderSetViewport(renderer, NULL);
   187                 displayrect.w = window_w = event.window.data1;
   188                 displayrect.h = window_h = event.window.data2;
   189             }
   190             break;
   191         case SDL_MOUSEBUTTONDOWN:
   192             displayrect.x = event.button.x - window_w / 2;
   193             displayrect.y = event.button.y - window_h / 2;
   194             break;
   195         case SDL_MOUSEMOTION:
   196             if (event.motion.state) {
   197                 displayrect.x = event.motion.x - window_w / 2;
   198                 displayrect.y = event.motion.y - window_h / 2;
   199             }
   200             break;
   201         case SDL_KEYDOWN:
   202             if (event.key.keysym.sym == SDLK_SPACE) {
   203                 paused = !paused;
   204                 break;
   205             }
   206             if (event.key.keysym.sym != SDLK_ESCAPE) {
   207                 break;
   208             }
   209         case SDL_QUIT:
   210             done = SDL_TRUE;
   211             break;
   212         }
   213     }
   214 
   215 #ifndef __EMSCRIPTEN__
   216     SDL_Delay(fpsdelay);
   217 #endif
   218 
   219     if (!paused) {
   220         i = (i + 1) % MOOSEFRAMES_COUNT;
   221 
   222         SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W);
   223     }
   224     SDL_RenderClear(renderer);
   225     SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
   226     SDL_RenderPresent(renderer);
   227 
   228 #ifdef __EMSCRIPTEN__
   229     if (done) {
   230         emscripten_cancel_main_loop();
   231     }
   232 #endif
   233 }
   234 
   235 int
   236 main(int argc, char **argv)
   237 {
   238     Uint8 *RawMooseData;
   239     SDL_RWops *handle;
   240     SDL_Window *window;
   241     int j;
   242     int fps = 12;
   243     int nodelay = 0;
   244     int scale = 5;
   245 
   246     /* Enable standard application logging */
   247     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   248 
   249     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   250         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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 = SDL_atoi(argv[2]);
   258                 if (fps == 0) {
   259                     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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                     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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 = SDL_atoi(argv[2]);
   282                 if (scale == 0) {
   283                     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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                     SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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                 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
   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             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't allocate memory for movie !\n");
   313         quit(1);
   314     }
   315 
   316     /* load the trojan moose images */
   317     handle = SDL_RWFromFile("moose.dat", "rb");
   318     if (handle == NULL) {
   319         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Can't find the file moose.dat !\n");
   320         free(RawMooseData);
   321         quit(2);
   322     }
   323 
   324     SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
   325 
   326     SDL_RWclose(handle);
   327 
   328     /* Create the window and renderer */
   329     window_w = MOOSEPIC_W * scale;
   330     window_h = MOOSEPIC_H * scale;
   331     window = SDL_CreateWindow("Happy Moose",
   332                               SDL_WINDOWPOS_UNDEFINED,
   333                               SDL_WINDOWPOS_UNDEFINED,
   334                               window_w, window_h,
   335                               SDL_WINDOW_RESIZABLE);
   336     if (!window) {
   337         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create window: %s\n", SDL_GetError());
   338         free(RawMooseData);
   339         quit(4);
   340     }
   341 
   342     renderer = SDL_CreateRenderer(window, -1, 0);
   343     if (!renderer) {
   344         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create renderer: %s\n", SDL_GetError());
   345         free(RawMooseData);
   346         quit(4);
   347     }
   348 
   349     MooseTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_YV12, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
   350     if (!MooseTexture) {
   351         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't set create texture: %s\n", SDL_GetError());
   352         free(RawMooseData);
   353         quit(5);
   354     }
   355     /* Uncomment this to check vertex color with a YUV texture */
   356     /* SDL_SetTextureColorMod(MooseTexture, 0xff, 0x80, 0x80); */
   357 
   358     for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
   359         Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
   360         Uint8 *rgb;
   361         Uint8 *frame;
   362 
   363         rgb = MooseFrameRGB;
   364         frame = RawMooseData + i * MOOSEFRAME_SIZE;
   365         for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
   366             rgb[0] = MooseColors[frame[j]].r;
   367             rgb[1] = MooseColors[frame[j]].g;
   368             rgb[2] = MooseColors[frame[j]].b;
   369             rgb += 3;
   370         }
   371         ConvertRGBtoYUV(SDL_PIXELFORMAT_YV12, MooseFrameRGB, MOOSEPIC_W*3, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H,
   372             SDL_GetYUVConversionModeForResolution(MOOSEPIC_W, MOOSEPIC_H),
   373             0, 100);
   374     }
   375 
   376     free(RawMooseData);
   377 
   378     /* set the start frame */
   379     i = 0;
   380     if (nodelay) {
   381         fpsdelay = 0;
   382     } else {
   383         fpsdelay = 1000 / fps;
   384     }
   385 
   386     displayrect.x = 0;
   387     displayrect.y = 0;
   388     displayrect.w = window_w;
   389     displayrect.h = window_h;
   390 
   391     /* Ignore key up events, they don't even get filtered */
   392     SDL_EventState(SDL_KEYUP, SDL_IGNORE);
   393 
   394     /* Loop, waiting for QUIT or RESIZE */
   395 #ifdef __EMSCRIPTEN__
   396     emscripten_set_main_loop(loop, nodelay ? 0 : fps, 1);
   397 #else
   398     while (!done) {
   399         loop();
   400             }
   401 #endif
   402 
   403     SDL_DestroyRenderer(renderer);
   404     quit(0);
   405     return 0;
   406 }
   407 
   408 /* vi: set ts=4 sw=4 expandtab: */