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