File: sound_sdl.c

package info (click to toggle)
gltron 0.61-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 3,948 kB
  • ctags: 1,175
  • sloc: ansic: 7,580; perl: 168; makefile: 149; sh: 15
file content (145 lines) | stat: -rw-r--r-- 2,618 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "sound.h"
#include "gltron.h"

/* linux only, at the moment */

static Mix_Music *music;
static int sound_open = 0;
#define SOUND_IS_OK if(!sound_open) return;
/*
#define NUM_GAME_FX 5
*/
#define NUM_GAME_FX 2
#define NUM_MENU_FX 2

Mix_Chunk *game_fx[NUM_GAME_FX];
static Mix_Chunk *menu_fx[NUM_MENU_FX];

static char *game_fx_names[] = {
  "game_engine.wav",
  "game_crash.wav",

  "game_start.wav",
  "game_win.wav",
  "game_lose.wav"
};

static char *menu_fx_names[] = {
  "menu_action.wav",
  "menu_highlight.wav"
};

void loadFX() {
  int i;
  char *path;

  SOUND_IS_OK;

  for(i = 0; i < NUM_GAME_FX; i++) {
    path = getFullPath(game_fx_names[i]);
    if(path) {
      game_fx[i] = Mix_LoadWAV(path);
      free(path);
    } else {
      printf("error: can't load sound file %s\n",
	     game_fx_names[i]);
      exit(1);
    }
  }

  for(i = 0; i < NUM_MENU_FX; i++) {
    path = getFullPath(menu_fx_names[i]);
    if(path) {
      menu_fx[i] = Mix_LoadWAV(path);
      free(path);
    }
  }
}
 
int initSound() {
  /* open the audio device */
  if(Mix_OpenAudio(22050, AUDIO_S16SYS, 2, 1024) < 0) {
    fprintf(stderr, "can't open audio: %s\n", SDL_GetError());
    return 1;
  }
  sound_open = 1;

  loadFX();
  return 0;
}


void shutdownSound() {
  SOUND_IS_OK;
  stopSound();
  Mix_CloseAudio();
}
  

void loadSound(char *name) {
  SOUND_IS_OK;
  music = Mix_LoadMUS(name);
  return;
}

void playSound() {
  SOUND_IS_OK;
  if( ! Mix_PlayingMusic() )
    Mix_PlayMusic(music, -1);
  /* todo: remove the following once the bug in SDL_mixer is fixed */
  /* we don't want too many references to game objects here */
  setMusicVolume(game->settings->musicVolume);
  return;
}

void stopSound() {
  SOUND_IS_OK;
  if( Mix_PlayingMusic() )
    Mix_HaltMusic();
  return;
}

void soundIdle() {
  /* sdl_mixer uses pthreads, so no work here */
  return;
}

int getFreeChannel() {
  int i;
  for(i = 0; i < MIX_CHANNELS; i++) {
    if(!Mix_Playing(i)) {
      return i;
    }
  }
  return -1;
}

void playGameFX(int fx, float volume) {
  int channel;
  SOUND_IS_OK;
  if((channel = getFreeChannel()) >= 0) {
       Mix_Volume(channel, volume * MIX_MAX_VOLUME);
       Mix_PlayChannel(channel, game_fx[fx], 0);
  }
}

void playMenuFX(int fx) {
  SOUND_IS_OK;
  Mix_PlayChannel(-1, menu_fx[fx], 0);
}

void setMusicVolume(float volume) {
  SOUND_IS_OK;
  if(volume > 1) volume = 1;
  if(volume < 0) volume = 0;

  Mix_VolumeMusic((int)(volume * 128));
}

void setFxVolume(float volume) {
  SOUND_IS_OK;
  if(volume > 1) volume = 1;
  if(volume < 0) volume = 0;

  Mix_Volume(-1, (int)(volume * 128));
}