0
0
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:
ineslelin 2024-01-29 20:53:35 +01:00
parent d155753840
commit 06a603b9d5
8 changed files with 52 additions and 6 deletions

View File

@ -24,7 +24,6 @@ public:
SDL_Texture* getTexture(std::string id); SDL_Texture* getTexture(std::string id);
Mix_Chunk* getSound(std::string id); Mix_Chunk* getSound(std::string id);
private: private:
Manager* man; Manager* man;

View File

@ -3,6 +3,7 @@
#include "Component.h" #include "Component.h"
#include "Vector2D.h" #include "Vector2D.h"
#include "SoundManager.h"
class TransformComponent; class TransformComponent;
class SpriteComponent; class SpriteComponent;
@ -35,4 +36,6 @@ private:
//for creation of projectiles //for creation of projectiles
TransformComponent* player; //for starting position of projectile TransformComponent* player; //for starting position of projectile
Vector2D fireVelocity; //decide source of projectile and flying direction Vector2D fireVelocity; //decide source of projectile and flying direction
// SoundManager* soundEffect = Game::assets->getSound;
//SoundManager* soundEffect = new SoundManager();
}; };

View File

@ -6,6 +6,12 @@
#include "TextureManager.h" #include "TextureManager.h"
enum SoundTypes
{
STEPS,
THROW_EGG,
};
class SoundManager class SoundManager
{ {
public: public:
@ -15,8 +21,8 @@ class SoundManager
return instance; return instance;
} }
private:
SoundManager() {} SoundManager() {}
private:
~SoundManager() { ~SoundManager() {
for (auto& it : this->sound_cache) { for (auto& it : this->sound_cache) {
Mix_FreeChunk(it.second); Mix_FreeChunk(it.second);
@ -27,7 +33,8 @@ class SoundManager
SoundManager(SoundManager const&) = delete; SoundManager(SoundManager const&) = delete;
void operator=(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); Mix_Chunk* loadSound(const char* fileName);
static void playSound(SoundTypes sound);
}; };

View File

@ -41,6 +41,11 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
return; 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); window = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
if (!window) if (!window)
{ {
@ -68,6 +73,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
return; return;
} }
Mix_Volume(-1, MIX_MAX_VOLUME);
Mix_AllocateChannels(16);
//SDL_Event event; //SDL_Event event;
bool hasQuit = false; bool hasQuit = false;

View File

@ -69,6 +69,5 @@ void KeyboardController::update()
lastFireTime = currentTicks; lastFireTime = currentTicks;
} }
} }
} }

View File

@ -5,6 +5,7 @@
void ProjectileComponent::init() void ProjectileComponent::init()
{ {
transformComponent = &entity->getComponent<TransformComponent>(); transformComponent = &entity->getComponent<TransformComponent>();
SoundManager::playSound(THROW_EGG);
} }
void ProjectileComponent::update() void ProjectileComponent::update()

View File

@ -2,18 +2,45 @@
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <iostream>
#include "Game.h" #include "Game.h"
#include "AssetManager.h"
Mix_Chunk* SoundManager::loadSound(const char* fileName) Mix_Chunk* SoundManager::loadSound(const char* fileName)
{ {
auto it = this->sound_cache.find(fileName); auto it = this->sound_cache.find(fileName);
if (it != this->sound_cache.end()) { if (it != this->sound_cache.end()) {
return it->second; return it->second;
} }
auto sound = Mix_LoadWAV(fileName); 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); this->sound_cache.emplace(fileName, sound);
printf("Loaded sound at '%s'\n", fileName); printf("Loaded sound at '%s'\n", fileName);
return sound; 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;
}
}

View File

@ -1,6 +1,8 @@
#include "TransformComponent.h" #include "TransformComponent.h"
#include "Constants.h" #include "Constants.h"
#include "SoundManager.h"
TransformComponent::TransformComponent() TransformComponent::TransformComponent()
{ {
position.zero(); position.zero();