Skip to content

Commit eec8b05

Browse files
committed
- Update: Sound Manager, Score Manager, Levels, Backgrounds
1 parent 7f66794 commit eec8b05

10 files changed

+54
-24
lines changed

CApplication.cpp

+13-14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
// Main functions
44

55
bool CApplication::LoadTextures() {
6-
m_Textures["Player"] = sf::Texture();
7-
m_Textures["Missile"] = sf::Texture();
8-
m_Textures["Env_BG"] = sf::Texture();
9-
m_Textures["Rock"] = sf::Texture();
10-
116
if(!m_Textures["Player"].loadFromFile("data/textures/player.png"))
127
return false;
138
if(!m_Textures["Missile"].loadFromFile("data/textures/missile.png"))
149
return false;
15-
if(!m_Textures["Env_BG"].loadFromFile("data/textures/env_bg.png"))
10+
// Levels
11+
if(!m_Textures["level_1"].loadFromFile("data/textures/levels/level_1.png"))
12+
return false;
13+
if(!m_Textures["level_2"].loadFromFile("data/textures/levels/level_2.png"))
1614
return false;
15+
// Items
1716
if(!m_Textures["Rock"].loadFromFile("data/textures/meteor.png"))
1817
return false;
1918
if(!m_Textures["Health"].loadFromFile("data/textures/health.png"))
@@ -186,13 +185,14 @@ void CApplication::Game_movePlayer(float& dt, signed short int direction) {
186185
}
187186

188187
// Game - Environment
189-
void CApplication::Game_createEnvironment() {
188+
void CApplication::Game_createEnvironment(std::string envTexture, bool setSmooth, bool setRepeated) {
190189
// Texture settings
191-
m_Textures["Env_BG"].setSmooth(true);
192-
m_Textures["Env_BG"].setRepeated(true);
190+
m_Textures[envTexture].setSmooth(setSmooth);
191+
m_Textures[envTexture].setRepeated(setRepeated);
193192

194-
m_EnvironmentBackground.setTexture(m_Textures["Env_BG"]);
195-
m_EnvironmentBackground.setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(m_Textures["Env_BG"].getSize().x * 2, m_Textures["Env_BG"].getSize().y)));
193+
m_EnvironmentBackground.setTexture(m_Textures[envTexture]);
194+
//m_EnvironmentBackground.setScale(m_Window->getView().getSize().x / m_EnvironmentBackground.getLocalBounds().width, m_Window->getView().getSize().y / m_EnvironmentBackground.getLocalBounds().height);
195+
m_EnvironmentBackground.setTextureRect(sf::IntRect(sf::Vector2i(0, 0), sf::Vector2i(m_Window->getSize().x*2, m_Window->getSize().y)));
196196
m_EnvironmentBackground.setPosition(0, 0);
197197
}
198198

@@ -209,8 +209,7 @@ void CApplication::Game_drawEnvironment(float& dt) {
209209

210210
// Game - Rocks Manager
211211
void CApplication::Game_createItemsManager() {
212-
for(std::unordered_map<std::string, sf::Texture>::iterator it = m_Textures.begin(); it != m_Textures.end(); it++) {
213-
it->second.setSmooth(true);
214-
}
212+
m_Textures["Rock"].setSmooth(true);
213+
m_Textures["Health"].setSmooth(true);
215214
m_Items = new CItemsManager();
216215
}

CPlayer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ CPlayer::CPlayer(sf::Texture& playerTexture, sf::Texture& missileTexture)
1111
m_Missile.setTexture(missileTexture);
1212

1313
m_Health.setFillColor(sf::Color::Green);
14-
m_Health.setPosition(Game.m_Window->getSize().x * 0.5f, Game.m_Window->getSize().y * 0.02f);
14+
m_Health.setPosition(Game.m_Window->getSize().x * 0.725f, Game.m_Window->getSize().y * 0.02f);
1515

1616
m_HealthBackground.setFillColor(sf::Color::Color(50, 50, 50, 200));
1717
m_HealthBackground.setSize({200.0f + 2.0f, 15.0f + 2.0f});
18-
m_HealthBackground.setPosition(Game.m_Window->getSize().x * 0.5f - 1, Game.m_Window->getSize().y * 0.02f - 1);
18+
m_HealthBackground.setPosition(m_Health.getPosition().x- 1, m_Health.getPosition().y - 1);
1919
}
2020

2121
CPlayer::~CPlayer()

CScoreManager.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ void CScoreManager::update() {
2424
m_Score.setString("Score: " + std::to_string(m_Points));
2525
m_Best.setString("Best Score: " + std::to_string(m_BestPoints));
2626
m_Best.setPosition(m_Score.getGlobalBounds().width + m_Score.getCharacterSize(), 5);
27+
28+
if(m_Points == 50) {
29+
Game.Game_createEnvironment("level_2");
30+
}
2731
}
2832

2933
void CScoreManager::reset() {

CSoundManager.cpp

+20-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ void CSoundManager::playSound(std::string fileName, bool loop) {
2828
m_Sounds.push_back(sound);
2929
}
3030

31-
void CSoundManager::playMusic(std::string fileName, bool loop) {
31+
void CSoundManager::playMusic(std::string fileName, bool loop, std::string soundID) {
3232
sf::Music* music = new sf::Music();
3333
if(!music->openFromFile("data/sounds/" + fileName)) {
3434
std::cout << "[CSoundManager] Unable to load music: " << fileName << std::endl;
@@ -37,7 +37,15 @@ void CSoundManager::playMusic(std::string fileName, bool loop) {
3737
}
3838
music->setLoop(loop);
3939
music->play();
40-
m_Musics.push_back(music);
40+
m_Musics[soundID] = music;
41+
}
42+
43+
void CSoundManager::stopMusic(std::string soundID) {
44+
if(m_Musics.find(soundID) == m_Musics.end())
45+
return;
46+
47+
delete m_Musics[soundID];
48+
m_Musics.erase(soundID);
4149
}
4250

4351
void CSoundManager::update() {
@@ -50,4 +58,14 @@ void CSoundManager::update() {
5058
sound_it++;
5159
}
5260
}
61+
62+
std::unordered_map<std::string, sf::Music*>::const_iterator music_it;
63+
for(music_it = m_Musics.begin(); music_it != m_Musics.end();) {
64+
if(music_it->second->getStatus() == sf::Music::Stopped) {
65+
delete (music_it->second);
66+
music_it = m_Musics.erase(music_it);
67+
} else {
68+
music_it++;
69+
}
70+
}
5371
}
File renamed without changes.

data/textures/levels/level_2.png

550 KB
Loading

include/CApplication.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#pragma once
22
#include <SFML/Graphics.hpp>
33
#include <iostream>
4+
#include <sstream>
45
#include <unordered_map>
56

67
#include "CPlayer.h"
@@ -44,14 +45,14 @@ struct CApplication {
4445
m_EndMenu.m_Buttons.at(1).m_Colors["hover"] = sf::Color::Yellow;
4546

4647
// Set up game
47-
Game_createEnvironment();
48+
Game_createEnvironment("level_1");
4849
Game_createPlayer();
4950
Game_createItemsManager();
5051

5152
m_SoundManager->createAudio("death.ogg");
5253
m_SoundManager->createAudio("hit.ogg");
5354

54-
//m_SoundManager->playMusic("bg.ogg", true);
55+
m_SoundManager->playMusic("bg.ogg", true, "Background Music");
5556

5657
// Title screen
5758
m_GameTitleScreen.setFont(*m_Font);
@@ -115,7 +116,7 @@ struct CApplication {
115116
void Game_destroyPlayer(bool = false);
116117
void Game_movePlayer(float&, signed short int = 0);
117118

118-
void Game_createEnvironment();
119+
void Game_createEnvironment(std::string, bool = true, bool = true);
119120
void Game_drawEnvironment(float&);
120121

121122
void Game_createItemsManager();

include/CScoreManager.h

+6
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ class CScoreManager
1515
return *this;
1616
}
1717

18+
inline void setLevel(signed short int lvl) { m_Level = lvl; }
19+
inline signed short int getLevel() { return m_Level; }
20+
21+
inline signed short int getPoints() { return m_Points; }
22+
1823
void update();
1924
void reset();
2025
void draw();
2126
private:
2227
sf::Text m_Score;
2328
sf::Text m_Best;
2429

30+
signed short int m_Level = 1;
2531
signed short int m_Points;
2632
signed short int m_BestPoints;
2733
};

include/CSoundManager.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ class CSoundManager
1414
void createAudio(std::string);
1515

1616
void playSound(std::string, bool = false);
17-
void playMusic(std::string, bool = false);
17+
void playMusic(std::string, bool, std::string);
18+
19+
void stopMusic(std::string);
1820

1921
void update();
2022
private:
2123
std::unordered_map<std::string, sf::SoundBuffer> m_Buffers;
2224

2325
std::vector<sf::Sound*> m_Sounds;
24-
std::vector<sf::Music*> m_Musics;
26+
std::unordered_map<std::string, sf::Music*> m_Musics;
2527
};
2628

sfml_2dScroll.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ int main(int argsc, char** args)
2121
Game.m_Window->setTitle(ss.str());
2222
}
2323

24-
//ShowWindow(GetConsoleWindow(), SW_HIDE);
24+
ShowWindow(GetConsoleWindow(), SW_HIDE);
2525

2626
srand((unsigned int)time(NULL));
2727

0 commit comments

Comments
 (0)