From e39ee21ca835754f83c02d0cf2a624a964dc3355 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Fri, 9 Sep 2016 21:13:20 +0100 Subject: [PATCH] clang-format files in rwgame --- rwgame/DrawUI.cpp | 383 +++++----- rwgame/DrawUI.hpp | 3 +- rwgame/GameConfig.cpp | 128 ++-- rwgame/GameConfig.hpp | 72 +- rwgame/GameWindow.cpp | 89 +-- rwgame/GameWindow.hpp | 36 +- rwgame/MenuSystem.hpp | 283 ++++---- rwgame/RWGame.cpp | 1597 ++++++++++++++++++++--------------------- rwgame/RWGame.hpp | 197 +++-- rwgame/State.cpp | 74 +- rwgame/State.hpp | 188 +++-- rwgame/game.hpp | 15 +- rwgame/main.cpp | 49 +- 13 files changed, 1512 insertions(+), 1602 deletions(-) diff --git a/rwgame/DrawUI.cpp b/rwgame/DrawUI.cpp index 87285696..7994bd39 100644 --- a/rwgame/DrawUI.cpp +++ b/rwgame/DrawUI.cpp @@ -1,9 +1,9 @@ #include "DrawUI.hpp" -#include #include -#include #include #include +#include +#include #include #include @@ -26,121 +26,121 @@ const float ui_mapSize = 150.f; const float ui_worldSizeMin = 200.f; const float ui_worldSizeMax = 300.f; -void drawMap(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render) -{ - MapRenderer::MapInfo map; - - glm::quat camRot = currentView.rotation; - - map.rotation = glm::roll(camRot) - glm::half_pi(); - map.worldSize = ui_worldSizeMin; - map.worldSize = ui_worldSizeMax; - if( player ) - { - map.worldCenter = glm::vec2(player->getCharacter()->getPosition()); - } +void drawMap(ViewCamera& currentView, PlayerController* player, + GameWorld* world, GameRenderer* render) { + MapRenderer::MapInfo map; - const glm::ivec2 &vp = render->getRenderer()->getViewport(); + glm::quat camRot = currentView.rotation; - glm::vec2 mapTop = glm::vec2(ui_outerMargin, vp.y - (ui_outerMargin + ui_mapSize)); - glm::vec2 mapBottom = glm::vec2(ui_outerMargin + ui_mapSize, vp.y - ui_outerMargin); + map.rotation = glm::roll(camRot) - glm::half_pi(); + map.worldSize = ui_worldSizeMin; + map.worldSize = ui_worldSizeMax; + if (player) { + map.worldCenter = glm::vec2(player->getCharacter()->getPosition()); + } - map.screenPosition = (mapTop + mapBottom)/2.f; - map.screenSize = ui_mapSize * 0.95; - - render->map.draw(world, map); + const glm::ivec2& vp = render->getRenderer()->getViewport(); + + glm::vec2 mapTop = + glm::vec2(ui_outerMargin, vp.y - (ui_outerMargin + ui_mapSize)); + glm::vec2 mapBottom = + glm::vec2(ui_outerMargin + ui_mapSize, vp.y - ui_outerMargin); + + map.screenPosition = (mapTop + mapBottom) / 2.f; + map.screenSize = ui_mapSize * 0.95; + + render->map.draw(world, map); } -void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* render) -{ - float infoTextX = render->getRenderer()->getViewport().x - - (ui_outerMargin + ui_weaponSize + ui_infoMargin); - float infoTextY = 0.f + ui_outerMargin; - float iconX = render->getRenderer()->getViewport().x - - (ui_outerMargin + ui_weaponSize); - float iconY = ui_outerMargin; - float wantedX = render->getRenderer()->getViewport().x - - (ui_outerMargin); - float wantedY = ui_outerMargin + ui_weaponSize + 3.f; +void drawPlayerInfo(PlayerController* player, GameWorld* world, + GameRenderer* render) { + float infoTextX = render->getRenderer()->getViewport().x - + (ui_outerMargin + ui_weaponSize + ui_infoMargin); + float infoTextY = 0.f + ui_outerMargin; + float iconX = render->getRenderer()->getViewport().x - + (ui_outerMargin + ui_weaponSize); + float iconY = ui_outerMargin; + float wantedX = render->getRenderer()->getViewport().x - (ui_outerMargin); + float wantedY = ui_outerMargin + ui_weaponSize + 3.f; - TextRenderer::TextInfo ti; - ti.font = 1; - ti.size = ui_textSize; - ti.align = TextRenderer::TextInfo::Right; + TextRenderer::TextInfo ti; + ti.font = 1; + ti.size = ui_textSize; + ti.align = TextRenderer::TextInfo::Right; - { - std::stringstream ss; - ss << std::setw(2) << std::setfill('0') << world->getHour() - << std::setw(0) << ":" - << std::setw(2) << world->getMinute(); + { + std::stringstream ss; + ss << std::setw(2) << std::setfill('0') << world->getHour() + << std::setw(0) << ":" << std::setw(2) << world->getMinute(); - ti.text = GameStringUtil::fromString(ss.str()); - } - ti.baseColour = ui_shadowColour; - ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f); - render->text.renderText(ti); + ti.text = GameStringUtil::fromString(ss.str()); + } + ti.baseColour = ui_shadowColour; + ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); + render->text.renderText(ti); - ti.baseColour = ui_timeColour; - ti.screenPosition = glm::vec2(infoTextX, infoTextY); - render->text.renderText(ti); + ti.baseColour = ui_timeColour; + ti.screenPosition = glm::vec2(infoTextX, infoTextY); + render->text.renderText(ti); - infoTextY += ui_textHeight; + infoTextY += ui_textHeight; - { - std::stringstream ss; - ss << std::setw(8) << std::setfill('0') << world->state->playerInfo.displayedMoney; + { + std::stringstream ss; + ss << std::setw(8) << std::setfill('0') + << world->state->playerInfo.displayedMoney; - ti.text = GameSymbols::Money + GameStringUtil::fromString(ss.str()); - } - ti.baseColour = ui_shadowColour; - ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f); - render->text.renderText(ti); + ti.text = GameSymbols::Money + GameStringUtil::fromString(ss.str()); + } + ti.baseColour = ui_shadowColour; + ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); + render->text.renderText(ti); - ti.baseColour = ui_moneyColour; - ti.screenPosition = glm::vec2(infoTextX, infoTextY); - render->text.renderText(ti); + ti.baseColour = ui_moneyColour; + ti.screenPosition = glm::vec2(infoTextX, infoTextY); + render->text.renderText(ti); - infoTextY += ui_textHeight; + infoTextY += ui_textHeight; - { - std::stringstream ss; - ss << std::setw(3) << std::setfill('0') - << (int)player->getCharacter()->getCurrentState().health; - ti.text = GameSymbols::Heart + GameStringUtil::fromString(ss.str()); - } - ti.baseColour = ui_shadowColour; - ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f); - render->text.renderText(ti); + { + std::stringstream ss; + ss << std::setw(3) << std::setfill('0') + << (int)player->getCharacter()->getCurrentState().health; + ti.text = GameSymbols::Heart + GameStringUtil::fromString(ss.str()); + } + ti.baseColour = ui_shadowColour; + ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); + render->text.renderText(ti); - ti.baseColour = ui_healthColour; - ti.screenPosition = glm::vec2(infoTextX, infoTextY); - render->text.renderText(ti); + ti.baseColour = ui_healthColour; + ti.screenPosition = glm::vec2(infoTextX, infoTextY); + render->text.renderText(ti); - if (player->getCharacter()->getCurrentState().armour > 0) - { - std::stringstream ss; - ss << std::setw(3) << std::setfill('0') - << (int)player->getCharacter()->getCurrentState().armour; - ti.text = GameSymbols::Armour + GameStringUtil::fromString(ss.str()); - ti.baseColour = ui_shadowColour; - ti.screenPosition = glm::vec2(infoTextX + 1.f - ui_armourOffset, infoTextY+1.f); - render->text.renderText(ti); + if (player->getCharacter()->getCurrentState().armour > 0) { + std::stringstream ss; + ss << std::setw(3) << std::setfill('0') + << (int)player->getCharacter()->getCurrentState().armour; + ti.text = GameSymbols::Armour + GameStringUtil::fromString(ss.str()); + ti.baseColour = ui_shadowColour; + ti.screenPosition = + glm::vec2(infoTextX + 1.f - ui_armourOffset, infoTextY + 1.f); + render->text.renderText(ti); - ti.baseColour = ui_armourColour; - ti.screenPosition = glm::vec2(infoTextX - ui_armourOffset, infoTextY); - render->text.renderText(ti); - } + ti.baseColour = ui_armourColour; + ti.screenPosition = glm::vec2(infoTextX - ui_armourOffset, infoTextY); + render->text.renderText(ti); + } - GameString s; - for (size_t i = 0; i < ui_maxWantedLevel; ++i) { - s += GameSymbols::Star; - } - ti.text = s; - ti.baseColour = ui_shadowColour; - ti.screenPosition = glm::vec2(wantedX + 1.f, wantedY + 1.f); - render->text.renderText(ti); + GameString s; + for (size_t i = 0; i < ui_maxWantedLevel; ++i) { + s += GameSymbols::Star; + } + ti.text = s; + ti.baseColour = ui_shadowColour; + ti.screenPosition = glm::vec2(wantedX + 1.f, wantedY + 1.f); + render->text.renderText(ti); -#if 0 // Useful for debugging +#if 0 // Useful for debugging ti.text = "ABCDEFGHIJKLMANOQRTSWXYZ\nM0123456789"; ti.size = 30; ti.align = TextRenderer::TextInfo::Left; @@ -152,115 +152,110 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re render->text.renderText(ti); #endif - InventoryItem *current = player->getCharacter()->getActiveItem(); - std::string itemTextureName = "fist"; - if (current) { - uint16_t model = current->getModelID(); - if (model > 0) { - ObjectDataPtr weaponData = world - ->data - ->findObjectType(model); - if (weaponData != nullptr) { - itemTextureName = weaponData->modelName; - } - } - } - // Urgh - if (itemTextureName == "colt45") { - itemTextureName = "pistol"; - } + InventoryItem* current = player->getCharacter()->getActiveItem(); + std::string itemTextureName = "fist"; + if (current) { + uint16_t model = current->getModelID(); + if (model > 0) { + ObjectDataPtr weaponData = + world->data->findObjectType(model); + if (weaponData != nullptr) { + itemTextureName = weaponData->modelName; + } + } + } + // Urgh + if (itemTextureName == "colt45") { + itemTextureName = "pistol"; + } - TextureData::Handle itemTexture = render->getData()->findTexture(itemTextureName); - RW_CHECK(itemTexture != nullptr, "Item has 0 texture"); - if (itemTexture != nullptr) { - RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture"); - render->drawTexture(itemTexture.get(), - glm::vec4(iconX, - iconY, - ui_weaponSize, - ui_weaponSize)); - } + TextureData::Handle itemTexture = + render->getData()->findTexture(itemTextureName); + RW_CHECK(itemTexture != nullptr, "Item has 0 texture"); + if (itemTexture != nullptr) { + RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture"); + render->drawTexture( + itemTexture.get(), + glm::vec4(iconX, iconY, ui_weaponSize, ui_weaponSize)); + } - if (current) { - WeaponItem* wep = static_cast(current); - if (wep->getWeaponData()->fireType != WeaponData::MELEE) { - const CharacterState& cs = player->getCharacter()->getCurrentState(); - const CharacterWeaponSlot& slotInfo = cs.weapons[cs.currentWeapon]; - ti.text = GameStringUtil::fromString( - std::to_string(slotInfo.bulletsClip) + "-" - + std::to_string(slotInfo.bulletsTotal)); + if (current) { + WeaponItem* wep = static_cast(current); + if (wep->getWeaponData()->fireType != WeaponData::MELEE) { + const CharacterState& cs = + player->getCharacter()->getCurrentState(); + const CharacterWeaponSlot& slotInfo = cs.weapons[cs.currentWeapon]; + ti.text = GameStringUtil::fromString( + std::to_string(slotInfo.bulletsClip) + "-" + + std::to_string(slotInfo.bulletsTotal)); - ti.baseColour = ui_shadowColour; - ti.font = 2; - ti.size = ui_ammoSize; - ti.align = TextRenderer::TextInfo::Center; - ti.screenPosition = glm::vec2(iconX + ui_weaponSize / 2.f, - iconY + ui_weaponSize - ui_ammoHeight); - render->text.renderText(ti); - } - } + ti.baseColour = ui_shadowColour; + ti.font = 2; + ti.size = ui_ammoSize; + ti.align = TextRenderer::TextInfo::Center; + ti.screenPosition = + glm::vec2(iconX + ui_weaponSize / 2.f, + iconY + ui_weaponSize - ui_ammoHeight); + render->text.renderText(ti); + } + } } -void drawHUD(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render) -{ - if(player && player->getCharacter()) { - drawMap(currentView, player, world, render); - drawPlayerInfo(player, world, render); - } +void drawHUD(ViewCamera& currentView, PlayerController* player, + GameWorld* world, GameRenderer* render) { + if (player && player->getCharacter()) { + drawMap(currentView, player, world, render); + drawPlayerInfo(player, world, render); + } } -void drawOnScreenText(GameWorld* world, GameRenderer* renderer) -{ - const auto vp = glm::vec2(renderer->getRenderer()->getViewport()); - - TextRenderer::TextInfo ti; - ti.font = 2; - ti.screenPosition = glm::vec2( 10.f, 10.f ); - ti.size = 20.f; +void drawOnScreenText(GameWorld* world, GameRenderer* renderer) { + const auto vp = glm::vec2(renderer->getRenderer()->getViewport()); - auto& alltext = world->state->text.getAllText(); + TextRenderer::TextInfo ti; + ti.font = 2; + ti.screenPosition = glm::vec2(10.f, 10.f); + ti.size = 20.f; - for(auto& l : alltext) - { - for (auto& t : l) - { - ti.size = t.size; - ti.font = t.font; - ti.text = t.text; - ti.wrapX = t.wrapX; - ti.screenPosition = (t.position/glm::vec2(640.f, 480.f)) * vp; - switch(t.alignment) { - case 0: - ti.align = TextRenderer::TextInfo::Left; - break; - case 1: - ti.align = TextRenderer::TextInfo::Center; - break; - case 2: - ti.align = TextRenderer::TextInfo::Right; - break; - } + auto& alltext = world->state->text.getAllText(); - // Check for the background type - if (t.colourBG.a == 0) - { - glm::vec2 shadowPosition((int8_t)t.colourBG.x, (int8_t)t.colourBG.y); + for (auto& l : alltext) { + for (auto& t : l) { + ti.size = t.size; + ti.font = t.font; + ti.text = t.text; + ti.wrapX = t.wrapX; + ti.screenPosition = (t.position / glm::vec2(640.f, 480.f)) * vp; + switch (t.alignment) { + case 0: + ti.align = TextRenderer::TextInfo::Left; + break; + case 1: + ti.align = TextRenderer::TextInfo::Center; + break; + case 2: + ti.align = TextRenderer::TextInfo::Right; + break; + } - ti.baseColour = glm::vec3(0.f); - ti.screenPosition += shadowPosition; - ti.backgroundColour = {0, 0, 0, 0}; + // Check for the background type + if (t.colourBG.a == 0) { + glm::vec2 shadowPosition((int8_t)t.colourBG.x, + (int8_t)t.colourBG.y); - renderer->text.renderText(ti, true); + ti.baseColour = glm::vec3(0.f); + ti.screenPosition += shadowPosition; + ti.backgroundColour = {0, 0, 0, 0}; - ti.screenPosition -= shadowPosition; - } - else if(t.colourBG.a > 0) - { - ti.backgroundColour = t.colourBG; - } + renderer->text.renderText(ti, true); - ti.baseColour = t.colourFG; - renderer->text.renderText(ti); - } - } + ti.screenPosition -= shadowPosition; + } else if (t.colourBG.a > 0) { + ti.backgroundColour = t.colourBG; + } + + ti.baseColour = t.colourFG; + renderer->text.renderText(ti); + } + } } diff --git a/rwgame/DrawUI.hpp b/rwgame/DrawUI.hpp index 102509e3..74f38af5 100644 --- a/rwgame/DrawUI.hpp +++ b/rwgame/DrawUI.hpp @@ -4,6 +4,7 @@ class PlayerController; -void drawHUD(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render); +void drawHUD(ViewCamera& currentView, PlayerController* player, + GameWorld* world, GameRenderer* render); void drawOnScreenText(GameWorld* world, GameRenderer* renderer); diff --git a/rwgame/GameConfig.cpp b/rwgame/GameConfig.cpp index bfee9e96..09f1f546 100644 --- a/rwgame/GameConfig.cpp +++ b/rwgame/GameConfig.cpp @@ -1,100 +1,86 @@ #include "GameConfig.hpp" -#include -#include #include +#include +#include #include const std::string kConfigDirectoryName("OpenRW"); -GameConfig::GameConfig(const std::string& configName, const std::string& configPath) - : m_configName(configName) - , m_configPath(configPath) - , m_valid(false) - , m_inputInvertY(false) -{ - if (m_configPath.empty()) - { - m_configPath = getDefaultConfigPath(); - } +GameConfig::GameConfig(const std::string& configName, + const std::string& configPath) + : m_configName(configName) + , m_configPath(configPath) + , m_valid(false) + , m_inputInvertY(false) { + if (m_configPath.empty()) { + m_configPath = getDefaultConfigPath(); + } - // Look up the path to use - auto configFile = getConfigFile(); + // Look up the path to use + auto configFile = getConfigFile(); - if (ini_parse(configFile.c_str(), handler, this) < 0) - { - m_valid = false; - } - else - { - m_valid = true; - } + if (ini_parse(configFile.c_str(), handler, this) < 0) { + m_valid = false; + } else { + m_valid = true; + } } -std::string GameConfig::getConfigFile() -{ - return m_configPath + "/" + m_configName; +std::string GameConfig::getConfigFile() { + return m_configPath + "/" + m_configName; } -bool GameConfig::isValid() -{ - return m_valid; +bool GameConfig::isValid() { + return m_valid; } -std::string GameConfig::getDefaultConfigPath() -{ -#if defined(RW_LINUX) || defined(RW_FREEBSD) || defined(RW_NETBSD) || defined(RW_OPENBSD) - char* config_home = getenv("XDG_CONFIG_HOME"); - if (config_home != nullptr) { - return std::string(config_home) + "/" + kConfigDirectoryName; - } - char* home = getenv("HOME"); - if (home != nullptr) { - return std::string(home) + "/.config/" + kConfigDirectoryName; - } +std::string GameConfig::getDefaultConfigPath() { +#if defined(RW_LINUX) || defined(RW_FREEBSD) || defined(RW_NETBSD) || \ + defined(RW_OPENBSD) + char* config_home = getenv("XDG_CONFIG_HOME"); + if (config_home != nullptr) { + return std::string(config_home) + "/" + kConfigDirectoryName; + } + char* home = getenv("HOME"); + if (home != nullptr) { + return std::string(home) + "/.config/" + kConfigDirectoryName; + } #elif defined(RW_OSX) - char* home = getenv("HOME"); - if (home) - return std::string(home) + "/Library/Preferences/" + kConfigDirectoryName; + char* home = getenv("HOME"); + if (home) + return std::string(home) + "/Library/Preferences/" + + kConfigDirectoryName; #else - return "."; + return "."; #endif - // Well now we're stuck. - RW_ERROR("No default config path found."); - return "."; + // Well now we're stuck. + RW_ERROR("No default config path found."); + return "."; } -int GameConfig::handler(void* user, - const char* section, - const char* name, - const char* value) -{ - auto self = static_cast(user); +int GameConfig::handler(void* user, const char* section, const char* name, + const char* value) { + auto self = static_cast(user); #define MATCH(_s, _n) (strcmp(_s, section) == 0 && strcmp(_n, name) == 0) - if (MATCH("game", "path")) - { - self->m_gamePath = value; - } - else if (MATCH("game", "language")) - { - // @todo Don't allow path seperators and relative directories - self->m_gameLanguage = value; - } - else if (MATCH("input", "invert_y")) - { - self->m_inputInvertY = atoi(value) > 0; - } - else - { - RW_MESSAGE("Unhandled config entry [" << section << "] " << name << " = " << value); - return 0; - } + if (MATCH("game", "path")) { + self->m_gamePath = value; + } else if (MATCH("game", "language")) { + // @todo Don't allow path seperators and relative directories + self->m_gameLanguage = value; + } else if (MATCH("input", "invert_y")) { + self->m_inputInvertY = atoi(value) > 0; + } else { + RW_MESSAGE("Unhandled config entry [" << section << "] " << name + << " = " << value); + return 0; + } - return 1; + return 1; #undef MATCH } diff --git a/rwgame/GameConfig.hpp b/rwgame/GameConfig.hpp index f7624efa..850b73ee 100644 --- a/rwgame/GameConfig.hpp +++ b/rwgame/GameConfig.hpp @@ -2,50 +2,56 @@ #define RWGAME_GAMECONFIG_HPP #include -class GameConfig -{ +class GameConfig { public: - /** - * @brief GameConfig Loads a game configuration - * @param configName The configuration filename to load - * @param configPath Where to look. - */ - GameConfig(const std::string& configName, const std::string& configPath = getDefaultConfigPath()); + /** + * @brief GameConfig Loads a game configuration + * @param configName The configuration filename to load + * @param configPath Where to look. + */ + GameConfig(const std::string& configName, + const std::string& configPath = getDefaultConfigPath()); - /** - * @brief getFilePath Returns the system file path for the configuration - */ - std::string getConfigFile(); + /** + * @brief getFilePath Returns the system file path for the configuration + */ + std::string getConfigFile(); - /** - * @brief isValid - * @return True if the loaded configuration is valid - */ - bool isValid(); + /** + * @brief isValid + * @return True if the loaded configuration is valid + */ + bool isValid(); - const std::string& getGameDataPath() const { return m_gamePath; } - const std::string& getGameLanguage() const { return m_gameLanguage; } - bool getInputInvertY() const { return m_inputInvertY; } + const std::string& getGameDataPath() const { + return m_gamePath; + } + const std::string& getGameLanguage() const { + return m_gameLanguage; + } + bool getInputInvertY() const { + return m_inputInvertY; + } private: - static std::string getDefaultConfigPath(); - static int handler(void*, const char*, const char*, const char*); + static std::string getDefaultConfigPath(); + static int handler(void*, const char*, const char*, const char*); - /* Config State */ - std::string m_configName; - std::string m_configPath; - bool m_valid; + /* Config State */ + std::string m_configName; + std::string m_configPath; + bool m_valid; - /* Actual Configuration */ + /* Actual Configuration */ - /// Path to the game data - std::string m_gamePath; + /// Path to the game data + std::string m_gamePath; - /// Language for game - std::string m_gameLanguage = "american"; + /// Language for game + std::string m_gameLanguage = "american"; - /// Invert the y axis for camera control. - bool m_inputInvertY; + /// Invert the y axis for camera control. + bool m_inputInvertY; }; #endif diff --git a/rwgame/GameWindow.cpp b/rwgame/GameWindow.cpp index 2f40e55f..0140b688 100644 --- a/rwgame/GameWindow.cpp +++ b/rwgame/GameWindow.cpp @@ -1,68 +1,57 @@ -#include #include "GameWindow.hpp" +#include -GameWindow::GameWindow() : - window(nullptr), glcontext(nullptr) -{ - +GameWindow::GameWindow() : window(nullptr), glcontext(nullptr) { } +void GameWindow::create(const std::string& title, size_t w, size_t h, + bool fullscreen) { + Uint32 style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN; + if (fullscreen) style |= SDL_WINDOW_FULLSCREEN; -void GameWindow::create(const std::string& title, size_t w, size_t h, bool fullscreen) -{ - Uint32 style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN; - if (fullscreen) - style |= SDL_WINDOW_FULLSCREEN; + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, + SDL_GL_CONTEXT_PROFILE_CORE); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); + SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); + SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); - SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24); - SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8); + window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, w, h, style); + if (window == nullptr) { + // Window creation failure is fatal + std::string sdlErrorStr = SDL_GetError(); + throw std::runtime_error("SDL_CreateWindow failed: " + sdlErrorStr); + } + glcontext = SDL_GL_CreateContext(window); + if (glcontext == nullptr) { + // context creation failure is fatal + std::string sdlErrorStr = SDL_GetError(); + throw std::runtime_error("SDL_GL_CreateContext failed: " + sdlErrorStr); + } - window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, style); - if (window == nullptr) { - // Window creation failure is fatal - std::string sdlErrorStr = SDL_GetError(); - throw std::runtime_error("SDL_CreateWindow failed: " + sdlErrorStr); - } - glcontext = SDL_GL_CreateContext(window); - if (glcontext == nullptr) { - // context creation failure is fatal - std::string sdlErrorStr = SDL_GetError(); - throw std::runtime_error("SDL_GL_CreateContext failed: " + sdlErrorStr); - } - - SDL_ShowWindow(window); + SDL_ShowWindow(window); } +void GameWindow::close() { + SDL_GL_DeleteContext(glcontext); + SDL_DestroyWindow(window); -void GameWindow::close() -{ - SDL_GL_DeleteContext(glcontext); - SDL_DestroyWindow(window); - - window = nullptr; + window = nullptr; } - -void GameWindow::showCursor() -{ - SDL_SetRelativeMouseMode(SDL_FALSE); +void GameWindow::showCursor() { + SDL_SetRelativeMouseMode(SDL_FALSE); } - -void GameWindow::hideCursor() -{ - SDL_SetRelativeMouseMode(SDL_TRUE); +void GameWindow::hideCursor() { + SDL_SetRelativeMouseMode(SDL_TRUE); } +glm::ivec2 GameWindow::getSize() const { + int x, y; + SDL_GL_GetDrawableSize(window, &x, &y); -glm::ivec2 GameWindow::getSize() const -{ - int x, y; - SDL_GL_GetDrawableSize(window, &x, &y); - - return glm::ivec2(x, y); + return glm::ivec2(x, y); } diff --git a/rwgame/GameWindow.hpp b/rwgame/GameWindow.hpp index 7e11696d..0b34076f 100644 --- a/rwgame/GameWindow.hpp +++ b/rwgame/GameWindow.hpp @@ -1,38 +1,34 @@ #ifndef GAMEWINDOW_HPP #define GAMEWINDOW_HPP +#include #include #include "SDL.h" -#include #include - -class GameWindow -{ - SDL_Window* window; - SDL_GLContext glcontext; +class GameWindow { + SDL_Window* window; + SDL_GLContext glcontext; public: - GameWindow(); + GameWindow(); - void create(const std::string& title, size_t w, size_t h, bool fullscreen); - void close(); + void create(const std::string& title, size_t w, size_t h, bool fullscreen); + void close(); - void showCursor(); - void hideCursor(); + void showCursor(); + void hideCursor(); - glm::ivec2 getSize() const; + glm::ivec2 getSize() const; - void swap() const - { - SDL_GL_SwapWindow(window); - } + void swap() const { + SDL_GL_SwapWindow(window); + } - bool isOpen() const - { - return !!window; - } + bool isOpen() const { + return !!window; + } }; #endif diff --git a/rwgame/MenuSystem.hpp b/rwgame/MenuSystem.hpp index 30e9bd76..220d73d8 100644 --- a/rwgame/MenuSystem.hpp +++ b/rwgame/MenuSystem.hpp @@ -1,17 +1,16 @@ #ifndef _GAME_MENUSYSTEM_HPP_ #define _GAME_MENUSYSTEM_HPP_ -#include -#include -#include -#include -#include #include +#include +#include +#include +#include +#include /** * Default values for menus that should match the look and feel of the original */ -namespace MenuDefaults -{ +namespace MenuDefaults { constexpr int kFont = 1; constexpr const char* kStartGameId = "FET_SAN"; @@ -22,161 +21,133 @@ constexpr const char* kOptionsId = "FET_OPT"; constexpr const char* kQuitGameId = "FET_QG"; } -class Menu -{ - int font; +class Menu { + int font; + public: - - Menu(int font = MenuDefaults::kFont) - : font(font), activeEntry(-1) {} - - struct MenuEntry - { - GameString text; - float _size; - - MenuEntry(const GameString& n, float size = 30.f) - : text(n) - , _size(size) - { - } - - float getHeight() { return _size; } - - virtual void draw(int font, bool active, GameRenderer* r, glm::vec2& basis) - { - TextRenderer::TextInfo ti; - ti.font = font; - ti.screenPosition = basis; - ti.text = text; - ti.size = getHeight(); - if( ! active ) - { - ti.baseColour = glm::u8vec3(255); - } - else - { - ti.baseColour = glm::u8vec3(255, 255, 0); - } - r->text.renderText(ti); - basis.y += getHeight(); - } - - virtual void activate(float clickX, float clickY) = 0; - }; + Menu(int font = MenuDefaults::kFont) : font(font), activeEntry(-1) { + } - struct Entry : public MenuEntry - { - std::function callback; + struct MenuEntry { + GameString text; + float _size; - Entry(const GameString& title, - std::function cb, - float size) - : MenuEntry(title, size) - , callback(cb) - { - } + MenuEntry(const GameString& n, float size = 30.f) + : text(n), _size(size) { + } - void activate(float clickX, float clickY) - { - RW_UNUSED(clickX); - RW_UNUSED(clickY); - callback(); - } - }; + float getHeight() { + return _size; + } - static std::shared_ptr lambda(const GameString& n, std::function callback, float size = 30.f) - { - return std::shared_ptr(new Entry(n, callback, size)); - } + virtual void draw(int font, bool active, GameRenderer* r, + glm::vec2& basis) { + TextRenderer::TextInfo ti; + ti.font = font; + ti.screenPosition = basis; + ti.text = text; + ti.size = getHeight(); + if (!active) { + ti.baseColour = glm::u8vec3(255); + } else { + ti.baseColour = glm::u8vec3(255, 255, 0); + } + r->text.renderText(ti); + basis.y += getHeight(); + } - static std::shared_ptr lambda(const std::string& n, std::function callback, float size = 30.f) - { - return lambda(GameStringUtil::fromString(n), callback, size); - } - - std::vector> entries; - - /** - * Active Entry index - */ - int activeEntry; - - glm::vec2 offset; - - void addEntry(std::shared_ptr entry) - { - entries.push_back(entry); - } - - void draw(GameRenderer* r) - { - glm::vec2 basis(offset); - for(size_t i = 0; - i < entries.size(); - ++i) - { - bool active = false; - if(activeEntry >= 0 && i == (unsigned) activeEntry) - { - active = true; - } - entries[i]->draw(font, active, r, basis); - } - } - - void hover(const float x, const float y) - { - glm::vec2 c(x - offset.x, y - offset.y); - for(size_t i = 0; - i < entries.size(); - ++i) - { - if( c.y > 0.f && c.y < entries[i]->getHeight() ) { - activeEntry = i; - return; - } - else { - c.y -= entries[i]->getHeight(); - } - } - } - - void click(const float x, const float y) - { - glm::vec2 c(x - offset.x, y - offset.y); - for(auto it = entries.begin(); - it != entries.end(); - ++it) - { - if( c.y > 0.f && c.y < (*it)->getHeight() ) { - (*it)->activate(c.x, c.y); - return; - } - else { - c.y -= (*it)->getHeight(); - } - } - } - - // Activates the menu entry at the current active index. - void activate() - { - if(activeEntry >= 0 && (unsigned) activeEntry < entries.size()) { - entries[activeEntry]->activate(0.f, 0.f); - } - } - - void move(int movement) - { - activeEntry += movement; - if (activeEntry >= int(entries.size())) { - activeEntry = 0; - } - else if (activeEntry < 0) { - activeEntry = entries.size() - 1; - } - } + virtual void activate(float clickX, float clickY) = 0; + }; + + struct Entry : public MenuEntry { + std::function callback; + + Entry(const GameString& title, std::function cb, float size) + : MenuEntry(title, size), callback(cb) { + } + + void activate(float clickX, float clickY) { + RW_UNUSED(clickX); + RW_UNUSED(clickY); + callback(); + } + }; + + static std::shared_ptr lambda(const GameString& n, + std::function callback, + float size = 30.f) { + return std::shared_ptr(new Entry(n, callback, size)); + } + + static std::shared_ptr lambda(const std::string& n, + std::function callback, + float size = 30.f) { + return lambda(GameStringUtil::fromString(n), callback, size); + } + + std::vector> entries; + + /** + * Active Entry index + */ + int activeEntry; + + glm::vec2 offset; + + void addEntry(std::shared_ptr entry) { + entries.push_back(entry); + } + + void draw(GameRenderer* r) { + glm::vec2 basis(offset); + for (size_t i = 0; i < entries.size(); ++i) { + bool active = false; + if (activeEntry >= 0 && i == (unsigned)activeEntry) { + active = true; + } + entries[i]->draw(font, active, r, basis); + } + } + + void hover(const float x, const float y) { + glm::vec2 c(x - offset.x, y - offset.y); + for (size_t i = 0; i < entries.size(); ++i) { + if (c.y > 0.f && c.y < entries[i]->getHeight()) { + activeEntry = i; + return; + } else { + c.y -= entries[i]->getHeight(); + } + } + } + + void click(const float x, const float y) { + glm::vec2 c(x - offset.x, y - offset.y); + for (auto it = entries.begin(); it != entries.end(); ++it) { + if (c.y > 0.f && c.y < (*it)->getHeight()) { + (*it)->activate(c.x, c.y); + return; + } else { + c.y -= (*it)->getHeight(); + } + } + } + + // Activates the menu entry at the current active index. + void activate() { + if (activeEntry >= 0 && (unsigned)activeEntry < entries.size()) { + entries[activeEntry]->activate(0.f, 0.f); + } + } + + void move(int movement) { + activeEntry += movement; + if (activeEntry >= int(entries.size())) { + activeEntry = 0; + } else if (activeEntry < 0) { + activeEntry = entries.size() - 1; + } + } }; #endif diff --git a/rwgame/RWGame.cpp b/rwgame/RWGame.cpp index 0946817c..9e1ac54d 100644 --- a/rwgame/RWGame.cpp +++ b/rwgame/RWGame.cpp @@ -1,25 +1,25 @@ #include "RWGame.hpp" -#include "State.hpp" -#include "states/LoadingState.hpp" -#include "states/IngameState.hpp" -#include "states/MenuState.hpp" -#include "states/BenchmarkState.hpp" #include "DrawUI.hpp" +#include "State.hpp" +#include "states/BenchmarkState.hpp" +#include "states/IngameState.hpp" +#include "states/LoadingState.hpp" +#include "states/MenuState.hpp" #include -#include #include -#include #include -#include +#include +#include #include +#include #include