TapestryEngine

A 2D Platformer Game Engine
Log | Files | Refs

e0344d48098f78f9fe7751ff035345396c4b0b95.svn-base (3931B)


      1 #include "sound.h"
      2 #include "Actor.h"
      3 
      4 Mix_Music* LoadMusic(char* filename)
      5 {
      6 #ifdef DEBUG
      7 	char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\sounds\\";
      8 #endif
      9 
     10 #ifdef RELEASE
     11 	char* basepath = ".\\sounds\\";
     12 #endif
     13 
     14 	char pathbuffer[1024];
     15 	strcpy(pathbuffer, basepath);
     16 
     17 	Mix_Music* sound = Mix_LoadMUS(strcat(pathbuffer, filename));
     18 	if (sound == NULL)
     19 	{
     20 		gCons->ConsPrintf("Mix_Music Error: %s", Mix_GetError());
     21 		return NULL;
     22 	}
     23 	return sound;
     24 }
     25 
     26 Mix_Chunk* LoadSound(char* filename)
     27 {
     28 #ifdef DEBUG
     29 	char* basepath = "C:\\Users\\baptistac1\\Documents\\Visual Studio 2015\\Projects\\TapestryEngineDev\\TapestryEngine\\sounds\\";
     30 #endif
     31 
     32 #ifdef RELEASE
     33 	char* basepath = ".\\sounds\\";
     34 #endif
     35 
     36 	char pathbuffer[1024];
     37 	strcpy(pathbuffer, basepath);
     38 
     39 	Mix_Chunk* sound = Mix_LoadWAV(strcat(pathbuffer, filename));
     40 	if (sound == NULL)
     41 	{
     42 		char s[1024];
     43 		strcpy(s, Mix_GetError());
     44 		gCons->ConsPrintf("Mix_Music Error: %s", Mix_GetError());
     45 		return NULL;
     46 	}
     47 	return sound;
     48 }
     49 
     50 bool ChannelManager::PlaySound(int channel, Mix_Chunk* snd, int distance)
     51 {
     52 	if (distance = -1)
     53 	{
     54 		Mix_PlayChannel(channel, snd, 0);
     55 		Mix_SetDistance(channel, 0);
     56 		return true;
     57 	}
     58 	
     59 	//gCons->ConsPrintf("Distance from camera: %i\n", distance);
     60 	if (distance >= MIN_VOL_RANGE) //If the sound is out of hearing range
     61 	{
     62 		//gCons->ConsPrintf("Sound out of range\n\n");
     63 		return false;
     64 	}
     65 	else
     66 	{
     67 		Mix_PlayChannel(channel, snd, 0);
     68 		if (distance <= MAX_VOL_RANGE) //if the sound is within range to play for no attenuation
     69 		{
     70 			//gCons->ConsPrintf("Sound within maximum volume range\n\n");
     71 			//Mix_SetDistance(channel, 0);
     72 		}
     73 		else //between min and max volume ranges
     74 		{
     75 			int fade = int(((float)(distance - MAX_VOL_RANGE) / FADE_DISTANCE) * 255.0);
     76 
     77 			//gCons->ConsPrintf("Attenuation value: %i\n\n", fade);
     78 			Mix_SetDistance(channel, fade);
     79 		}
     80 	}
     81 	return true;
     82 }
     83 
     84 bool ChannelManager::PlayLoop(int channel, Mix_Chunk* snd, int distance)
     85 {
     86 	if (distance = -1)
     87 	{
     88 		Mix_PlayChannel(channel, snd, -1);
     89 		Mix_SetDistance(channel, 0);
     90 		return true;
     91 	}
     92 
     93 	//gCons->ConsPrintf("Distance from camera: %i\n", distance);
     94 	if (distance >= MIN_VOL_RANGE) //If the sound is out of hearing range
     95 	{
     96 		//gCons->ConsPrintf("Sound out of range\n\n");
     97 		return false;
     98 	}
     99 	else
    100 	{
    101 		Mix_PlayChannel(channel, snd, -1);
    102 		if (distance <= MAX_VOL_RANGE) //if the sound is within range to play for no attenuation
    103 		{
    104 			//gCons->ConsPrintf("Sound within maximum volume range\n\n");
    105 			//Mix_SetDistance(channel, 0);
    106 		}
    107 		else //between min and max volume ranges
    108 		{
    109 			int fade = int(((float)(distance - MAX_VOL_RANGE) / FADE_DISTANCE) * 255.0);
    110 
    111 			//gCons->ConsPrintf("Attenuation value: %i\n\n", fade);
    112 			Mix_SetDistance(channel, fade);
    113 		}
    114 	}
    115 	return true;
    116 }
    117 
    118 //Sound Manager Functions
    119 bool SoundManager::UpdatePointSounds(int listen_x, int listen_y)
    120 {
    121 	for (int i = 0; i < (int)mEffects.size(); i++)
    122 	{
    123 		mEffects.at(i)->PlayPointSound(listen_x, listen_y);
    124 	}
    125 	return true;
    126 }
    127 
    128 bool SoundManager::UpdateRegionSounds(SDL_Rect* pos)
    129 {
    130 	for (int i = 0; i < (int)mRegions.size(); i++)
    131 	{
    132 		mRegions.at(i)->PlayRegionSound(pos);
    133 	}
    134 	return true;
    135 }
    136 
    137 bool SoundManager::EventProcess(Event eve)
    138 {
    139 	switch (*eve.GetEventType())
    140 	{
    141 	case CREATE_POINT_SOUND:
    142 		SpawnPointSound(eve.GetEventData()->psd.x, eve.GetEventData()->psd.y, eve.GetEventData()->psd.name);
    143 		return true;
    144 		break;
    145 	case ACTOR_PLAY_SOUND:
    146 		return true;
    147 		break;
    148 	case SPAWN:
    149 	{
    150 		int* channel = (int*)eve.GetEventData()->p;
    151 		*channel = mChannelManager.AssignChannel();
    152 		return true;
    153 		break;
    154 	}
    155 	case DEATH:
    156 		mChannelManager.FreeChannel(((Actor*)(eve.GetEventData()->p))->GetChannel());
    157 		return true;
    158 		break;
    159 	case SOUND_COMPLETE:
    160 		ClearPointSound((PointSound*)eve.GetEventData()->p);
    161 		return true;
    162 		break;
    163 	default:
    164 		gCons->ConsPrintf("Sound Manager recieved unregcognized even\n");
    165 		return false;
    166 		break;
    167 	}
    168 }