[Updated] Codebase to deal with wasm-style keyboard and mouse inputs

This commit is contained in:
Ninjaws
2024-12-10 14:41:59 +01:00
parent cbf6e658ea
commit 97d93ab113
33 changed files with 344 additions and 124 deletions
+52
View File
@@ -0,0 +1,52 @@
#ifndef ASSET_SERVICE_H
#define ASSET_SERVICE_H
#include "raylib.h"
#include <vector>
#include <string>
#include <unordered_map>
#include "enums/EFont.h"
#include "enums/EMusic.h"
#include "enums/EBackground.h"
#include "enums/EMap.h"
#include "services/Service.h"
class AssetService: public Service<AssetService>
{
public:
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
Font getFont(EFont font, int fontSize);
Music getMusic(EMusic music) {
return this->music[music];
}
Texture2D getBackground(EBackground background){
return this->backgrounds[background];
}
Texture2D loadTexture(std::string filename);
std::string getMapUrl(EMap map) {
return this->assetDir + this->maps[map];
}
protected:
AssetService();
private:
/** Friend to allow access to the constructor */
friend class Service<AssetService>;
std::string assetDir;
std::unordered_map<EFont, std::string> fonts;
std::vector<Sound> sounds;
std::unordered_map<EMusic, Music> music;
std::unordered_map<EBackground, Texture2D> backgrounds;
std::unordered_map<EMap, std::string> maps;
void setAssets();
Music loadMusic(std::string filename, bool loops);
Sound loadSound(std::string filename);
std::string getFontUrl(EFont font);
};
#endif
+30
View File
@@ -0,0 +1,30 @@
#ifndef AUDIO_SERVICE_H
#define AUDIO_SERVICE_H
#include "raylib.h"
#include "enums/EMusic.h"
#include "services/Service.h"
#include <optional>
class AudioService : public Service<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();
protected:
private:
std::optional<Music> music;
EMusic currentTrack = EMusic::NONE;
};
#endif
+89
View File
@@ -0,0 +1,89 @@
#ifndef INPUT_SERVICE_H
#define INPUT_SERVICE_H
#include "Service.h"
#include "raylib.h"
#include <vector>
#include <unordered_set>
#include <unordered_map>
/**
* Tracks all user input, which is then retrieved by the screens
*/
class InputService : public Service<InputService>
{
public:
/**
* Setting up the keys to watch, so that only the keys that might get pressed are being watched
*/
void setKeysToWatch(std::vector<KeyboardKey> keysToWatch, std::vector<MouseButton> mouseButtonsToWatch)
{
this->watchedKeys.clear();
this->keysPressed.clear();
for (int i = 0; i < keysToWatch.size(); i++)
{
this->watchedKeys.insert(keysToWatch[i]);
this->keysPressed[keysToWatch[i]] = false;
}
setMouseButtonsToWatch(mouseButtonsToWatch);
}
/**
* Checks if a button has been pressed
* The flow:
* 1. Screen constructor: Announce which keys to watch
* 2. StateService loop: Check keys, store the result
* 3. Screen logic: If pressed, execute logic
*/
void checkKeys()
{
for (auto key : watchedKeys)
{
keysPressed[key] = false;
if (IsKeyPressed(key))
{
keysPressed[key] = true;
}
}
for (auto button : watchedMouseButtons)
{
mouseButtonsPressed[button] = false;
if (IsMouseButtonPressed(button))
{
mouseButtonsPressed[button] = true;
}
}
}
bool isKeyPressed(KeyboardKey key)
{
return keysPressed[key];
}
bool isMouseButtonPressed(MouseButton button)
{
return mouseButtonsPressed[button];
}
private:
std::unordered_set<KeyboardKey> watchedKeys;
std::unordered_map<KeyboardKey, bool> keysPressed;
std::unordered_set<MouseButton> watchedMouseButtons;
std::unordered_map<MouseButton, bool> mouseButtonsPressed;
void setMouseButtonsToWatch(std::vector<MouseButton> mouseButtonsToWatch)
{
this->watchedMouseButtons.clear();
this->mouseButtonsPressed.clear();
for (int i = 0; i < mouseButtonsToWatch.size(); i++)
{
this->watchedMouseButtons.insert(mouseButtonsToWatch[i]);
this->mouseButtonsPressed[mouseButtonsToWatch[i]] = false;
}
}
};
#endif
+25
View File
@@ -0,0 +1,25 @@
#ifndef SERVICE_H
#define SERVICE_H
template <typename T>
class Service
{
public:
Service(const Service &) = delete;
Service &operator=(const Service &) = delete;
static T& getInstance()
{
static T instance;
return instance;
}
protected:
static T* instance;
Service() {}
private:
};
#endif
+61
View File
@@ -0,0 +1,61 @@
#ifndef STATE_SERVICE_H
#define STATE_SERVICE_H
#include <iostream>
#include <memory>
#include "services/Service.h"
#include "screens/Screen.h"
#include "enums/EScreen.h"
#include "raylib.h"
#if defined(PLATFORM_WEB)
#include <emscripten/emscripten.h>
#endif
/**
* Handles switching between screens
*/
class StateService : public Service<StateService>
{
public:
Vector2 getScreenSize()
{
return screenSize;
}
void setScreenSize(Vector2 screenSize)
{
this->screenSize = screenSize;
}
void setScreen(EScreen screen);
Screen *getScreen();
/**
* Start the game loop
*/
void startGame();
static void staticStep()
{
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
std::cout << "Input!" << std::endl;
}
// Access the singleton instance and call step
getInstance().step();
}
private:
std::unique_ptr<Screen> currentScreen;
bool gameRunning = true;
Vector2 screenSize;
/**
* One step of the gameloop
*/
void step();
void handleMusic();
};
#endif