test/testoverlay2.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1151 be9c9c8f6d53
child 1439 4d3bb026cd16
permissions -rw-r--r--
[PATCH] SDL_GetVideoMode() does not find best mode, part 2

Following commit 1.51, I come accross a problem when SDL must choose between
several video modes that could suit the one asked.

If I ask 320x240 with this list:
768x480 768x240 640x400 640x200 384x480 384x240 320x400 320x200

The smallest selectables modes are 384x240 and 320x400. And SDL choose the later
in this list, but 384x240 is more suitable. So I added a check to compare
the pixel count (surface) of modes, and select the one which has the smallest
pixel count.

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