[Added] Start of State Management
This commit is contained in:
+10
-108
@@ -1,113 +1,15 @@
|
||||
#include "raylib.h"
|
||||
#include <cstdio> // For printf
|
||||
#include "Screen.h"
|
||||
#include "TitleScreen.h"
|
||||
#include "StateService.h"
|
||||
#include <iostream>
|
||||
|
||||
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;
|
||||
// }
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
Reference in New Issue
Block a user