test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5357 ea8fec220775
child 6256 1d905b13b102
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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@1895
   209
void
slouken@1895
   210
ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1895
   211
                 int luminance)
slouken@672
   212
{
slouken@1895
   213
    int x, y;
slouken@1895
   214
    int yuv[3];
slouken@1895
   215
    Uint8 *p, *op[3];
slouken@672
   216
slouken@1895
   217
    SDL_LockSurface(s);
slouken@1895
   218
    SDL_LockYUVOverlay(o);
slouken@672
   219
slouken@1895
   220
    /* Convert */
slouken@1895
   221
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1895
   222
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1895
   223
        op[0] = o->pixels[0] + o->pitches[0] * y;
slouken@1895
   224
        op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
slouken@1895
   225
        op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
slouken@1895
   226
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1895
   227
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1895
   228
            *(op[0]++) = yuv[0];
slouken@1895
   229
            if (x % 2 == 0 && y % 2 == 0) {
slouken@1895
   230
                *(op[1]++) = yuv[1];
slouken@1895
   231
                *(op[2]++) = yuv[2];
slouken@1895
   232
            }
slouken@1895
   233
            p += s->format->BytesPerPixel;
slouken@1895
   234
        }
slouken@1895
   235
    }
slouken@672
   236
slouken@1895
   237
    SDL_UnlockYUVOverlay(o);
slouken@1895
   238
    SDL_UnlockSurface(s);
slouken@672
   239
}
slouken@672
   240
slouken@1895
   241
void
slouken@1895
   242
ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1895
   243
                 int luminance)
slouken@672
   244
{
slouken@1895
   245
    int x, y;
slouken@1895
   246
    int yuv[3];
slouken@1895
   247
    Uint8 *p, *op;
slouken@672
   248
slouken@1895
   249
    SDL_LockSurface(s);
slouken@1895
   250
    SDL_LockYUVOverlay(o);
slouken@672
   251
slouken@1895
   252
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1895
   253
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1895
   254
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1895
   255
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1895
   256
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1895
   257
            if (x % 2 == 0) {
slouken@1895
   258
                *(op++) = yuv[1];
slouken@1895
   259
                *(op++) = yuv[0];
slouken@1895
   260
                *(op++) = yuv[2];
slouken@1895
   261
            } else
slouken@1895
   262
                *(op++) = yuv[0];
slouken@672
   263
slouken@1895
   264
            p += s->format->BytesPerPixel;
slouken@1895
   265
        }
slouken@1895
   266
    }
slouken@672
   267
slouken@1895
   268
    SDL_UnlockYUVOverlay(o);
slouken@1895
   269
    SDL_UnlockSurface(s);
slouken@672
   270
}
slouken@672
   271
slouken@1895
   272
void
slouken@1895
   273
ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1895
   274
                 int luminance)
slouken@672
   275
{
slouken@1895
   276
    int x, y;
slouken@1895
   277
    int yuv[3];
slouken@1895
   278
    Uint8 *p, *op;
slouken@672
   279
slouken@1895
   280
    SDL_LockSurface(s);
slouken@1895
   281
    SDL_LockYUVOverlay(o);
slouken@672
   282
slouken@1895
   283
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1895
   284
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1895
   285
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1895
   286
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1895
   287
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1895
   288
            if (x % 2 == 0) {
slouken@1895
   289
                *(op++) = yuv[0];
slouken@1895
   290
                *(op++) = yuv[2];
slouken@1895
   291
                op[1] = yuv[1];
slouken@1895
   292
            } else {
slouken@1895
   293
                *op = yuv[0];
slouken@1895
   294
                op += 2;
slouken@1895
   295
            }
slouken@672
   296
slouken@1895
   297
            p += s->format->BytesPerPixel;
slouken@1895
   298
        }
slouken@1895
   299
    }
slouken@672
   300
slouken@1895
   301
    SDL_UnlockYUVOverlay(o);
slouken@1895
   302
    SDL_UnlockSurface(s);
slouken@672
   303
}
slouken@672
   304
slouken@1895
   305
void
slouken@1895
   306
ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1895
   307
                 int luminance)
slouken@672
   308
{
slouken@1895
   309
    int x, y;
slouken@1895
   310
    int yuv[3];
slouken@1895
   311
    Uint8 *p, *op;
slouken@672
   312
slouken@1895
   313
    SDL_LockSurface(s);
slouken@1895
   314
    SDL_LockYUVOverlay(o);
slouken@672
   315
slouken@1895
   316
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1895
   317
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1895
   318
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1895
   319
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1895
   320
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1895
   321
            if (x % 2 == 0) {
slouken@1895
   322
                *(op++) = yuv[0];
slouken@1895
   323
                *(op++) = yuv[1];
slouken@1895
   324
                op[1] = yuv[2];
slouken@1895
   325
            } else {
slouken@1895
   326
                *op = yuv[0];
slouken@1895
   327
                op += 2;
slouken@1895
   328
            }
slouken@672
   329
slouken@1895
   330
            p += s->format->BytesPerPixel;
slouken@1895
   331
        }
slouken@1895
   332
    }
slouken@1895
   333
slouken@1895
   334
    SDL_UnlockYUVOverlay(o);
slouken@1895
   335
    SDL_UnlockSurface(s);
slouken@672
   336
}
slouken@672
   337
slouken@1895
   338
static void
slouken@1895
   339
PrintUsage(char *argv0)
slouken@672
   340
{
slouken@672
   341
    fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
slouken@886
   342
    fprintf(stderr, "\n");
slouken@886
   343
    fprintf(stderr, "Where 'arg' is any of the following options:\n");
slouken@886
   344
    fprintf(stderr, "\n");
slouken@5216
   345
    fprintf(stderr, "    -fps <frames per second>\n");
slouken@5216
   346
    fprintf(stderr, "    -nodelay\n");
slouken@5216
   347
    fprintf(stderr, "    -format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
slouken@5216
   348
    fprintf(stderr, "    -scale <scale factor> (initial scale of the overlay)\n");
slouken@5216
   349
    fprintf(stderr, "    -help (shows this help)\n");
slouken@672
   350
    fprintf(stderr, "\n");
slouken@1895
   351
    fprintf(stderr,
slouken@1895
   352
            "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
slouken@672
   353
    fprintf(stderr, "\n");
slouken@672
   354
}
slouken@672
   355
slouken@1895
   356
int
slouken@1895
   357
main(int argc, char **argv)
slouken@672
   358
{
slouken@1895
   359
    Uint8 *RawMooseData;
slouken@1895
   360
    SDL_RWops *handle;
slouken@5216
   361
    int window_w;
slouken@5216
   362
    int window_h;
slouken@5216
   363
    SDL_Window *window;
slouken@5216
   364
    SDL_Renderer *renderer;
slouken@5216
   365
    Uint8 MooseFrame[MOOSEFRAMES_COUNT][MOOSEFRAME_SIZE*2];
slouken@5216
   366
    SDL_Texture *MooseTexture;
slouken@5216
   367
    SDL_Rect displayrect;
slouken@672
   368
    SDL_Event event;
slouken@1895
   369
    int paused = 0;
slouken@5216
   370
    int i, j;
slouken@1895
   371
    int fps = 12;
slouken@672
   372
    int fpsdelay;
slouken@5216
   373
    int nodelay = 0;
slouken@5264
   374
    Uint32 pixel_format = SDL_PIXELFORMAT_YV12;
slouken@1895
   375
    int scale = 5;
slouken@5216
   376
    SDL_bool done = SDL_FALSE;
slouken@672
   377
slouken@1895
   378
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
icculus@1151
   379
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
   380
        return 3;
icculus@1151
   381
    }
icculus@1151
   382
slouken@1895
   383
    while (argc > 1) {
slouken@1895
   384
        if (strcmp(argv[1], "-fps") == 0) {
slouken@1895
   385
            if (argv[2]) {
slouken@672
   386
                fps = atoi(argv[2]);
slouken@1895
   387
                if (fps == 0) {
slouken@1895
   388
                    fprintf(stderr,
slouken@1895
   389
                            "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
icculus@1151
   390
                    quit(10);
slouken@672
   391
                }
slouken@1895
   392
                if ((fps < 0) || (fps > 1000)) {
slouken@1895
   393
                    fprintf(stderr,
slouken@1895
   394
                            "The -fps option must be in range from 1 to 1000, default is 12.\n");
icculus@1151
   395
                    quit(10);
slouken@672
   396
                }
slouken@672
   397
                argv += 2;
slouken@672
   398
                argc -= 2;
slouken@1895
   399
            } else {
slouken@1895
   400
                fprintf(stderr,
slouken@1895
   401
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
icculus@1151
   402
                quit(10);
slouken@672
   403
            }
slouken@5216
   404
        } else if (strcmp(argv[1], "-nodelay") == 0) {
slouken@5216
   405
            nodelay = 1;
slouken@5216
   406
            argv += 1;
slouken@5216
   407
            argc -= 1;
slouken@1895
   408
        } else if (strcmp(argv[1], "-format") == 0) {
slouken@1895
   409
            if (argv[2]) {
slouken@1895
   410
                if (!strcmp(argv[2], "YV12"))
slouken@5264
   411
                    pixel_format = SDL_PIXELFORMAT_YV12;
slouken@1895
   412
                else if (!strcmp(argv[2], "IYUV"))
slouken@5264
   413
                    pixel_format = SDL_PIXELFORMAT_IYUV;
slouken@1895
   414
                else if (!strcmp(argv[2], "YUY2"))
slouken@5264
   415
                    pixel_format = SDL_PIXELFORMAT_YUY2;
slouken@1895
   416
                else if (!strcmp(argv[2], "UYVY"))
slouken@5264
   417
                    pixel_format = SDL_PIXELFORMAT_UYVY;
slouken@1895
   418
                else if (!strcmp(argv[2], "YVYU"))
slouken@5264
   419
                    pixel_format = SDL_PIXELFORMAT_YVYU;
slouken@1895
   420
                else {
slouken@1895
   421
                    fprintf(stderr,
slouken@1895
   422
                            "The -format option %s is not recognized, see help for info.\n",
slouken@1895
   423
                            argv[2]);
icculus@1151
   424
                    quit(10);
slouken@672
   425
                }
slouken@672
   426
                argv += 2;
slouken@672
   427
                argc -= 2;
slouken@1895
   428
            } else {
slouken@1895
   429
                fprintf(stderr,
slouken@1895
   430
                        "The -format option requires an argument, default is YUY2.\n");
icculus@1151
   431
                quit(10);
slouken@672
   432
            }
slouken@1895
   433
        } else if (strcmp(argv[1], "-scale") == 0) {
slouken@1895
   434
            if (argv[2]) {
slouken@672
   435
                scale = atoi(argv[2]);
slouken@1895
   436
                if (scale == 0) {
slouken@1895
   437
                    fprintf(stderr,
slouken@1895
   438
                            "The -scale option requires an argument [from 1 to 50], default is 5.\n");
icculus@1151
   439
                    quit(10);
slouken@672
   440
                }
slouken@1895
   441
                if ((scale < 0) || (scale > 50)) {
slouken@1895
   442
                    fprintf(stderr,
slouken@1895
   443
                            "The -scale option must be in range from 1 to 50, default is 5.\n");
icculus@1151
   444
                    quit(10);
slouken@672
   445
                }
slouken@672
   446
                argv += 2;
slouken@672
   447
                argc -= 2;
slouken@1895
   448
            } else {
slouken@1895
   449
                fprintf(stderr,
slouken@1895
   450
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
icculus@1151
   451
                quit(10);
slouken@672
   452
            }
slouken@1895
   453
        } else if ((strcmp(argv[1], "-help") == 0)
slouken@1895
   454
                   || (strcmp(argv[1], "-h") == 0)) {
slouken@672
   455
            PrintUsage(argv[0]);
icculus@1151
   456
            quit(0);
slouken@1895
   457
        } else {
slouken@672
   458
            fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
icculus@1151
   459
            quit(10);
slouken@672
   460
        }
slouken@672
   461
        break;
slouken@672
   462
    }
slouken@1895
   463
slouken@1895
   464
    RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
slouken@1895
   465
    if (RawMooseData == NULL) {
slouken@672
   466
        fprintf(stderr, "Can't allocate memory for movie !\n");
slouken@672
   467
        free(RawMooseData);
icculus@1151
   468
        quit(1);
slouken@672
   469
    }
slouken@672
   470
slouken@672
   471
    /* load the trojan moose images */
slouken@1895
   472
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@1895
   473
    if (handle == NULL) {
slouken@672
   474
        fprintf(stderr, "Can't find the file moose.dat !\n");
slouken@672
   475
        free(RawMooseData);
icculus@1151
   476
        quit(2);
slouken@672
   477
    }
slouken@1895
   478
slouken@672
   479
    SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@672
   480
slouken@672
   481
    SDL_RWclose(handle);
slouken@672
   482
slouken@5216
   483
    /* Create the window and renderer */
slouken@5216
   484
    window_w = MOOSEPIC_W * scale;
slouken@5216
   485
    window_h = MOOSEPIC_H * scale;
slouken@5216
   486
    window = SDL_CreateWindow("Happy Moose",
slouken@5216
   487
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5216
   488
                              SDL_WINDOWPOS_UNDEFINED,
slouken@5216
   489
                              window_w, window_h,
slouken@5216
   490
                              SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
slouken@5216
   491
    if (!window) {
slouken@5216
   492
        fprintf(stderr, "Couldn't set create window: %s\n", SDL_GetError());
slouken@672
   493
        free(RawMooseData);
icculus@1151
   494
        quit(4);
slouken@672
   495
    }
slouken@672
   496
slouken@5216
   497
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@5216
   498
    if (!renderer) {
slouken@5216
   499
        fprintf(stderr, "Couldn't set create renderer: %s\n", SDL_GetError());
slouken@5216
   500
        free(RawMooseData);
slouken@5216
   501
        quit(4);
slouken@5216
   502
    }
slouken@5216
   503
slouken@5264
   504
    MooseTexture = SDL_CreateTexture(renderer, pixel_format, SDL_TEXTUREACCESS_STREAMING, MOOSEPIC_W, MOOSEPIC_H);
slouken@5252
   505
    if (!MooseTexture) {
slouken@5216
   506
        fprintf(stderr, "Couldn't set create texture: %s\n", SDL_GetError());
slouken@5216
   507
        free(RawMooseData);
slouken@5216
   508
        quit(5);
slouken@5216
   509
    }
slouken@672
   510
slouken@1895
   511
    for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
slouken@5216
   512
        Uint8 MooseFrameRGB[MOOSEFRAME_SIZE*3];
slouken@5216
   513
        Uint8 *rgb;
slouken@5216
   514
        Uint8 *frame;
slouken@5216
   515
slouken@5216
   516
        rgb = MooseFrameRGB;
slouken@5216
   517
        frame = RawMooseData + i * MOOSEFRAME_SIZE;
slouken@5216
   518
        for (j = 0; j < MOOSEFRAME_SIZE; ++j) {
slouken@5216
   519
            rgb[0] = MooseColors[frame[j]].r;
slouken@5216
   520
            rgb[1] = MooseColors[frame[j]].g;
slouken@5216
   521
            rgb[2] = MooseColors[frame[j]].b;
slouken@5216
   522
            rgb += 3;
slouken@672
   523
        }
slouken@5216
   524
        ConvertRGBtoYV12(MooseFrameRGB, MooseFrame[i], MOOSEPIC_W, MOOSEPIC_H, 0, 100);
slouken@672
   525
    }
slouken@672
   526
slouken@672
   527
    free(RawMooseData);
slouken@672
   528
slouken@5216
   529
    /* set the start frame */
slouken@5216
   530
    i = 0;
slouken@5216
   531
    if (nodelay) {
slouken@5216
   532
        fpsdelay = 0;
slouken@5216
   533
    } else {
slouken@5216
   534
        fpsdelay = 1000 / fps;
slouken@886
   535
    }
slouken@672
   536
slouken@5216
   537
    displayrect.x = 0;
slouken@5216
   538
    displayrect.y = 0;
slouken@5216
   539
    displayrect.w = window_w;
slouken@5216
   540
    displayrect.h = window_h;
slouken@672
   541
slouken@672
   542
    /* Ignore key up events, they don't even get filtered */
slouken@672
   543
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);
slouken@672
   544
slouken@672
   545
    /* Loop, waiting for QUIT or RESIZE */
slouken@5216
   546
    while (!done) {
slouken@5216
   547
        while (SDL_PollEvent(&event)) {
slouken@1895
   548
            switch (event.type) {
slouken@5216
   549
            case SDL_WINDOWEVENT:
slouken@5216
   550
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@5357
   551
                    SDL_RenderSetViewport(renderer, NULL);
slouken@5216
   552
                    displayrect.w = window_w = event.window.data1;
slouken@5274
   553
                    displayrect.h = window_h = event.window.data2;
slouken@1895
   554
                }
slouken@1895
   555
                break;
slouken@1895
   556
            case SDL_MOUSEBUTTONDOWN:
slouken@5216
   557
                displayrect.x = event.button.x - window_w / 2;
slouken@5216
   558
                displayrect.y = event.button.y - window_h / 2;
slouken@5216
   559
                break;
slouken@5216
   560
            case SDL_MOUSEMOTION:
slouken@5216
   561
                if (event.motion.state) {
slouken@5216
   562
                    displayrect.x = event.motion.x - window_w / 2;
slouken@5216
   563
                    displayrect.y = event.motion.y - window_h / 2;
slouken@5216
   564
                }
slouken@1895
   565
                break;
slouken@1895
   566
            case SDL_KEYDOWN:
slouken@1895
   567
                if (event.key.keysym.sym == SDLK_SPACE) {
slouken@1895
   568
                    paused = !paused;
slouken@1895
   569
                    break;
slouken@1895
   570
                }
slouken@1895
   571
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@1895
   572
                    break;
slouken@1895
   573
                }
slouken@1895
   574
            case SDL_QUIT:
slouken@5216
   575
                done = SDL_TRUE;
slouken@5216
   576
                break;
slouken@672
   577
            }
slouken@672
   578
        }
slouken@5216
   579
        SDL_Delay(fpsdelay);
slouken@672
   580
slouken@5216
   581
        if (!paused) {
slouken@5216
   582
            i = (i + 1) % MOOSEFRAMES_COUNT;
slouken@672
   583
slouken@5264
   584
            SDL_UpdateTexture(MooseTexture, NULL, MooseFrame[i], MOOSEPIC_W*SDL_BYTESPERPIXEL(pixel_format));
slouken@672
   585
        }
slouken@5216
   586
        SDL_RenderClear(renderer);
slouken@5216
   587
        SDL_RenderCopy(renderer, MooseTexture, NULL, &displayrect);
slouken@5216
   588
        SDL_RenderPresent(renderer);
slouken@672
   589
    }
slouken@5216
   590
    SDL_DestroyRenderer(renderer);
slouken@5216
   591
    quit(0);
slouken@672
   592
    return 0;
slouken@672
   593
}
slouken@5216
   594
slouken@5216
   595
/* vi: set ts=4 sw=4 expandtab: */