Skip to content

Commit

Permalink
refactor: memory and performance optimizations
Browse files Browse the repository at this point in the history
- Store sound files in a hash map to enable fast and efficient lookups.
- Replace raw pointers with smart pointers to improve memory safety and management.
- Pass arguments by reference to reduce overhead and enhance performance.
- Adjust code indentation for improved readability and maintainability.
  • Loading branch information
zEuS0390 committed Aug 17, 2024
1 parent a9a8a28 commit cf6ec76
Show file tree
Hide file tree
Showing 14 changed files with 317 additions and 438 deletions.
6 changes: 3 additions & 3 deletions include/AI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ class AI
{
public:
std::string status;
AI (player*, Pongball*);
AI (player&, Pongball&);
void init ();
void sense ();
void update ();
private:
player* pPlayer;
Pongball* pBall;
player& pPlayer;
Pongball& pBall;
};

#endif
4 changes: 2 additions & 2 deletions include/background.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Background
{
public:
Background (sf::RenderWindow*, sf::Vector2f);
Background (sf::RenderWindow&, sf::Vector2f);
void init ();
void update ();
void render ();
Expand All @@ -16,7 +16,7 @@ class Background
sf::Image image;
sf::Texture texture;
sf::Sprite sprite;
sf::RenderWindow* renderWin;
sf::RenderWindow& renderWin;
};

#endif
76 changes: 37 additions & 39 deletions include/menu.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,57 @@
#define MENU_H

#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include <list>
#include <memory>
#include "sound.hpp"

class button
{
public:
bool isHovered;
bool isSelected;
std::string str;
sf::Color buttonColor;
sf::Color textColor;
float textCharSize;
sf::Vector2f buttonSize;
sf::Vector2f position;
button (std::string, sf::Color, sf::Color, float, sf::Vector2f, sf::Vector2f);
void init ();
bool isHovered;
bool isSelected;
std::string str;
sf::Color buttonColor;
sf::Color textColor;
float textCharSize;
sf::Vector2f buttonSize;
sf::Vector2f position;
button (const std::string&, const sf::Color&, const sf::Color&, float, const sf::Vector2f&, const sf::Vector2f&);
void init ();
public:
sf::RectangleShape buttonObj;
sf::Text textObj;
sf::Font fontObj;
sf::RectangleShape buttonObj;
sf::Text textObj;
sf::Font fontObj;
};

// menu class
class menu
{
public:
bool isPlaying;
bool isMenu;
menu (sf::RenderWindow*, soundManager*);
void init ();
template <class T>
void mouseSelect (std::list<T*>, sf::Color, sf::Color);
std::string checkSelected ();
void update ();
template <class T>
void renderMenu (std::list<T*>);
void render ();
std::list<button*> menuButtons;
bool isPlaying;
bool isMenu;
menu (sf::RenderWindow&, soundManager&);
void init ();
void mouseSelect (std::vector<std::unique_ptr<button>>&, const sf::Color&, const sf::Color&);
std::string checkSelected ();
void update ();
void renderMenu (std::vector<std::unique_ptr<button>>&);
void render ();
std::vector<std::unique_ptr<button>> menuButtons;
private:
template <class T>
void createButton (std::list<T*>&, std::string, sf::Color, sf::Color, float, sf::Vector2f, sf::Vector2f);
sf::Image gameLogo;
sf::Image menuBackground;
sf::Texture gameLogoTexture;
sf::Texture menuTexture;
sf::Texture mouseTexture;
sf::Sprite gameLogoSprite;
sf::Sprite menuSprite;
sf::Sprite mouseSprite;
sf::RenderWindow* renderWin;
soundManager* sManager;
sf::Mouse mouse;
void createButton (std::vector<std::unique_ptr<button>>&, const std::string&, const sf::Color&, const sf::Color&, float, const sf::Vector2f&, const sf::Vector2f&);
sf::Image gameLogo;
sf::Image menuBackground;
sf::Texture gameLogoTexture;
sf::Texture menuTexture;
sf::Texture mouseTexture;
sf::Sprite gameLogoSprite;
sf::Sprite menuSprite;
sf::Sprite mouseSprite;
sf::RenderWindow& renderWin;
soundManager& sManager;
sf::Mouse mouse;
};

#endif
10 changes: 5 additions & 5 deletions include/players.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class player
float velY;
float accY;
float fric;
player (std::string, std::string, std::string, sf::Vector2f, soundManager*);
player (std::string, std::string, std::string, sf::Vector2f, soundManager&);
void init ();
void movement ();
void collision ();
Expand All @@ -37,7 +37,7 @@ class player
Side side;
private:
sf::Clock clock;
soundManager* sManager;
soundManager& sManager;
void setSides ();
};

Expand All @@ -46,7 +46,7 @@ class players
{
public:
std::vector<player> playersVec;
players (sf::RenderWindow*, soundManager*);
players (sf::RenderWindow&, soundManager&);
~players ();
void init ();
void setScore (std::string, int);
Expand All @@ -60,8 +60,8 @@ class players
void updates ();
void renders ();
private:
sf::RenderWindow* renderWin;
soundManager* sManager;
sf::RenderWindow& renderWin;
soundManager& sManager;
};

#endif
8 changes: 4 additions & 4 deletions include/pongball.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Pongball
float acc;
float ballAngle;
float counterVar;
Pongball (sf::RenderWindow*, players*, soundManager*);
Pongball (sf::RenderWindow&, players&, soundManager&);
~Pongball ();
void init ();
void hitExplode ();
Expand Down Expand Up @@ -50,10 +50,10 @@ class Pongball
sf::IntRect effectRect;
sf::Sprite effectSp;
sf::Texture effectTex;
soundManager* sManager;
soundManager& sManager;
std::mt19937 eng;
sf::RenderWindow* renderWin;
players* cplayers;
sf::RenderWindow& renderWin;
players& cplayers;
};

#endif
28 changes: 15 additions & 13 deletions include/sound.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,36 @@
#define SOUND_H

#include <SFML/Audio.hpp>
#include <unordered_map>
#include <string>
#include <memory>

// sound class
class sound
{
public:
std::string name;
sound (std::string, std::string, float);
void playSound ();
void setPitch (float);
sound (const std::string&, const std::string&, float);
void playSound ();
void setPitch (float);
private:
void init (std::string, float);
sf::SoundBuffer buffer;
sf::Sound soundObj;
void init (const std::string&, float);
sf::SoundBuffer buffer;
sf::Sound soundObj;
};

// soundManager class
class soundManager
{
public:
soundManager ();
~soundManager ();
void audioInit ();
void playAudio (std::string);
void setPitch (std::string, float);
void terminateAudioThreads ();
soundManager ();
~soundManager ();
void audioInit ();
void playAudio (const std::string&);
void setPitch (const std::string&, float);
void terminateAudioThreads ();
private:
std::vector<sound*> audioCont;
std::unordered_map<std::string, std::unique_ptr<sound>> audioCont;
};

#endif
10 changes: 5 additions & 5 deletions include/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <SFML/Graphics.hpp>
#include <string>
#include <memory>
#include "pongball.hpp"
#include "players.hpp"
#include "sound.hpp"
Expand All @@ -17,13 +18,12 @@ class window
bool isAI;
bool isGameActive;
bool isRunning;
window (unsigned int, unsigned int, std::string);
window (unsigned int, unsigned int, const std::string&);
~window ();
sf::RenderWindow renderWin;
private:
void init ();
template <class T>
void textInit (std::vector<T>*, float, sf::Color, sf::Vector2f);
void textInit (std::vector<std::unique_ptr<sf::Text>>&, float, sf::Color, sf::Vector2f);
void events ();
void menuSelection ();
void updateScores ();
Expand All @@ -39,8 +39,8 @@ class window
Pongball pongball;
soundManager sManager;
AI AIplayer;
std::vector<sf::Text> scoresTxt;
std::vector<sf::Text> AITxt;
std::vector<std::unique_ptr<sf::Text>> scoresTxt;
std::vector<std::unique_ptr<sf::Text>> AITxt;
};

#endif
64 changes: 32 additions & 32 deletions src/AI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,80 +3,80 @@
#include "players.hpp"
#include "AI.hpp"

AI::AI (player* pPlayer, Pongball* pBall):
AI::AI (player& pPlayer, Pongball& pBall):
pPlayer(pPlayer), pBall(pBall)
{
init();
}

void AI::init ()
{
if (pPlayer->id == "player2")
pPlayer->fric = -0.08f;
if (pPlayer.id == "player2")
pPlayer.fric = -0.08f;
}

void AI::sense ()
{
if (pPlayer->id == "player2")
if (pPlayer.id == "player2")
{
pPlayer->isPlaying = true;
if (pBall->isMoving &&
pBall->currPos.x > 220 &&
pBall->vertex2.position.y < 2*480 &&
pBall->vertex2.position.y > -480)
pPlayer.isPlaying = true;
if (pBall.isMoving &&
pBall.currPos.x > 220 &&
pBall.vertex2.position.y < 2*480 &&
pBall.vertex2.position.y > -480)
{
if (pBall->vertex1.position.y > pPlayer->side.top &&
pBall->vertex1.position.y < pPlayer->side.bottom)
if (pBall.vertex1.position.y > pPlayer.side.top &&
pBall.vertex1.position.y < pPlayer.side.bottom)
{
status = "zEuS0390 (AI): Stop";
pPlayer->dir = {false, false};
pPlayer.dir = {false, false};
}
else if (pBall->vertex1.position.y > pPlayer->currPos.y)
else if (pBall.vertex1.position.y > pPlayer.currPos.y)
{
status = "zEuS0390 (AI): Down";
pPlayer->dir = {false, true};
pPlayer.dir = {false, true};
}
else if (pBall->vertex1.position.y < pPlayer->currPos.y)
else if (pBall.vertex1.position.y < pPlayer.currPos.y)
{
status = "zEuS0390 (AI): Up";
pPlayer->dir = {true, false};
pPlayer.dir = {true, false};
}
}
else
{
status = "zEuS0390 (AI):";
if (pPlayer->dir.up || pPlayer->dir.down)
if (pPlayer.dir.up || pPlayer.dir.down)
{
pPlayer->dir = {false, false};
pPlayer.dir = {false, false};
}
}
}
if (pPlayer->id == "player1")
if (pPlayer.id == "player1")
{
if (pBall->isMoving &&
pBall->currPos.x < 420 &&
pBall->vertex2.position.y < 2*480 &&
pBall->vertex2.position.y > -480)
if (pBall.isMoving &&
pBall.currPos.x < 420 &&
pBall.vertex2.position.y < 2*480 &&
pBall.vertex2.position.y > -480)
{
if (pBall->vertex2.position.y > pPlayer->side.top &&
pBall->vertex2.position.y < pPlayer->side.bottom)
if (pBall.vertex2.position.y > pPlayer.side.top &&
pBall.vertex2.position.y < pPlayer.side.bottom)
{
pPlayer->dir = {false, false};
pPlayer.dir = {false, false};
}
else if (pBall->vertex2.position.y > pPlayer->currPos.y)
else if (pBall.vertex2.position.y > pPlayer.currPos.y)
{
pPlayer->dir = {false, true};
pPlayer.dir = {false, true};
}
else if (pBall->vertex2.position.y < pPlayer->currPos.y)
else if (pBall.vertex2.position.y < pPlayer.currPos.y)
{
pPlayer->dir = {true, false};
pPlayer.dir = {true, false};
}
}
else
{
if (pPlayer->dir.up || pPlayer->dir.down)
if (pPlayer.dir.up || pPlayer.dir.down)
{
pPlayer->dir = {false, false};
pPlayer.dir = {false, false};
}
}
}
Expand Down
Loading

0 comments on commit cf6ec76

Please sign in to comment.