mirror of
https://github.com/Nimac0/SDL_Minigame
synced 2026-01-12 15:53:42 +00:00
commit
30807aa9bf
BIN
assets/sound/steps.wav
Normal file
BIN
assets/sound/steps.wav
Normal file
Binary file not shown.
BIN
assets/sound/throw_egg.wav
Normal file
BIN
assets/sound/throw_egg.wav
Normal file
Binary file not shown.
@ -1,4 +1,5 @@
|
||||
#include <SDL_render.h>
|
||||
#include <SDL_mixer.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
@ -17,10 +18,15 @@ 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;
|
||||
};
|
||||
|
||||
@ -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();
|
||||
};
|
||||
|
||||
40
include/SoundManager.h
Normal file
40
include/SoundManager.h
Normal 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);
|
||||
};
|
||||
@ -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());
|
||||
|
||||
17
src/Game.cpp
17
src/Game.cpp
@ -42,6 +42,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)
|
||||
{
|
||||
@ -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_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;
|
||||
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("egg", "assets/egg.png");
|
||||
|
||||
// loading sounds
|
||||
assets->addSoundEffect("throw_egg", "assets/sound/throw_egg.wav");
|
||||
assets->addSoundEffect("steps", "assets/sound/steps.wav");
|
||||
|
||||
//ecs implementation
|
||||
|
||||
|
||||
@ -30,20 +30,24 @@ void KeyboardController::update()
|
||||
if (keystates[this->up]) {
|
||||
transform->velocity.y = -1;
|
||||
sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(STEPS);
|
||||
}
|
||||
if (keystates[this->left]) {
|
||||
transform->velocity.x = -1;
|
||||
sprite->playAnimation(WALK);
|
||||
sprite->setDirection(LEFT);
|
||||
SoundManager::playSound(STEPS);
|
||||
}
|
||||
if (keystates[this->down]) {
|
||||
transform->velocity.y = 1;
|
||||
sprite->playAnimation(WALK);
|
||||
SoundManager::playSound(STEPS);
|
||||
}
|
||||
if (keystates[this->right]) {
|
||||
transform->velocity.x = 1;
|
||||
sprite->playAnimation(WALK);
|
||||
sprite->setDirection(RIGHT);
|
||||
SoundManager::playSound(STEPS);
|
||||
}
|
||||
|
||||
if (keystates[this->fire]) {
|
||||
@ -69,6 +73,5 @@ void KeyboardController::update()
|
||||
|
||||
lastFireTime = currentTicks;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@ -5,6 +5,7 @@
|
||||
void ProjectileComponent::init()
|
||||
{
|
||||
transformComponent = &entity->getComponent<TransformComponent>();
|
||||
SoundManager::playSound(THROW_EGG);
|
||||
}
|
||||
|
||||
void ProjectileComponent::update()
|
||||
|
||||
50
src/SoundManager.cpp
Normal file
50
src/SoundManager.cpp
Normal 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;
|
||||
}
|
||||
}
|
||||
@ -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