mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
added ability to load in sound
This commit is contained in:
parent
dfa6768a96
commit
d155753840
@ -1,4 +1,5 @@
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
@ -17,10 +18,16 @@ public:
|
||||
//texture management
|
||||
void addTexture(std::string id, const char* path);
|
||||
|
||||
// sound management
|
||||
void addSoundEffect(std::string id, const char* path);
|
||||
|
||||
SDL_Texture* getTexture(std::string id);
|
||||
Mix_Chunk* getSound(std::string id);
|
||||
|
||||
|
||||
private:
|
||||
|
||||
Manager* man;
|
||||
std::map<std::string, SDL_Texture*> textures;
|
||||
std::map<std::string, Mix_Chunk*> soundEffects;
|
||||
};
|
||||
|
||||
33
include/SoundManager.h
Normal file
33
include/SoundManager.h
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL_Mixer.h>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
|
||||
#include "TextureManager.h"
|
||||
|
||||
class SoundManager
|
||||
{
|
||||
public:
|
||||
static SoundManager& get()
|
||||
{
|
||||
static SoundManager instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
SoundManager() {}
|
||||
~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*, cmp_str> sound_cache;
|
||||
|
||||
Mix_Chunk* loadSound(const char* fileName);
|
||||
};
|
||||
@ -1,6 +1,7 @@
|
||||
#include "AssetManager.h"
|
||||
|
||||
#include "TextureManager.h"
|
||||
#include "SoundManager.h"
|
||||
#include "Components.h"
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
void AssetManager::addSoundEffect(std::string id, const char* path)
|
||||
{
|
||||
soundEffects.emplace(id, SoundManager::get().loadSound(path));
|
||||
}
|
||||
|
||||
SDL_Texture* AssetManager::getTexture(std::string 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) {
|
||||
|
||||
auto& projectile(man->addEntity());
|
||||
|
||||
@ -62,6 +62,12 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
|
||||
SDL_RenderCopy(renderer, backgroundTexture, NULL, NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
|
||||
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0)
|
||||
{
|
||||
std::cout << "ERROR: Mixer couldnt be initialized!" << std::endl;
|
||||
return;
|
||||
}
|
||||
|
||||
//SDL_Event event;
|
||||
bool hasQuit = false;
|
||||
|
||||
@ -113,6 +119,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("egg", "assets/egg.png");
|
||||
|
||||
// loading sounds
|
||||
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
|
||||
assets->addSoundEffect("steps", "assets/sound/steps.wav");
|
||||
|
||||
//ecs implementation
|
||||
|
||||
|
||||
19
src/SoundManager.cpp
Normal file
19
src/SoundManager.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
#include "SoundManager.h"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include "Game.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;
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user