test/loopwave.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 8535 e8ee0708ef5c
child 8582 c3e9a2b93517
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

Previously, the shaders would get compiled separately, the output of which would need to be packaged into the app. This change should make SDL's dll be the only binary needed to include SDL in a WinRT app.
dludwig@8535
     1
/*
dludwig@8535
     2
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
dludwig@8535
     3
dludwig@8535
     4
  This software is provided 'as-is', without any express or implied
dludwig@8535
     5
  warranty.  In no event will the authors be held liable for any damages
dludwig@8535
     6
  arising from the use of this software.
dludwig@8535
     7
dludwig@8535
     8
  Permission is granted to anyone to use this software for any purpose,
dludwig@8535
     9
  including commercial applications, and to alter it and redistribute it
dludwig@8535
    10
  freely.
dludwig@8535
    11
*/
dludwig@8535
    12
dludwig@8535
    13
/* Program to load a wave file and loop playing it using SDL sound */
dludwig@8535
    14
dludwig@8535
    15
/* loopwaves.c is much more robust in handling WAVE files --
dludwig@8535
    16
    This is only for simple WAVEs
dludwig@8535
    17
*/
dludwig@8535
    18
#include "SDL_config.h"
dludwig@8535
    19
dludwig@8535
    20
#include <stdio.h>
dludwig@8535
    21
#include <stdlib.h>
dludwig@8535
    22
dludwig@8535
    23
#if HAVE_SIGNAL_H
dludwig@8535
    24
#include <signal.h>
dludwig@8535
    25
#endif
dludwig@8535
    26
dludwig@8535
    27
#include "SDL.h"
dludwig@8535
    28
#include "SDL_audio.h"
dludwig@8535
    29
dludwig@8535
    30
struct
dludwig@8535
    31
{
dludwig@8535
    32
    SDL_AudioSpec spec;
dludwig@8535
    33
    Uint8 *sound;               /* Pointer to wave data */
dludwig@8535
    34
    Uint32 soundlen;            /* Length of wave data */
dludwig@8535
    35
    int soundpos;               /* Current play position */
dludwig@8535
    36
} wave;
dludwig@8535
    37
dludwig@8535
    38
dludwig@8535
    39
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
dludwig@8535
    40
static void
dludwig@8535
    41
quit(int rc)
dludwig@8535
    42
{
dludwig@8535
    43
    SDL_Quit();
dludwig@8535
    44
    exit(rc);
dludwig@8535
    45
}
dludwig@8535
    46
dludwig@8535
    47
dludwig@8535
    48
void SDLCALL
dludwig@8535
    49
fillerup(void *unused, Uint8 * stream, int len)
dludwig@8535
    50
{
dludwig@8535
    51
    Uint8 *waveptr;
dludwig@8535
    52
    int waveleft;
dludwig@8535
    53
dludwig@8535
    54
    /* Set up the pointers */
dludwig@8535
    55
    waveptr = wave.sound + wave.soundpos;
dludwig@8535
    56
    waveleft = wave.soundlen - wave.soundpos;
dludwig@8535
    57
dludwig@8535
    58
    /* Go! */
dludwig@8535
    59
    while (waveleft <= len) {
dludwig@8535
    60
        SDL_memcpy(stream, waveptr, waveleft);
dludwig@8535
    61
        stream += waveleft;
dludwig@8535
    62
        len -= waveleft;
dludwig@8535
    63
        waveptr = wave.sound;
dludwig@8535
    64
        waveleft = wave.soundlen;
dludwig@8535
    65
        wave.soundpos = 0;
dludwig@8535
    66
    }
dludwig@8535
    67
    SDL_memcpy(stream, waveptr, len);
dludwig@8535
    68
    wave.soundpos += len;
dludwig@8535
    69
}
dludwig@8535
    70
dludwig@8535
    71
static int done = 0;
dludwig@8535
    72
void
dludwig@8535
    73
poked(int sig)
dludwig@8535
    74
{
dludwig@8535
    75
    done = 1;
dludwig@8535
    76
}
dludwig@8535
    77
dludwig@8535
    78
int
dludwig@8535
    79
main(int argc, char *argv[])
dludwig@8535
    80
{
dludwig@8535
    81
    int i;
dludwig@8535
    82
    char filename[4096];
dludwig@8535
    83
dludwig@8535
    84
	/* Enable standard application logging */
dludwig@8535
    85
	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
dludwig@8535
    86
dludwig@8535
    87
    /* Load the SDL library */
dludwig@8535
    88
    if (SDL_Init(SDL_INIT_AUDIO) < 0) {
dludwig@8535
    89
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
dludwig@8535
    90
        return (1);
dludwig@8535
    91
    }
dludwig@8535
    92
dludwig@8535
    93
    if (argc >= 1) {
dludwig@8535
    94
        SDL_strlcpy(filename, argv[1], sizeof(filename));
dludwig@8535
    95
    } else {
dludwig@8535
    96
        SDL_strlcpy(filename, "sample.wav", sizeof(filename));
dludwig@8535
    97
    }
dludwig@8535
    98
    /* Load the wave file into memory */
dludwig@8535
    99
    if (SDL_LoadWAV(filename, &wave.spec, &wave.sound, &wave.soundlen) == NULL) {
dludwig@8535
   100
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", argv[1], SDL_GetError());
dludwig@8535
   101
        quit(1);
dludwig@8535
   102
    }
dludwig@8535
   103
dludwig@8535
   104
    wave.spec.callback = fillerup;
dludwig@8535
   105
#if HAVE_SIGNAL_H
dludwig@8535
   106
    /* Set the signals */
dludwig@8535
   107
#ifdef SIGHUP
dludwig@8535
   108
    signal(SIGHUP, poked);
dludwig@8535
   109
#endif
dludwig@8535
   110
    signal(SIGINT, poked);
dludwig@8535
   111
#ifdef SIGQUIT
dludwig@8535
   112
    signal(SIGQUIT, poked);
dludwig@8535
   113
#endif
dludwig@8535
   114
    signal(SIGTERM, poked);
dludwig@8535
   115
#endif /* HAVE_SIGNAL_H */
dludwig@8535
   116
dludwig@8535
   117
    /* Show the list of available drivers */
dludwig@8535
   118
    SDL_Log("Available audio drivers:");
dludwig@8535
   119
    for (i = 0; i < SDL_GetNumAudioDrivers(); ++i) {
dludwig@8535
   120
		SDL_Log("%i: %s", i, SDL_GetAudioDriver(i));
dludwig@8535
   121
    }
dludwig@8535
   122
dludwig@8535
   123
    /* Initialize fillerup() variables */
dludwig@8535
   124
    if (SDL_OpenAudio(&wave.spec, NULL) < 0) {
dludwig@8535
   125
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't open audio: %s\n", SDL_GetError());
dludwig@8535
   126
        SDL_FreeWAV(wave.sound);
dludwig@8535
   127
        quit(2);
dludwig@8535
   128
    }
dludwig@8535
   129
dludwig@8535
   130
    SDL_Log("Using audio driver: %s\n", SDL_GetCurrentAudioDriver());
dludwig@8535
   131
dludwig@8535
   132
    /* Let the audio run */
dludwig@8535
   133
    SDL_PauseAudio(0);
dludwig@8535
   134
    while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
dludwig@8535
   135
        SDL_Delay(1000);
dludwig@8535
   136
dludwig@8535
   137
    /* Clean up on signal */
dludwig@8535
   138
    SDL_CloseAudio();
dludwig@8535
   139
    SDL_FreeWAV(wave.sound);
dludwig@8535
   140
    SDL_Quit();
dludwig@8535
   141
    return (0);
dludwig@8535
   142
}
dludwig@8535
   143
dludwig@8535
   144
/* vi: set ts=4 sw=4 expandtab: */