mirror of
https://github.com/rwengine/openrw.git
synced 2024-09-03 00:59:47 +02:00
clang-format files in rwgame
This commit is contained in:
parent
e888d04303
commit
e39ee21ca8
@ -1,9 +1,9 @@
|
||||
#include "DrawUI.hpp"
|
||||
#include <render/GameRenderer.hpp>
|
||||
#include <ai/PlayerController.hpp>
|
||||
#include <objects/CharacterObject.hpp>
|
||||
#include <engine/GameState.hpp>
|
||||
#include <items/WeaponItem.hpp>
|
||||
#include <objects/CharacterObject.hpp>
|
||||
#include <render/GameRenderer.hpp>
|
||||
|
||||
#include <glm/gtc/constants.hpp>
|
||||
#include <iomanip>
|
||||
@ -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<float>();
|
||||
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<float>();
|
||||
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<ObjectData>(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<ObjectData>(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<WeaponItem*>(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<WeaponItem*>(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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -1,100 +1,86 @@
|
||||
#include "GameConfig.hpp"
|
||||
#include <rw/defines.hpp>
|
||||
#include <cstring>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <rw/defines.hpp>
|
||||
|
||||
#include <ini.h>
|
||||
|
||||
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<GameConfig*>(user);
|
||||
int GameConfig::handler(void* user, const char* section, const char* name,
|
||||
const char* value) {
|
||||
auto self = static_cast<GameConfig*>(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
|
||||
}
|
||||
|
@ -2,50 +2,56 @@
|
||||
#define RWGAME_GAMECONFIG_HPP
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
@ -1,68 +1,57 @@
|
||||
#include <core/Logger.hpp>
|
||||
#include "GameWindow.hpp"
|
||||
#include <core/Logger.hpp>
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -1,38 +1,34 @@
|
||||
#ifndef GAMEWINDOW_HPP
|
||||
#define GAMEWINDOW_HPP
|
||||
|
||||
#include <glm/vec2.hpp>
|
||||
#include <string>
|
||||
#include "SDL.h"
|
||||
#include <glm/vec2.hpp>
|
||||
|
||||
#include <render/GameRenderer.hpp>
|
||||
|
||||
|
||||
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
|
||||
|
@ -1,17 +1,16 @@
|
||||
#ifndef _GAME_MENUSYSTEM_HPP_
|
||||
#define _GAME_MENUSYSTEM_HPP_
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
#include <render/GameRenderer.hpp>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <render/GameRenderer.hpp>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* 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<void(void)> callback;
|
||||
struct MenuEntry {
|
||||
GameString text;
|
||||
float _size;
|
||||
|
||||
Entry(const GameString& title,
|
||||
std::function<void(void)> 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<MenuEntry> lambda(const GameString& n, std::function<void (void)> callback, float size = 30.f)
|
||||
{
|
||||
return std::shared_ptr<MenuEntry>(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<MenuEntry> lambda(const std::string& n, std::function<void (void)> callback, float size = 30.f)
|
||||
{
|
||||
return lambda(GameStringUtil::fromString(n), callback, size);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<MenuEntry>> entries;
|
||||
|
||||
/**
|
||||
* Active Entry index
|
||||
*/
|
||||
int activeEntry;
|
||||
|
||||
glm::vec2 offset;
|
||||
|
||||
void addEntry(std::shared_ptr<MenuEntry> 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<void(void)> callback;
|
||||
|
||||
Entry(const GameString& title, std::function<void(void)> 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<MenuEntry> lambda(const GameString& n,
|
||||
std::function<void(void)> callback,
|
||||
float size = 30.f) {
|
||||
return std::shared_ptr<MenuEntry>(new Entry(n, callback, size));
|
||||
}
|
||||
|
||||
static std::shared_ptr<MenuEntry> lambda(const std::string& n,
|
||||
std::function<void(void)> callback,
|
||||
float size = 30.f) {
|
||||
return lambda(GameStringUtil::fromString(n), callback, size);
|
||||
}
|
||||
|
||||
std::vector<std::shared_ptr<MenuEntry>> entries;
|
||||
|
||||
/**
|
||||
* Active Entry index
|
||||
*/
|
||||
int activeEntry;
|
||||
|
||||
glm::vec2 offset;
|
||||
|
||||
void addEntry(std::shared_ptr<MenuEntry> 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
|
||||
|
1597
rwgame/RWGame.cpp
1597
rwgame/RWGame.cpp
File diff suppressed because it is too large
Load Diff
@ -1,12 +1,12 @@
|
||||
#ifndef _RWGAME_HPP_
|
||||
#define _RWGAME_HPP_
|
||||
|
||||
#include <chrono>
|
||||
#include <core/Logger.hpp>
|
||||
#include <engine/GameData.hpp>
|
||||
#include <engine/GameWorld.hpp>
|
||||
#include <render/GameRenderer.hpp>
|
||||
#include <script/ScriptMachine.hpp>
|
||||
#include <chrono>
|
||||
#include "game.hpp"
|
||||
|
||||
#include "GameConfig.hpp"
|
||||
@ -16,131 +16,128 @@
|
||||
|
||||
class PlayerController;
|
||||
|
||||
class RWGame
|
||||
{
|
||||
Logger log;
|
||||
GameConfig config {"openrw.ini"};
|
||||
GameState* state = nullptr;
|
||||
GameData* data = nullptr;
|
||||
GameWorld* world = nullptr;
|
||||
// must be allocated after Logger setup.
|
||||
GameRenderer* renderer = nullptr;
|
||||
class RWGame {
|
||||
Logger log;
|
||||
GameConfig config{"openrw.ini"};
|
||||
GameState* state = nullptr;
|
||||
GameData* data = nullptr;
|
||||
GameWorld* world = nullptr;
|
||||
// must be allocated after Logger setup.
|
||||
GameRenderer* renderer = nullptr;
|
||||
ScriptMachine* script = nullptr;
|
||||
GameWindow *window = nullptr;
|
||||
// Background worker
|
||||
WorkContext *work = nullptr;
|
||||
std::chrono::steady_clock clock;
|
||||
std::chrono::steady_clock::time_point last_clock_time;
|
||||
GameWindow* window = nullptr;
|
||||
// Background worker
|
||||
WorkContext* work = nullptr;
|
||||
std::chrono::steady_clock clock;
|
||||
std::chrono::steady_clock::time_point last_clock_time;
|
||||
|
||||
bool inFocus = true;
|
||||
ViewCamera lastCam, nextCam;
|
||||
bool showDebugStats = false;
|
||||
bool showDebugPaths = false;
|
||||
bool showDebugPhysics = false;
|
||||
int lastDraws; /// Number of draws issued for the last frame.
|
||||
bool inFocus = true;
|
||||
ViewCamera lastCam, nextCam;
|
||||
bool showDebugStats = false;
|
||||
bool showDebugPaths = false;
|
||||
bool showDebugPhysics = false;
|
||||
int lastDraws; /// Number of draws issued for the last frame.
|
||||
|
||||
std::string cheatInputWindow = std::string(32, ' ');
|
||||
std::string cheatInputWindow = std::string(32, ' ');
|
||||
|
||||
float accum = 0.f;
|
||||
float timescale = 1.f;
|
||||
|
||||
float accum = 0.f;
|
||||
float timescale = 1.f;
|
||||
public:
|
||||
RWGame(int argc, char* argv[]);
|
||||
~RWGame();
|
||||
|
||||
RWGame(int argc, char* argv[]);
|
||||
~RWGame();
|
||||
int run();
|
||||
|
||||
int run();
|
||||
/**
|
||||
* Initalizes a new game
|
||||
*/
|
||||
void newGame();
|
||||
|
||||
/**
|
||||
* Initalizes a new game
|
||||
*/
|
||||
void newGame();
|
||||
GameState* getState() const {
|
||||
return state;
|
||||
}
|
||||
|
||||
GameState* getState() const
|
||||
{
|
||||
return state;
|
||||
}
|
||||
GameWorld* getWorld() const {
|
||||
return world;
|
||||
}
|
||||
|
||||
GameWorld* getWorld() const
|
||||
{
|
||||
return world;
|
||||
}
|
||||
GameData* getGameData() const {
|
||||
return data;
|
||||
}
|
||||
|
||||
GameData* getGameData() const
|
||||
{
|
||||
return data;
|
||||
}
|
||||
GameRenderer* getRenderer() const {
|
||||
return renderer;
|
||||
}
|
||||
|
||||
GameRenderer* getRenderer() const
|
||||
{
|
||||
return renderer;
|
||||
}
|
||||
GameWindow& getWindow() {
|
||||
return *window;
|
||||
}
|
||||
|
||||
GameWindow& getWindow()
|
||||
{
|
||||
return *window;
|
||||
}
|
||||
ScriptMachine* getScript() const {
|
||||
return script;
|
||||
}
|
||||
|
||||
ScriptMachine* getScript() const
|
||||
{
|
||||
return script;
|
||||
}
|
||||
const GameConfig& getConfig() const {
|
||||
return config;
|
||||
}
|
||||
|
||||
const GameConfig& getConfig() const
|
||||
{
|
||||
return config;
|
||||
}
|
||||
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
|
||||
GameObject** object = nullptr) {
|
||||
auto vc = nextCam;
|
||||
glm::vec3 from(vc.position.x, vc.position.y, vc.position.z);
|
||||
glm::vec3 tmp = vc.rotation * glm::vec3(1000.f, 0.f, 0.f);
|
||||
|
||||
bool hitWorldRay(glm::vec3 &hit, glm::vec3 &normal, GameObject** object = nullptr)
|
||||
{
|
||||
auto vc = nextCam;
|
||||
glm::vec3 from(vc.position.x, vc.position.y, vc.position.z);
|
||||
glm::vec3 tmp = vc.rotation * glm::vec3(1000.f, 0.f, 0.f);
|
||||
return hitWorldRay(from, tmp, hit, normal, object);
|
||||
}
|
||||
|
||||
return hitWorldRay(from, tmp, hit, normal, object);
|
||||
}
|
||||
bool hitWorldRay(const glm::vec3& start, const glm::vec3& direction,
|
||||
glm::vec3& hit, glm::vec3& normal,
|
||||
GameObject** object = nullptr) {
|
||||
auto from = btVector3(start.x, start.y, start.z);
|
||||
auto to = btVector3(start.x + direction.x, start.y + direction.y,
|
||||
start.z + direction.z);
|
||||
btCollisionWorld::ClosestRayResultCallback ray(from, to);
|
||||
|
||||
bool hitWorldRay(const glm::vec3 &start, const glm::vec3 &direction, glm::vec3 &hit, glm::vec3 &normal, GameObject **object = nullptr)
|
||||
{
|
||||
auto from = btVector3(start.x, start.y, start.z);
|
||||
auto to = btVector3(start.x+direction.x, start.y+direction.y, start.z+direction.z);
|
||||
btCollisionWorld::ClosestRayResultCallback ray(from, to);
|
||||
world->dynamicsWorld->rayTest(from, to, ray);
|
||||
if (ray.hasHit()) {
|
||||
hit = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
||||
ray.m_hitPointWorld.z());
|
||||
normal =
|
||||
glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
||||
ray.m_hitNormalWorld.z());
|
||||
if (object) {
|
||||
*object = static_cast<GameObject*>(
|
||||
ray.m_collisionObject->getUserPointer());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
world->dynamicsWorld->rayTest(from, to, ray);
|
||||
if( ray.hasHit() )
|
||||
{
|
||||
hit = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
|
||||
ray.m_hitPointWorld.z());
|
||||
normal = glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
|
||||
ray.m_hitNormalWorld.z());
|
||||
if(object) {
|
||||
*object = static_cast<GameObject*>(ray.m_collisionObject->getUserPointer());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void startScript(const std::string& name);
|
||||
void startScript(const std::string& name);
|
||||
|
||||
bool hasFocus() const { return inFocus; }
|
||||
bool hasFocus() const {
|
||||
return inFocus;
|
||||
}
|
||||
|
||||
void saveGame(const std::string& savename);
|
||||
void loadGame(const std::string& savename);
|
||||
void saveGame(const std::string& savename);
|
||||
void loadGame(const std::string& savename);
|
||||
|
||||
/** shortcut for getWorld()->state.player->getCharacter() */
|
||||
PlayerController* getPlayer();
|
||||
/** shortcut for getWorld()->state.player->getCharacter() */
|
||||
PlayerController* getPlayer();
|
||||
|
||||
private:
|
||||
void tick(float dt);
|
||||
void render(float alpha, float dt);
|
||||
|
||||
void renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime);
|
||||
void renderDebugPaths(float time);
|
||||
void renderProfile();
|
||||
void tick(float dt);
|
||||
void render(float alpha, float dt);
|
||||
|
||||
void handleCheatInput(char symbol);
|
||||
void renderDebugStats(float time, Renderer::ProfileInfo& worldRenderTime);
|
||||
void renderDebugPaths(float time);
|
||||
void renderProfile();
|
||||
|
||||
void globalKeyEvent(const SDL_Event& event);
|
||||
void handleCheatInput(char symbol);
|
||||
|
||||
void globalKeyEvent(const SDL_Event& event);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -2,57 +2,53 @@
|
||||
#include "RWGame.hpp"
|
||||
|
||||
// This serves as the "initial" camera position.
|
||||
ViewCamera defaultView({-250.f, -550.f, 75.f}, glm::angleAxis(glm::radians(5.f), glm::vec3(0.f, 1.f, 0.f)));
|
||||
ViewCamera defaultView({-250.f, -550.f, 75.f},
|
||||
glm::angleAxis(glm::radians(5.f),
|
||||
glm::vec3(0.f, 1.f, 0.f)));
|
||||
|
||||
void State::handleEvent(const SDL_Event& e) {
|
||||
auto m = getCurrentMenu();
|
||||
if (!m) return;
|
||||
|
||||
void State::handleEvent(const SDL_Event& e)
|
||||
{
|
||||
auto m = getCurrentMenu();
|
||||
if(!m) return;
|
||||
switch (e.type) {
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (e.button.button == SDL_BUTTON_LEFT)
|
||||
m->click(e.button.x, e.button.y);
|
||||
break;
|
||||
|
||||
switch(e.type) {
|
||||
case SDL_MOUSEBUTTONUP:
|
||||
if (e.button.button == SDL_BUTTON_LEFT)
|
||||
m->click(e.button.x, e.button.y);
|
||||
break;
|
||||
case SDL_MOUSEMOTION:
|
||||
m->hover(e.motion.x, e.motion.y);
|
||||
break;
|
||||
|
||||
case SDL_MOUSEMOTION:
|
||||
m->hover(e.motion.x, e.motion.y);
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_UP:
|
||||
m->move(-1);
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
switch (e.key.keysym.sym) {
|
||||
case SDLK_UP:
|
||||
m->move(-1);
|
||||
break;
|
||||
case SDLK_DOWN:
|
||||
m->move(1);
|
||||
break;
|
||||
|
||||
case SDLK_DOWN:
|
||||
m->move(1);
|
||||
break;
|
||||
|
||||
case SDLK_RETURN:
|
||||
m->activate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
case SDLK_RETURN:
|
||||
m->activate();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ViewCamera& State::getCamera()
|
||||
{
|
||||
return defaultView;
|
||||
const ViewCamera& State::getCamera() {
|
||||
return defaultView;
|
||||
}
|
||||
|
||||
bool State::shouldWorldUpdate()
|
||||
{
|
||||
return false;
|
||||
bool State::shouldWorldUpdate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
GameWorld* State::getWorld()
|
||||
{
|
||||
return game->getWorld();
|
||||
GameWorld* State::getWorld() {
|
||||
return game->getWorld();
|
||||
}
|
||||
|
||||
GameWindow& State::getWindow()
|
||||
{
|
||||
return game->getWindow();
|
||||
GameWindow& State::getWindow() {
|
||||
return game->getWindow();
|
||||
}
|
||||
|
188
rwgame/State.hpp
188
rwgame/State.hpp
@ -1,123 +1,111 @@
|
||||
#ifndef _GAME_STATE_HPP_
|
||||
#define _GAME_STATE_HPP_
|
||||
#include <functional>
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include <queue>
|
||||
#include <render/ViewCamera.hpp>
|
||||
#include "GameWindow.hpp"
|
||||
#include "MenuSystem.hpp"
|
||||
#include <glm/gtc/quaternion.hpp>
|
||||
#include "SDL.h"
|
||||
#include "SDL_events.h"
|
||||
#include "GameWindow.hpp"
|
||||
|
||||
class RWGame;
|
||||
class GameWorld;
|
||||
|
||||
struct State
|
||||
{
|
||||
// Helper for global menu behaviour
|
||||
Menu* currentMenu;
|
||||
Menu* nextMenu;
|
||||
struct State {
|
||||
// Helper for global menu behaviour
|
||||
Menu* currentMenu;
|
||||
Menu* nextMenu;
|
||||
|
||||
RWGame* game;
|
||||
|
||||
State(RWGame* game)
|
||||
: currentMenu(nullptr), nextMenu(nullptr), game(game) {}
|
||||
|
||||
virtual void enter() = 0;
|
||||
virtual void exit() = 0;
|
||||
|
||||
virtual void tick(float dt) = 0;
|
||||
|
||||
virtual void draw(GameRenderer* r)
|
||||
{
|
||||
if(getCurrentMenu()) {
|
||||
getCurrentMenu()->draw(r);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~State() {
|
||||
if(getCurrentMenu()) {
|
||||
delete getCurrentMenu();
|
||||
}
|
||||
}
|
||||
|
||||
void enterMenu(Menu* menu)
|
||||
{
|
||||
nextMenu = menu;
|
||||
}
|
||||
|
||||
Menu* getCurrentMenu()
|
||||
{
|
||||
if(nextMenu) {
|
||||
if(currentMenu) {
|
||||
delete currentMenu;
|
||||
}
|
||||
currentMenu = nextMenu;
|
||||
nextMenu = nullptr;
|
||||
}
|
||||
return currentMenu;
|
||||
}
|
||||
RWGame* game;
|
||||
|
||||
virtual void handleEvent(const SDL_Event& e);
|
||||
State(RWGame* game) : currentMenu(nullptr), nextMenu(nullptr), game(game) {
|
||||
}
|
||||
|
||||
virtual const ViewCamera& getCamera();
|
||||
|
||||
/**
|
||||
* Returns false if the game world should not should
|
||||
* not update while this state is active
|
||||
*/
|
||||
virtual bool shouldWorldUpdate();
|
||||
virtual void enter() = 0;
|
||||
virtual void exit() = 0;
|
||||
|
||||
GameWorld* getWorld();
|
||||
GameWindow& getWindow();
|
||||
virtual void tick(float dt) = 0;
|
||||
|
||||
virtual void draw(GameRenderer* r) {
|
||||
if (getCurrentMenu()) {
|
||||
getCurrentMenu()->draw(r);
|
||||
}
|
||||
}
|
||||
|
||||
virtual ~State() {
|
||||
if (getCurrentMenu()) {
|
||||
delete getCurrentMenu();
|
||||
}
|
||||
}
|
||||
|
||||
void enterMenu(Menu* menu) {
|
||||
nextMenu = menu;
|
||||
}
|
||||
|
||||
Menu* getCurrentMenu() {
|
||||
if (nextMenu) {
|
||||
if (currentMenu) {
|
||||
delete currentMenu;
|
||||
}
|
||||
currentMenu = nextMenu;
|
||||
nextMenu = nullptr;
|
||||
}
|
||||
return currentMenu;
|
||||
}
|
||||
|
||||
virtual void handleEvent(const SDL_Event& e);
|
||||
|
||||
virtual const ViewCamera& getCamera();
|
||||
|
||||
/**
|
||||
* Returns false if the game world should not should
|
||||
* not update while this state is active
|
||||
*/
|
||||
virtual bool shouldWorldUpdate();
|
||||
|
||||
GameWorld* getWorld();
|
||||
GameWindow& getWindow();
|
||||
};
|
||||
|
||||
struct StateManager
|
||||
{
|
||||
static StateManager& get()
|
||||
{
|
||||
static StateManager m;
|
||||
return m;
|
||||
}
|
||||
|
||||
std::deque<State*> states;
|
||||
struct StateManager {
|
||||
static StateManager& get() {
|
||||
static StateManager m;
|
||||
return m;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
states.clear();
|
||||
}
|
||||
|
||||
void enter(State* state)
|
||||
{
|
||||
states.push_back(state);
|
||||
state->enter();
|
||||
}
|
||||
std::deque<State*> states;
|
||||
|
||||
void exec(State* state)
|
||||
{
|
||||
exit();
|
||||
enter(state);
|
||||
}
|
||||
|
||||
void tick(float dt)
|
||||
{
|
||||
states.back()->tick(dt);
|
||||
}
|
||||
|
||||
void draw(GameRenderer* r)
|
||||
{
|
||||
states.back()->draw(r);
|
||||
}
|
||||
|
||||
void exit()
|
||||
{
|
||||
// TODO: Resole states being leaked.
|
||||
states.back()->exit();
|
||||
states.pop_back();
|
||||
if(states.size() > 0) {
|
||||
states.back()->enter();
|
||||
}
|
||||
}
|
||||
void clear() {
|
||||
states.clear();
|
||||
}
|
||||
|
||||
void enter(State* state) {
|
||||
states.push_back(state);
|
||||
state->enter();
|
||||
}
|
||||
|
||||
void exec(State* state) {
|
||||
exit();
|
||||
enter(state);
|
||||
}
|
||||
|
||||
void tick(float dt) {
|
||||
states.back()->tick(dt);
|
||||
}
|
||||
|
||||
void draw(GameRenderer* r) {
|
||||
states.back()->draw(r);
|
||||
}
|
||||
|
||||
void exit() {
|
||||
// TODO: Resole states being leaked.
|
||||
states.back()->exit();
|
||||
states.pop_back();
|
||||
if (states.size() > 0) {
|
||||
states.back()->enter();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
||||
|
@ -1,20 +1,21 @@
|
||||
#ifndef GAME_HPP
|
||||
#define GAME_HPP
|
||||
|
||||
#include <objects/GameObject.hpp>
|
||||
#include <engine/GameWorld.hpp>
|
||||
#include <objects/GameObject.hpp>
|
||||
|
||||
constexpr double PiOver180 = 3.1415926535897932384626433832795028/180;
|
||||
constexpr double PiOver180 = 3.1415926535897932384626433832795028 / 180;
|
||||
|
||||
// TODO: Move all of this stuff so it's not just lying around.
|
||||
|
||||
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal, GameObject** object = nullptr);
|
||||
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
|
||||
GameObject** object = nullptr);
|
||||
bool hitWorldRay(const glm::vec3& start, const glm::vec3& direction,
|
||||
glm::vec3& hit, glm::vec3& normal, GameObject** object = nullptr);
|
||||
glm::vec3& hit, glm::vec3& normal,
|
||||
GameObject** object = nullptr);
|
||||
|
||||
#define GAME_TIMESTEP (1.f/30.f)
|
||||
#define GAME_TIMESTEP (1.f / 30.f)
|
||||
#define GAME_WINDOW_WIDTH 800
|
||||
#define GAME_WINDOW_HEIGHT 600
|
||||
|
||||
#endif // GAME_HPP
|
||||
|
||||
#endif // GAME_HPP
|
||||
|
@ -1,35 +1,34 @@
|
||||
#include <iostream>
|
||||
#include "RWGame.hpp"
|
||||
#include "SDL.h"
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
try {
|
||||
RWGame game(argc, argv);
|
||||
int main(int argc, char* argv[]) {
|
||||
try {
|
||||
RWGame game(argc, argv);
|
||||
|
||||
return game.run();
|
||||
} catch (std::invalid_argument& ex) {
|
||||
// This exception is thrown when either an invalid command line option
|
||||
// or a --help is found. The RWGame constructor prints a usage message
|
||||
// in this case and then throws this exception.
|
||||
return -2;
|
||||
} catch (std::runtime_error& ex) {
|
||||
// Catch runtime_error as these are fatal issues the user may want to
|
||||
// know about like corrupted files or GL initialisation failure.
|
||||
// Catching other types (out_of_range, bad_alloc) would just make
|
||||
// debugging them more difficult.
|
||||
return game.run();
|
||||
} catch (std::invalid_argument& ex) {
|
||||
// This exception is thrown when either an invalid command line option
|
||||
// or a --help is found. The RWGame constructor prints a usage message
|
||||
// in this case and then throws this exception.
|
||||
return -2;
|
||||
} catch (std::runtime_error& ex) {
|
||||
// Catch runtime_error as these are fatal issues the user may want to
|
||||
// know about like corrupted files or GL initialisation failure.
|
||||
// Catching other types (out_of_range, bad_alloc) would just make
|
||||
// debugging them more difficult.
|
||||
|
||||
const char* kErrorTitle = "Fatal Error";
|
||||
const char* kErrorTitle = "Fatal Error";
|
||||
|
||||
std::cerr << kErrorTitle << "\n" << ex.what() << std::endl;
|
||||
std::cerr << kErrorTitle << "\n" << ex.what() << std::endl;
|
||||
|
||||
if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, kErrorTitle,
|
||||
ex.what(), NULL) < 0) {
|
||||
SDL_Log("Failed to show message box\n");
|
||||
}
|
||||
if (SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, kErrorTitle,
|
||||
ex.what(), NULL) < 0) {
|
||||
SDL_Log("Failed to show message box\n");
|
||||
}
|
||||
|
||||
SDL_Quit();
|
||||
SDL_Quit();
|
||||
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user