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