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