Skip to content

Commit

Permalink
feat: add state engine (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
alegemaate authored Dec 17, 2023
1 parent 7008062 commit b3e15c8
Show file tree
Hide file tree
Showing 17 changed files with 639 additions and 24 deletions.
69 changes: 68 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,74 @@
"ostream": "cpp",
"cstddef": "cpp",
"fstream": "cpp",
"*.tcc": "cpp"
"*.tcc": "cpp",
"memory": "cpp",
"cctype": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"cstdarg": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"charconv": "cpp",
"chrono": "cpp",
"codecvt": "cpp",
"compare": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"cstdint": "cpp",
"deque": "cpp",
"forward_list": "cpp",
"list": "cpp",
"map": "cpp",
"set": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"vector": "cpp",
"exception": "cpp",
"algorithm": "cpp",
"iterator": "cpp",
"memory_resource": "cpp",
"numeric": "cpp",
"optional": "cpp",
"random": "cpp",
"ratio": "cpp",
"source_location": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"format": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"istream": "cpp",
"limits": "cpp",
"mutex": "cpp",
"new": "cpp",
"numbers": "cpp",
"ranges": "cpp",
"semaphore": "cpp",
"span": "cpp",
"sstream": "cpp",
"stdexcept": "cpp",
"stdfloat": "cpp",
"stop_token": "cpp",
"streambuf": "cpp",
"thread": "cpp",
"cinttypes": "cpp",
"typeinfo": "cpp",
"valarray": "cpp",
"variant": "cpp"
},
"sonarlint.pathToCompileCommands": "${workspaceFolder}/compile_commands.json"
}
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ WIP Open Source Stornghold Crusader clone

## Setup

### Assets

Copy your entire Stronghold Crusader folder to `assets/proprietary/`

### Windows (MSYS2)

```bash
Expand Down
4 changes: 2 additions & 2 deletions src/core/asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "../lib/parsers/tgx_parser.h"
#include "core.h"

namespace oshc::asset
namespace oshc::core::asset
{

// Texture
Expand Down Expand Up @@ -173,4 +173,4 @@ bool Music::is_playing() const
return Mix_PlayingMusic() == 1;
}

} // namespace oshc::asset
} // namespace oshc::core::asset
4 changes: 2 additions & 2 deletions src/core/asset.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include <memory>
#include <vector>

namespace oshc::asset
namespace oshc::core::asset
{

/**
Expand Down Expand Up @@ -205,4 +205,4 @@ class Music : public Asset
/// @brief Shared pointer to underlying music
std::shared_ptr<Mix_Music> m_data;
};
} // namespace oshc::asset
} // namespace oshc::core::asset
4 changes: 2 additions & 2 deletions src/core/asset_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <iostream>
#include <nlohmann/json.hpp>

namespace oshc::asset
namespace oshc::core::asset
{

// Load assets from json
Expand Down Expand Up @@ -68,4 +68,4 @@ Music AssetManager::get_music(const std::string &id)
return music[id];
}

} // namespace oshc::asset
} // namespace oshc::core::asset
4 changes: 2 additions & 2 deletions src/core/asset_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

#include "asset.h"

namespace oshc::asset
namespace oshc::core::asset
{

class AssetManager
Expand Down Expand Up @@ -67,4 +67,4 @@ class AssetManager
std::map<std::string, Music, std::less<>> music;
};

} // namespace oshc::asset
} // namespace oshc::core::asset
4 changes: 3 additions & 1 deletion src/core/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ SDL_Renderer *renderer = nullptr;

SDL_Window *window = nullptr;

oshc::asset::AssetManager asset_manager = oshc::asset::AssetManager();
oshc::core::asset::AssetManager asset_manager = oshc::core::asset::AssetManager();

oshc::core::state::StateEngine state_engine = oshc::core::state::StateEngine();

void init()
{
Expand Down
6 changes: 5 additions & 1 deletion src/core/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#pragma once

#include "asset_manager.h"
#include "state.h"

#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
Expand All @@ -29,7 +30,10 @@ extern SDL_Renderer *renderer;
extern SDL_Window *window;

/// @brief Global asset manager
extern oshc::asset::AssetManager asset_manager;
extern oshc::core::asset::AssetManager asset_manager;

// @breif Global state engine
extern oshc::core::state::StateEngine state_engine;

/**
* @brief Initialize SDL2 and create window
Expand Down
100 changes: 100 additions & 0 deletions src/core/state.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "state.h"

#include <SDL2/SDL.h>
#include <iostream>

#include "core.h"

#include "../game/state_game.h"
#include "../game/state_init.h"
#include "../game/state_intro.h"
#include "../game/state_menu.h"

namespace oshc::core::state
{

void StateEngine::render() const
{
if (!m_state)
{
return;
}

SDL_RenderClear(oshc::core::renderer);
m_state->render();
SDL_RenderPresent(oshc::core::renderer);
}

void StateEngine::update()
{
if (m_state)
{
m_state->update();
}

change_state();
}

void StateEngine::set_next_state(const ProgramState state)
{
m_next_state = state;
}

ProgramState StateEngine::get_active_state_id() const
{
return m_active_state;
}

void StateEngine::change_state()
{
// If the state needs to be changed
if (m_next_state == ProgramState::STATE_NULL)
{
return;
}

// Delete the current state
if (m_state)
{
m_state->destroy();
m_state = nullptr;
}

// Change the state
switch (m_next_state)
{
case ProgramState::STATE_GAME:
m_state = std::make_unique<oshc::state::StateGame>(*this);
std::cout << "Switched state to game." << std::endl;
break;

case ProgramState::STATE_MENU:
m_state = std::make_unique<oshc::state::StateMenu>(*this);
std::cout << "Switched state to main menu." << std::endl;
break;

case ProgramState::STATE_INIT:
m_state = std::make_unique<oshc::state::StateInit>(*this);
std::cout << "Switched state to init." << std::endl;
break;

case ProgramState::STATE_INTRO:
m_state = std::make_unique<oshc::state::StateIntro>(*this);
std::cout << "Switched state to intro." << std::endl;
break;

default:
std::cout << "Exiting program." << std::endl;
break;
}

m_state->init();

// Change the current state ID
m_active_state = m_next_state;

// NULL the next state ID
m_next_state = ProgramState::STATE_NULL;
}

} // namespace oshc::core::state
Loading

0 comments on commit b3e15c8

Please sign in to comment.