test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 04:04:35 +0000
branchSDL-1.3
changeset 1668 4da1ee79c9af
parent 1662 782fd950bd46
permissions -rw-r--r--
more tweaking indent options
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@1662
    20
SDL_Color MooseColors[84] = {
slouken@1662
    21
    {49, 49, 49}
slouken@1662
    22
    , {66, 24, 0}
slouken@1662
    23
    , {66, 33, 0}
slouken@1662
    24
    , {66, 66, 66}
slouken@1662
    25
    ,
slouken@1662
    26
    {66, 115, 49}
slouken@1662
    27
    , {74, 33, 0}
slouken@1662
    28
    , {74, 41, 16}
slouken@1662
    29
    , {82, 33, 8}
slouken@1662
    30
    ,
slouken@1662
    31
    {82, 41, 8}
slouken@1662
    32
    , {82, 49, 16}
slouken@1662
    33
    , {82, 82, 82}
slouken@1662
    34
    , {90, 41, 8}
slouken@1662
    35
    ,
slouken@1662
    36
    {90, 41, 16}
slouken@1662
    37
    , {90, 57, 24}
slouken@1662
    38
    , {99, 49, 16}
slouken@1662
    39
    , {99, 66, 24}
slouken@1662
    40
    ,
slouken@1662
    41
    {99, 66, 33}
slouken@1662
    42
    , {99, 74, 33}
slouken@1662
    43
    , {107, 57, 24}
slouken@1662
    44
    , {107, 82, 41}
slouken@1662
    45
    ,
slouken@1662
    46
    {115, 57, 33}
slouken@1662
    47
    , {115, 66, 33}
slouken@1662
    48
    , {115, 66, 41}
slouken@1662
    49
    , {115, 74, 0}
slouken@1662
    50
    ,
slouken@1662
    51
    {115, 90, 49}
slouken@1662
    52
    , {115, 115, 115}
slouken@1662
    53
    , {123, 82, 0}
slouken@1662
    54
    , {123, 99, 57}
slouken@1662
    55
    ,
slouken@1662
    56
    {132, 66, 41}
slouken@1662
    57
    , {132, 74, 41}
slouken@1662
    58
    , {132, 90, 8}
slouken@1662
    59
    , {132, 99, 33}
slouken@1662
    60
    ,
slouken@1662
    61
    {132, 99, 66}
slouken@1662
    62
    , {132, 107, 66}
slouken@1662
    63
    , {140, 74, 49}
slouken@1662
    64
    , {140, 99, 16}
slouken@1662
    65
    ,
slouken@1662
    66
    {140, 107, 74}
slouken@1662
    67
    , {140, 115, 74}
slouken@1662
    68
    , {148, 107, 24}
slouken@1662
    69
    , {148, 115, 82}
slouken@1662
    70
    ,
slouken@1662
    71
    {148, 123, 74}
slouken@1662
    72
    , {148, 123, 90}
slouken@1662
    73
    , {156, 115, 33}
slouken@1662
    74
    , {156, 115, 90}
slouken@1662
    75
    ,
slouken@1662
    76
    {156, 123, 82}
slouken@1662
    77
    , {156, 132, 82}
slouken@1662
    78
    , {156, 132, 99}
slouken@1662
    79
    , {156, 156, 156}
slouken@1662
    80
    ,
slouken@1662
    81
    {165, 123, 49}
slouken@1662
    82
    , {165, 123, 90}
slouken@1662
    83
    , {165, 132, 82}
slouken@1662
    84
    , {165, 132, 90}
slouken@1662
    85
    ,
slouken@1662
    86
    {165, 132, 99}
slouken@1662
    87
    , {165, 140, 90}
slouken@1662
    88
    , {173, 132, 57}
slouken@1662
    89
    , {173, 132, 99}
slouken@1662
    90
    ,
slouken@1662
    91
    {173, 140, 107}
slouken@1662
    92
    , {173, 140, 115}
slouken@1662
    93
    , {173, 148, 99}
slouken@1662
    94
    , {173, 173, 173}
slouken@1662
    95
    ,
slouken@1662
    96
    {181, 140, 74}
slouken@1662
    97
    , {181, 148, 115}
slouken@1662
    98
    , {181, 148, 123}
slouken@1662
    99
    , {181, 156, 107}
slouken@1662
   100
    ,
slouken@1662
   101
    {189, 148, 123}
slouken@1662
   102
    , {189, 156, 82}
slouken@1662
   103
    , {189, 156, 123}
slouken@1662
   104
    , {189, 156, 132}
slouken@1662
   105
    ,
slouken@1662
   106
    {189, 189, 189}
slouken@1662
   107
    , {198, 156, 123}
slouken@1662
   108
    , {198, 165, 132}
slouken@1662
   109
    , {206, 165, 99}
slouken@1662
   110
    ,
slouken@1662
   111
    {206, 165, 132}
slouken@1662
   112
    , {206, 173, 140}
slouken@1662
   113
    , {206, 206, 206}
slouken@1662
   114
    , {214, 173, 115}
slouken@1662
   115
    ,
slouken@1662
   116
    {214, 173, 140}
slouken@1662
   117
    , {222, 181, 148}
slouken@1662
   118
    , {222, 189, 132}
slouken@1662
   119
    , {222, 189, 156}
slouken@1662
   120
    ,
slouken@1662
   121
    {222, 222, 222}
slouken@1662
   122
    , {231, 198, 165}
slouken@1662
   123
    , {231, 231, 231}
slouken@1662
   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@1662
   129
static void
slouken@1668
   130
quit(int rc)
icculus@1151
   131
{
slouken@1668
   132
    SDL_Quit();
slouken@1668
   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@1662
   142
void
slouken@1668
   143
RGBtoYUV(Uint8 * rgb, int *yuv, int monochrome, int luminance)
slouken@672
   144
{
slouken@1662
   145
    if (monochrome) {
slouken@1662
   146
#if 1                           /* these are the two formulas that I found on the FourCC site... */
slouken@1662
   147
        yuv[0] = 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
slouken@672
   151
        yuv[0] = (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@1662
   155
    } else {
slouken@1662
   156
#if 1                           /* these are the two formulas that I found on the FourCC site... */
slouken@1662
   157
        yuv[0] = 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2];
slouken@1662
   158
        yuv[1] = (rgb[2] - yuv[0]) * 0.565 + 128;
slouken@1662
   159
        yuv[2] = (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@1662
   167
    if (luminance != 100) {
slouken@1662
   168
        yuv[0] = yuv[0] * luminance / 100;
slouken@1662
   169
        if (yuv[0] > 255)
slouken@1662
   170
            yuv[0] = 255;
slouken@672
   171
    }
slouken@672
   172
}
slouken@672
   173
slouken@1662
   174
void
slouken@1668
   175
ConvertRGBtoYV12(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1668
   176
                 int luminance)
slouken@672
   177
{
slouken@1662
   178
    int x, y;
slouken@1662
   179
    int yuv[3];
slouken@1662
   180
    Uint8 *p, *op[3];
slouken@672
   181
slouken@1668
   182
    SDL_LockSurface(s);
slouken@1668
   183
    SDL_LockYUVOverlay(o);
slouken@672
   184
slouken@1662
   185
    /* Convert */
slouken@1662
   186
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1662
   187
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1662
   188
        op[0] = o->pixels[0] + o->pitches[0] * y;
slouken@1662
   189
        op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
slouken@1662
   190
        op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
slouken@1662
   191
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1668
   192
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1662
   193
            *(op[0]++) = yuv[0];
slouken@1662
   194
            if (x % 2 == 0 && y % 2 == 0) {
slouken@1662
   195
                *(op[1]++) = yuv[2];
slouken@1662
   196
                *(op[2]++) = yuv[1];
slouken@1662
   197
            }
slouken@1662
   198
            p += s->format->BytesPerPixel;
slouken@1662
   199
        }
slouken@1662
   200
    }
slouken@672
   201
slouken@1668
   202
    SDL_UnlockYUVOverlay(o);
slouken@1668
   203
    SDL_UnlockSurface(s);
slouken@672
   204
}
slouken@672
   205
slouken@1662
   206
void
slouken@1668
   207
ConvertRGBtoIYUV(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1668
   208
                 int luminance)
slouken@672
   209
{
slouken@1662
   210
    int x, y;
slouken@1662
   211
    int yuv[3];
slouken@1662
   212
    Uint8 *p, *op[3];
slouken@672
   213
slouken@1668
   214
    SDL_LockSurface(s);
slouken@1668
   215
    SDL_LockYUVOverlay(o);
slouken@672
   216
slouken@1662
   217
    /* Convert */
slouken@1662
   218
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1662
   219
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1662
   220
        op[0] = o->pixels[0] + o->pitches[0] * y;
slouken@1662
   221
        op[1] = o->pixels[1] + o->pitches[1] * (y / 2);
slouken@1662
   222
        op[2] = o->pixels[2] + o->pitches[2] * (y / 2);
slouken@1662
   223
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1668
   224
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1662
   225
            *(op[0]++) = yuv[0];
slouken@1662
   226
            if (x % 2 == 0 && y % 2 == 0) {
slouken@1662
   227
                *(op[1]++) = yuv[1];
slouken@1662
   228
                *(op[2]++) = yuv[2];
slouken@1662
   229
            }
slouken@1662
   230
            p += s->format->BytesPerPixel;
slouken@1662
   231
        }
slouken@1662
   232
    }
slouken@672
   233
slouken@1668
   234
    SDL_UnlockYUVOverlay(o);
slouken@1668
   235
    SDL_UnlockSurface(s);
slouken@672
   236
}
slouken@672
   237
slouken@1662
   238
void
slouken@1668
   239
ConvertRGBtoUYVY(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1668
   240
                 int luminance)
slouken@672
   241
{
slouken@1662
   242
    int x, y;
slouken@1662
   243
    int yuv[3];
slouken@1662
   244
    Uint8 *p, *op;
slouken@672
   245
slouken@1668
   246
    SDL_LockSurface(s);
slouken@1668
   247
    SDL_LockYUVOverlay(o);
slouken@672
   248
slouken@1662
   249
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1662
   250
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1662
   251
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1662
   252
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1668
   253
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1662
   254
            if (x % 2 == 0) {
slouken@1662
   255
                *(op++) = yuv[1];
slouken@1662
   256
                *(op++) = yuv[0];
slouken@1662
   257
                *(op++) = yuv[2];
slouken@1662
   258
            } else
slouken@1662
   259
                *(op++) = yuv[0];
slouken@672
   260
slouken@1662
   261
            p += s->format->BytesPerPixel;
slouken@1662
   262
        }
slouken@1662
   263
    }
slouken@672
   264
slouken@1668
   265
    SDL_UnlockYUVOverlay(o);
slouken@1668
   266
    SDL_UnlockSurface(s);
slouken@672
   267
}
slouken@672
   268
slouken@1662
   269
void
slouken@1668
   270
ConvertRGBtoYVYU(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1668
   271
                 int luminance)
slouken@672
   272
{
slouken@1662
   273
    int x, y;
slouken@1662
   274
    int yuv[3];
slouken@1662
   275
    Uint8 *p, *op;
slouken@672
   276
slouken@1668
   277
    SDL_LockSurface(s);
slouken@1668
   278
    SDL_LockYUVOverlay(o);
slouken@672
   279
slouken@1662
   280
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1662
   281
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1662
   282
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1662
   283
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1668
   284
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1662
   285
            if (x % 2 == 0) {
slouken@1662
   286
                *(op++) = yuv[0];
slouken@1662
   287
                *(op++) = yuv[2];
slouken@1662
   288
                op[1] = yuv[1];
slouken@1662
   289
            } else {
slouken@1662
   290
                *op = yuv[0];
slouken@1662
   291
                op += 2;
slouken@1662
   292
            }
slouken@672
   293
slouken@1662
   294
            p += s->format->BytesPerPixel;
slouken@1662
   295
        }
slouken@1662
   296
    }
slouken@672
   297
slouken@1668
   298
    SDL_UnlockYUVOverlay(o);
slouken@1668
   299
    SDL_UnlockSurface(s);
slouken@672
   300
}
slouken@672
   301
slouken@1662
   302
void
slouken@1668
   303
ConvertRGBtoYUY2(SDL_Surface * s, SDL_Overlay * o, int monochrome,
slouken@1668
   304
                 int luminance)
slouken@672
   305
{
slouken@1662
   306
    int x, y;
slouken@1662
   307
    int yuv[3];
slouken@1662
   308
    Uint8 *p, *op;
slouken@672
   309
slouken@1668
   310
    SDL_LockSurface(s);
slouken@1668
   311
    SDL_LockYUVOverlay(o);
slouken@672
   312
slouken@1662
   313
    for (y = 0; y < s->h && y < o->h; y++) {
slouken@1662
   314
        p = ((Uint8 *) s->pixels) + s->pitch * y;
slouken@1662
   315
        op = o->pixels[0] + o->pitches[0] * y;
slouken@1662
   316
        for (x = 0; x < s->w && x < o->w; x++) {
slouken@1668
   317
            RGBtoYUV(p, yuv, monochrome, luminance);
slouken@1662
   318
            if (x % 2 == 0) {
slouken@1662
   319
                *(op++) = yuv[0];
slouken@1662
   320
                *(op++) = yuv[1];
slouken@1662
   321
                op[1] = yuv[2];
slouken@1662
   322
            } else {
slouken@1662
   323
                *op = yuv[0];
slouken@1662
   324
                op += 2;
slouken@1662
   325
            }
slouken@672
   326
slouken@1662
   327
            p += s->format->BytesPerPixel;
slouken@1662
   328
        }
slouken@1662
   329
    }
slouken@1662
   330
slouken@1668
   331
    SDL_UnlockYUVOverlay(o);
slouken@1668
   332
    SDL_UnlockSurface(s);
slouken@672
   333
}
slouken@672
   334
slouken@1662
   335
static void
slouken@1668
   336
PrintUsage(char *argv0)
slouken@672
   337
{
slouken@1668
   338
    fprintf(stderr, "Usage: %s [arg] [arg] [arg] ...\n", argv0);
slouken@1668
   339
    fprintf(stderr, "\n");
slouken@1668
   340
    fprintf(stderr, "Where 'arg' is any of the following options:\n");
slouken@1668
   341
    fprintf(stderr, "\n");
slouken@1668
   342
    fprintf(stderr, "	-fps <frames per second>\n");
slouken@1668
   343
    fprintf(stderr,
slouken@1668
   344
            "	-format <fmt> (one of the: YV12, IYUV, YUY2, UYVY, YVYU)\n");
slouken@1668
   345
    fprintf(stderr,
slouken@1668
   346
            "	-scale <scale factor> (initial scale of the overlay)\n");
slouken@1668
   347
    fprintf(stderr, "	-help (shows this help)\n");
slouken@1668
   348
    fprintf(stderr, "\n");
slouken@1668
   349
    fprintf(stderr,
slouken@1668
   350
            "Press ESC to exit, or SPACE to freeze the movie while application running.\n");
slouken@1668
   351
    fprintf(stderr, "\n");
slouken@672
   352
}
slouken@672
   353
slouken@1662
   354
int
slouken@1668
   355
main(int argc, char **argv)
slouken@672
   356
{
slouken@1662
   357
    Uint8 *RawMooseData;
slouken@1662
   358
    SDL_RWops *handle;
slouken@1662
   359
    SDL_Surface *screen;
slouken@1662
   360
    SDL_Surface *MooseFrame[MOOSEFRAMES_COUNT];
slouken@1662
   361
    SDL_Overlay *overlay;
slouken@672
   362
    SDL_Rect overlayrect;
slouken@672
   363
    SDL_Event event;
slouken@672
   364
    Uint32 lastftick;
slouken@1662
   365
    int paused = 0;
slouken@1662
   366
    int resized = 0;
slouken@672
   367
    int i;
slouken@1662
   368
    int fps = 12;
slouken@672
   369
    int fpsdelay;
slouken@1662
   370
    int overlay_format = SDL_YUY2_OVERLAY;
slouken@1662
   371
    int scale = 5;
slouken@672
   372
slouken@1668
   373
    if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
slouken@1668
   374
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
icculus@1151
   375
        return 3;
icculus@1151
   376
    }
icculus@1151
   377
slouken@1662
   378
    while (argc > 1) {
slouken@1668
   379
        if (strcmp(argv[1], "-fps") == 0) {
slouken@1662
   380
            if (argv[2]) {
slouken@1668
   381
                fps = atoi(argv[2]);
slouken@1662
   382
                if (fps == 0) {
slouken@1668
   383
                    fprintf(stderr,
slouken@1668
   384
                            "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
slouken@1668
   385
                    quit(10);
slouken@672
   386
                }
slouken@1662
   387
                if ((fps < 0) || (fps > 1000)) {
slouken@1668
   388
                    fprintf(stderr,
slouken@1668
   389
                            "The -fps option must be in range from 1 to 1000, default is 12.\n");
slouken@1668
   390
                    quit(10);
slouken@672
   391
                }
slouken@672
   392
                argv += 2;
slouken@672
   393
                argc -= 2;
slouken@1662
   394
            } else {
slouken@1668
   395
                fprintf(stderr,
slouken@1668
   396
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
slouken@1668
   397
                quit(10);
slouken@672
   398
            }
slouken@1668
   399
        } else if (strcmp(argv[1], "-format") == 0) {
slouken@1662
   400
            if (argv[2]) {
slouken@1668
   401
                if (!strcmp(argv[2], "YV12"))
slouken@672
   402
                    overlay_format = SDL_YV12_OVERLAY;
slouken@1668
   403
                else if (!strcmp(argv[2], "IYUV"))
slouken@672
   404
                    overlay_format = SDL_IYUV_OVERLAY;
slouken@1668
   405
                else if (!strcmp(argv[2], "YUY2"))
slouken@672
   406
                    overlay_format = SDL_YUY2_OVERLAY;
slouken@1668
   407
                else if (!strcmp(argv[2], "UYVY"))
slouken@672
   408
                    overlay_format = SDL_UYVY_OVERLAY;
slouken@1668
   409
                else if (!strcmp(argv[2], "YVYU"))
slouken@672
   410
                    overlay_format = SDL_YVYU_OVERLAY;
slouken@1662
   411
                else {
slouken@1668
   412
                    fprintf(stderr,
slouken@1668
   413
                            "The -format option %s is not recognized, see help for info.\n",
slouken@1668
   414
                            argv[2]);
slouken@1668
   415
                    quit(10);
slouken@672
   416
                }
slouken@672
   417
                argv += 2;
slouken@672
   418
                argc -= 2;
slouken@1662
   419
            } else {
slouken@1668
   420
                fprintf(stderr,
slouken@1668
   421
                        "The -format option requires an argument, default is YUY2.\n");
slouken@1668
   422
                quit(10);
slouken@672
   423
            }
slouken@1668
   424
        } else if (strcmp(argv[1], "-scale") == 0) {
slouken@1662
   425
            if (argv[2]) {
slouken@1668
   426
                scale = atoi(argv[2]);
slouken@1662
   427
                if (scale == 0) {
slouken@1668
   428
                    fprintf(stderr,
slouken@1668
   429
                            "The -scale option requires an argument [from 1 to 50], default is 5.\n");
slouken@1668
   430
                    quit(10);
slouken@672
   431
                }
slouken@1662
   432
                if ((scale < 0) || (scale > 50)) {
slouken@1668
   433
                    fprintf(stderr,
slouken@1668
   434
                            "The -scale option must be in range from 1 to 50, default is 5.\n");
slouken@1668
   435
                    quit(10);
slouken@672
   436
                }
slouken@672
   437
                argv += 2;
slouken@672
   438
                argc -= 2;
slouken@1662
   439
            } else {
slouken@1668
   440
                fprintf(stderr,
slouken@1668
   441
                        "The -fps option requires an argument [from 1 to 1000], default is 12.\n");
slouken@1668
   442
                quit(10);
slouken@672
   443
            }
slouken@1668
   444
        } else if ((strcmp(argv[1], "-help") == 0)
slouken@1668
   445
                   || (strcmp(argv[1], "-h") == 0)) {
slouken@1668
   446
            PrintUsage(argv[0]);
slouken@1668
   447
            quit(0);
slouken@1662
   448
        } else {
slouken@1668
   449
            fprintf(stderr, "Unrecognized option: %s.\n", argv[1]);
slouken@1668
   450
            quit(10);
slouken@672
   451
        }
slouken@672
   452
        break;
slouken@672
   453
    }
slouken@1662
   454
slouken@1668
   455
    RawMooseData = (Uint8 *) malloc(MOOSEFRAME_SIZE * MOOSEFRAMES_COUNT);
slouken@1662
   456
    if (RawMooseData == NULL) {
slouken@1668
   457
        fprintf(stderr, "Can't allocate memory for movie !\n");
slouken@1668
   458
        free(RawMooseData);
slouken@1668
   459
        quit(1);
slouken@672
   460
    }
slouken@672
   461
slouken@672
   462
    /* load the trojan moose images */
slouken@1668
   463
    handle = SDL_RWFromFile("moose.dat", "rb");
slouken@1662
   464
    if (handle == NULL) {
slouken@1668
   465
        fprintf(stderr, "Can't find the file moose.dat !\n");
slouken@1668
   466
        free(RawMooseData);
slouken@1668
   467
        quit(2);
slouken@672
   468
    }
slouken@672
   469
slouken@1668
   470
    SDL_RWread(handle, RawMooseData, MOOSEFRAME_SIZE, MOOSEFRAMES_COUNT);
slouken@1662
   471
slouken@1668
   472
    SDL_RWclose(handle);
slouken@672
   473
slouken@673
   474
    /* Set video mode */
slouken@1662
   475
    if ((screen =
slouken@1668
   476
         SDL_SetVideoMode(MOOSEPIC_W * scale, MOOSEPIC_H * scale, 0,
slouken@1668
   477
                          SDL_RESIZABLE | SDL_SWSURFACE)) == NULL) {
slouken@1668
   478
        fprintf(stderr, "Couldn't set video mode: %s\n", SDL_GetError());
slouken@1668
   479
        free(RawMooseData);
slouken@1668
   480
        quit(4);
slouken@672
   481
    }
slouken@672
   482
slouken@672
   483
    /* Set the window manager title bar */
slouken@1668
   484
    SDL_WM_SetCaption("SDL test overlay: running moose", "testoverlay2");
slouken@672
   485
slouken@1662
   486
    for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
slouken@1662
   487
        MooseFrame[i] =
slouken@1668
   488
            SDL_CreateRGBSurfaceFrom(RawMooseData + i * MOOSEFRAME_SIZE,
slouken@1668
   489
                                     MOOSEPIC_W, MOOSEPIC_H, 8, MOOSEPIC_W,
slouken@1668
   490
                                     0, 0, 0, 0);
slouken@1662
   491
        if (MooseFrame[i] == NULL) {
slouken@1668
   492
            fprintf(stderr, "Couldn't create SDL_Surfaces:%s\n",
slouken@1668
   493
                    SDL_GetError());
slouken@1668
   494
            free(RawMooseData);
slouken@1668
   495
            quit(5);
slouken@1662
   496
        }
slouken@1668
   497
        SDL_SetColors(MooseFrame[i], MooseColors, 0, 84);
slouken@1662
   498
slouken@672
   499
        {
slouken@1662
   500
            SDL_Surface *newsurf;
slouken@1662
   501
            SDL_PixelFormat format;
slouken@1662
   502
slouken@1662
   503
            format.palette = NULL;
slouken@1662
   504
            format.BitsPerPixel = 32;
slouken@1662
   505
            format.BytesPerPixel = 4;
slouken@1662
   506
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
slouken@1662
   507
            format.Rshift = 0;
slouken@1662
   508
            format.Gshift = 8;
slouken@1662
   509
            format.Bshift = 16;
slouken@1662
   510
#else
slouken@1662
   511
            format.Rshift = 24;
slouken@1662
   512
            format.Gshift = 16;
slouken@1662
   513
            format.Bshift = 8;
slouken@1662
   514
#endif
slouken@1662
   515
            format.Ashift = 0;
slouken@1662
   516
            format.Rmask = 0xff << format.Rshift;
slouken@1662
   517
            format.Gmask = 0xff << format.Gshift;
slouken@1662
   518
            format.Bmask = 0xff << format.Bshift;
slouken@1662
   519
            format.Amask = 0;
slouken@1662
   520
            format.Rloss = 0;
slouken@1662
   521
            format.Gloss = 0;
slouken@1662
   522
            format.Bloss = 0;
slouken@1662
   523
            format.Aloss = 8;
slouken@1662
   524
            format.colorkey = 0;
slouken@1662
   525
            format.alpha = 0;
slouken@1662
   526
slouken@1662
   527
            newsurf =
slouken@1668
   528
                SDL_ConvertSurface(MooseFrame[i], &format, SDL_SWSURFACE);
slouken@1662
   529
            if (!newsurf) {
slouken@1668
   530
                fprintf(stderr,
slouken@1668
   531
                        "Couldn't convert picture to 32bits RGB: %s\n",
slouken@1668
   532
                        SDL_GetError());
slouken@1668
   533
                quit(6);
slouken@1662
   534
            }
slouken@1668
   535
            SDL_FreeSurface(MooseFrame[i]);
slouken@1662
   536
            MooseFrame[i] = newsurf;
slouken@672
   537
        }
slouken@672
   538
    }
slouken@672
   539
slouken@1668
   540
    free(RawMooseData);
slouken@672
   541
slouken@1662
   542
    overlay =
slouken@1668
   543
        SDL_CreateYUVOverlay(MOOSEPIC_W, MOOSEPIC_H, overlay_format, screen);
slouken@1662
   544
    if (!overlay) {
slouken@1668
   545
        fprintf(stderr, "Couldn't create overlay: %s\n", SDL_GetError());
slouken@1668
   546
        quit(7);
slouken@886
   547
    }
slouken@672
   548
slouken@1668
   549
    printf("Created %dx%dx%d %s %s overlay\n", overlay->w, overlay->h,
slouken@1668
   550
           overlay->planes, overlay->hw_overlay ? "hardware" : "software",
slouken@1668
   551
           overlay->format == SDL_YV12_OVERLAY ? "YV12" : overlay->format ==
slouken@1668
   552
           SDL_IYUV_OVERLAY ? "IYUV" : overlay->format ==
slouken@1668
   553
           SDL_YUY2_OVERLAY ? "YUY2" : overlay->format ==
slouken@1668
   554
           SDL_UYVY_OVERLAY ? "UYVY" : overlay->format ==
slouken@1668
   555
           SDL_YVYU_OVERLAY ? "YVYU" : "Unknown");
slouken@672
   556
slouken@1662
   557
    for (i = 0; i < overlay->planes; i++) {
slouken@1668
   558
        printf("  plane %d: pitch=%d\n", i, overlay->pitches[i]);
slouken@672
   559
    }
slouken@672
   560
slouken@1662
   561
    overlayrect.x = 0;
slouken@1662
   562
    overlayrect.y = 0;
slouken@1662
   563
    overlayrect.w = MOOSEPIC_W * scale;
slouken@1662
   564
    overlayrect.h = MOOSEPIC_H * scale;
slouken@672
   565
slouken@672
   566
    /* set the start frame */
slouken@1662
   567
    i = 0;
slouken@1662
   568
    fpsdelay = 1000 / fps;
slouken@672
   569
slouken@672
   570
    /* Ignore key up events, they don't even get filtered */
slouken@1668
   571
    SDL_EventState(SDL_KEYUP, SDL_IGNORE);
slouken@672
   572
slouken@1668
   573
    lastftick = SDL_GetTicks();
slouken@672
   574
slouken@672
   575
    /* Loop, waiting for QUIT or RESIZE */
slouken@1662
   576
    while (1) {
slouken@1668
   577
        if (SDL_PollEvent(&event)) {
slouken@1662
   578
            switch (event.type) {
slouken@1662
   579
            case SDL_VIDEORESIZE:
slouken@1662
   580
                screen =
slouken@1668
   581
                    SDL_SetVideoMode(event.resize.w, event.resize.h, 0,
slouken@1668
   582
                                     SDL_RESIZABLE | SDL_SWSURFACE);
slouken@1662
   583
                overlayrect.w = event.resize.w;
slouken@1662
   584
                overlayrect.h = event.resize.h;
slouken@1662
   585
                if (paused) {
slouken@1662
   586
                    resized = 1;
slouken@1662
   587
                }
slouken@1662
   588
                break;
slouken@1662
   589
            case SDL_MOUSEBUTTONDOWN:
slouken@1662
   590
                overlayrect.x = event.button.x - overlayrect.w / 2;
slouken@1662
   591
                overlayrect.y = event.button.y - overlayrect.h / 2;
slouken@1662
   592
                break;
slouken@1662
   593
            case SDL_KEYDOWN:
slouken@1662
   594
                if (event.key.keysym.sym == SDLK_SPACE) {
slouken@1662
   595
                    paused = !paused;
slouken@1662
   596
                    break;
slouken@1662
   597
                }
slouken@1662
   598
                if (event.key.keysym.sym != SDLK_ESCAPE) {
slouken@1662
   599
                    break;
slouken@1662
   600
                }
slouken@1662
   601
            case SDL_QUIT:
slouken@1668
   602
                SDL_FreeYUVOverlay(overlay);
slouken@1662
   603
                for (i = 0; i < MOOSEFRAMES_COUNT; i++) {
slouken@1668
   604
                    SDL_FreeSurface(MooseFrame[i]);
slouken@1662
   605
                }
slouken@1668
   606
                quit(0);
slouken@672
   607
            }
slouken@672
   608
        }
slouken@672
   609
slouken@1662
   610
        if ((!paused) || (resized)) {
slouken@1668
   611
            if (((SDL_GetTicks() - lastftick) > fpsdelay) || (resized)) {
slouken@1668
   612
                lastftick = SDL_GetTicks();
slouken@672
   613
slouken@1662
   614
                switch (overlay_format) {
slouken@1662
   615
                case SDL_YUY2_OVERLAY:
slouken@1668
   616
                    ConvertRGBtoYUY2(MooseFrame[i], overlay, 0, 100);
slouken@1662
   617
                    break;
slouken@1662
   618
                case SDL_YV12_OVERLAY:
slouken@1668
   619
                    ConvertRGBtoYV12(MooseFrame[i], overlay, 0, 100);
slouken@1662
   620
                    break;
slouken@1662
   621
                case SDL_UYVY_OVERLAY:
slouken@1668
   622
                    ConvertRGBtoUYVY(MooseFrame[i], overlay, 0, 100);
slouken@1662
   623
                    break;
slouken@1662
   624
                case SDL_YVYU_OVERLAY:
slouken@1668
   625
                    ConvertRGBtoYVYU(MooseFrame[i], overlay, 0, 100);
slouken@1662
   626
                    break;
slouken@1662
   627
                case SDL_IYUV_OVERLAY:
slouken@1668
   628
                    ConvertRGBtoIYUV(MooseFrame[i], overlay, 0, 100);
slouken@1662
   629
                    break;
slouken@672
   630
                }
slouken@672
   631
slouken@1668
   632
                SDL_DisplayYUVOverlay(overlay, &overlayrect);
slouken@1662
   633
                if (!resized) {
slouken@753
   634
                    i++;
slouken@1662
   635
                    if (i == 10) {
slouken@1662
   636
                        i = 0;
slouken@753
   637
                    }
slouken@1662
   638
                } else {
slouken@1662
   639
                    resized = 0;
slouken@672
   640
                }
slouken@672
   641
            }
slouken@672
   642
        }
slouken@672
   643
        /* kind of timeslice to OS */
slouken@1668
   644
        SDL_Delay(1);
slouken@672
   645
    }
slouken@672
   646
slouken@1668
   647
    SDL_Quit();
slouken@672
   648
    return 0;
slouken@672
   649
}