1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-06-09 01:10:43 +00:00

Added start of scene management

This commit is contained in:
Tim Sarbin 2019-02-24 21:51:20 -05:00
parent 56b2f146d3
commit e68eafd5f6
13 changed files with 179 additions and 93 deletions

View File

@ -56,6 +56,10 @@ set( BGFX_CONFIG_DEBUG OFF )
set( SDL_STATIC_PIC ON )
set( SDL_SHARED OFF )
set( BUILD_SHARED_LIBS OFF )
set( CMAKE_CXX_STANDARD 17 )
set( CXX_STANDARD 17 )
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/spdlog spdlog)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../3rdparty/cli11 cli11)

View File

@ -1,24 +1,30 @@
add_executable(OpenDiablo2.Game
src/main.cpp
src/D2Engine.cpp
set (SOURCES
src/main.cpp
src/D2Engine.cpp
src/Scenes/D2MainMenu.cpp
)
set (HEADERS
include/OpenDiablo2.Game/D2Engine.h
include/OpenDiablo2.Game/D2EngineConfig.h
include/OpenDiablo2.Game/Scenes/D2Scene.h
include/OpenDiablo2.Game/Scenes/D2MainMenu.h
)
add_executable(OpenDiablo2.Game ${SOURCES} ${HEADERS})
target_include_directories(OpenDiablo2.SDL2
PUBLIC
include
PRIVATE
include
)
target_link_libraries(OpenDiablo2.Game
OpenDiablo2.SDL2
storm
CLI11::CLI11
stdc++fs
spdlog::spdlog
PUBLIC
include
PRIVATE
include
)
set(CMAKE_CXX_STANDARD 17)
set(CXX_STANDARD 17)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
target_link_libraries(OpenDiablo2.Game
OpenDiablo2.SDL2
storm
CLI11::CLI11
stdc++fs
spdlog::spdlog
)

View File

@ -1,26 +1,41 @@
#ifndef OPENDIABLO2_D2ENGINE_H
#define OPENDIABLO2_D2ENGINE_H
#include <stack>
#include <memory>
#include <OpenDiablo2.System/D2Graphics.h>
#include <OpenDiablo2.System/D2Input.h>
#include <OpenDiablo2.Game/Scenes/D2Scene.h>
#include "D2EngineConfig.h"
namespace OpenDiablo2 {
namespace Game {
class D2Engine {
public:
D2Engine(const D2EngineConfig& config);
void Run();
private:
const D2EngineConfig config;
OpenDiablo2::System::D2Graphics::Ptr gfx;
OpenDiablo2::System::D2Input::Ptr input;
bool isRunning = true;
namespace OpenDiablo2::Game
{
// The main OpenDiablo2 engine
class D2Engine : public std::enable_shared_from_this<D2Engine>
{
public:
D2Engine(const D2EngineConfig &config);
// Runs the engine
void Run();
private:
// Represents the engine configuration
const D2EngineConfig config;
// The graphics subsystem
OpenDiablo2::System::D2Graphics::Ptr gfx;
// The input subsystem
OpenDiablo2::System::D2Input::Ptr input;
// Indicates the system should keep running (if set to true)
bool isRunning = true;
std::stack<std::shared_ptr<OpenDiablo2::Game::Scenes::D2Scene>> sceneStack;
};
};
}
}
#endif //OPENDIABLO2_D2ENGINE_H

View File

@ -3,14 +3,14 @@
#include <string>
namespace OpenDiablo2 {
namespace Game {
namespace OpenDiablo2::Game
{
struct D2EngineConfig {
std::string BasePath; // The base path where the MPQ files are located
};
struct D2EngineConfig
{
std::string BasePath; // The base path where the MPQ files are located
};
}
}
} // namespace OpenDiablo2::Game
#endif //OPENDIABLO2_D2ENGINECONFIG_H

View File

@ -0,0 +1,19 @@
#ifndef OPENDIALOB2_SCENES_D2MAINMENU_H
#define OPENDIALOB2_SCENES_D2MAINMENU_H
#include <OpenDiablo2.Game/Scenes/D2Scene.h>
#include <OpenDiablo2.Game/D2Engine.h>
namespace OpenDiablo2::Game::Scenes {
class MainMenu : public D2Scene {
public:
MainMenu(std::shared_ptr<D2Engine> engine);
void Render();
void Update();
private:
std::shared_ptr<D2Engine> engine;
};
}
#endif // OPENDIALOB2_SCENES_D2MAINMENU_H

View File

@ -0,0 +1,18 @@
#ifndef OPENDIABLO2_D2SCENE_H
#define OPENDIABLO2_D2SCENE_H
#include <memory>
namespace OpenDiablo2::Game::Scenes
{
class D2Scene
{
public:
virtual void Render() = 0;
virtual void Update() = 0;
};
}
#endif

View File

@ -1,4 +1,5 @@
#include <OpenDiablo2.Game/D2Engine.h>
#include <OpenDiablo2.Game/Scenes/D2MainMenu.h>
OpenDiablo2::Game::D2Engine::D2Engine(const D2EngineConfig &config)
@ -11,14 +12,17 @@ void
OpenDiablo2::Game::D2Engine::Run() {
gfx->InitializeWindow();
sceneStack.emplace(std::make_shared<Scenes::MainMenu>(shared_from_this()));
while (isRunning) {
input->ProcessEvents();
sceneStack.top()->Update();
if (input->QuitIsRequested()) {
isRunning = false;
break;
}
gfx->Clear();
sceneStack.top()->Render();
gfx->Present();
}
}

View File

@ -0,0 +1,14 @@
#include <OpenDiablo2.Game/Scenes/D2MainMenu.h>
OpenDiablo2::Game::Scenes::MainMenu::MainMenu(std::shared_ptr<D2Engine> engine)
: engine(engine) {
}
void OpenDiablo2::Game::Scenes::MainMenu::Render() {
}
void OpenDiablo2::Game::Scenes::MainMenu::Update() {
}

View File

@ -37,8 +37,8 @@ main(int argc,
SFileCloseFile(mpq);
spdlog::info("Content files were located, starting engine.");
// Start up the engine
auto engine = std::make_unique<OpenDiablo2::Game::D2Engine>(engineConfig);
// Off to the races we go!
auto engine = std::make_shared<OpenDiablo2::Game::D2Engine>(engineConfig);
engine->Run();
return 0;

View File

@ -1,10 +1,17 @@
project(OpenDiablo2.SDL2 VERSION 0.1 LANGUAGES CXX)
add_library(OpenDiablo2.SDL2 STATIC
set (SOURCES
src/D2Graphics.cpp
src/D2Input.cpp
)
set (HEADERS
include/OpenDiablo2.System/D2Graphics.h
include/OpenDiablo2.System/D2Input.h
)
add_library(OpenDiablo2.SDL2 SHARED ${SOURCES} ${HEADERS})
target_include_directories(OpenDiablo2.SDL2
PUBLIC
include
@ -14,5 +21,5 @@ target_include_directories(OpenDiablo2.SDL2
target_link_libraries(OpenDiablo2.SDL2
spdlog::spdlog
SDL2
SDL2-static
)

View File

@ -5,41 +5,43 @@
#include <spdlog/spdlog.h>
#include <SDL2/SDL.h>
namespace OpenDiablo2::System
{
namespace OpenDiablo2 {
namespace System {
struct SDLWindowDestroyer
{
void operator()(SDL_Window* w) const
{
spdlog::debug("Destroying SDL window");
if (w) SDL_DestroyWindow(w);
}
};
struct SDLRendererDestroyer
{
void operator()(SDL_Renderer* r) const
{
spdlog::debug("Destroying SDL renderer");
if (r) SDL_DestroyRenderer(r);
}
};
class D2Graphics {
public:
typedef std::unique_ptr<D2Graphics> Ptr;
D2Graphics();
void InitializeWindow();
void Clear();
void Present();
private:
std::unique_ptr<SDL_Window, SDLWindowDestroyer> window;
std::unique_ptr<SDL_Renderer, SDLRendererDestroyer> renderer;
};
struct SDLWindowDestroyer
{
void operator()(SDL_Window *w) const
{
spdlog::debug("Destroying SDL window");
if (w)
SDL_DestroyWindow(w);
}
};
struct SDLRendererDestroyer
{
void operator()(SDL_Renderer *r) const
{
spdlog::debug("Destroying SDL renderer");
if (r)
SDL_DestroyRenderer(r);
}
};
class D2Graphics
{
public:
typedef std::unique_ptr<D2Graphics> Ptr;
D2Graphics();
void InitializeWindow();
void Clear();
void Present();
private:
std::unique_ptr<SDL_Window, SDLWindowDestroyer> window;
std::unique_ptr<SDL_Renderer, SDLRendererDestroyer> renderer;
};
}
#endif //OPENDIABLO2_D2GRAPHICS_H

View File

@ -3,21 +3,18 @@
#include <memory>
namespace OpenDiablo2 {
namespace System {
namespace OpenDiablo2::System {
class D2Input {
public:
typedef std::unique_ptr<D2Input> Ptr;
D2Input();
void ProcessEvents();
bool QuitIsRequested();
private:
bool quitIsRequested = false;
};
class D2Input {
public:
typedef std::unique_ptr<D2Input> Ptr;
D2Input();
void ProcessEvents();
bool QuitIsRequested();
private:
bool quitIsRequested = false;
};
}
}
#endif //OPENDIABLO2_D2INPUT_H

View File

@ -34,7 +34,7 @@ namespace OpenDiablo2 {
exit(1);
}
spdlog::debug("Destroying SDL renderer");
spdlog::debug("Initializing SDL renderer");
renderer = std::unique_ptr<SDL_Renderer, SDLRendererDestroyer>(SDL_CreateRenderer(window.get(), -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC));
if (renderer == nullptr){
spdlog::error("Could not create sdl2 window: " + std::string(SDL_GetError()));