[Added] Services for assets and audio, main screen, simplified drawing, [Updated] Dockerfile, so the libraries get their own folder

This commit is contained in:
Ninjaws
2024-12-07 22:36:20 +01:00
parent 8d1c3bc429
commit 1849382c01
22 changed files with 435 additions and 282 deletions
+19 -31
View File
@@ -1,52 +1,40 @@
#ifndef ASSET_SERVICE_H
#define ASSET_SERVICE_H
#include "raylib.h"
#include <vector>
#include <string>
#include "raylib.h"
#include <unordered_map>
#include "EFont.h"
#include "EMusic.h"
#include "Service.h"
class AssetService
class AssetService: public Service<AssetService>
{
private:
AssetService() {
setAssets();
};
~AssetService() = default;
/** Friend to allow access to the constructor */
friend class Service<AssetService>;
std::string assetDir;
std::vector<Font> fonts;
std::unordered_map<EFont, std::string> fonts;
std::vector<Sound> sounds;
std::vector<Music> music;
public:
AssetService(const AssetService&) = delete;
AssetService& operator=(const AssetService&) = delete;
static AssetService& getInstance() {
static AssetService instance;
return instance;
}
std::vector<Font> getFonts() {
return fonts;
}
std::vector<Sound> getSounds() {
return sounds;
}
std::vector<Music> getMusic() {
return music;
}
std::unordered_map<EMusic, Music> music;
void setAssets();
Font loadFont(std::string filename, int fontSize);
Font loadFont(std::string filename);
Music loadMusic(std::string filename, bool loops);
Sound loadSound(std::string filename);
std::string getFontUrl(EFont font);
protected:
AssetService();
void setAssetDir(std::string assetDir);
public:
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
Font getFont(EFont font, int fontSize);
Music getMusic(EMusic music) {
return this->music[music];
}
};
#endif
+32
View File
@@ -0,0 +1,32 @@
#ifndef AUDIO_SERVICE_H
#define AUDIO_SERVICE_H
#include "raylib.h"
#include "EMusic.h"
#include "Service.h"
#include <optional>
class AudioService : public Service<AudioService>
{
private:
/** Friend to allow access to the destructor */
friend class Service<AudioService>;
std::optional<Music> music;
EMusic currentTrack;
protected:
~AudioService();
public:
void setMusic(EMusic music);
std::optional<Music> getMusic(){return music;}
EMusic getCurrentTrack(){return currentTrack;}
void playMusic();
void pauseMusic();
void stopMusic();
void playSound();
void stopSounds();
};
#endif
+15
View File
@@ -0,0 +1,15 @@
#ifndef EFONT_H
#define EFONT_H
/**
* All available fonts
*/
enum class EFont {
GTEK,
OXIN,
RECKONER_BOLD,
RECKONER,
SANSATION,
};
#endif
+11
View File
@@ -0,0 +1,11 @@
#ifndef EMUSIC_H
#define EMUSIC_H
enum class EMusic {
NONE,
MAIN,
GAME,
GAME_OVER
};
#endif
+17
View File
@@ -0,0 +1,17 @@
#ifndef ESCREEN_H
#define ESCREEN_H
/**
* All possible screens that the game can be displaying
*/
enum class EScreen {
TITLE,
MAIN,
TUTORIAL,
GAME,
PAUSE,
GAMEOVER,
VICTORY
};
#endif
+2 -10
View File
@@ -7,21 +7,13 @@
class MainScreen : public Screen {
private:
Music bg;
Font font1;
Font font2;
std::string assetDir;
Vector2 dimTitle;
Vector2 dimSub;
Vector2 dimHead;
int hoveredButton = 0;
void logic() override;
void draw() override;
public:
MainScreen();
~MainScreen();
void step() override;
void logic() override;
};
#endif
+16 -13
View File
@@ -1,26 +1,29 @@
#ifndef SCREEN_H
#define SCREEN_H
#include <vector>
#include "Text.h"
class Screen {
private:
/**
* All the logic that concerns the screen currently visible to the player
*/
virtual void logic();
/**
* Draws everything for the current screen
*/
virtual void draw();
void handleMusic();
protected:
std::vector<Text> text;
public:
Screen();
~Screen();
Screen(){}
~Screen(){}
/**
* One step of the game loop
* Execute all logic for this frame
*/
virtual void step();
virtual void logic() = 0;
/**
* Draw frame to the screen
*/
void draw();
};
-14
View File
@@ -1,14 +0,0 @@
#ifndef SCREENSTATE_H
#define SCREENSTATE_H
enum class ScreenState {
TITLE,
MAIN,
TUTORIAL,
GAME,
PAUSE,
GAMEOVER,
VICTORY
};
#endif
+23
View File
@@ -0,0 +1,23 @@
#ifndef SERVICE_H
#define SERVICE_H
template <typename T>
class Service
{
private:
protected:
static T* instance;
Service() {}
public:
Service(const Service &) = delete;
Service &operator=(const Service &) = delete;
static T& getInstance()
{
static T instance;
return instance;
}
};
#endif
+12 -60
View File
@@ -2,80 +2,32 @@
#define STATE_SERVICE_H
#include "Screen.h"
#include "ScreenState.h"
#include <iostream>
#include <memory>
#include "EScreen.h"
#include "Service.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
class StateService
class StateService : public Service<StateService>
{
private:
std::unique_ptr<Screen> currentScreen;
static StateService* instance;
// StateService() {}
// StateService() = default;
// friend StateService* StateService::getInstance();
StateService()
{
std::cout << "Singleton instance created."
<< std::endl;
}
// Private destructor to prevent external deletion
~StateService()
{
std::cout << "Singleton instance destroyed."
<< std::endl;
}
public:
StateService(const StateService&) = delete;
StateService& operator=(const StateService&) = delete;
// ~StateService() = default;
// StateService(const StateService &) = delete;
// StateService &operator=(const StateService &) = delete;
// StateService& myStateService = StateService::instance();
// StateService(const StateService&) = delete;
// StateService& operator=(const StateService&) = delete;
// static StateService* getInstance();
// static std::shared_ptr<StateService> getInstance() {
// static std::shared_ptr<StateService> instance = std::make_shared<StateService>();
// return instance;
// }
static StateService& getInstance()
{
if (instance == nullptr) {
instance = new StateService();
}
return *instance;
}
// static StateService* getInstance();
// {
// if (!instance)
// {
// instance = std::make_unique<StateService>(); // This works because of the friend declaration
// }
// return instance.get(); // Return the raw pointer of the unique_ptr
// }
void setScreen(ScreenState screen);
void setScreen(EScreen screen);
Screen *getScreen();
void gameloop();
/**
* Start the game loop
*/
void startGame();
/**
* One step of the gameloop
*/
void step();
};
// StateService* StateService::instance = nullptr;
// std::unique_ptr<StateService> StateService::instance = nullptr;
#endif
+44
View File
@@ -0,0 +1,44 @@
#ifndef TEXT_H
#define TEXT_H
#include "raylib.h"
#include "EFont.h"
#include <string>
#include <iostream>
#include "AssetService.h"
class Text {
private:
std::string text;
Font font;
int fontSize;
int spacing;
Color color;
Vector2 position;
public:
Text(std::string text, EFont font, int fontSize = 30, int spacing = 0, Color color = WHITE, Vector2 position = {100,100});
~Text();
Vector2 getDimensions();
void setPosition(Vector2 position) {
this->position = position;
}
void setColor(Color color) {
this->color = color;
}
std::string getText() {
return this->text;
}
void setText(std::string text) {
this->text = text;
}
void draw();
};
#endif
+6 -9
View File
@@ -3,25 +3,22 @@
#include "Screen.h"
#include "raylib.h"
#include "Text.h"
#include <string>
#include <vector>
class TitleScreen : public Screen {
private:
Music bg;
Font font1;
Font font2;
std::string assetDir;
Vector2 dimTitle;
Vector2 dimSub;
Vector2 dimHead;
// std::vector<Text> text;
void logic() override;
void draw() override;
public:
TitleScreen();
~TitleScreen();
void step() override;
// void step() override;
void logic() override;
// void draw() override;
};
#endif