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

added ability to play music

This commit is contained in:
ineslelin 2024-05-27 12:04:09 +02:00
parent db44d4d619
commit 70c4d3c030
7 changed files with 33 additions and 4 deletions

Binary file not shown.

View File

@ -36,12 +36,16 @@ public:
// sound management // sound management
void addSoundEffect(std::string id, const char* path); void addSoundEffect(std::string id, const char* path);
void addMusic(std::string id, const char* path);
SDL_Texture* getTexture(std::string id); SDL_Texture* getTexture(std::string id);
Mix_Chunk* getSound(std::string id); Mix_Chunk* getSound(std::string id);
Mix_Music* getMusic(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; std::map<std::string, Mix_Chunk*> soundEffects;
std::map<std::string, Mix_Music*> music;
}; };

View File

@ -38,6 +38,6 @@ class SoundManager
Mix_Chunk* loadSound(const char* fileName); Mix_Chunk* loadSound(const char* fileName);
static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops); static void playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops);
// static void playMusic(...); static void playMusic(Game* game, std::string sound, int loops);
private: private:
}; };

View File

@ -29,6 +29,11 @@ void AssetManager::addSoundEffect(std::string id, const char* path)
soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path)); soundEffects.emplace(id, this->man->getGame()->soundManager->loadSound(path));
} }
void AssetManager::addMusic(std::string id, const char* path)
{
music.emplace(id, this->man->getGame()->soundManager->loadMusic(path));
}
SDL_Texture* AssetManager::getTexture(std::string id) { SDL_Texture* AssetManager::getTexture(std::string id) {
return textures.at(id); return textures.at(id);
} }
@ -37,6 +42,11 @@ Mix_Chunk* AssetManager::getSound(std::string id) {
return soundEffects.at(id); return soundEffects.at(id);
} }
Mix_Music* AssetManager::getMusic(std::string id)
{
return music.at(id);
}
void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel) { void AssetManager::createProjectile(Vector2D pos, Vector2D velocity, int scale, int range, int speed, const char* texturePath, Entity::TeamLabel teamLabel) {
auto& projectile(man->addEntity()); auto& projectile(man->addEntity());

View File

@ -156,6 +156,9 @@ void Game::init(const char* title, int xpos, int ypos, int width, int height, bo
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav"); assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
assets->addSoundEffect("steps", "assets/sound/steps.wav"); assets->addSoundEffect("steps", "assets/sound/steps.wav");
// loading music
assets->addMusic("background_music", "assets/sound/background_music.mp3");
//ecs implementation //ecs implementation
player1.setTeam(Entity::TeamLabel::BLUE); player1.setTeam(Entity::TeamLabel::BLUE);

View File

@ -22,7 +22,10 @@ void KeyboardController::init()
} }
void KeyboardController::update() void KeyboardController::update()
{ {#
// TODO: move this, this is definitely the wrong place to put this but i wanted to put it somewhere to test it
SoundManager::playMusic(this->entity->getManager().getGame(), "background_music", PLAY_LOOPED);
transform->direction.x = 0; transform->direction.x = 0;
transform->direction.y = 0; transform->direction.y = 0;
sprite->playAnimation(IDLE); sprite->playAnimation(IDLE);

View File

@ -47,8 +47,6 @@ Mix_Chunk* SoundManager::loadSound(const char* fileName)
return sound; return sound;
} }
// void SoundManager::playMusic(...)
// TODO: using a string here is probably... a less than stellar method, figure out how to change this // TODO: using a string here is probably... a less than stellar method, figure out how to change this
void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops) void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound, bool canOverlap, int loops)
{ {
@ -64,6 +62,17 @@ void SoundManager::playSound(Game* game, /*SoundTypes sound*/ std::string sound,
} }
} }
void SoundManager::playMusic(Game* game, std::string music, int loops)
{
if (Mix_PlayingMusic() != 0)
return;
if (Mix_PlayMusic(game->assets->getMusic(music), loops) == -1)
{
std::cerr << "Error playing music '" << music << "': " << Mix_GetError() << std::endl;
}
}
// TODO: // TODO:
// functions to // functions to
// 1. free music and sound // 1. free music and sound