From 2f118631dc134f522a4f3cdcf305951ca0b29aad Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Sun, 16 Oct 2016 18:21:50 +0100 Subject: [PATCH] Move some non-game code into GameBase class This moves window setup and configuration loading into a base class so that RWGame can focus on game related matters --- rwgame/CMakeLists.txt | 2 + rwgame/GameBase.cpp | 69 +++++++++++++++++++++++++++++++ rwgame/GameBase.hpp | 33 +++++++++++++++ rwgame/RWGame.cpp | 95 +++++-------------------------------------- rwgame/RWGame.hpp | 19 +-------- rwgame/game.hpp | 2 - 6 files changed, 117 insertions(+), 103 deletions(-) create mode 100644 rwgame/GameBase.cpp create mode 100644 rwgame/GameBase.hpp diff --git a/rwgame/CMakeLists.txt b/rwgame/CMakeLists.txt index 050064d3..57dda9bd 100644 --- a/rwgame/CMakeLists.txt +++ b/rwgame/CMakeLists.txt @@ -8,6 +8,8 @@ set(RWGAME_SOURCES main.cpp + GameBase.hpp + GameBase.cpp RWGame.cpp GameConfig.cpp diff --git a/rwgame/GameBase.cpp b/rwgame/GameBase.cpp new file mode 100644 index 00000000..b80ccc9f --- /dev/null +++ b/rwgame/GameBase.cpp @@ -0,0 +1,69 @@ +#include "GameBase.hpp" +#include "GitSHA1.h" + +#include "SDL.h" + +// Use first 8 chars of git hash as the build string +const std::string kBuildStr(kGitSHA1Hash, 8); +const std::string kWindowTitle = "RWGame"; +constexpr int kWindowWidth = 800; +constexpr int kWindowHeight = 600; + +GameBase::GameBase(Logger &inlog, int argc, char *argv[]) : log(inlog) { + log.info("Game", "Build: " + kBuildStr); + + if (!config.isValid()) { + throw std::runtime_error("Invalid configuration file at: " + + config.getConfigFile()); + } + + size_t w = kWindowWidth, h = kWindowHeight; + bool fullscreen = false; + bool help = false; + + // Define and parse command line options + namespace po = boost::program_options; + po::options_description desc("Available options"); + desc.add_options()("help", "Show this help message")( + "width,w", po::value(), "Game resolution width in pixel")( + "height,h", po::value(), "Game resolution height in pixel")( + "fullscreen,f", "Enable fullscreen mode")("newgame,n", + "Directly start a new game")( + "test,t", "Starts a new game in a test location")( + "load,l", po::value(), "Load save file")( + "benchmark,b", po::value(), "Run benchmark from file"); + + po::variables_map &vm = options; + try { + po::store(po::parse_command_line(argc, argv, desc), vm); + po::notify(vm); + } catch (po::error &ex) { + help = true; + std::cout << "Error parsing arguments: " << ex.what() << std::endl; + } + + if (help || vm.count("help")) { + std::cout << desc; + throw std::invalid_argument(""); + } + if (vm.count("width")) { + w = vm["width"].as(); + } + if (vm.count("height")) { + h = vm["height"].as(); + } + if (vm.count("fullscreen")) { + fullscreen = true; + } + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + throw std::runtime_error("Failed to initialize SDL2!"); + + window.create(kWindowTitle + " [" + kBuildStr + "]", w, h, fullscreen); +} + +GameBase::~GameBase() { + SDL_Quit(); + + log.info("Game", "Done cleaning up"); +} diff --git a/rwgame/GameBase.hpp b/rwgame/GameBase.hpp new file mode 100644 index 00000000..a85eea6b --- /dev/null +++ b/rwgame/GameBase.hpp @@ -0,0 +1,33 @@ +#ifndef RWGAME_GAMEBASE_HPP +#define RWGAME_GAMEBASE_HPP +#include "GameConfig.hpp" +#include "GameWindow.hpp" + +#include + +#include + +/** + * @brief Handles basic window and setup + */ +class GameBase { +public: + GameBase(Logger& inlog, int argc, char* argv[]); + ~GameBase(); + + GameWindow& getWindow() { + return window; + } + + const GameConfig& getConfig() const { + return config; + } + +protected: + Logger& log; + GameConfig config{"openrw.ini"}; + GameWindow window; + boost::program_options::variables_map options; +}; + +#endif diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index e55ce7bd..1c94256d 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -7,7 +7,6 @@ #include "states/MenuState.hpp" #include -#include #include #include @@ -25,15 +24,8 @@ #include #include -#include #include -#include "GitSHA1.h" - -// Use first 8 chars of git hash as the build string -const std::string kBuildStr(kGitSHA1Hash, 8); -const std::string kWindowTitle = "RWGame"; - std::map kSpecialModels = { {GameRenderer::ZoneCylinderA, "zonecyla.dff"}, {GameRenderer::ZoneCylinderB, "zonecylb.dff"}, @@ -43,77 +35,19 @@ std::map kSpecialModels = { DebugDraw* debug = nullptr; -RWGame::RWGame(Logger& log, int argc, char* argv[]) : log(log) { - if (!config.isValid()) { - throw std::runtime_error("Invalid configuration file at: " + - config.getConfigFile()); - } - - size_t w = GAME_WINDOW_WIDTH, h = GAME_WINDOW_HEIGHT; - bool fullscreen = false; - bool newgame = false; - bool test = false; - bool help = false; - std::string startSave; - std::string benchFile; - - // Define and parse command line options - namespace po = boost::program_options; - po::options_description desc("Available options"); - desc.add_options()("help", "Show this help message")( - "width,w", po::value(), "Game resolution width in pixel")( - "height,h", po::value(), "Game resolution height in pixel")( - "fullscreen,f", "Enable fullscreen mode")("newgame,n", - "Directly start a new game")( - "test,t", "Starts a new game in a test location")( - "load,l", po::value(), "Load save file")( - "benchmark,b", po::value(), "Run benchmark from file"); - - po::variables_map vm; - try { - po::store(po::parse_command_line(argc, argv, desc), vm); - po::notify(vm); - } catch (po::error& ex) { - help = true; - std::cout << "Error parsing arguments: " << ex.what() << std::endl; - } - - if (help || vm.count("help")) { - std::cout << desc; - throw std::invalid_argument(""); - } - if (vm.count("width")) { - w = vm["width"].as(); - } - if (vm.count("height")) { - h = vm["height"].as(); - } - if (vm.count("fullscreen")) { - fullscreen = true; - } - if (vm.count("newgame")) { - newgame = true; - } - if (vm.count("test")) { - test = true; - } - if (vm.count("load")) { - startSave = vm["load"].as(); - } - if (vm.count("benchmark")) { - benchFile = vm["benchmark"].as(); - } - - if (SDL_Init(SDL_INIT_VIDEO) < 0) - throw std::runtime_error("Failed to initialize SDL2!"); - - window = new GameWindow(); - window->create(kWindowTitle + " [" + kBuildStr + "]", w, h, fullscreen); +RWGame::RWGame(Logger& log, int argc, char* argv[]) + : GameBase(log, argc, argv) { + bool newgame = options.count("newgame"); + bool test = options.count("test"); + std::string startSave( + options.count("load") ? options["load"].as() : ""); + std::string benchFile(options.count("benchmark") + ? options["benchmark"].as() + : ""); work = new WorkContext(); log.info("Game", "Game directory: " + config.getGameDataPath()); - log.info("Game", "Build: " + kBuildStr); if (!GameData::isValidGameDirectory(config.getGameDataPath())) { throw std::runtime_error("Invalid game directory path: " + @@ -192,15 +126,8 @@ RWGame::~RWGame() { log.info("Game", "Cleaning up state"); delete state; - log.info("Game", "Cleaning up window"); - delete window; - log.info("Game", "Cleaning up work queue"); delete work; - - SDL_Quit(); - - log.info("Game", "Done cleaning up"); } void RWGame::newGame() { @@ -561,7 +488,7 @@ int RWGame::run() { renderProfile(); - window->swap(); + getWindow().swap(); } return 0; @@ -649,7 +576,7 @@ void RWGame::render(float alpha, float time) { getRenderer()->getRenderer()->swap(); - glm::ivec2 windowSize = window->getSize(); + glm::ivec2 windowSize = getWindow().getSize(); renderer->setViewport(windowSize.x, windowSize.y); ViewCamera viewCam; diff --git a/rwgame/RWGame.hpp b/rwgame/RWGame.hpp index c5bdf56f..df250a32 100644 --- a/rwgame/RWGame.hpp +++ b/rwgame/RWGame.hpp @@ -8,24 +8,17 @@ #include