Skip to content

Commit

Permalink
Menu music. and better sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
Matqyou committed Dec 8, 2024
1 parent 0a303ae commit 71bbc3d
Show file tree
Hide file tree
Showing 58 changed files with 212 additions and 49 deletions.
Binary file added bin/assets/music/elevator.wav
Binary file not shown.
Binary file added bin/assets/sounds/entity/crate/broken/1.wav
Binary file not shown.
Binary file added bin/assets/sounds/entity/crate/broken/2.wav
Binary file not shown.
Binary file added bin/assets/sounds/entity/crate/broken/3.wav
Binary file not shown.
Binary file removed bin/assets/sounds/entity/crate/death.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/1.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/10.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/11.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/12.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/13.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/14.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/15.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/16.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/17.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/18.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/2.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/3.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/4.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/5.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/6.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/7.wav
Binary file not shown.
Binary file added bin/assets/sounds/glitch/8.wav
Binary file not shown.
Binary file added bin/assets/sounds/unused/phone.wav
Binary file not shown.
Binary file added bin/assets/sounds/unused/reveal.wav
Binary file not shown.
Binary file added bin/assets/sounds/unused/womp.wav
Binary file not shown.
File renamed without changes.
Binary file added bin/assets/sounds/weapon/default/reload.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/glock/reload.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/glock/shoot2.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/minigun/reload.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/shotgun/reload.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/shotgun/shoot2.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/shotgun/shoot3.wav
Binary file not shown.
Binary file added bin/assets/sounds/weapon/sniper/reload.wav
Binary file not shown.
1 change: 1 addition & 0 deletions src/GameReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ void GameReference::Deinitialize(bool keep_sound) {
std::cout << FStringColors(
"&8---------------------------- &fDeinitializing(keep_sound = %s) &8----------------------------",
keep_sound ? "true" : "false") << std::endl;
Assets::PauseMusic();
delete m_GameWorld;
m_GameWorld = nullptr;
delete m_Controllers;
Expand Down
132 changes: 112 additions & 20 deletions src/client/Assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@
#include "../technical stuff/TextManager.h"

Assets* Assets::Instance = nullptr;
std::vector<LoadedTexture*> Assets::m_RegisterTextures = {};
std::vector<LoadedSound*> Assets::m_RegisterSounds = {};
std::vector<LoadedTexture*> Assets::m_RegisterTextures = { };
std::vector<LoadedSound*> Assets::m_RegisterSounds = { };
std::vector<LoadedMusic*> Assets::m_RegisterMusic = { };

Texture::Texture(std::string key, SDL_Texture* sdl_texture, std::string load_extension)
: m_Key(std::move(key)),
m_LoadExtension(std::move(load_extension)) {
: m_Key(std::move(key)),
m_LoadExtension(std::move(load_extension)) {
m_SDLTexture = sdl_texture;

m_Information = {};
m_Information = { };
SDL_QueryTexture(m_SDLTexture,
&m_Information.format,
&m_Information.access,
Expand All @@ -46,8 +47,8 @@ void Texture::SetAlphaMod(int alpha) {
}

Sound::Sound(std::string key, Mix_Chunk* mix_chunk, std::string load_extension)
: m_Key(std::move(key)),
m_LoadExtension(std::move(load_extension)) {
: m_Key(std::move(key)),
m_LoadExtension(std::move(load_extension)) {
m_MixChunk = mix_chunk;
}

Expand All @@ -60,16 +61,33 @@ void Sound::SetVolume(int volume) {
Mix_VolumeChunk(m_MixChunk, volume);
}

void Sound::PlaySound()
{
void Sound::PlaySound() {
if (m_MixChunk == nullptr || !Assets::Get()->SoundsEnabled())
return;

Mix_PlayChannel(-1, m_MixChunk, 0);
}

Music::Music(std::string key, Mix_Music* mix_music, std::string load_extension)
: m_Key(std::move(key)),
m_LoadExtension(std::move(load_extension)) {
m_MixMusic = mix_music;
}

Music::~Music() {
if (m_MixMusic)
Mix_FreeMusic(m_MixMusic);
}

void Music::PlayMusic(int loops) {
if (m_MixMusic == nullptr || !Assets::Get()->SoundsEnabled())
return;

Mix_PlayMusic(m_MixMusic, loops);
}

std::vector<std::tuple<std::string, std::string, std::string>> GetResourceKeys(const char* directory,
const std::unordered_set<std::string>& supported_extensions) {
const std::unordered_set<std::string>& supported_extensions) {
std::vector<std::tuple<std::string, std::string, std::string>> results;

for (const auto& entry : std::filesystem::recursive_directory_iterator(directory)) {
Expand Down Expand Up @@ -109,7 +127,7 @@ std::vector<std::tuple<std::string, std::string, std::string>> GetResourceKeys(c
}

Assets::Assets(SDL_Renderer* renderer, bool sounds_enabled)
: m_InvalidTexture(nullptr) {
: m_InvalidTexture(nullptr) {
m_Renderer = renderer;
m_SoundsEnabled = sounds_enabled;

Expand Down Expand Up @@ -185,6 +203,34 @@ Assets::Assets(SDL_Renderer* renderer, bool sounds_enabled)
}
std::cout << FStringColors("[Assets] &5Loaded %i sounds", m_Sounds.size()) << std::endl;

auto music_keys = GetResourceKeys(R"(.\assets\music\)", sound_extensions);
for (auto entry : music_keys) {
std::string& key = std::get<0>(entry);
std::string& file_path = std::get<1>(entry);
std::string& extension = std::get<2>(entry);

auto it = m_Music.find(key);
if (it != m_Music.end()) {
std::cout << FStringColors("[Assets] &8Duplicate music '%s' for existing '%s'(%s)",
extension.c_str(),
key.c_str(),
it->second->m_LoadExtension.c_str()) << std::endl;
continue;
}

// Load the sound
Mix_Music* NewMixMusic = Mix_LoadMUS(file_path.c_str());
if (!NewMixMusic) {
std::cout << FStringColors("[Assets] &cFailed to load music '%s'", file_path.c_str()) << std::endl;
std::cout << FStringColors("[Assets] &cReason (%s)", SDL_GetError()) << std::endl;
continue;
}

// Add it to all the textures
auto new_music = new Music(key, NewMixMusic, extension);
m_Music[key] = new_music;
}
std::cout << FStringColors("[Assets] &5Loaded %i music", m_Music.size()) << std::endl;

// LINK REQUIRED DEPENDENCIES
// Textures
Expand All @@ -204,12 +250,21 @@ Assets::Assets(SDL_Renderer* renderer, bool sounds_enabled)
}
std::cout << FStringColors("[Assets] &5Linked %zu sounds", m_RegisterSounds.size()) << std::endl;
m_RegisterSounds.clear();

// Music
for (auto required_music : m_RegisterMusic) {
const std::string& music_key = required_music->Key();

required_music->m_Music = GetMusic(music_key);
}
std::cout << FStringColors("[Assets] &5Linked %zu music", m_RegisterMusic.size()) << std::endl;
m_RegisterMusic.clear();
}

Assets::~Assets()
{
Assets::~Assets() {
size_t destroyed_textures = 0;
size_t destroyed_sounds = 0;
size_t destroyed_music = 0;
for (const auto& entry : m_Textures) {
delete entry.second;
destroyed_textures++;
Expand All @@ -218,24 +273,26 @@ Assets::~Assets()
delete entry.second;
destroyed_sounds++;
}
for (const auto& entry : m_Music) {
delete entry.second;
destroyed_music++;
}
std::cout << FStringColors("[Assets] &5Unloaded %zu textures", destroyed_textures) << std::endl;
std::cout << FStringColors("[Assets] &5Unloaded %zu sounds", destroyed_sounds) << std::endl;
std::cout << FStringColors("[Assets] &5Unloaded %zu music", destroyed_music) << std::endl;
}

void Assets::initialize(SDL_Renderer* renderer, bool sounds_enabled)
{
void Assets::initialize(SDL_Renderer* renderer, bool sounds_enabled) {
if (Instance == nullptr)
Instance = new Assets(renderer, sounds_enabled);
}

void Assets::deinitialize()
{
void Assets::deinitialize() {
delete Instance;
Instance = nullptr;
}

Assets* Assets::Get()
{
Assets* Assets::Get() {
if (Instance == nullptr)
throw std::runtime_error("Assets not initialized. Call initialize() first.");

Expand All @@ -260,6 +317,15 @@ Sound* Assets::GetSound(const std::string& sound_key) {
return nullptr;
}

Music* Assets::GetMusic(const std::string& music_key) {
auto it = m_Music.find(music_key);
if (it != m_Music.end())
return it->second;

std::cout << FStringColors("[Assets] &8Music '%s' not found", music_key.c_str()) << std::endl;
return nullptr;
}

Texture* Assets::TextureFromSurface(SDL_Surface* sdl_surface) {
SDL_Texture* NewSDLTexture = SDL_CreateTextureFromSurface(m_Renderer, sdl_surface);

Expand All @@ -280,6 +346,18 @@ void Assets::RequireSound(LoadedSound* register_sound) {
m_RegisterSounds.push_back(register_sound);
}

void Assets::RequireMusic(LoadedMusic* register_music) {
m_RegisterMusic.push_back(register_music);
}

void Assets::SetMusicVolume(int volume) {
Mix_VolumeMusic(volume);
}

void Assets::PauseMusic() {
Mix_PauseMusic();
}

LoadedTexture::LoadedTexture(std::string texture_key)
: m_Key(std::move(texture_key)) {
m_Texture = nullptr;
Expand All @@ -288,7 +366,7 @@ LoadedTexture::LoadedTexture(std::string texture_key)
}

LoadedSound::LoadedSound(std::string sound_key)
: m_Key(std::move(sound_key)) {
: m_Key(std::move(sound_key)) {
m_Sound = nullptr;

Assets::RequireSound(this);
Expand All @@ -299,4 +377,18 @@ Sound* LoadedSound::GetSound() const {
throw std::runtime_error(FString("[Sound] GetSound '%s' was nullptr", m_Key.c_str()));

return m_Sound;
}

LoadedMusic::LoadedMusic(std::string music_key)
: m_Key(std::move(music_key)) {
m_Music = nullptr;

Assets::RequireMusic(this);
}

Music* LoadedMusic::GetMusic() const {
if (m_Music == nullptr)
throw std::runtime_error(FString("[Sound] GetMusic '%s' was nullptr", m_Key.c_str()));

return m_Music;
}
42 changes: 42 additions & 0 deletions src/client/Assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,42 @@ class Sound {
void PlaySound();
};

class Music {
private:
friend class Assets;
const std::string m_Key;
Mix_Music* m_MixMusic;
std::string m_LoadExtension;

public:
explicit Music(std::string key = "NaN", Mix_Music* mix_music = nullptr, std::string load_extension = "NaN");
~Music();

// Getting
[[nodiscard]] Mix_Music* MixMusic() const { return m_MixMusic; }

// Manipulating
void PlayMusic(int loops);

};

class LoadedTexture;
class LoadedSound;
class LoadedMusic;
class Assets {
static Assets* Instance;
SDL_Renderer* m_Renderer;
bool m_SoundsEnabled;

std::unordered_map<std::string, Texture*> m_Textures;
std::unordered_map<std::string, Sound*> m_Sounds;
std::unordered_map<std::string, Music*> m_Music;
// std::vector<Texture2> m_UsedTextures;
Texture* m_InvalidTexture;

static std::vector<LoadedTexture*> m_RegisterTextures;
static std::vector<LoadedSound*> m_RegisterSounds;
static std::vector<LoadedMusic*> m_RegisterMusic;

public:
static void initialize(SDL_Renderer* renderer, bool sounds_enabled);
Expand All @@ -89,6 +112,7 @@ class Assets {
// Getting
Texture* GetTexture(const std::string& texture_key);
Sound* GetSound(const std::string& sound_key);
Music* GetMusic(const std::string& music_key);
bool SoundsEnabled() const { return m_SoundsEnabled; }

// Generating
Expand All @@ -98,6 +122,9 @@ class Assets {
// Manipulating
static void RequireTexture(LoadedTexture* register_texture);
static void RequireSound(LoadedSound* register_sound);
static void RequireMusic(LoadedMusic* register_music);
static void SetMusicVolume(int volume);
static void PauseMusic();

private:
Assets(SDL_Renderer* renderer, bool sounds_enabled);
Expand Down Expand Up @@ -133,4 +160,19 @@ class LoadedSound {
[[nodiscard]] const std::string& Key() const { return m_Key; }
[[nodiscard]] Sound* GetSound() const;

};

class LoadedMusic {
private:
friend class Assets;
std::string m_Key;
Music* m_Music;

public:
explicit LoadedMusic(std::string music_key);

// Getting
[[nodiscard]] const std::string& Key() const { return m_Key; }
[[nodiscard]] Music* GetMusic() const;

};
8 changes: 6 additions & 2 deletions src/game/entities/Crate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#include "characters/character/Character.h"
#include "Crate.h"
#include <iostream>
LoadedSound Crate::sHitSound("entity.crate.death");
LoadedSound Crate::sHitSound[] = {
LoadedSound("entity.crate.broken.1"),
LoadedSound("entity.crate.broken.2"),
LoadedSound("entity.crate.broken.3"),
};
LoadedSound Crate::sBoxSound("entity.crate.hurt");
LoadedTexture Crate::sBoxTexture("entity.crate");
LoadedTexture Crate::sBreakingBox1Texture("entity.crate2");
Expand Down Expand Up @@ -69,7 +73,7 @@ void Crate::Tick() {
// Die
if (!m_HealthComponent.IsAlive()) {
m_Alive = false;
sHitSound.GetSound()->PlaySound();
sHitSound[rand()%3].GetSound()->PlaySound();
if (m_DropType != ERROR) {
auto Ammo_type = m_World->GameWindow()->Random()->UnsignedInt() % NUM_AMMO_TYPES;
new AmmoBox(m_World, AmmoType(Ammo_type), m_Core.Pos, 20);
Expand Down
2 changes: 1 addition & 1 deletion src/game/entities/Crate.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Crate : public Entity {
Texture* m_Texture;

public:
static LoadedSound sHitSound;
static LoadedSound sHitSound[3];
static LoadedSound sBoxSound;
static LoadedTexture sBoxTexture;
static LoadedTexture sBreakingBox1Texture;
Expand Down
3 changes: 2 additions & 1 deletion src/game/entities/characters/character/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ LoadedSound Character::sDeathSound("entity.character.death");
LoadedSound Character::sAmmoPickupSound("entity.ammo_box.pickup.6");
LoadedSound Character::sItemSwitchSound("weapon.switch");
LoadedSound Character::sThrowItemSound("weapon.throw");
LoadedSound Character::sPickupItemSound("weapon.pickup");

// Other
TextSurface* Character::ms_BotNamePlate = nullptr;
Expand Down Expand Up @@ -1065,7 +1066,7 @@ void Character::DrawHands() {

// Laser sight
if (m_CurrentWeapon->WepType() == WeaponType::WEAPON_SNIPER) {
Vec2d end_position = m_Core.Pos + m_DirectionalCore.Direction * 1000;
Vec2d end_position = m_Core.Pos + m_DirectionalCore.Direction * 3000; // Todo make it collide with stuff
Render->SetColor(255, 0, 0, 255);
Render->LineCamera(m_Core.Pos.x, m_Core.Pos.y, end_position.x, end_position.y);
}
Expand Down
1 change: 1 addition & 0 deletions src/game/entities/characters/character/Character.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class Character : public DirectionalEntity {
static LoadedSound sAmmoPickupSound;
static LoadedSound sItemSwitchSound;
static LoadedSound sThrowItemSound;
static LoadedSound sPickupItemSound;
static TextSurface* ms_BotNamePlate;
TextSurface* m_ErrorText;

Expand Down
1 change: 1 addition & 0 deletions src/game/entities/characters/character/Hands.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmath>

LoadedTexture Hands::sFistTexture("entity.character.fist");
LoadedSound Hands::sPunchSound("entity.character.punch");

Hands::Hands(Character* parent, double hand_spacing, double fist_animation_duration)
: m_HandSpacing(hand_spacing / 180.0 * M_PI),
Expand Down
1 change: 1 addition & 0 deletions src/game/entities/characters/character/Hands.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Hands {
double m_Size, m_Size2;

static LoadedTexture sFistTexture;
static LoadedSound sPunchSound;

public:
Hands(Character* parent, double hand_spacing, double fist_animation_duration);
Expand Down
Loading

0 comments on commit 71bbc3d

Please sign in to comment.