URI:
       tsound_sdl.c - vaccinewars - be a doctor and try to vaccinate the world
  HTML git clone git://src.adamsgaard.dk/vaccinewars
   DIR Log
   DIR Files
   DIR Refs
   DIR README
   DIR LICENSE
       ---
       tsound_sdl.c (3429B)
       ---
            1 /************************************************************************
            2  * sound_sdl.c    dopewars sound system (SDL driver)                    *
            3  * Copyright (C)  1998-2021  Ben Webb                                   *
            4  *                Email: benwebb@users.sf.net                           *
            5  *                WWW: https://dopewars.sourceforge.io/                 *
            6  *                                                                      *
            7  * This program is free software; you can redistribute it and/or        *
            8  * modify it under the terms of the GNU General Public License          *
            9  * as published by the Free Software Foundation; either version 2       *
           10  * of the License, or (at your option) any later version.               *
           11  *                                                                      *
           12  * This program is distributed in the hope that it will be useful,      *
           13  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
           14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
           15  * GNU General Public License for more details.                         *
           16  *                                                                      *
           17  * You should have received a copy of the GNU General Public License    *
           18  * along with this program; if not, write to the Free Software          *
           19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,               *
           20  *                   MA  02111-1307, USA.                               *
           21  ************************************************************************/
           22 
           23 #ifdef HAVE_CONFIG_H
           24 #include <config.h>
           25 #endif
           26 
           27 #ifdef HAVE_SDL_MIXER
           28 #include <stdio.h>
           29 #include <string.h>
           30 #include <SDL.h>
           31 #include <SDL_mixer.h>
           32 #include <glib.h>
           33 #include "../sound.h"
           34 
           35 struct ChannelStruct {
           36   Mix_Chunk *chunk;
           37   gchar *name;
           38 } channel[MIX_CHANNELS];
           39   
           40 static gboolean SoundOpen_SDL(void)
           41 {
           42   const int audio_rate = MIX_DEFAULT_FREQUENCY;
           43   const int audio_format = MIX_DEFAULT_FORMAT;
           44   const int audio_channels = 2;
           45   int i;
           46 
           47   if (SDL_Init(SDL_INIT_AUDIO) < 0) {
           48     return FALSE;
           49   }
           50 
           51   if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, 4096) < 0) {
           52     SDL_Quit();
           53     return FALSE;
           54   }
           55   Mix_AllocateChannels(MIX_CHANNELS);
           56 
           57   for (i = 0; i < MIX_CHANNELS; i++) {
           58     channel[i].chunk = NULL;
           59     channel[i].name = NULL;
           60   }
           61   return TRUE;
           62 }
           63 
           64 static void SoundClose_SDL(void)
           65 {
           66   int i;
           67 
           68   for (i = 0; i < MIX_CHANNELS; i++) {
           69     g_free(channel[i].name);
           70     if (channel[i].chunk) {
           71       Mix_FreeChunk(channel[i].chunk);
           72     }
           73   }
           74   Mix_CloseAudio();
           75   SDL_Quit();
           76 }
           77 
           78 static void SoundPlay_SDL(const gchar *snd)
           79 {
           80   int i, chan_num;
           81   Mix_Chunk *chunk;
           82 
           83   for (i = 0; i < MIX_CHANNELS; i++) {
           84     if (channel[i].name && strcmp(channel[i].name, snd) == 0) {
           85       Mix_PlayChannel(-1, channel[i].chunk, 0);
           86       return;
           87     }
           88   }
           89 
           90   chunk = Mix_LoadWAV(snd);
           91   if (!chunk) {
           92     return;
           93   }
           94 
           95   chan_num = Mix_PlayChannel(-1, chunk, 0);
           96   if (chan_num < 0) {
           97     Mix_FreeChunk(chunk);
           98     return;
           99   }
          100 
          101   if (channel[chan_num].chunk) {
          102     Mix_FreeChunk(channel[chan_num].chunk);
          103     g_free(channel[chan_num].name);
          104   }
          105 
          106   channel[chan_num].chunk = chunk;
          107   channel[chan_num].name = g_strdup(snd);
          108 }
          109 
          110 SoundDriver *sound_sdl_init(void)
          111 {
          112   static SoundDriver driver;
          113 
          114   driver.name = "sdl";
          115   driver.open = SoundOpen_SDL;
          116   driver.close = SoundClose_SDL;
          117   driver.play = SoundPlay_SDL;
          118   return &driver;
          119 }
          120 
          121 #endif /* HAVE_SDL_MIXER */