test/loopwave.c
author Patrice Mandin <patmandin@gmail.com>
Thu, 19 Jan 2006 21:28:52 +0000
changeset 1257 448a9a64537b
parent 1151 be9c9c8f6d53
child 1463 0394f8ebc42d
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@0
     1
slouken@0
     2
/* Program to load a wave file and loop playing it using SDL sound */
slouken@0
     3
slouken@0
     4
/* loopwaves.c is much more robust in handling WAVE files -- 
slouken@0
     5
	This is only for simple WAVEs
slouken@0
     6
*/
slouken@0
     7
slouken@0
     8
#include <stdio.h>
slouken@0
     9
#include <stdlib.h>
slouken@0
    10
#include <signal.h>
slouken@0
    11
slouken@0
    12
#include "SDL.h"
slouken@0
    13
#include "SDL_audio.h"
slouken@0
    14
slouken@0
    15
struct {
slouken@0
    16
	SDL_AudioSpec spec;
slouken@0
    17
	Uint8   *sound;			/* Pointer to wave data */
slouken@0
    18
	Uint32   soundlen;		/* Length of wave data */
slouken@0
    19
	int      soundpos;		/* Current play position */
slouken@0
    20
} wave;
slouken@0
    21
icculus@1151
    22
icculus@1151
    23
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
icculus@1151
    24
static void quit(int rc)
icculus@1151
    25
{
icculus@1151
    26
	SDL_Quit();
icculus@1151
    27
	exit(rc);
icculus@1151
    28
}
icculus@1151
    29
icculus@1151
    30
slouken@0
    31
void fillerup(void *unused, Uint8 *stream, int len)
slouken@0
    32
{
slouken@0
    33
	Uint8 *waveptr;
slouken@0
    34
	int    waveleft;
slouken@0
    35
slouken@0
    36
	/* Set up the pointers */
slouken@0
    37
	waveptr = wave.sound + wave.soundpos;
slouken@0
    38
	waveleft = wave.soundlen - wave.soundpos;
slouken@0
    39
slouken@0
    40
	/* Go! */
slouken@0
    41
	while ( waveleft <= len ) {
slouken@0
    42
		SDL_MixAudio(stream, waveptr, waveleft, SDL_MIX_MAXVOLUME);
slouken@0
    43
		stream += waveleft;
slouken@0
    44
		len -= waveleft;
slouken@0
    45
		waveptr = wave.sound;
slouken@0
    46
		waveleft = wave.soundlen;
slouken@0
    47
		wave.soundpos = 0;
slouken@0
    48
	}
slouken@0
    49
	SDL_MixAudio(stream, waveptr, len, SDL_MIX_MAXVOLUME);
slouken@0
    50
	wave.soundpos += len;
slouken@0
    51
}
slouken@0
    52
slouken@0
    53
static int done = 0;
slouken@0
    54
void poked(int sig)
slouken@0
    55
{
slouken@0
    56
	done = 1;
slouken@0
    57
}
slouken@0
    58
slouken@0
    59
int main(int argc, char *argv[])
slouken@0
    60
{
slouken@0
    61
	char name[32];
slouken@0
    62
slouken@0
    63
	/* Load the SDL library */
slouken@0
    64
	if ( SDL_Init(SDL_INIT_AUDIO) < 0 ) {
slouken@0
    65
		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
icculus@1151
    66
		return(1);
slouken@0
    67
	}
slouken@0
    68
slouken@0
    69
	if ( argv[1] == NULL ) {
slouken@0
    70
		fprintf(stderr, "Usage: %s <wavefile>\n", argv[0]);
icculus@1151
    71
		quit(1);
slouken@0
    72
	}
slouken@0
    73
slouken@0
    74
	/* Load the wave file into memory */
slouken@0
    75
	if ( SDL_LoadWAV(argv[1],
slouken@0
    76
			&wave.spec, &wave.sound, &wave.soundlen) == NULL ) {
slouken@0
    77
		fprintf(stderr, "Couldn't load %s: %s\n",
slouken@0
    78
						argv[1], SDL_GetError());
icculus@1151
    79
		quit(1);
slouken@0
    80
	}
slouken@0
    81
	wave.spec.callback = fillerup;
slouken@0
    82
slouken@0
    83
	/* Set the signals */
slouken@0
    84
#ifdef SIGHUP
slouken@0
    85
	signal(SIGHUP, poked);
slouken@0
    86
#endif
slouken@0
    87
	signal(SIGINT, poked);
slouken@0
    88
#ifdef SIGQUIT
slouken@0
    89
	signal(SIGQUIT, poked);
slouken@0
    90
#endif
slouken@0
    91
	signal(SIGTERM, poked);
slouken@0
    92
slouken@0
    93
	/* Initialize fillerup() variables */
slouken@0
    94
	if ( SDL_OpenAudio(&wave.spec, NULL) < 0 ) {
slouken@0
    95
		fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
slouken@0
    96
		SDL_FreeWAV(wave.sound);
icculus@1151
    97
		quit(2);
slouken@0
    98
	}
slouken@0
    99
	SDL_PauseAudio(0);
slouken@0
   100
slouken@0
   101
	/* Let the audio run */
slouken@0
   102
	printf("Using audio driver: %s\n", SDL_AudioDriverName(name, 32));
slouken@0
   103
	while ( ! done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING) )
slouken@0
   104
		SDL_Delay(1000);
slouken@0
   105
slouken@0
   106
	/* Clean up on signal */
slouken@0
   107
	SDL_CloseAudio();
slouken@0
   108
	SDL_FreeWAV(wave.sound);
icculus@1151
   109
	SDL_Quit();
slouken@0
   110
	return(0);
slouken@0
   111
}