[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
@@ -2,11 +2,11 @@
#define BACKGROUND_H
#include "raylib.h"
#include "EBackground.h"
#include "enums/EBackground.h"
#include <vector>
#include "TextureWrapper.h"
#include "AssetService.h"
#include "StateService.h"
#include "entities/TextureWrapper.h"
#include "services/AssetService.h"
#include "services/StateService.h"
class Background {
public:
@@ -14,8 +14,9 @@ class Background {
}
Background(EBackground background){
Texture2D texture = AssetService::getInstance().getBackground(background);
TextureWrapper wrapper = TextureWrapper(texture, StateService::getInstance().getScreenSize(), {0,0});
// Texture2D texture = AssetService::getInstance().getBackground(background);
TextureWrapper wrapper = TextureWrapper(AssetService::getInstance().getBackground(background), StateService::getInstance().getScreenSize(), {0,0});
wrapper.setSourceRect({0,0,wrapper.getSize().x, wrapper.getSize().y});
textures.push_back(wrapper);
}
Background(std::vector<TextureWrapper> backgrounds){
+8 -4
View File
@@ -1,9 +1,10 @@
#ifndef GAME_H
#define GAME_H
#include "Map.h"
#include "Background.h"
#include "StateService.h"
#include "entities/Map.h"
#include "entities/Background.h"
#include "services/StateService.h"
#include "services/AudioService.h"
class Game
{
@@ -16,7 +17,7 @@ public:
wrapper2.setSourceRect({0,200,wrapper2.getSize().x, 200+wrapper2.getSize().y});
TextureWrapper wrapper3 = TextureWrapper(AssetService::getInstance().getBackground(EBackground::GAME3), {1792.0f,(float)GetScreenHeight()}, {wrapper1.getSize().x+wrapper2.getSize().x,0.0});
wrapper3.setSourceRect({64,200,64+wrapper3.getSize().x, 200+wrapper3.getSize().y});
std::vector<TextureWrapper> wrappers;
wrappers.push_back(wrapper1);
wrappers.push_back(wrapper2);
@@ -31,6 +32,9 @@ public:
camera.rotation = 0.0f;
camera.zoom = 1.0f;
this->camera = camera;
AudioService::getInstance().setMusic(EMusic::GAME);
AudioService::getInstance().playMusic();
}
~Game()
{
+2 -2
View File
@@ -2,10 +2,10 @@
#define TEXT_H
#include "raylib.h"
#include "EFont.h"
#include "enums/EFont.h"
#include <string>
#include <iostream>
#include "AssetService.h"
#include "services/AssetService.h"
class Text {
private:
@@ -5,7 +5,7 @@
class TextureWrapper {
public:
TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,100,100}) {
TextureWrapper(Texture2D texture, Vector2 size = {32,32}, Vector2 position = {0,0}, Rectangle sourceRect = {0,0,32,32}) {
this->texture = texture;
this->size = size;
this->position = position;
+10
View File
@@ -0,0 +1,10 @@
#ifndef EMAP_H
#define EMAP_H
enum class EMap {
TEXTURE,
COLLISION
};
#endif
@@ -1,8 +1,8 @@
#ifndef GAME_SCREEN_H
#define GAME_SCREEN_H
#include "Screen.h"
#include "Game.h"
#include "screens/Screen.h"
#include "entities/Game.h"
class GameScreen: public Screen {
@@ -1,9 +1,9 @@
#ifndef MAIN_SCREEN_H
#define MAIN_SCREEN_H
#include "Screen.h"
#include "screens/Screen.h"
#include <vector>
#include "Text.h"
#include "entities/Text.h"
class MainScreen : public Screen {
private:
@@ -1,9 +1,9 @@
#ifndef TITLE_SCREEN_H
#define TITLE_SCREEN_H
#include "Screen.h"
#include "screens/Screen.h"
#include "raylib.h"
#include "Text.h"
#include "entities/Text.h"
class TitleScreen : public Screen {
private:
@@ -1,9 +1,9 @@
#ifndef TUTORIAL_SCREEN_H
#define TUTORIAL_SCREEN_H
#include "Screen.h"
#include "Background.h"
#include "Text.h"
#include "screens/Screen.h"
#include "entities/Background.h"
#include "entities/Text.h"
class TutorialScreen : public Screen
{
@@ -5,31 +5,14 @@
#include <vector>
#include <string>
#include <unordered_map>
#include "EFont.h"
#include "EMusic.h"
#include "EBackground.h"
#include "Service.h"
#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>
{
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;
void setAssets();
Music loadMusic(std::string filename, bool loops);
Sound loadSound(std::string filename);
std::string getFontUrl(EFont font);
protected:
AssetService();
public:
void setAssetDir(std::string assetDir){this->assetDir = assetDir;}
@@ -41,6 +24,29 @@ public:
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
@@ -2,18 +2,12 @@
#define AUDIO_SERVICE_H
#include "raylib.h"
#include "EMusic.h"
#include "Service.h"
#include "enums/EMusic.h"
#include "services/Service.h"
#include <optional>
class AudioService : public Service<AudioService>
{
private:
std::optional<Music> music;
EMusic currentTrack = EMusic::NONE;
protected:
public:
void setMusic(EMusic music);
std::optional<Music> getMusic(){return music;}
@@ -24,6 +18,13 @@ public:
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
@@ -4,11 +4,6 @@
template <typename T>
class Service
{
private:
protected:
static T* instance;
Service() {}
public:
Service(const Service &) = delete;
Service &operator=(const Service &) = delete;
@@ -18,6 +13,13 @@ public:
static T instance;
return instance;
}
protected:
static T* instance;
Service() {}
private:
};
#endif
@@ -1,11 +1,11 @@
#ifndef STATE_SERVICE_H
#define STATE_SERVICE_H
#include "Screen.h"
#include <iostream>
#include <memory>
#include "EScreen.h"
#include "Service.h"
#include "services/Service.h"
#include "screens/Screen.h"
#include "enums/EScreen.h"
#include "raylib.h"
#if defined(PLATFORM_WEB)
@@ -17,6 +17,32 @@
*/
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;
@@ -30,20 +56,6 @@ private:
void step();
void handleMusic();
public:
Vector2 getScreenSize(){
return screenSize;
}
void setScreenSize(Vector2 screenSize) {
this->screenSize = screenSize;
}
void setScreen(EScreen screen);
Screen *getScreen();
/**
* Start the game loop
*/
void startGame();
};
#endif
+11 -1
View File
@@ -1,5 +1,15 @@
#!/bin/bash
emcc -o dist/game.js src/main.cpp -Os -Wall /app/raylib/src/libraylib.a -I. -I/app/raylib/build/raylib/include -L. -L$LD_LIBRARY_PATH -s USE_GLFW=3 -s --preload-file assets@/assets -DPLATFORM_WEB
# emcc src/main.cpp -o dist/game.js -Wno-missing-braces -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s USE_GLFW=3 -DPLATFORM_WEB
emcc $(find src/ -name "*.cpp") -o dist/game.js -Wno-missing-braces -I./include -I/app/libs/raylib/build/raylib/include -L/app/libs/raylib/src /app/libs/raylib/src/libraylib.a -s ALLOW_MEMORY_GROWTH=1 -s INITIAL_MEMORY=268435456 -s MAXIMUM_MEMORY=4294967296 -s --preload-file assets@/assets -s USE_GLFW=3 -DPLATFORM_WEB
# -I./include
# -L$LD_LIBRARY_PATH
# -I.
# -L.
# -s --preload-file assets@/assets
# /app/libs/raylib/src/libraylib.a
# -Os -Wall
# --shell-file /app/raylib/src/shell.html
cd dist
python3 -m http.server 8080;
+2 -2
View File
@@ -1,6 +1,6 @@
#include "Background.h"
#include "entities/Background.h"
#include "AssetService.h"
#include "services/AssetService.h"
// Background::Background(EBackground background)
// {
+3 -3
View File
@@ -1,9 +1,9 @@
#include "Map.h"
#include "entities/Map.h"
#include <string>
#include <fstream>
#include <sstream>
#include <iostream>
#include "AssetService.h"
#include "services/AssetService.h"
Map::Map()
{
@@ -14,7 +14,7 @@ Map::Map()
void Map::loadMap()
{
std::vector<Vector2> tempMap;
std::ifstream openfile("assets/Maps/map1.txt");
std::ifstream openfile(AssetService::getInstance().getMapUrl(EMap::TEXTURE));
map.clear();
+1 -1
View File
@@ -1,4 +1,4 @@
#include "Text.h"
#include "entities/Text.h"
Text::Text(std::string text, EFont font, int fontSize, int spacing, Color color, Vector2 position)
{
+57 -7
View File
@@ -1,16 +1,66 @@
#include "raylib.h"
#include "Screen.h"
#include "TitleScreen.h"
#include "StateService.h"
#include <iostream>
// #include "screens/Screen.h"
// #include "screens/TitleScreen.h"
#include "services/StateService.h"
// #include <iostream>
// #if defined(PLATFORM_WEB)
// #include <emscripten/emscripten.h>
// #endif
int main(void)
// void step();
// static void gameloop();
int main()
{
StateService& stateService = StateService::getInstance();
stateService.setScreenSize({640,544});
stateService.startGame();
// std::cout << "StateService instance: " << service << std::endl;
// InitWindow(640, 544, "Jelly Rescue");
// // SetTargetFPS(60);
// // setScreen(EScreen::TITLE);
// #if defined(PLATFORM_WEB)
// emscripten_set_main_loop(step, 0, 1);
// #else
// while (!WindowShouldClose())
// {
// step();
// }
// #endif
// CloseWindow();
return 0;
}
}
// static void gameloop()
// {
// // InitAudioDevice();
// InitWindow(640, 544, "Jelly Rescue");
// // SetTargetFPS(60);
// // setScreen(EScreen::TITLE);
// #if defined(PLATFORM_WEB)
// emscripten_set_main_loop(step, 0, 1);
// #else
// while (!WindowShouldClose())
// {
// step();
// }
// #endif
// CloseWindow();
// // CloseAudioDevice();
// }
// void step()
// {
// ClearBackground(BLACK);
// }
+3 -3
View File
@@ -1,6 +1,6 @@
#include "GameScreen.h"
#include "Map.h"
#include "StateService.h"
#include "screens/GameScreen.h"
#include "entities/Map.h"
#include "services/StateService.h"
GameScreen::GameScreen()
{
+9 -6
View File
@@ -1,7 +1,8 @@
#include "MainScreen.h"
#include "AudioService.h"
#include "StateService.h"
#include "screens/MainScreen.h"
#include "services/AudioService.h"
#include "services/StateService.h"
#include "raylib.h"
#include "services/InputService.h"
MainScreen::MainScreen()
{
@@ -33,6 +34,8 @@ MainScreen::MainScreen()
AudioService::getInstance().setMusic(EMusic::MAIN);
AudioService::getInstance().playMusic();
}
InputService::getInstance().setKeysToWatch({KEY_ENTER, KEY_UP, KEY_DOWN}, {MOUSE_BUTTON_LEFT});
}
void MainScreen::logic()
@@ -78,18 +81,18 @@ void MainScreen::mouseLogic()
void MainScreen::keyboardLogic()
{
if (IsKeyPressed(KEY_DOWN) || IsKeyPressed(KEY_UP))
if (InputService::getInstance().isKeyPressed(KEY_DOWN) || InputService::getInstance().isKeyPressed(KEY_UP))
{
buttons[hoveredButton].setColor(WHITE);
/** Move up or down depending on the pressed button. +4 is used so that -1 becomes 3 (the last button) */
hoveredButton = ((hoveredButton - (IsKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4;
hoveredButton = ((hoveredButton - (InputService::getInstance().isKeyPressed(KEY_DOWN) ? -1 : 1)) + 4) % 4;
buttons[hoveredButton].setColor(RED);
}
}
void MainScreen::clickLogic()
{
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
std::string selectedButton = buttons[hoveredButton].getText();
+1 -4
View File
@@ -1,7 +1,4 @@
#include "Screen.h"
#include "AudioService.h"
#include <optional>
#include "screens/Screen.h"
// void Screen::draw()
// {
+10 -7
View File
@@ -1,9 +1,10 @@
#include "TitleScreen.h"
#include "AssetService.h"
#include "StateService.h"
#include "EFont.h"
#include "AudioService.h"
#include "screens/TitleScreen.h"
#include "services/AssetService.h"
#include "services/StateService.h"
#include "enums/EFont.h"
#include "services/AudioService.h"
#include <iostream>
#include "services/InputService.h"
TitleScreen::TitleScreen()
{
@@ -27,14 +28,16 @@ TitleScreen::TitleScreen()
AudioService::getInstance().setMusic(EMusic::MAIN);
AudioService::getInstance().playMusic();
}
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
}
void TitleScreen::logic()
{
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
// std::cout << "Next screen!" << std::endl;
if (InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
StateService::getInstance().setScreen(EScreen::MAIN);
// std::cout << "Next screen!" << std::endl;
}
}
+8 -5
View File
@@ -1,8 +1,9 @@
#include "TutorialScreen.h"
#include "screens/TutorialScreen.h"
#include "StateService.h"
#include "EFont.h"
#include "EScreen.h"
#include "services/StateService.h"
#include "enums/EFont.h"
#include "enums/EScreen.h"
#include "services/InputService.h"
TutorialScreen::TutorialScreen() {
Text textBack = Text("Press Enter to go back", EFont::SANSATION, 30, 0, RED);
@@ -11,11 +12,13 @@ TutorialScreen::TutorialScreen() {
this->text.push_back(textBack);
this->background = Background(EBackground::TUTORIAL);
InputService::getInstance().setKeysToWatch({KEY_ENTER}, {MOUSE_BUTTON_LEFT});
}
void TutorialScreen::logic()
{
if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
if ( InputService::getInstance().isKeyPressed(KEY_ENTER) || InputService::getInstance().isMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
StateService::getInstance().setScreen(EScreen::MAIN);
}
+5 -2
View File
@@ -1,5 +1,5 @@
#include "AssetService.h"
#include "services/AssetService.h"
#include <iostream>
AssetService::AssetService()
{
setAssetDir("assets/");
@@ -25,6 +25,9 @@ void AssetService::setAssets()
backgrounds[EBackground::GAME2] = loadTexture("Textures/GameBackground_2.jpg");
backgrounds[EBackground::GAME3] = loadTexture("Textures/GameBackground_3.png");
backgrounds[EBackground::VICTORY] = loadTexture("Textures/VictoryScreenBackground.png");
maps[EMap::TEXTURE] = "Maps/map1.txt";
maps[EMap::COLLISION] = "Maps/colmap1.txt";
}
Font AssetService::getFont(EFont font, int fontSize)
+2 -2
View File
@@ -1,5 +1,5 @@
#include "AudioService.h"
#include "AssetService.h"
#include "services/AudioService.h"
#include "services/AssetService.h"
// AudioService::~AudioService()
// {
+33 -7
View File
@@ -1,11 +1,12 @@
#include "StateService.h"
#include "services/StateService.h"
#include "raylib.h"
#include "TitleScreen.h"
#include "MainScreen.h"
#include "TutorialScreen.h"
#include "GameScreen.h"
#include "screens/TitleScreen.h"
#include "screens/MainScreen.h"
#include "screens/TutorialScreen.h"
#include "screens/GameScreen.h"
#include <optional>
#include "AudioService.h"
#include "services/AudioService.h"
#include "services/InputService.h"
void StateService::setScreen(EScreen state)
{
@@ -38,16 +39,30 @@ Screen *StateService::getScreen()
void StateService::startGame()
{
// if(!instance)
// return;
InitAudioDevice();
// InitWindow(640,544, "Jelly Rescue");
InitWindow(screenSize.x, screenSize.y, "Jelly Rescue");
SetTargetFPS(60);
// #if defined(PLATFORM_WEB)
// emscripten_set_keypress_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, staticStep);
// #endif
// #if defined(PLATFORM_WEB)
// emscripten_set_focusin_callback(EMSCRIPTEN_EVENT_TARGET_CANVAS, nullptr, 1, [](const EmscriptenFocusEvent* event) {
// std::cout << "Canvas Focused!" << std::endl;
// });
// #endif
setScreen(EScreen::TITLE);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(step, 0, 1);
emscripten_set_main_loop(StateService::staticStep, 0, 1);
#else
// while (!WindowShouldClose() && instance->gameRunning)
while (!WindowShouldClose() && gameRunning)
{
step();
@@ -69,7 +84,18 @@ void StateService::handleMusic()
void StateService::step()
{
InputService::getInstance().checkKeys();
handleMusic();
currentScreen->draw();
currentScreen->logic();
// if (IsKeyPressed(KEY_ENTER) || IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
// {
// setScreen(EScreen::MAIN);
// }
// if(instance) {
// instance->handleMusic();
// instance->currentScreen->draw();
// instance->currentScreen->logic();
// }
}