[Added] Start of State Management

This commit is contained in:
Ninjaws
2024-12-06 23:51:56 +01:00
parent 0c1f12d534
commit 5d2251c0f4
10 changed files with 250 additions and 110 deletions
+25
View File
@@ -36,6 +36,7 @@ RUN git clone https://github.com/emscripten-core/emsdk.git && \
cd .. cd ..
RUN echo 'source "/app/emsdk/emsdk_env.sh"' >> $HOME/.bash_profile RUN echo 'source "/app/emsdk/emsdk_env.sh"' >> $HOME/.bash_profile
# RUN source "/app/emsdk/emsdk_env.sh"
RUN git clone https://github.com/raysan5/raylib.git raylib \ RUN git clone https://github.com/raysan5/raylib.git raylib \
&& cd raylib \ && cd raylib \
@@ -50,4 +51,28 @@ RUN git clone https://github.com/raysan5/raylib.git raylib \
ENV LD_LIBRARY_PATH=/app/raylib/build/raylib ENV LD_LIBRARY_PATH=/app/raylib/build/raylib
# RUN cd raylib/src
# # Builds raylib for WebAssembly usage
# RUN emcc -c rcore.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2
# RUN emcc -c rshapes.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2
# RUN emcc -c rtextures.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2
# RUN emcc -c rtext.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2
# RUN emcc -c rmodels.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2
# RUN emcc -c utils.c -Os -Wall -DPLATFORM_WEB
# RUN emcc -c raudio.c -Os -Wall -DPLATFORM_WEB
# RUN emar rcs libraylib.a rcore.o rshapes.o rtextures.o rtext.o rmodels.o utils.o raudio.o
# RUN cd ../..
RUN cd raylib/src && \
source /app/emsdk/emsdk_env.sh && \
emcc -c rcore.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2 && \
emcc -c rshapes.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2 && \
emcc -c rtextures.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2 && \
emcc -c rtext.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2 && \
emcc -c rmodels.c -Os -Wall -DPLATFORM_WEB -DGRAPHICS_API_OPENGL_ES2 && \
emcc -c utils.c -Os -Wall -DPLATFORM_WEB && \
emcc -c raudio.c -Os -Wall -DPLATFORM_WEB && \
emar rcs libraylib.a rcore.o rshapes.o rtextures.o rtext.o rmodels.o utils.o raudio.o && \
cd ../..
CMD ["bash"] CMD ["bash"]
+2 -1
View File
@@ -1,5 +1,6 @@
#!/bin/bash #!/bin/bash
clear clear
g++ src/main.cpp -o dist/jelly_rescue -L$LD_LIBRARY_PATH -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 # g++ src/main.cpp -o dist/jelly_rescue -L$LD_LIBRARY_PATH -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
g++ $(find src/ -name "*.cpp") -o dist/jelly_rescue -I./include -L$LD_LIBRARY_PATH -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
# g++ src/main.cpp -o dist/jelly_rescue -L$LD_LIBRARY_PATH -lraylib -lGL -lm -lpthread -ldl -lrt -lX11 # g++ src/main.cpp -o dist/jelly_rescue -L$LD_LIBRARY_PATH -lraylib -lGL -lm -lpthread -ldl -lrt -lX11
./dist/jelly_rescue ./dist/jelly_rescue
+5
View File
@@ -0,0 +1,5 @@
#!/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
# --shell-file /app/raylib/src/shell.html
cd dist
python3 -m http.server 8080;
+2 -1
View File
@@ -1,2 +1,3 @@
#!/bin/bash #!/bin/bash
find ./src/ -name "*.cpp" -o -name "*.h" | entr -r ./scripts/build_and_run_project.sh find src/ -name "*.cpp" -o -wholename "./include/**/*.h" | entr -r ./scripts/build_and_run_project.sh
# find ./include/ -name "*.h" | entr -r ./scripts/build_and_run_project.sh
+8 -106
View File
@@ -1,113 +1,15 @@
#include "raylib.h" #include "raylib.h"
#include <cstdio> // For printf #include "Screen.h"
#include "TitleScreen.h"
#include "StateService.h"
#include <iostream> #include <iostream>
int main()
int main(void)
{ {
// SetConfigFlags(FLAG_MSAA_4X_HINT); StateService& stateService = StateService::getInstance();
stateService.gameloop();
// std::cout << "StateService instance: " << service << std::endl;
InitAudioDevice();
SetTargetFPS(60);
InitWindow(640, 544, "Jelly Rescue");
// Main game loop
// Texture2D background = LoadTexture("/app/dist/Data/Textures/TitleScreenBackground.png");
Font font1 = LoadFontEx("./dist/Data/Fonts/Reckoner_Bold.ttf", 100,0,0);
SetTextureFilter(font1.texture, TEXTURE_FILTER_BILINEAR);
Font font2 = LoadFont("./dist/Data/Fonts/Sansation.ttf");
SetTextureFilter(font2.texture, TEXTURE_FILTER_BILINEAR);
Music bg = LoadMusicStream("./dist/Data/Audio/Music/Crash_WarpRoomTheme.ogg");
bg.looping = true;
SetMasterVolume(1.0f);
SetMusicVolume(bg,1.0f);
PlayMusicStream(bg);
Vector2 dimTitle = MeasureTextEx(font1, "Jelly rescue", 100,0);
Vector2 dimSub = MeasureTextEx(font2, "Press Enter to continue", 30,0);
Vector2 dimHead = MeasureTextEx(font2, "Made by Ian Vink", 20,0);
while (!WindowShouldClose())
{
UpdateMusicStream(bg);
BeginDrawing();
ClearBackground(BLACK);
DrawTextEx(font2,"Made by Ian Vink", {((float)GetScreenWidth()-dimHead.x)/1.2f, (float(GetScreenHeight()-dimHead.y)/6.0f)}, 20, 0, WHITE);
DrawTextEx(font1, "Jelly rescue", {((float)GetScreenWidth()-dimTitle.x)/2.0f, (float(GetScreenHeight()-dimTitle.y)/2.0f)}, 100,0, {255,25,25,255});
DrawTextEx(font2,"Press Enter to continue", {((float)GetScreenWidth()-dimSub.x)/2.0f, (float(GetScreenHeight()-dimSub.y)/1.5f)}, 30, 0, WHITE);
EndDrawing();
}
// UnloadTexture(background);
CloseWindow();
UnloadMusicStream(bg);
CloseAudioDevice();
return 0; return 0;
} }
// class TitleScreen {
// public:
// Image background;
// }
// game.SetMainMusic("Data/Audio/Music/Crash_WarpRoomTheme.ogg", 50);
// game.SetTitleFont("Data/Fonts/Reckoner_Bold.ttf");
// game.SetTitleText("Jelly rescue", 100, sf::Color::Red);
// game.SetUnderTitleFont("Data/Fonts/Sansation.ttf");
// game.SetUnderTitleText(30, sf::Color::White);
// game.SetNameText(20, sf::Color::White);
// void TitleScreen::DrawTitleScreenBackground()
// {
// GetWindow().draw(titleScreenBackground);
// }
// void TitleScreen::SetTitleFont(std::string fontlocation)
// {
// titleFont.loadFromFile(fontlocation);
// }
// void TitleScreen::SetTitleText(std::string titletext, float textsize, sf::Color color)
// {
// titleText.setFont(titleFont);
// titleText.setPosition(GetWindow().getSize().x / 7, GetWindow().getSize().y / 3);
// titleText.setCharacterSize(textsize);
// titleText.setColor(color);
// titleText.setString(titletext);
// }
// void TitleScreen::SetUnderTitleFont(std::string fontlocation)
// {
// underTitleFont.loadFromFile(fontlocation);
// }
// void TitleScreen::SetUnderTitleText(float textsize, sf::Color color)
// {
// underTitleText.setFont(underTitleFont);
// underTitleText.setPosition(GetWindow().getSize().x / 4, GetWindow().getSize().y / 3 * 2);
// underTitleText.setCharacterSize(textsize);
// underTitleText.setColor(color);
// underTitleText.setString("Press Enter to continue");
// }
// void TitleScreen::SetNameText(float textsize, sf::Color color)
// {
// nameText.setFont(underTitleFont);
// nameText.setPosition(GetWindow().getSize().x / 1.5, GetWindow().getSize().y / 8);
// nameText.setCharacterSize(textsize);
// nameText.setColor(color);
// nameText.setString("Made by Ian Vink");
// }
// void TitleScreen::SetTitleScreenBackground(std::string texturelocation)
// {
// sf::Vector2f screenDimensions;
// screenDimensions.x = GetWindow().getSize().x;
// screenDimensions.y = GetWindow().getSize().y;
// titleScreenTexture.loadFromFile(texturelocation);
// titleScreenBackground.setTexture(&titleScreenTexture);
// titleScreenBackground.setSize(screenDimensions);
// std::cout << "Titlescreen background loaded" << std::endl;
// }
+46
View File
@@ -0,0 +1,46 @@
#include "MainScreen.h"
#include "AssetService.h"
#include <iostream>
MainScreen::MainScreen() {
assetDir = "assets";
font1 = LoadFontEx((assetDir+"/Fonts/Reckoner_Bold.ttf").c_str(), 100,0,0);
SetTextureFilter(font1.texture, TEXTURE_FILTER_BILINEAR);
font2 = LoadFont((assetDir+"/Fonts/Sansation.ttf").c_str());
SetTextureFilter(font2.texture, TEXTURE_FILTER_BILINEAR);
dimTitle = MeasureTextEx(font1, "Jelly sadsdsdsdrescue", 100,0);
dimSub = MeasureTextEx(font2, "Press Entsder to continue", 30,0);
dimHead = MeasureTextEx(font2, "Made by Ian Vink", 20,0);
bg = LoadMusicStream((assetDir+"/Audio/Music/Crash_WarpRoomTheme.ogg").c_str());
bg.looping = true;
SetMasterVolume(1.0f);
SetMusicVolume(bg,1.0f);
PlayMusicStream(bg);
}
MainScreen::~MainScreen() {
UnloadMusicStream(bg);
UnloadFont(font1);
UnloadFont(font2);
}
void MainScreen::step() {
draw();
logic();
}
void MainScreen::logic() {
// if(IsKeyPressed(KEY_ENTER)) {
// std::cout << "Next screen!" << std::endl;
// }
}
void MainScreen::draw() {
UpdateMusicStream(bg);
BeginDrawing();
ClearBackground(BLACK);
DrawTextEx(font2,"Made by Ian Vink", {((float)GetScreenWidth()-dimHead.x)/1.2f, (float(GetScreenHeight()-dimHead.y)/6.0f)}, 20, 0, WHITE);
DrawTextEx(font1,"Jelly rescue", {((float)GetScreenWidth()-dimTitle.x)/2.0f, (float(GetScreenHeight()-dimTitle.y)/2.0f)}, 100,0, {255,25,25,255});
DrawTextEx(font2,"Press Enter to continue", {((float)GetScreenWidth()-dimSub.x)/2.0f, (float(GetScreenHeight()-dimSub.y)/1.5f)}, 30, 0, WHITE);
EndDrawing();
}
+22
View File
@@ -0,0 +1,22 @@
#include "Screen.h"
#include <iostream>
Screen::Screen() {
}
Screen::~Screen() {
}
void Screen::step() {
}
void Screen::logic() {
}
void Screen::draw() {
std::cout << "DURAWU" << std::endl;
}
+48
View File
@@ -0,0 +1,48 @@
#include "TitleScreen.h"
#include "AssetService.h"
#include "StateService.h"
#include <iostream>
TitleScreen::TitleScreen() {
assetDir = "assets";
font1 = LoadFontEx((assetDir+"/Fonts/Reckoner_Bold.ttf").c_str(), 100,0,0);
SetTextureFilter(font1.texture, TEXTURE_FILTER_BILINEAR);
font2 = LoadFont((assetDir+"/Fonts/Sansation.ttf").c_str());
SetTextureFilter(font2.texture, TEXTURE_FILTER_BILINEAR);
dimTitle = MeasureTextEx(font1, "Jelly rescue", 100,0);
dimSub = MeasureTextEx(font2, "Press Enter to continue", 30,0);
dimHead = MeasureTextEx(font2, "Made by Ian Vink", 20,0);
bg = LoadMusicStream((assetDir+"/Audio/Music/Crash_WarpRoomTheme.ogg").c_str());
bg.looping = true;
SetMasterVolume(1.0f);
SetMusicVolume(bg,1.0f);
PlayMusicStream(bg);
}
TitleScreen::~TitleScreen() {
UnloadMusicStream(bg);
UnloadFont(font1);
UnloadFont(font2);
}
void TitleScreen::step() {
draw();
logic();
}
void TitleScreen::logic() {
if(IsKeyPressed(KEY_ENTER)) {
StateService::getInstance().setScreen(ScreenState::MAIN);
// std::cout << "Next screen!" << std::endl;
}
}
void TitleScreen::draw() {
UpdateMusicStream(bg);
BeginDrawing();
ClearBackground(BLACK);
DrawTextEx(font2,"Made by Ian Vink", {((float)GetScreenWidth()-dimHead.x)/1.2f, (float(GetScreenHeight()-dimHead.y)/6.0f)}, 20, 0, WHITE);
DrawTextEx(font1,"Jelly rescue", {((float)GetScreenWidth()-dimTitle.x)/2.0f, (float(GetScreenHeight()-dimTitle.y)/2.0f)}, 100,0, {255,25,25,255});
DrawTextEx(font2,"Press Enter to continue", {((float)GetScreenWidth()-dimSub.x)/2.0f, (float(GetScreenHeight()-dimSub.y)/1.5f)}, 30, 0, WHITE);
EndDrawing();
}
+40
View File
@@ -0,0 +1,40 @@
#include "AssetService.h"
#include <iostream>
void AssetService::setAssetDir(std::string newDir) {
assetDir = newDir;
}
void AssetService::setAssets() {
setAssetDir("assets/");
fonts.push_back(loadFont("Fonts/Gtek.ttf"));
fonts.push_back(loadFont("Fonts/Oxin.ttf"));
fonts.push_back(loadFont("Fonts/Reckoner_Bold.ttf"));
fonts.push_back(loadFont("Fonts/Reckoner.ttf"));
fonts.push_back(loadFont("Fonts/Sansation.ttf"));
music.push_back(loadMusic("Audio/Music/Crash_WarpRoomTheme.ogg", true));
}
Font AssetService::loadFont(std::string filename, int fontSize) {
Font font = LoadFontEx((assetDir + filename).c_str(), fontSize,0,0);
return font;
}
Font AssetService::loadFont(std::string filename) {
Font font = LoadFont((assetDir + filename).c_str());
return font;
}
Music AssetService::loadMusic(std::string filename, bool loops) {
Music music = LoadMusicStream((assetDir + filename).c_str());
music.looping = loops;
return music;
}
Sound AssetService::loadSound(std::string filename)
{
Sound sound = LoadSound((assetDir + filename).c_str());
return sound;
}
+50
View File
@@ -0,0 +1,50 @@
#include "StateService.h"
#include "raylib.h"
#include "TitleScreen.h"
#include "MainScreen.h"
StateService* StateService::instance = nullptr;
void StateService::setScreen(ScreenState state)
{
switch (state)
{
case ScreenState::TITLE:
currentScreen = std::make_unique<TitleScreen>();
break;
case ScreenState::MAIN:
currentScreen = std::make_unique<MainScreen>();
break;
default:
currentScreen = std::make_unique<TitleScreen>(); // Default case
}
// currentScreen = screen;
}
Screen* StateService::getScreen()
{
return currentScreen.get();
}
void StateService::gameloop()
{
InitAudioDevice();
InitWindow(640, 544, "Jelly Rescue");
SetTargetFPS(60);
setScreen(ScreenState::TITLE);
#if defined(PLATFORM_WEB)
emscripten_set_main_loop(currentScreen->step, 0, 1);
#else
while (!WindowShouldClose())
{
currentScreen->step();
// UpdateDrawFrame();
}
#endif
CloseWindow();
CloseAudioDevice();
}