diff --git a/Dockerfile b/Dockerfile index 22c1de9..aadb1cb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -36,6 +36,7 @@ RUN git clone https://github.com/emscripten-core/emsdk.git && \ cd .. 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 \ && cd raylib \ @@ -50,4 +51,28 @@ RUN git clone https://github.com/raysan5/raylib.git 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"] diff --git a/scripts/build_and_run_project.sh b/scripts/build_and_run_project.sh index 705226f..96d0c67 100755 --- a/scripts/build_and_run_project.sh +++ b/scripts/build_and_run_project.sh @@ -1,5 +1,6 @@ #!/bin/bash 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 ./dist/jelly_rescue diff --git a/scripts/build_wasm.sh b/scripts/build_wasm.sh new file mode 100755 index 0000000..8e2a1cc --- /dev/null +++ b/scripts/build_wasm.sh @@ -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; \ No newline at end of file diff --git a/scripts/watch_project.sh b/scripts/watch_project.sh index 965c58d..5638ce4 100755 --- a/scripts/watch_project.sh +++ b/scripts/watch_project.sh @@ -1,2 +1,3 @@ #!/bin/bash -find ./src/ -name "*.cpp" -o -name "*.h" | entr -r ./scripts/build_and_run_project.sh \ No newline at end of file +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 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e005e2b..64c6d1c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,113 +1,15 @@ #include "raylib.h" -#include // For printf +#include "Screen.h" +#include "TitleScreen.h" +#include "StateService.h" #include -int main() + +int main(void) { - // SetConfigFlags(FLAG_MSAA_4X_HINT); - - 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(); + StateService& stateService = StateService::getInstance(); + stateService.gameloop(); + // std::cout << "StateService instance: " << service << std::endl; + 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; -// } +} \ No newline at end of file diff --git a/src/screens/MainScreen.cpp b/src/screens/MainScreen.cpp new file mode 100644 index 0000000..966bde0 --- /dev/null +++ b/src/screens/MainScreen.cpp @@ -0,0 +1,46 @@ +#include "MainScreen.h" +#include "AssetService.h" +#include + +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(); +} \ No newline at end of file diff --git a/src/screens/Screen.cpp b/src/screens/Screen.cpp new file mode 100644 index 0000000..b75b096 --- /dev/null +++ b/src/screens/Screen.cpp @@ -0,0 +1,22 @@ +#include "Screen.h" +#include + +Screen::Screen() { + +} + +Screen::~Screen() { + +} + +void Screen::step() { + +} + +void Screen::logic() { + +} + +void Screen::draw() { + std::cout << "DURAWU" << std::endl; +} diff --git a/src/screens/TitleScreen.cpp b/src/screens/TitleScreen.cpp new file mode 100644 index 0000000..04475a1 --- /dev/null +++ b/src/screens/TitleScreen.cpp @@ -0,0 +1,48 @@ +#include "TitleScreen.h" +#include "AssetService.h" +#include "StateService.h" +#include + +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(); +} \ No newline at end of file diff --git a/src/services/AssetService.cpp b/src/services/AssetService.cpp new file mode 100644 index 0000000..6f5eb38 --- /dev/null +++ b/src/services/AssetService.cpp @@ -0,0 +1,40 @@ +#include "AssetService.h" +#include + +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; +} diff --git a/src/services/StateService.cpp b/src/services/StateService.cpp new file mode 100644 index 0000000..ffa41c3 --- /dev/null +++ b/src/services/StateService.cpp @@ -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(); + break; + case ScreenState::MAIN: + currentScreen = std::make_unique(); + break; + default: + currentScreen = std::make_unique(); // 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(); +} \ No newline at end of file