test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 10 Dec 2003 12:35:56 +0000
changeset 753 b14fdadd8311
parent 673 63419da96d13
child 886 05c551e5bc64
permissions -rw-r--r--
Date: Thu, 4 Dec 2003 07:48:40 +0200
From: "Mike Gorchak"
Subject: SDL/QNX6 new patch

Here in attachment my patch for the SDL/QNX6 again :) It contain non-crtitical/cosmetic fixes:

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