mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 10:13:42 +00:00
throwing projectile makes sound
This commit is contained in:
parent
d155753840
commit
06a603b9d5
@ -24,7 +24,6 @@ public:
|
||||
SDL_Texture* getTexture(std::string id);
|
||||
Mix_Chunk* getSound(std::string id);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Manager* man;
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include "Component.h"
|
||||
#include "Vector2D.h"
|
||||
#include "SoundManager.h"
|
||||
|
||||
class TransformComponent;
|
||||
class SpriteComponent;
|
||||
@ -35,4 +36,6 @@ private:
|
||||
//for creation of projectiles
|
||||
TransformComponent* player; //for starting position of projectile
|
||||
Vector2D fireVelocity; //decide source of projectile and flying direction
|
||||
// SoundManager* soundEffect = Game::assets->getSound;
|
||||
//SoundManager* soundEffect = new SoundManager();
|
||||
};
|
||||
|
||||
@ -6,6 +6,12 @@
|
||||
|
||||
#include "TextureManager.h"
|
||||
|
||||
enum SoundTypes
|
||||
{
|
||||
STEPS,
|
||||
THROW_EGG,
|
||||
};
|
||||
|
||||
class SoundManager
|
||||
{
|
||||
public:
|
||||
@ -15,8 +21,8 @@ class SoundManager
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
SoundManager() {}
|
||||
private:
|
||||
~SoundManager() {
|
||||
for (auto& it : this->sound_cache) {
|
||||
Mix_FreeChunk(it.second);
|
||||
@ -27,7 +33,8 @@ class SoundManager
|
||||
SoundManager(SoundManager const&) = delete;
|
||||
void operator=(SoundManager const&) = delete;
|
||||
|
||||
std::map<const char*, Mix_Chunk*, cmp_str> sound_cache;
|
||||
std::map<const char*, Mix_Chunk*> sound_cache;
|
||||
|
||||
Mix_Chunk* loadSound(const char* fileName);
|
||||
static void playSound(SoundTypes sound);
|
||||
};
|
||||
@ -41,6 +41,11 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
return;
|
||||
}
|
||||
|
||||
if (Mix_Init(MIX_INIT_MP3) != MIX_INIT_MP3) {
|
||||
std::cout << "ERROR. Subsystem couldnt be initialized!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
|
||||
if (!window)
|
||||
{
|
||||
@ -68,6 +73,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
return;
|
||||
}
|
||||
|
||||
Mix_Volume(-1, MIX_MAX_VOLUME);
|
||||
Mix_AllocateChannels(16);
|
||||
|
||||
//SDL_Event event;
|
||||
bool hasQuit = false;
|
||||
|
||||
|
||||
@ -69,6 +69,5 @@ void KeyboardController::update()
|
||||
|
||||
lastFireTime = currentTicks;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,7 @@
|
||||
void ProjectileComponent::init()
|
||||
{
|
||||
transformComponent = &entity->getComponent<TransformComponent>();
|
||||
SoundManager::playSound(THROW_EGG);
|
||||
}
|
||||
|
||||
void ProjectileComponent::update()
|
||||
|
||||
@ -2,18 +2,45 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
#include "Game.h"
|
||||
#include "AssetManager.h"
|
||||
|
||||
Mix_Chunk* SoundManager::loadSound(const char* fileName)
|
||||
{
|
||||
auto it = this->sound_cache.find(fileName);
|
||||
|
||||
if (it != this->sound_cache.end()) {
|
||||
return it->second;
|
||||
}
|
||||
|
||||
auto sound = Mix_LoadWAV(fileName);
|
||||
if (sound == NULL) throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'");
|
||||
|
||||
if (sound == NULL)
|
||||
throw std::runtime_error(std::string("Couldn't load sound '") + fileName + "'");
|
||||
|
||||
this->sound_cache.emplace(fileName, sound);
|
||||
|
||||
printf("Loaded sound at '%s'\n", fileName);
|
||||
|
||||
return sound;
|
||||
}
|
||||
}
|
||||
|
||||
void SoundManager::playSound(SoundTypes sound)
|
||||
{
|
||||
switch (sound)
|
||||
{
|
||||
case SoundTypes::STEPS:
|
||||
if (Mix_PlayChannel(-1, Game::assets->getSound("steps"), 0) == -1) {
|
||||
std::cerr << "Error playing sound 'steps': " << Mix_GetError() << std::endl;
|
||||
}
|
||||
break;
|
||||
|
||||
case SoundTypes::THROW_EGG:
|
||||
if (Mix_PlayChannel(-1, Game::assets->getSound("throw_egg"), 0) == -1) {
|
||||
std::cerr << "Error playing sound 'throw_egg': " << Mix_GetError() << std::endl;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
#include "TransformComponent.h"
|
||||
#include "Constants.h"
|
||||
|
||||
#include "SoundManager.h"
|
||||
|
||||
TransformComponent::TransformComponent()
|
||||
{
|
||||
position.zero();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user