Skip to content

Commit

Permalink
CLIENT-GAME: Fix audio encapsulation
Browse files Browse the repository at this point in the history
  • Loading branch information
TTENSHII committed Sep 27, 2023
1 parent 8893274 commit 5df5008
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 65 deletions.
38 changes: 34 additions & 4 deletions src/Client/Raylib/Audio/Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ namespace Raylib {

// Sounds
Sound::Sound(const std::string& fileName)
: sound(LoadSound(fileName.c_str()))
: sound(LoadSound(fileName.c_str())), _path(fileName)
{
}

Sound::~Sound()
void Sound::unload()
{
UnloadSound(sound);
}
Expand Down Expand Up @@ -75,14 +75,29 @@ namespace Raylib {
SetSoundPitch(sound, pan);
}

bool Sound::NeedToPlay() const
{
return _needToPlay;
}

void Sound::setNeedToPlay(bool needToPlay)
{
_needToPlay = needToPlay;
}

std::string Sound::getPath() const
{
return _path;
}

// Music

Music::Music(const std::string& fileName)
: music(LoadMusicStream(fileName.c_str()))
: music(LoadMusicStream(fileName.c_str())), _path(fileName)
{
}

Music::~Music()
void Music::unload()
{
UnloadMusicStream(music);
}
Expand Down Expand Up @@ -146,4 +161,19 @@ namespace Raylib {
{
return GetMusicTimePlayed(music);
}

bool Music::NeedToPlay() const
{
return _needToPlay;
}

void Music::setNeedToPlay(bool needToPlay)
{
_needToPlay = needToPlay;
}

std::string Music::getPath() const
{
return _path;
}
} // namespace Raylib
15 changes: 12 additions & 3 deletions src/Client/Raylib/Audio/Audio.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ namespace Raylib {
class Sound {
public:
Sound(const std::string& fileName);
~Sound();

void unload();
void play() const;
void stop() const;
void pause() const;
Expand All @@ -24,15 +23,20 @@ namespace Raylib {
void setVolume(float volume) const;
void setPitch(float pitch) const;
void setPan(float pan) const;
bool NeedToPlay() const;
void setNeedToPlay(bool needToPlay);
std::string getPath() const;

private:
::Sound sound;
bool _needToPlay {false};
std::string _path;
};

class Music {
public:
Music(const std::string& fileName);
~Music();
void unload();
bool isReady() const;
void play() const;
bool isPlaying() const;
Expand All @@ -45,8 +49,13 @@ namespace Raylib {
void setPan(float pan) const;
float getTimeLength() const;
float getTimePlayed() const;
bool NeedToPlay() const;
void setNeedToPlay(bool needToPlay);
std::string getPath() const;

private:
::Music music;
bool _needToPlay {false};
std::string _path;
};
} // namespace Raylib
7 changes: 6 additions & 1 deletion src/Client/Raylib/Graphics/Graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ namespace Raylib {
return IsWindowFocused();
}

void setConfigFlags(size_t flags)
{
SetConfigFlags(static_cast<unsigned int>(flags));
}

bool isWindowResized()
{
return IsWindowResized();
Expand Down Expand Up @@ -452,7 +457,7 @@ namespace Raylib {
_texture = LoadTextureFromImage(img);
}

Sprite::~Sprite()
void Sprite::unloadSprite()
{
UnloadTexture(_texture);
}
Expand Down
31 changes: 17 additions & 14 deletions src/Client/Raylib/Graphics/Graphics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace Raylib {
bool isWindowMinimized();
bool isWindowMaximized();
bool isWindowFocused();
void setConfigFlags(size_t flags);
bool isWindowResized();
bool isWindowState(size_t flag);
void setWindowState(size_t flags);
Expand Down Expand Up @@ -88,6 +89,21 @@ namespace Raylib {
void drawCircle(int centerX, int centerY, float radius, Color color);
void drawRectangle(int posX, int posY, int width, int height, Color color);

// Color/pixel related functions
Color fade(Color color, float alpha);
int colorToInt(Color color);
Vector4 colorNormalize(Color color);
Color colorFromNormalized(Vector4 normalized);
Color getColor(unsigned int hexValue);

// Text functions
void drawFPS(int posX, int posY);
void
drawText(std::string text, int posX, int posY, int fontSize, Color color);
int measureText(const std::string text, int fontSize);

// Graphic classes

class Image {
public:
Image(std::string fileName);
Expand All @@ -109,14 +125,14 @@ namespace Raylib {
public:
Sprite(std::string fileName, float width, float height);
Sprite(Image image, float width, float height);
~Sprite();
unsigned int getId() const;
float getWidth() const;
float getHeight() const;
int getTextureWidth() const;
int getTextureHeight() const;
int getMipmaps() const;
int getFormat() const;
void unloadSprite();

// draw texture functions

Expand All @@ -140,17 +156,4 @@ namespace Raylib {
float _height;
};

// Color/pixel related functions
Color fade(Color color, float alpha);
int colorToInt(Color color);
Vector4 colorNormalize(Color color);
Color colorFromNormalized(Vector4 normalized);
Color getColor(unsigned int hexValue);

// Text functions
void drawFPS(int posX, int posY);
void
drawText(std::string text, int posX, int posY, int fontSize, Color color);
int measureText(const std::string text, int fontSize);

} // namespace Raylib
25 changes: 12 additions & 13 deletions src/Client/Systems/ClientSystems.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,36 +171,35 @@ namespace Systems {

void GraphicSystems::soundEffectPlayer(std::size_t /*unused*/)
{
Registry::components<Types::SoundEffect> arrSoundEffect =
Registry::getInstance().getComponents<Types::SoundEffect>();
Registry::components<Raylib::Sound> arrSoundEffect =
Registry::getInstance().getComponents<Raylib::Sound>();

for (auto &soundEffect : arrSoundEffect) {
if (!soundEffect.has_value()) {
continue;
}
if (soundEffect.value().needToPlay) {
PlaySound(soundEffect.value().sound);
soundEffect.value().needToPlay = false;
if (soundEffect.value().NeedToPlay()) {
soundEffect.value().play();
soundEffect.value().setNeedToPlay(false);
}
}
}

void GraphicSystems::musicPlayer(std::size_t /*unused*/)
{
Registry::components<Types::MusicStream> arrMusics =
Registry::getInstance().getComponents<Types::MusicStream>();
Registry::components<Raylib::Music> arrMusics =
Registry::getInstance().getComponents<Raylib::Music>();

for (auto &music : arrMusics) {
if (!music.has_value()) {
continue;
}
if (music.value().needToPlay) {
PlayMusicStream(music.value().music);
music.value().needToPlay = false;
music.value().isPlaying = true;
if (music.value().NeedToPlay()) {
music.value().play();
music.value().setNeedToPlay(false);
}
if (music.value().isPlaying) {
UpdateMusicStream(music.value().music);
if (music.value().isPlaying()) {
music.value().update();
}
}
}
Expand Down
23 changes: 0 additions & 23 deletions src/Client/Systems/CustomTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,29 +24,6 @@ namespace Types {
float y;
};

struct SoundEffect {
SoundEffect(std::string soundPath)
: sound(LoadSound(soundPath.c_str())),
path(soundPath)
{
}
Sound sound;
bool needToPlay {false};
std::string path;
};

struct MusicStream {
MusicStream(std::string musicPath)
: music(LoadMusicStream(musicPath.c_str())),
path(musicPath)
{
}
Music music;
bool needToPlay {false};
bool isPlaying {false};
std::string path;
};

struct RectangleShape {
float width;
float height;
Expand Down
16 changes: 9 additions & 7 deletions src/main_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void init()
const int initialEntityPositionY = 0;
const int collisionRectWidth = 10;
const int collisionRectHeight = 20;
const std::string spriteImagePath = "./assets/R-TypeSheet/r-typesheet18.gif";
const std::string spriteImagePath = "./assets/R-TypeSheet/r-typesheet18r.gif";
const int spriteWidth = 10;
const int spriteHeight = 20;
const float rectX = 2.0F;
Expand Down Expand Up @@ -55,19 +55,21 @@ void init()
registry.getComponents<Types::Text>()
.back() = {"Player", BLACK, LoadFont(fontPath.c_str()), textFontSize};
registry.addEntity();
registry.getComponents<Types::SoundEffect>().back() =
Types::SoundEffect(soundEffectPath);
registry.getComponents<Raylib::Sound>().back() =
Raylib::Sound(soundEffectPath);
registry.addEntity();
registry.getComponents<Types::MusicStream>().back() =
Types::MusicStream(musicStreamPath);
registry.getComponents<Raylib::Music>().back() =
Raylib::Music(musicStreamPath);
}

int main()
{
const int screenWidth = 1920;
const int screenHeight = 1080;
const int screenWidth = 800;
const int screenHeight = 600;
const int fps = 60;

Raylib::setConfigFlags(FLAG_WINDOW_RESIZABLE);

Systems::SystemManagersDirector& director =
Systems::SystemManagersDirector::getInstance();
director.addSystemManager(Systems::EventsSystems::eventSystems);
Expand Down

0 comments on commit 5df5008

Please sign in to comment.