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