1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-11-25 11:52:40 +01:00

Add wrapper class for HUD drawing parameters

This commit is contained in:
Petar Kukolj 2018-10-27 22:35:10 +02:00 committed by Filip Gawin
parent ef29878039
commit 811765fd79
3 changed files with 131 additions and 94 deletions

View File

@ -10,54 +10,18 @@
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
constexpr size_t ui_textSize = 25;
constexpr size_t ui_textHeight = 22;
constexpr size_t ui_elementMargin = 3;
constexpr size_t ui_outerMargin = 20;
constexpr size_t ui_infoMargin = 10;
constexpr size_t ui_weaponSize = 64;
constexpr size_t ui_ammoSize = 14;
constexpr size_t ui_ammoHeight = 16;
constexpr size_t ui_wantedLevelHeight =
ui_outerMargin + ui_weaponSize + ui_elementMargin;
constexpr size_t ui_scriptTimerHeight =
ui_wantedLevelHeight + ui_textHeight + ui_elementMargin;
constexpr size_t ui_armourOffset = ui_textSize * 3;
constexpr size_t ui_maxWantedLevel = 6;
constexpr size_t ui_lowHealth = 9;
const glm::u8vec3 ui_timeColour(196, 165, 119);
const glm::u8vec3 ui_moneyColour(89, 113, 147);
const glm::u8vec3 ui_healthColour(187, 102, 47);
const glm::u8vec3 ui_armourColour(123, 136, 93);
const glm::u8vec3 ui_scriptTimerColour(186, 101, 50);
const glm::u8vec3 ui_shadowColour(0, 0, 0);
constexpr float ui_mapSize = 150.f;
constexpr float ui_worldSizeMin = 200.f;
constexpr float ui_worldSizeMax = 300.f;
static float hudScale = 1.f;
static float final_ui_textSize = ui_textSize;
static float final_ui_textHeight = ui_textHeight;
static float final_ui_elementMargin = ui_elementMargin;
static float final_ui_outerMargin = ui_outerMargin;
static float final_ui_infoMargin = ui_infoMargin;
static float final_ui_weaponSize = ui_weaponSize;
static float final_ui_ammoSize = ui_ammoSize;
static float final_ui_ammoHeight = ui_ammoHeight;
static float final_ui_wantedLevelHeight = ui_wantedLevelHeight;
static float final_ui_scriptTimerHeight = ui_scriptTimerHeight;
static float final_ui_armourOffset = ui_armourOffset;
static float final_ui_mapSize = ui_mapSize;
void drawScriptTimer(GameWorld* world, GameRenderer* render) { void drawScriptTimer(GameWorld* world, GameRenderer* render) {
HUDParameters& hudParams = HUDParameters::getInstance();
HUDParameters::Parameters parameters = hudParams.getHUDParameters();
if (world->state->scriptTimerVariable) { if (world->state->scriptTimerVariable) {
float scriptTimerTextX = static_cast<float>( float scriptTimerTextX = static_cast<float>(
render->getRenderer()->getViewport().x - final_ui_outerMargin); render->getRenderer()->getViewport().x - parameters.uiOuterMargin);
float scriptTimerTextY = final_ui_scriptTimerHeight; float scriptTimerTextY = parameters.uiScriptTimerHeight;
TextRenderer::TextInfo ti; TextRenderer::TextInfo ti;
ti.font = FONT_PRICEDOWN; ti.font = FONT_PRICEDOWN;
ti.size = final_ui_textSize; ti.size = parameters.uiTextSize;
ti.align = TextRenderer::TextInfo::TextAlignment::Right; ti.align = TextRenderer::TextInfo::TextAlignment::Right;
{ {
@ -69,12 +33,12 @@ void drawScriptTimer(GameWorld* world, GameRenderer* render) {
ti.text = GameStringUtil::fromString(ss.str(), ti.font); ti.text = GameStringUtil::fromString(ss.str(), ti.font);
} }
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = ti.screenPosition =
glm::vec2(scriptTimerTextX + 1.f, scriptTimerTextY + 1.f); glm::vec2(scriptTimerTextX + 1.f, scriptTimerTextY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
ti.baseColour = ui_scriptTimerColour; ti.baseColour = hudParams.uiScriptTimerColour;
ti.screenPosition = glm::vec2(scriptTimerTextX, scriptTimerTextY); ti.screenPosition = glm::vec2(scriptTimerTextX, scriptTimerTextY);
render->text.renderText(ti); render->text.renderText(ti);
} }
@ -83,14 +47,16 @@ void drawScriptTimer(GameWorld* world, GameRenderer* render) {
void drawMap(ViewCamera& currentView, PlayerController* player, void drawMap(ViewCamera& currentView, PlayerController* player,
GameWorld* world, GameRenderer* render) { GameWorld* world, GameRenderer* render) {
MapRenderer::MapInfo map; MapRenderer::MapInfo map;
HUDParameters& hudParams = HUDParameters::getInstance();
HUDParameters::Parameters parameters = hudParams.getHUDParameters();
if (world->state->hudFlash != HudFlash::FlashRadar || if (world->state->hudFlash != HudFlash::FlashRadar ||
std::fmod(world->getGameTime(), 0.5f) >= .25f) { std::fmod(world->getGameTime(), 0.5f) >= .25f) {
glm::quat camRot = currentView.rotation; glm::quat camRot = currentView.rotation;
map.rotation = glm::roll(camRot) - glm::half_pi<float>(); map.rotation = glm::roll(camRot) - glm::half_pi<float>();
map.worldSize = ui_worldSizeMin; map.worldSize = hudParams.uiWorldSizeMin;
map.worldSize = ui_worldSizeMax; map.worldSize = hudParams.uiWorldSizeMax;
if (player) { if (player) {
map.worldCenter = glm::vec2(player->getCharacter()->getPosition()); map.worldCenter = glm::vec2(player->getCharacter()->getPosition());
} }
@ -98,12 +64,12 @@ void drawMap(ViewCamera& currentView, PlayerController* player,
const glm::ivec2& vp = render->getRenderer()->getViewport(); const glm::ivec2& vp = render->getRenderer()->getViewport();
glm::vec2 mapTop = glm::vec2 mapTop =
glm::vec2(final_ui_outerMargin, vp.y - (final_ui_outerMargin + final_ui_mapSize)); glm::vec2(parameters.uiOuterMargin, vp.y - (parameters.uiOuterMargin + parameters.uiMapSize));
glm::vec2 mapBottom = glm::vec2 mapBottom =
glm::vec2(final_ui_outerMargin + final_ui_mapSize, vp.y - final_ui_outerMargin); glm::vec2(parameters.uiOuterMargin + parameters.uiMapSize, vp.y - parameters.uiOuterMargin);
map.screenPosition = (mapTop + mapBottom) / 2.f; map.screenPosition = (mapTop + mapBottom) / 2.f;
map.screenSize = final_ui_mapSize * 0.95f; map.screenSize = parameters.uiMapSize * 0.95f;
render->map.draw(world, map); render->map.draw(world, map);
} }
@ -111,18 +77,21 @@ void drawMap(ViewCamera& currentView, PlayerController* player,
void drawPlayerInfo(PlayerController* player, GameWorld* world, void drawPlayerInfo(PlayerController* player, GameWorld* world,
GameRenderer* render) { GameRenderer* render) {
HUDParameters& hudParams = HUDParameters::getInstance();
HUDParameters::Parameters parameters = hudParams.getHUDParameters();
float infoTextX = static_cast<float>(render->getRenderer()->getViewport().x - float infoTextX = static_cast<float>(render->getRenderer()->getViewport().x -
(final_ui_outerMargin + final_ui_weaponSize + ui_infoMargin)); (parameters.uiOuterMargin + parameters.uiWeaponSize + parameters.uiInfoMargin));
float infoTextY = 0.f + ui_outerMargin; float infoTextY = 0.f + parameters.uiOuterMargin;
float iconX = static_cast<float>(render->getRenderer()->getViewport().x - float iconX = static_cast<float>(render->getRenderer()->getViewport().x -
(final_ui_outerMargin + final_ui_weaponSize)); (parameters.uiOuterMargin + parameters.uiWeaponSize));
float iconY = final_ui_outerMargin; float iconY = parameters.uiOuterMargin;
float wantedX = static_cast<float>(render->getRenderer()->getViewport().x - final_ui_outerMargin); float wantedX = static_cast<float>(render->getRenderer()->getViewport().x - parameters.uiOuterMargin);
float wantedY = final_ui_wantedLevelHeight; float wantedY = parameters.uiWantedLevelHeight;
TextRenderer::TextInfo ti; TextRenderer::TextInfo ti;
ti.font = FONT_PRICEDOWN; ti.font = FONT_PRICEDOWN;
ti.size = final_ui_textSize; ti.size = parameters.uiTextSize;
ti.align = TextRenderer::TextInfo::TextAlignment::Right; ti.align = TextRenderer::TextInfo::TextAlignment::Right;
{ {
@ -133,16 +102,16 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
ti.text = GameStringUtil::fromString(ss.str(), ti.font); ti.text = GameStringUtil::fromString(ss.str(), ti.font);
} }
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
ti.baseColour = ui_timeColour; ti.baseColour = hudParams.uiTimeColour;
ti.screenPosition = glm::vec2(infoTextX, infoTextY); ti.screenPosition = glm::vec2(infoTextX, infoTextY);
render->text.renderText(ti); render->text.renderText(ti);
infoTextY += final_ui_textHeight; infoTextY += parameters.uiTextHeight;
{ {
std::stringstream ss; std::stringstream ss;
@ -152,19 +121,19 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
ti.text = GameSymbols::Money + GameStringUtil::fromString(ss.str(), ti.font); ti.text = GameSymbols::Money + GameStringUtil::fromString(ss.str(), ti.font);
} }
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
ti.baseColour = ui_moneyColour; ti.baseColour = hudParams.uiMoneyColour;
ti.screenPosition = glm::vec2(infoTextX, infoTextY); ti.screenPosition = glm::vec2(infoTextX, infoTextY);
render->text.renderText(ti); render->text.renderText(ti);
infoTextY += final_ui_textHeight; infoTextY += parameters.uiTextHeight;
if ((world->state->hudFlash != HudFlash::FlashHealth && if ((world->state->hudFlash != HudFlash::FlashHealth &&
player->getCharacter()->getCurrentState().health > ui_lowHealth) || player->getCharacter()->getCurrentState().health > hudParams.uiLowHealth) ||
std::fmod(world->getGameTime(), 0.5f) >= std::fmod(world->getGameTime(), 0.5f) >=
.25f) { // UI: Blinking health indicator if health is low .25f) { // UI: Blinking health indicator if health is low
std::stringstream ss; std::stringstream ss;
@ -173,12 +142,12 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
player->getCharacter()->getCurrentState().health); player->getCharacter()->getCurrentState().health);
ti.text = GameSymbols::Heart + GameStringUtil::fromString(ss.str(), ti.font); ti.text = GameSymbols::Heart + GameStringUtil::fromString(ss.str(), ti.font);
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f); ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
ti.baseColour = ui_healthColour; ti.baseColour = hudParams.uiHealthColour;
ti.screenPosition = glm::vec2(infoTextX, infoTextY); ti.screenPosition = glm::vec2(infoTextX, infoTextY);
render->text.renderText(ti); render->text.renderText(ti);
} }
@ -190,22 +159,22 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
player->getCharacter()->getCurrentState().armour); player->getCharacter()->getCurrentState().armour);
ti.text = GameSymbols::Armour + GameStringUtil::fromString(ss.str(), ti.font); ti.text = GameSymbols::Armour + GameStringUtil::fromString(ss.str(), ti.font);
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = ti.screenPosition =
glm::vec2(infoTextX + 1.f - final_ui_armourOffset, infoTextY + 1.f); glm::vec2(infoTextX + 1.f - parameters.uiArmourOffset, infoTextY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
ti.baseColour = ui_armourColour; ti.baseColour = hudParams.uiArmourColour;
ti.screenPosition = glm::vec2(infoTextX - final_ui_armourOffset, infoTextY); ti.screenPosition = glm::vec2(infoTextX - parameters.uiArmourOffset, infoTextY);
render->text.renderText(ti); render->text.renderText(ti);
} }
GameString s; GameString s;
for (size_t i = 0; i < ui_maxWantedLevel; ++i) { for (size_t i = 0; i < hudParams.uiMaxWantedLevel; ++i) {
s += GameSymbols::Star; s += GameSymbols::Star;
} }
ti.text = s; ti.text = s;
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.screenPosition = glm::vec2(wantedX + 1.f, wantedY + 1.f); ti.screenPosition = glm::vec2(wantedX + 1.f, wantedY + 1.f);
render->text.renderText(ti); render->text.renderText(ti);
@ -245,7 +214,7 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture"); RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture");
render->drawTexture( render->drawTexture(
itemTexture.get(), itemTexture.get(),
glm::vec4(iconX, iconY, final_ui_weaponSize, final_ui_weaponSize)); glm::vec4(iconX, iconY, parameters.uiWeaponSize, parameters.uiWeaponSize));
} }
if (weapon->fireType != WeaponData::MELEE) { if (weapon->fireType != WeaponData::MELEE) {
@ -275,12 +244,12 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world,
std::to_string(slotInfo.bulletsClip), ti.font); std::to_string(slotInfo.bulletsClip), ti.font);
} }
ti.baseColour = ui_shadowColour; ti.baseColour = hudParams.uiShadowColour;
ti.font = FONT_ARIAL; ti.font = FONT_ARIAL;
ti.size = final_ui_ammoSize; ti.size = parameters.uiAmmoSize;
ti.align = TextRenderer::TextInfo::TextAlignment::Center; ti.align = TextRenderer::TextInfo::TextAlignment::Center;
ti.screenPosition = glm::vec2(iconX + final_ui_weaponSize / 2.f, ti.screenPosition = glm::vec2(iconX + parameters.uiWeaponSize / 2.f,
iconY + final_ui_weaponSize - final_ui_ammoHeight); iconY + parameters.uiWeaponSize - parameters.uiAmmoHeight);
render->text.renderText(ti); render->text.renderText(ti);
} }
} }
@ -295,6 +264,8 @@ void drawHUD(ViewCamera& currentView, PlayerController* player,
} }
void drawOnScreenText(GameWorld* world, GameRenderer* renderer) { void drawOnScreenText(GameWorld* world, GameRenderer* renderer) {
HUDParameters::Parameters parameters = HUDParameters::getInstance().getHUDParameters();
const auto vp = glm::vec2(renderer->getRenderer()->getViewport()); const auto vp = glm::vec2(renderer->getRenderer()->getViewport());
TextRenderer::TextInfo ti; TextRenderer::TextInfo ti;
@ -306,7 +277,7 @@ void drawOnScreenText(GameWorld* world, GameRenderer* renderer) {
for (auto& l : alltext) { for (auto& l : alltext) {
for (auto& t : l) { for (auto& t : l) {
ti.size = static_cast<float>(t.size * hudScale); ti.size = static_cast<float>(t.size * parameters.hudScale);
ti.font = t.font; ti.font = t.font;
ti.text = t.text; ti.text = t.text;
ti.wrapX = t.wrapX; ti.wrapX = t.wrapX;
@ -345,18 +316,22 @@ void drawOnScreenText(GameWorld* world, GameRenderer* renderer) {
} }
} }
void scaleHUD(const float scale) { void HUDParameters::scaleHUD(float scale) {
hudScale = scale; hudParameters.hudScale = scale;
final_ui_textSize = ui_textSize * scale; hudParameters.uiTextSize = uiTextSize * scale;
final_ui_textHeight = ui_textHeight * scale; hudParameters.uiTextHeight = uiTextHeight * scale;
final_ui_elementMargin = ui_elementMargin * scale; hudParameters.uiElementMargin = uiElementMargin * scale;
final_ui_outerMargin = ui_outerMargin * scale; hudParameters.uiOuterMargin = uiOuterMargin * scale;
final_ui_infoMargin = ui_infoMargin * scale; hudParameters.uiInfoMargin = uiInfoMargin * scale;
final_ui_weaponSize = ui_weaponSize * scale; hudParameters.uiWeaponSize = uiWeaponSize * scale;
final_ui_ammoSize = ui_ammoSize * scale; hudParameters.uiAmmoSize = uiAmmoSize * scale;
final_ui_ammoHeight = ui_ammoHeight * scale; hudParameters.uiAmmoHeight = uiAmmoHeight * scale;
final_ui_wantedLevelHeight = ui_wantedLevelHeight * scale; hudParameters.uiWantedLevelHeight = uiWantedLevelHeight * scale;
final_ui_scriptTimerHeight = ui_scriptTimerHeight * scale; hudParameters.uiScriptTimerHeight = uiScriptTimerHeight * scale;
final_ui_armourOffset = ui_armourOffset * scale; hudParameters.uiArmourOffset = uiArmourOffset * scale;
final_ui_mapSize = ui_mapSize * scale; hudParameters.uiMapSize = uiMapSize * scale;
}
HUDParameters::Parameters HUDParameters::getHUDParameters() {
return hudParameters;
} }

View File

@ -5,11 +5,73 @@
class PlayerController; class PlayerController;
/**
* Wrapper class for HUD drawing parameters.
*/
class HUDParameters {
public:
struct Parameters {
float hudScale;
float uiTextSize;
float uiTextHeight;
float uiElementMargin;
float uiOuterMargin;
float uiInfoMargin;
float uiWeaponSize;
float uiAmmoSize;
float uiAmmoHeight;
float uiWantedLevelHeight;
float uiScriptTimerHeight;
float uiArmourOffset;
float uiMapSize;
};
static const size_t uiTextSize = 25;
static const size_t uiTextHeight = 22;
static const size_t uiElementMargin = 3;
static const size_t uiOuterMargin = 20;
static const size_t uiInfoMargin = 10;
static const size_t uiWeaponSize = 64;
static const size_t uiAmmoSize = 14;
static const size_t uiAmmoHeight = 16;
static const size_t uiWantedLevelHeight =
uiOuterMargin + uiWeaponSize + uiElementMargin;
static const size_t uiScriptTimerHeight =
uiWantedLevelHeight + uiTextHeight + uiElementMargin;
static const size_t uiArmourOffset = uiTextSize * 3;
static const size_t uiMaxWantedLevel = 6;
static const size_t uiLowHealth = 9;
static constexpr glm::u8vec3 uiTimeColour = glm::u8vec3(196, 165, 119);
static constexpr glm::u8vec3 uiMoneyColour = glm::u8vec3(89, 113, 147);
static constexpr glm::u8vec3 uiHealthColour = glm::u8vec3(187, 102, 47);
static constexpr glm::u8vec3 uiArmourColour = glm::u8vec3(123, 136, 93);
static constexpr glm::u8vec3 uiScriptTimerColour = glm::u8vec3(186, 101, 50);
static constexpr glm::u8vec3 uiShadowColour = glm::u8vec3(0, 0, 0);
static constexpr float uiMapSize = 150.f;
static constexpr float uiWorldSizeMin = 200.f;
static constexpr float uiWorldSizeMax = 300.f;
static HUDParameters& getInstance() {
static HUDParameters instance;
return instance;
}
void scaleHUD(float scale = 1.f);
Parameters getHUDParameters();
private:
Parameters hudParameters;
HUDParameters() = default;
~HUDParameters() = default;
HUDParameters(const HUDParameters&) = delete;
HUDParameters& operator=(const HUDParameters&) = delete;
};
void drawHUD(ViewCamera& currentView, PlayerController* player, void drawHUD(ViewCamera& currentView, PlayerController* player,
GameWorld* world, GameRenderer* render); GameWorld* world, GameRenderer* render);
void drawOnScreenText(GameWorld* world, GameRenderer* renderer); void drawOnScreenText(GameWorld* world, GameRenderer* renderer);
void scaleHUD(const float scale);
#endif #endif

View File

@ -75,7 +75,7 @@ RWGame::RWGame(Logger& log, int argc, char* argv[])
renderer.text.setFontTexture(FONT_PRICEDOWN, "font1"); renderer.text.setFontTexture(FONT_PRICEDOWN, "font1");
renderer.text.setFontTexture(FONT_ARIAL, "font2"); renderer.text.setFontTexture(FONT_ARIAL, "font2");
scaleHUD(config.getHUDScale()); HUDParameters::getInstance().scaleHUD(config.getHUDScale());
renderer.map.scaleHUD(config.getHUDScale()); renderer.map.scaleHUD(config.getHUDScale());
debug.setDebugMode(btIDebugDraw::DBG_DrawWireframe | debug.setDebugMode(btIDebugDraw::DBG_DrawWireframe |