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