Skip to content

Commit

Permalink
feat: added music, persistent music between scenes
Browse files Browse the repository at this point in the history
  • Loading branch information
phuwit committed Oct 18, 2023
1 parent ed2316e commit e3196d8
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 17 deletions.
Binary file added assets/music/Spow.ogg
Binary file not shown.
Binary file added assets/music/TheCyberGrind.ogg
Binary file not shown.
Binary file added assets/music/Votely.ogg
Binary file not shown.
Binary file added assets/sfx/levelup.ogg
Binary file not shown.
Binary file added assets/sfx/paused.ogg
Binary file not shown.
Binary file removed assets/sfx/shoot.wav
Binary file not shown.
9 changes: 7 additions & 2 deletions attribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
<!-- - Used [2D Dungeon Asset Pack](https://styloo.itch.io/2d-dungeon) by [Styloo](https://styloo.itch.io/) on itch.io, licensed under [CC0 1.0 (Public Domain Dedication)](https://creativecommons.org/publicdomain/zero/1.0/), or licensed under CC4.0 from readme but did not scpeified rights. contacted Styloo and waiting for a response. -->
- Used [2D Pixel Dungeon Asset Pack v2.0](https://pixel-poem.itch.io/dungeon-assetpuck) by [Pixel-Poem](https://pixel-poem.itch.io/) on itch.io, licensed under the clause `This asset pack can be used in free and commercial projects. You can modify it to suit your own needs. Credit is not necessary, but appreciated. You may not redistribute it or resell it.`
- Used [Space Shooter (Redux, plus fonts and sounds)](https://kenney.nl/assets/space-shooter-redux), Licensed Under [Creative Commons CC0](https://creativecommons.org/share-your-work/public-domain/cc0/) by [Kenney Vleugels](https://kenney.nl/)
- Used [Bebas Neue v2](http://bebasneue.com/), licensed under [SIL Open Font License, Version 1.1](https://github.com/dharmatype/Bebas-Neue/blob/master/OFL.txt)

# Audio
- Sound effects using [sfxr.me](sfxr.me)
- Used [ambient buttons » btn121.wav ](https://freesound.org/people/junggle/sounds/29301/), Licensed under [CC BY 4.0](https://creativecommons.org/licenses/by/4.0/), by [junggle](https://freesound.org/people/junggle/)
- Used [Clicks and switches » Click.wav](https://freesound.org/people/kwahmah_02/sounds/256116/), Licensed Under [CC0 1.0](https://creativecommons.org/publicdomain/zero/1.0/), by [kwahmah_02](https://freesound.org/people/kwahmah_02/)
- Sound effects using [sfxr.me](sfxr.me)
- Used [Bebas Neue v2](http://bebasneue.com/), licensed under [SIL Open Font License, Version 1.1](https://github.com/dharmatype/Bebas-Neue/blob/master/OFL.txt)
- [Meganeko](https://soundcloud.com/meganeko) - [The Cyber Grind (Ultrakill Soundtrack)](https://soundcloud.com/meganeko/ultrakill)
- [coal bones](https://www.youtube.com/@CoalBones) - [Votely](https://www.youtube.com/watch?v=U9uUf4PnHyY)
- [Cheesy Hfj](https://www.youtube.com/@CheesyHfj) - [Spow](https://www.youtube.com/watch?v=3re7zdPidbU)
1 change: 1 addition & 0 deletions source/Collision/Collision.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* File: collision.cpp
* Author: Nick (original version), ahnonay (SFML2 compatibility), Paweł Syska (C++17 refactor + naming convention)
*/

#include <SFML/Graphics.hpp>
#include <map>
#include <vector>
Expand Down
47 changes: 47 additions & 0 deletions source/Holders/MusicHolder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once
#include <cassert>
#include "MusicHolder.hpp"

MusicHolder* MusicHolder::m_s_Instance = nullptr;

MusicHolder::MusicHolder() {
// if m_s_Instance != nullptr; game will exit
assert(m_s_Instance == nullptr);
m_s_Instance = this;
}

Music& MusicHolder::GetMusic(const std::string &filename) {
// get a reference to m_Font via m_s_instance
auto& m = m_s_Instance -> m_Music;
// auto equivalent to map<string, Font>
// create an iterator to hold a key-value pair (kvp) and search for a required kvp using the passed in filename
auto keyValuePair = m.find(filename);
// auto equivalent to map<string, Font>::iterator

// match?
if (keyValuePair != m.end()) {
// match.
// return the font; second part of kvp
return keyValuePair -> second;
}
else {
// no match.
// create a new key value pair using the filename
auto& music = m[filename];
// load texture from a file
music.openFromFile(filename);
return music;
}
}

void MusicHolder::StopAll() {
for (auto &itr : m_s_Instance -> m_Music) {
itr.second.stop();
}
}

void MusicHolder::PauseAll() {
for (auto &itr : m_s_Instance -> m_Music) {
itr.second.pause();
}
}
20 changes: 20 additions & 0 deletions source/Holders/MusicHolder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <SFML/Audio.hpp>
#include <map>

using namespace sf;

class MusicHolder {
private:
// map container from STL, holds string-Font key-pair
std::map<std::string, Music> m_Music;
// pointer of the same type as the class - one and only instance
static MusicHolder* m_s_Instance;

public:
MusicHolder();
static Music& GetMusic(std::string const& filename);
static void StopAll();
static void PauseAll();
};
3 changes: 3 additions & 0 deletions source/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Holders/TextureHolder.cpp"
#include "Holders/FontHolder.cpp"
#include "Holders/SoundHolder.cpp"
#include "Holders/MusicHolder.hpp"

#include "Scenes/Scenes.hpp"
#include "Scenes/Menu.cpp"
Expand All @@ -17,6 +18,7 @@ int main() {
TextureHolder textureHolder;
FontHolder fontHolder;
SoundHolder soundHolder;
MusicHolder musicHolder;

// Applications variables
std::vector<Scenes*> Scenes;
Expand Down Expand Up @@ -55,6 +57,7 @@ int main() {
// Main loop
while (currentScene.getNextScreen() >= 0) {
Scenes[currentScene.getNextScreen()]->setScreenShot(currentScene.getScreenShot());
MusicHolder::PauseAll();
currentScene = Scenes[currentScene.getNextScreen()]->run(window);
}

Expand Down
12 changes: 12 additions & 0 deletions source/Scenes/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ Game::Game(Vector2f screenResolution, Vector2f levelSize) {
m_SoundPickupLow.setBuffer(SoundHolder::GetSound("assets/sfx/pickup1.ogg"));
m_SoundPickupHigh.setBuffer(SoundHolder::GetSound("assets/sfx/pickup2.ogg"));

m_SoundLoaded.setVolume(50);
m_SoundHit.setVolume(50);
m_SoundKilled.setVolume(50);
m_SoundShoot.setVolume(50);
m_SoundPickupLow.setVolume(50);
m_SoundPickupHigh.setVolume(50);

regenerate();
}

Expand Down Expand Up @@ -158,6 +165,11 @@ SceneChange Game::run(RenderWindow &window) {

m_SoundLoaded.play();

Music* musicPtr = &MusicHolder::GetMusic("assets/music/TheCyberGrind.ogg");
musicPtr->setLoop(true);
musicPtr->setVolume(5);
musicPtr->play();

while (!paused) {
// HANDLE INPUTS
Event event;
Expand Down
6 changes: 6 additions & 0 deletions source/Scenes/GameOver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ SceneChange GameOver::run(RenderWindow &window) {
saveButton.setPosition(saveText.getPosition() + Vector2f(0, 15));

std::vector<Score> scores = readScore();

Music* musicPtr = &MusicHolder::GetMusic("assets/music/Votely.ogg");
musicPtr->stop();
musicPtr->setLoop(true);
musicPtr->setVolume(5);
musicPtr->play();

while (window.isOpen()) {
// Update frame
Expand Down
3 changes: 3 additions & 0 deletions source/Scenes/Info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ SceneChange Info::run(RenderWindow &window) {
downloadText.setPosition(attributionText.getPosition() + Vector2f((attributionText.getLocalBounds().width / 2) + (downloadText.getLocalBounds().width / 2) + BUTTON_PADDING + 50, 0));
downloadButton.setPosition(downloadText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

Music* musicPtr = &MusicHolder::GetMusic("assets/music/Spow.ogg");
musicPtr->play();

while (window.isOpen()) {
// Update frame
Event event;
Expand Down
5 changes: 5 additions & 0 deletions source/Scenes/Leaderboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ SceneChange Leaderboard::run(RenderWindow &window) {
Text scoresText[SCORES_COUNT];
Text namesText[SCORES_COUNT];

Music* musicPtr = &MusicHolder::GetMusic("assets/music/Votely.ogg");
musicPtr->setLoop(true);
musicPtr->setVolume(20);
musicPtr->play();

for (int i = 0; i < SCORES_COUNT; i++) {
if (scores.empty()) {
break;
Expand Down
9 changes: 8 additions & 1 deletion source/Scenes/LevelUp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ SceneChange LevelUp::run(RenderWindow &window) {

// Make buttons for selecting upgrades from this list
const int UPGRADE_BUTTON_FONT_SIZE = 36;
const int NUM_UPGRADES = 6;
const int NUM_UPGRADES = 5;
const std::string UPGRADE_TEXT[NUM_UPGRADES] = {
"Increased rate of fire",
"Increased clip size (next reload)",
Expand Down Expand Up @@ -99,6 +99,13 @@ SceneChange LevelUp::run(RenderWindow &window) {

Sound soundClick(SoundHolder::GetSound("assets/sfx/click.ogg"));
Sound soundHover(SoundHolder::GetSound("assets/sfx/hover.ogg"));
Sound soundLevelUp(SoundHolder::GetSound("assets/sfx/levelup.ogg"));

soundLevelUp.play();

Music* musicPtr = &MusicHolder::GetMusic("assets/music/TheCyberGrind.ogg");
musicPtr->play();


while (window.isOpen()) {
// Update frame
Expand Down
36 changes: 22 additions & 14 deletions source/Scenes/Menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include "Game.hpp"
#include "../CommonEnum.hpp"
#include "../Holders/TextureHolder.hpp"
#include "../Holders/FontHolder.cpp"
#include "../Holders/FontHolder.hpp"
#include "../Holders/SoundHolder.hpp"
#include "../Holders/MusicHolder.cpp"
#include "../Tools/SetOriginCenter.cpp"
#include "../Tools/SetRectangleShapeOnMouseHover.cpp"

Expand All @@ -28,52 +29,59 @@ SceneChange Menu::run(RenderWindow &window) {
playText.setFillColor(Color::Black);
playText.setCharacterSize(60);
RectangleShape playButton(Vector2f(playText.getLocalBounds().width + BUTTON_PADDING, playText.getLocalBounds().height + BUTTON_PADDING));
playButton.setFillColor(Color(0x4DB6ACFF));
textSetOriginCenter(playText);
shapeSetOriginCenter(playButton);
playText.setPosition(logo.getPosition() + Vector2f(0, 400));
playButton.setPosition(playText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

Text infoText("Info", fontBebas);
infoText.setFillColor(Color::Black);
infoText.setCharacterSize(60);
RectangleShape infoButton(Vector2f(infoText.getLocalBounds().width + BUTTON_PADDING, infoText.getLocalBounds().height + BUTTON_PADDING));
textSetOriginCenter(infoText);
shapeSetOriginCenter(infoButton);
infoText.setPosition(playText.getPosition() + Vector2f(0, 100));
infoButton.setPosition(infoText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

Text leaderboardText("Leaderboard", fontBebas);
leaderboardText.setFillColor(Color::Black);
leaderboardText.setCharacterSize(60);
RectangleShape leaderboardButton(Vector2f(leaderboardText.getLocalBounds().width + BUTTON_PADDING, leaderboardText.getLocalBounds().height + BUTTON_PADDING));
textSetOriginCenter(leaderboardText);
shapeSetOriginCenter(leaderboardButton);
leaderboardText.setPosition(infoText.getPosition() + Vector2f(0, 100));
leaderboardText.setPosition(playText.getPosition() + Vector2f(0, 100));
leaderboardButton.setPosition(leaderboardText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

Text infoText("Info", fontBebas);
infoText.setFillColor(Color::Black);
infoText.setCharacterSize(60);
RectangleShape infoButton(Vector2f(infoText.getLocalBounds().width + BUTTON_PADDING, infoText.getLocalBounds().height + BUTTON_PADDING));
textSetOriginCenter(infoText);
shapeSetOriginCenter(infoButton);
infoText.setPosition(leaderboardText.getPosition() + Vector2f(0, 100));
infoButton.setPosition(infoText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

Text exitText("Exit", fontBebas);
exitText.setFillColor(Color::Black);
exitText.setCharacterSize(60);
RectangleShape exitButton(Vector2f(exitText.getLocalBounds().width + BUTTON_PADDING, exitText.getLocalBounds().height + BUTTON_PADDING));
exitButton.setFillColor(Color(0xE57373FF));
textSetOriginCenter(exitText);
shapeSetOriginCenter(exitButton);
exitText.setPosition(leaderboardText.getPosition() + Vector2f(0, 100));
exitText.setPosition(infoText.getPosition() + Vector2f(0, 100));
exitButton.setPosition(exitText.getPosition() + Vector2f(0, BUTTON_PADDING / 2));

// Sound soundClick(SoundHolder::GetSound("assets/sfx/click.ogg"));
// Sound soundHover(SoundHolder::GetSound("assets/sfx/hover.ogg"));

Music* musicPtr = &MusicHolder::GetMusic("assets/music/Spow.ogg");
musicPtr->setLoop(true);
musicPtr->setVolume(10);
musicPtr->play();

while (window.isOpen()) {
// Update frame
Event event;
while (window.pollEvent(event)) {
if (event.type == Event::MouseMoved) {
Vector2f mouseScreenPosition = Vector2f(Mouse::getPosition(window));

setRectangleShapeColorOnMouseHover(playButton, mouseScreenPosition, Color(189, 189, 189), Color::White);
setRectangleShapeColorOnMouseHover(playButton, mouseScreenPosition, Color(0x26A69AFF), Color(0x4DB6ACFF));
setRectangleShapeColorOnMouseHover(infoButton, mouseScreenPosition, Color(189, 189, 189), Color::White);
setRectangleShapeColorOnMouseHover(leaderboardButton, mouseScreenPosition, Color(189, 189, 189), Color::White);
setRectangleShapeColorOnMouseHover(exitButton, mouseScreenPosition, Color(189, 189, 189), Color::White);
setRectangleShapeColorOnMouseHover(exitButton, mouseScreenPosition, Color(0xEF5350FF), Color(0xE57373FF));
}

if (event.type == Event::MouseButtonPressed) {
Expand Down
3 changes: 3 additions & 0 deletions source/Scenes/Paused.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ SceneChange Paused::run(RenderWindow &window) {
exitText.setPosition(continueText.getPosition() + Vector2f(0, 100));
exitButton.setPosition(exitText.getPosition() + Vector2f(-BUTTON_PADDING / 2, BUTTON_PADDING / 2));

Sound soundPaused(SoundHolder::GetSound("assets/sfx/paused.ogg"));
soundPaused.play();

while (window.isOpen()) {
// Update frame
Event event;
Expand Down

0 comments on commit e3196d8

Please sign in to comment.