Changeset 551


Ignore:
Timestamp:
Apr 29, 2011, 1:15:05 AM (7 years ago)
Author:
Sam Hocevar
Message:

sdlport: make SDL_mixer mandatory. I'm not going to maintain legacy code
that cannot even play music.

Location:
abuse/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • abuse/trunk/configure.ac

    r537 r551  
    5151save_CPPFLAGS="${CPPFLAGS}"
    5252CPPFLAGS="${CPPFLAGS} ${SDL_CFLAGS}"
    53 AC_CHECK_HEADERS(SDL/SDL_mixer.h, [ac_cv_my_have_sdl="yes"])
     53AC_CHECK_HEADERS(SDL/SDL_mixer.h, [ac_cv_my_have_sdl_mixer="yes"])
    5454CPPFLAGS="${save_CPPFLAGS}"
    55 if test "${ac_cv_my_have_sdl}" != "no"; then
    56   AC_DEFINE(USE_SDL_MIXER, 1, Define to 1 to use SDL_Mixer)
    57   LIBS="$LIBS -lSDL_mixer"
     55if test "${ac_cv_my_have_sdl_mixer}" = "no"; then
     56  AC_MSG_ERROR([*** SDL_mixer not found!])
    5857fi
     58LIBS="$LIBS -lSDL_mixer"
    5959
    6060if test "${enable_debug}" = "yes"; then
  • abuse/trunk/src/sdlport/sound.cpp

    r536 r551  
    2121#include "config.h"
    2222
    23 #include <string.h>
     23#include <cstring>
    2424
    2525#include <SDL.h>
    26 #ifdef USE_SDL_MIXER
    2726#include <SDL/SDL_mixer.h>
     27
     28#include "sound.h"
    2829#include "hmi.h"
    29 #endif
    30 
    31 #include "sound.h"
    3230#include "readwav.h"
    3331#include "specs.h"
    3432#include "setup.h"
    3533
    36 class effect_handle
    37 {
    38 public:
    39     effect_handle    *prev;        // Handle to the previous effect
    40     effect_handle    *next;        // Handle to the next effect
    41     Uint8            *data;        // Audio data
    42     Uint8            *pos;        // current playing position in the data
    43     Uint32            length;        // Length of the data
    44     Uint32            volume;        // Volume for this effect.
    45 
    46     effect_handle( effect_handle *Next )
    47     {
    48         next = Next;
    49         if( next )
    50         {
    51             next->prev = this;
    52         }
    53         prev = NULL;
    54         data = NULL;
    55         pos = NULL;
    56     }
    57 
    58     ~effect_handle()
    59     {
    60         if( next )
    61         {
    62             next->prev = prev;
    63         }
    64         if( prev )
    65         {
    66             prev->next = next;
    67         }
    68     }
    69 };
    70 
    71 #ifndef USE_SDL_MIXER
    72 static effect_handle * fx_list = NULL;
    73 #endif
    7434extern flags_struct flags;
    7535static int sound_enabled = 0;
    7636static SDL_AudioSpec audioObtained;
    77 
    78 //
    79 // mix_audio
    80 //
    81 // Do the actual playing of the audio by looping through our effects list
    82 // and mixing each sample.
    83 //
    84 void mix_audio(void *udata, Uint8 *stream, int len)
    85 {
    86 #ifndef USE_SDL_MIXER
    87     effect_handle *handle = fx_list;
    88 
    89     while( handle )
    90     {
    91         if( handle->length > 0 && handle->pos )
    92         {
    93             len = ( len > (int)handle->length ? handle->length : len );
    94             SDL_MixAudio( stream, handle->pos, len, handle->volume );
    95             handle->pos += len;
    96             handle->length -= len;
    97             handle = handle->next;
    98         }
    99         else
    100         {
    101             // update the list pointer if we're deleting the first node
    102             if( fx_list == handle )
    103             {
    104                 fx_list = handle->next;
    105             }
    106             effect_handle *tmp = handle->next;
    107             if( !flags.mono )
    108             {
    109                 // delete the audio buffer
    110                 free( handle->data );
    111             }
    112             delete handle;
    113             handle = tmp;
    114         }
    115     }
    116 #endif
    117 }
    11837
    11938//
     
    14665    free( sfxdir );
    14766
    148 #ifdef USE_SDL_MIXER
    14967    if (Mix_OpenAudio(11025, AUDIO_U8, 2, 128) < 0)
    15068    {
     
    16179    sound_enabled = SFX_INITIALIZED | MUSIC_INITIALIZED;
    16280
    163     printf( "Music: Enabled\n" );
    164 #else
    165     SDL_AudioSpec audioWanted;
    166     audioWanted.freq = 11025;
    167     audioWanted.format = AUDIO_U8;
    168     audioWanted.channels = 2 - flags.mono;
    169     audioWanted.samples = 128;
    170     audioWanted.callback = mix_audio;
    171     audioWanted.userdata = NULL;
    172 
    173     // Now open the audio device
    174     if( SDL_OpenAudio( &audioWanted, &audioObtained ) < 0 )
    175     {
    176         printf( "Sound: Unable to open audio - %s\nSound: Disabled (error)\n", SDL_GetError() );
    177         return 0;
    178     }
    179 
    180     SDL_PauseAudio( 0 );
    181 
    182     sound_enabled = SFX_INITIALIZED;
    183 #endif
    184 
    18581    printf( "Sound: Enabled\n" );
    18682
     
    19692void sound_uninit()
    19793{
     94    if (sound_enabled)
     95    {
     96        Mix_CloseAudio();
     97    }
     98}
     99
     100//
     101// sound_effect constructor
     102//
     103// Read in the requested .wav file.
     104//
     105sound_effect::sound_effect( char * filename )
     106{
    198107    if( sound_enabled )
    199108    {
    200 #ifdef USE_SDL_MIXER
    201         Mix_CloseAudio();
    202 #else
    203         SDL_PauseAudio( 1 );
    204         while( fx_list )
    205         {
    206             effect_handle *last = fx_list;
    207             fx_list = fx_list->next;
    208             free( last );
    209         }
    210         SDL_CloseAudio();
    211 #endif
    212     }
    213 }
    214 
    215 //
    216 // sound_effect constructor
    217 //
    218 // Read in the requested .wav file.
    219 //
    220 sound_effect::sound_effect( char * filename )
    221 {
    222     if( sound_enabled )
    223     {
    224109        int sample_speed;
    225110
    226 #ifdef USE_SDL_MIXER
    227111        void* temp_data = (void *)read_wav( filename, sample_speed, size );
    228112
     
    241125
    242126        this->chunk = Mix_QuickLoad_RAW((Uint8*)data, size);
    243 #else
    244         data = (void *)read_wav( filename, sample_speed, size );
    245 #endif
    246127    }
    247128}
     
    256137    if( sound_enabled )
    257138    {
    258 #ifdef USE_SDL_MIXER
    259139        // Sound effect deletion only happens on level load, so there
    260140        // is no problem in stopping everything. But the original playing
     
    267147            SDL_Delay(10);
    268148        Mix_FreeChunk(this->chunk);
    269 #endif
    270149
    271150        if( data )
     
    279158// sound_effect::play
    280159//
    281 // Insert a new effect_handle into the list and modify the audio
    282 // if we're doing stereo.
     160// Add a new sample for playing.
    283161// panpot defines the pan position for the sound effect.
    284162//   0   - Completely to the right.
     
    288166void sound_effect::play( int volume, int pitch, int panpot )
    289167{
    290     if( sound_enabled )
    291     {
    292 #ifdef USE_SDL_MIXER
     168    if (sound_enabled)
     169    {
    293170        int channel = Mix_PlayChannel(-1, this->chunk, 0);
    294171        if (channel > -1)
     
    297174            Mix_SetPanning(channel, panpot, 255 - panpot);
    298175        }
    299 #else
    300         SDL_LockAudio();
    301 
    302         fx_list = new effect_handle( fx_list );
    303         if( fx_list == NULL )
    304         {
    305             printf( "Sound: ERROR - Failed to create new effect.\n" );
    306             SDL_UnlockAudio();
    307             return;
    308         }
    309 
    310         if( !flags.mono )
    311         {
    312             unsigned int i;
    313             Uint32 cvtBufferSize;
    314             SDL_AudioCVT audiocvt;
    315 
    316             // Do some audio conversion
    317             SDL_BuildAudioCVT( &audiocvt, AUDIO_U8, 1, 11025, audioObtained.format, audioObtained.channels, audioObtained.freq );
    318             audiocvt.buf = (Uint8 *)malloc( size * audiocvt.len_mult );
    319             audiocvt.len = size;
    320             memcpy( audiocvt.buf, data, size );
    321             SDL_ConvertAudio( &audiocvt );
    322             cvtBufferSize = (Uint32)((double)size * audiocvt.len_ratio);
    323 
    324             // Adjust for requested pan position
    325             if( panpot != 128 )
    326             {
    327                 if( panpot > 128 )
    328                 {
    329                     // Pan to the left
    330                     panpot = (panpot - 255) * -1;
    331                     for( i = 1 ; i <= cvtBufferSize; i += 2 )
    332                     {
    333                         audiocvt.buf[i] = (((audiocvt.buf[i] - 128) * panpot) / 128) + 128;
    334                     }
    335                 }
    336                 else
    337                 {
    338                     // Pan to the right
    339                     for( i = 0 ; i < cvtBufferSize; i += 2 )
    340                     {
    341                         audiocvt.buf[i] = (((audiocvt.buf[i] - 128) * panpot) / 128) + 128;
    342                     }
    343                 }
    344             }
    345 
    346             fx_list->data = audiocvt.buf;
    347             fx_list->pos = audiocvt.buf;
    348             fx_list->length = cvtBufferSize;
    349             fx_list->volume = volume;
    350         }
    351         else
    352         {
    353             // Only doing mono so don't mess with the audio data.
    354             fx_list->data = (Uint8 *)data;
    355             fx_list->pos = (Uint8 *)data;
    356             fx_list->length = size;
    357             fx_list->volume = volume;
    358         }
    359         SDL_UnlockAudio();
    360 #endif
    361176    }
    362177}
     
    371186    song_id = 0;
    372187
    373 #ifdef USE_SDL_MIXER
    374188    rw = NULL;
    375189    music = NULL;
     
    384198    if (!data)
    385199    {
    386         printf("Music: ERROR - could not load %s\n", realname);
     200        printf("Sound: ERROR - could not load %s\n", realname);
    387201        return;
    388202    }
     
    393207    if (!music)
    394208    {
    395         printf("Music: ERROR - %s while loading %s\n",
     209        printf("Sound: ERROR - %s while loading %s\n",
    396210               Mix_GetError(), realname);
    397211        return;
    398212    }
    399 #endif
    400213}
    401214
     
    407220    free(Name);
    408221
    409 #ifdef USE_SDL_MIXER
    410222    Mix_FreeMusic(music);
    411223    SDL_FreeRW(rw);
    412 #endif
    413224}
    414225
     
    417228    song_id = 1;
    418229
    419 #ifdef USE_SDL_MIXER
    420230    Mix_PlayMusic(this->music, 0);
    421231    Mix_VolumeMusic(volume);
    422 #endif
    423232}
    424233
     
    427236    song_id = 0;
    428237
    429 #ifdef USE_SDL_MIXER
    430238    Mix_FadeOutMusic(100);
    431 #endif
    432239}
    433240
    434241int song::playing()
    435242{
    436 #ifdef USE_SDL_MIXER
    437243    return Mix_PlayingMusic();
    438 #else
    439     return song_id;
    440 #endif
    441244}
    442245
    443246void song::set_volume( int volume )
    444247{
    445 #ifdef USE_SDL_MIXER
    446248    Mix_VolumeMusic(volume);
    447 #else
    448     // do nothing...
    449 #endif
    450 }
     249}
     250
  • abuse/trunk/src/sdlport/sound.h

    r534 r551  
    1212#define __SOUND_H__
    1313
    14 #ifdef USE_SDL_MIXER
    15 #   include <SDL/SDL_mixer.h>
    16 #endif
     14#include <SDL/SDL_mixer.h>
    1715
    1816/* options are passed via command line */
     
    3432private:
    3533    void *data;
    36 #ifdef USE_SDL_MIXER
    3734    Mix_Chunk* chunk;
    38 #endif
    3935    int size;
    4036};
     
    5551    unsigned char *data;
    5652    unsigned long song_id;
    57 #ifdef USE_SDL_MIXER
    5853    Mix_Music* music;
    5954    SDL_RWops* rw;
    60 #endif
    6155};
    6256
Note: See TracChangeset for help on using the changeset viewer.