0
0
mirror of https://github.com/Nimac0/SDL_Minigame synced 2026-01-12 13:43:41 +00:00

Merge pull request #20 from Nimac0/sound-effects

Sound effects
This commit is contained in:
Nanogamer7 2024-01-30 00:06:55 +01:00 committed by GitHub
commit 30807aa9bf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 133 additions and 1 deletions

BIN
assets/sound/steps.wav Normal file

Binary file not shown.

BIN
assets/sound/throw_egg.wav Normal file

Binary file not shown.

View File

@ -1,4 +1,5 @@
#include <SDL_render.h> #include <SDL_render.h>
#include <SDL_mixer.h>
#include <map> #include <map>
#include <string> #include <string>
@ -17,10 +18,15 @@ public:
//texture management //texture management
void addTexture(std::string id, const char* path); void addTexture(std::string id, const char* path);
// sound management
void addSoundEffect(std::string id, const char* path);
SDL_Texture* getTexture(std::string id); SDL_Texture* getTexture(std::string id);
Mix_Chunk* getSound(std::string id);
private: private:
Manager* man; Manager* man;
std::map<std::string, SDL_Texture*> textures; std::map<std::string, SDL_Texture*> textures;
std::map<std::string, Mix_Chunk*> soundEffects;
}; };

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();
}; };

40
include/SoundManager.h Normal file
View File

@ -0,0 +1,40 @@
#pragma once
#include <SDL_Mixer.h>
#include <map>
#include <vector>
#include "TextureManager.h"
enum SoundTypes
{
STEPS,
THROW_EGG,
};
class SoundManager
{
public:
static SoundManager& get()
{
static SoundManager instance;
return instance;
}
SoundManager() {}
private:
~SoundManager() {
for (auto& it : this->sound_cache) {
Mix_FreeChunk(it.second);
}
}
public:
SoundManager(SoundManager const&) = delete;
void operator=(SoundManager const&) = delete;
std::map<const char*, Mix_Chunk*> sound_cache;
Mix_Chunk* loadSound(const char* fileName);
static void playSound(SoundTypes sound);
};

View File

@ -1,6 +1,7 @@
#include "AssetManager.h" #include "AssetManager.h"
#include "TextureManager.h" #include "TextureManager.h"
#include "SoundManager.h"
#include "Components.h" #include "Components.h"
AssetManager::AssetManager(Manager* manager) : man(manager) {} AssetManager::AssetManager(Manager* manager) : man(manager) {}
@ -11,10 +12,19 @@ void AssetManager::addTexture(std::string id, const char* path) {
textures.emplace(id, TextureManager::get().loadTexture(path)); textures.emplace(id, TextureManager::get().loadTexture(path));
} }
void AssetManager::addSoundEffect(std::string id, const char* path)
{
soundEffects.emplace(id, SoundManager::get().loadSound(path));
}
SDL_Texture* AssetManager::getTexture(std::string id) { SDL_Texture* AssetManager::getTexture(std::string id) {
return textures.at(id); return textures.at(id);
} }
Mix_Chunk* AssetManager::getSound(std::string id) {
return soundEffects.at(id);
}
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) { void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, bool source, int scale, int range, int speed, const char* texturePath) {
auto& projectile(man->addEntity()); auto& projectile(man->addEntity());

View File

@ -42,6 +42,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)
{ {
@ -63,6 +68,15 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL); SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
SDL_RenderPresent(renderer); SDL_RenderPresent(renderer);
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
{
std::cout << "ERROR: Mixer couldnt be initialized!" << std::endl;
return;
}
Mix_Volume(-1, MIX_MAX_VOLUME);
Mix_AllocateChannels(16);
//SDL_Event event; //SDL_Event event;
bool hasQuit = false; bool hasQuit = false;
@ -114,6 +128,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
assets->addTexture("player2", "assets/chicken_neutral.png"); assets->addTexture("player2", "assets/chicken_neutral.png");
assets->addTexture("egg", "assets/egg.png"); assets->addTexture("egg", "assets/egg.png");
// loading sounds
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
assets->addSoundEffect("steps", "assets/sound/steps.wav");
//ecs implementation //ecs implementation

View File

@ -30,20 +30,24 @@ void KeyboardController::update()
if (keystates[this->up]) { if (keystates[this->up]) {
transform->velocity.y = -1; transform->velocity.y = -1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
SoundManager::playSound(STEPS);
} }
if (keystates[this->left]) { if (keystates[this->left]) {
transform->velocity.x = -1; transform->velocity.x = -1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
sprite->setDirection(LEFT); sprite->setDirection(LEFT);
SoundManager::playSound(STEPS);
} }
if (keystates[this->down]) { if (keystates[this->down]) {
transform->velocity.y = 1; transform->velocity.y = 1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
SoundManager::playSound(STEPS);
} }
if (keystates[this->right]) { if (keystates[this->right]) {
transform->velocity.x = 1; transform->velocity.x = 1;
sprite->playAnimation(WALK); sprite->playAnimation(WALK);
sprite->setDirection(RIGHT); sprite->setDirection(RIGHT);
SoundManager::playSound(STEPS);
} }
if (keystates[this->fire]) { if (keystates[this->fire]) {
@ -69,6 +73,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()

50
src/SoundManager.cpp Normal file
View File

@ -0,0 +1,50 @@
#include "SoundManager.h"
#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 + "'");
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_Playing(-1) != 0)
break;
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();