test/testoverlay2.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 09 May 2006 07:26:58 +0000
changeset 1790 828a17e05192
parent 1642 f1211a4b7380
child 1662 782fd950bd46
child 1895 c121d94672cb
permissions -rw-r--r--
Date: Mon, 8 May 2006 14:19:30 -0700
From: Bob Ippolito
Subject: SDL trunk (r2346) and Mac OS X

The current state of the trunk doesn't quite compile on Mac OS X,
I've attached a series of patches that gets it to compile and kills a
few warnings.

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