Click here to Skip to main content
15,896,201 members
Articles / Mobile Apps / Android

Diggers: cross-platform 2D game

Rate me:
Please Sign up or sign in to vote.
4.80/5 (17 votes)
5 Nov 2012CPOL4 min read 33.4K   1   43  
2D cross-platfrom game using SDL and Open GLES 2.0
#include "Sounds.h"

#include "JavaBridge.h"

#include <algorithm>



bool Sounds::m_NeedStartRain;
bool Sounds::m_NeedEndRain;
bool Sounds::m_NeedStartThunder;
int Sounds::m_ChannelRain;
int Sounds::m_ChannelThunder;



Sounds::Sounds(void)
{
	/* We're going to be requesting certain things from our audio
     device, so we set them up beforehand */
  int audio_rate = 22050;
  Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
  int audio_channels = 2;
  int audio_buffers = 4096;

  /* This is where we open up our audio device.  Mix_OpenAudio takes
     as its parameters the audio format we'd /like/ to have. */
  if (Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers))
  {
	  printf("Unable to open audio!\n");
	  exit(1);
  }

  m_MainTheme = Mix_LoadMUS("MainTheme.ogg");
  m_Rain = Mix_LoadWAV("Rain.ogg");
  m_Thunder = Mix_LoadWAV("Thunder.ogg");
  if ((!m_MainTheme) || (!m_Rain) || (!m_Thunder))
  {
	  printf("Load sound failed: %s\n", Mix_GetError());
	  // this might be a critical error...
  }
  m_NeedStartRain = false;
  m_NeedEndRain = false;
  m_NeedStartThunder = false;
};


void Sounds::MusicPlay(void)
{
	printf("Sounds::Play()\n");
	Mix_PlayMusic(m_MainTheme, -1);
	int curVol = (int)Mix_Volume(-1, -1);
	int musVol = (int)std::max(0.0f, (curVol)/4.0f);
	Mix_Volume( -1, curVol);
	Mix_VolumeMusic(musVol);
};


void Sounds::VolumeUp(void)
{
#ifdef WIN32
	int curVol = Mix_Volume(-1, -1);
	printf("Master Volume: %d\n", curVol);
	Mix_Volume( -1, curVol + 8);
	int musVol = (int)std::max(0.0f, (curVol - 8)/4.0f);
	Mix_VolumeMusic(musVol);
#endif
#ifdef ANDROID
	JavaBridge::SoundVolume(1.0f);
#endif
};


void Sounds::VolumeDown(void)
{
#ifdef WIN32
	int curVol = Mix_Volume(-1, -1);
	printf("Master Volume: %d\n", curVol);
	Mix_Volume( -1, curVol - 8);
	int musVol = (int)std::max(0.0f, (curVol - 8)/4.0f);
	Mix_VolumeMusic(musVol);
#endif
#ifdef ANDROID
	JavaBridge::SoundVolume(-1.0f);
#endif
};


void Sounds::StopAll(void)
{
	printf("Sound::Stop()\n");
	Mix_HaltChannel(-1);
	Mix_HaltMusic();
};


void Sounds::PauseAll(void)
{
	printf("Sounds::PauseAll()\n");
	Mix_PauseMusic();
	Mix_Pause(-1);
};


void Sounds::ResumeAll(void)
{
	printf("Sounds::ResumeAll()\n");
	Mix_ResumeMusic();
	Mix_Resume(-1);
};

Sounds::~Sounds(void)
{
	printf("Sounds::~Sounds()\n");
	Mix_CloseAudio();
};


void Sounds::PlayRain(void)
{
	printf("Sounds::RainPlay()\n");
	m_ChannelRain = Mix_PlayChannel(-1, m_Rain, -1);
/*
	int curVol = Mix_Volume(-1, -1);
	int musVol = std::max(0.0f, (curVol)/16.0f);
	Mix_Volume( -1, curVol);
	Mix_VolumeMusic(musVol);
*/
};


void Sounds::PlayThunder(void)
{
	printf("Sounds::PlayThunder()\n");
	m_ChannelThunder = Mix_PlayChannel(-1, m_Thunder, 0);
/*
	int curVol = Mix_Volume(-1, -1);
	int musVol = std::max(0.0f, (curVol)/16.0f);
	Mix_Volume( -1, curVol);
	Mix_VolumeMusic(musVol);
*/
};


void Sounds::ChannelFadeOut(int _channel)
{
	Mix_FadeOutChannel(_channel, 3000);
};

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions