Skip to content

Commit

Permalink
Merge pull request #52 from moggers87/25-volume-options
Browse files Browse the repository at this point in the history
Add volume controls to configuration file
  • Loading branch information
moggers87 committed Dec 14, 2023
2 parents e2969df + a6944e3 commit 209ca5b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 18 deletions.
3 changes: 3 additions & 0 deletions apricots/apricots.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ MISSION: 0
# The score that must be reached to win in missions 0 and 1
TARGET_SCORE: 1400

# Volume, 0 is muted and 100 is full volume
VOLUME: 100

# Plane types (1=Spitfire, 2=Jet, 3=Stealth Bomber)
PLANE1: 2
PLANE2: 2
Expand Down
3 changes: 2 additions & 1 deletion apricots/apricots.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ struct gamedata {
int targetscore;
int mission;
int winner;
float volume;
plane *player1;
plane *player2;
airbase base[7];
Expand Down Expand Up @@ -654,7 +655,7 @@ void gunshoot(guntype &, linkedlist<shottype> &, sampleio &, double[17], double[
void init_data(gamedata &);
void init_gameconstants(gamedata &);
void init_gamedata(gamedata &);
void init_sound(sampleio &);
void init_sound(gamedata &);
void keyboard(const Uint8 *, int &, int &, bool &, SDL_Scancode, SDL_Scancode, SDL_Scancode, SDL_Scancode,
SDL_Scancode);
void killbuilding(gamedata &, building &);
Expand Down
9 changes: 6 additions & 3 deletions apricots/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,15 @@ void load_shapes(gamedata &g, shape images[]) {

// Sound initialization

void init_sound(sampleio &sound) {
void init_sound(gamedata &game) {

char filenames[SOUNDS_COUNT][255];
for (int i = 0; i < SOUNDS_COUNT; i++) {
strcpy(filenames[i], AP_PATH);
strcat(filenames[i], SOUND_NAMES[i]);
}

sound.init(SOUNDS_COUNT, filenames, 2, 6);
game.sound.init(game.volume, SOUNDS_COUNT, filenames, 2, 6);
}

// Initialize the game constants
Expand Down Expand Up @@ -333,6 +333,9 @@ void init_gamedata(gamedata &g) {
// Targetscore for missions 0/1
g.targetscore = getConfig(config, "TARGET_SCORE", 1400, 100, 5000);

// Volume, 0 for mute and 100 for full volume
g.volume = getConfig(config, "VOLUME", 100, 0, 100) / 100.0;

// Planetypes: 1=Spitfire, 2=Jet, 3=Stealth Bomber
g.planeinfo[1].planetype = getConfig(config, "PLANE1", 1, 1, 3);
g.planeinfo[2].planetype = getConfig(config, "PLANE2", 1, 1, 3);
Expand Down Expand Up @@ -430,5 +433,5 @@ void init_data(gamedata &g) {

load_font(g.virtualscreen, g.whitefont, g.greenfont);

init_sound(g.sound);
init_sound(g);
}
17 changes: 4 additions & 13 deletions apricots/sampleio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
sampleio ::sampleio() { initdone = false; }

// Initialize OpenAL
void sampleio ::init(int nsamples, char filenames[][255], int nsources, int npool) {
void sampleio ::init(float the_volume, int nsamples, char filenames[][255], int nsources, int npool) {
if (initdone) {
cerr << "sampleio: call to init when already in use" << endl;
exit(1);
Expand All @@ -31,6 +31,7 @@ void sampleio ::init(int nsamples, char filenames[][255], int nsources, int npoo
samples = new ALuint[numsamples];
sources = new ALuint[numsources + numpool];
poolcount = numsources;
base_volume = the_volume;

// Initialize audio device
alureInitDevice(NULL, NULL);
Expand All @@ -46,32 +47,22 @@ void sampleio ::init(int nsamples, char filenames[][255], int nsources, int npoo
alListenerfv(AL_ORIENTATION, front);

// Load in samples
// ALvoid* data = malloc(5 * (512 * 3) * 1024);
alGenBuffers(numsamples, samples);

for (int i = 0; i < numsamples; i++) {
// ALsizei freq;
// ALboolean fileok;
// Evil OpenAL portability fix done here
#ifdef _WIN32
ALenum format;
ALboolean trash;
ALsizei filelen;
alureLoadWAVFile(filenames[i], &format, &data, &filelen, &freq, &trash);
fileok = (alGetError() == AL_NO_ERROR);
#else
// ALsizei format;
// ALsizei trash;
// ALsizei filelen;
// fileok = alureLoadWAV(filenames[i],&data,&format,&filelen,&trash,&freq);
samples[i] = alureCreateBufferFromFile(filenames[i]);
#endif
// if (!fileok){
if (samples[i] == AL_NONE) {
cerr << "sampleio: could not open " << filenames[i] << endl;
exit(1);
}
// alBufferData(samples[i], format, data, filelen, freq);
}

// Generate Sources
Expand All @@ -81,6 +72,7 @@ void sampleio ::init(int nsamples, char filenames[][255], int nsources, int npoo
alSourcefv(sources[j], AL_POSITION, position);
alSourcefv(sources[j], AL_VELOCITY, zeroes);
alSourcefv(sources[j], AL_ORIENTATION, back);
alSourcef(sources[j], AL_GAIN, ALfloat(base_volume));
}

// free(data);
Expand Down Expand Up @@ -212,8 +204,7 @@ void sampleio ::volume(int i, double vol) {
cerr << "sampleio: attempt to volume nonexistant source " << i << endl;
return;
}
ALfloat volf = ALfloat(vol);
alSourcef(sources[i], AL_GAIN, volf);
alSourcef(sources[i], AL_GAIN, ALfloat(vol * base_volume));
}

// Check on playing sources function
Expand Down
3 changes: 2 additions & 1 deletion apricots/sampleio.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ class sampleio {
int poolcount;
void psource(int source, int sample, bool loop);
ALboolean sourceisplaying(ALuint);
float base_volume;

public:
sampleio();
void init(int, char[][255], int, int);
void init(float, int, char[][255], int, int);
void close();
void update();
void channel(int, int);
Expand Down

0 comments on commit 209ca5b

Please sign in to comment.