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