From ac31d061068888ccdc6d13c5a4e212cf2d1f14c2 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 5 Aug 2010 00:09:45 +0000 Subject: [PATCH] - added more error checks in code and defaults for invalid font size values. - re-enabled client side changing of values from connect menu without waiting for server reply. --- source/glest_game/global/metrics.cpp | 89 +++++++++++++++++++ source/glest_game/graphics/renderer.cpp | 18 ++-- .../menu/menu_state_connected_game.cpp | 2 +- 3 files changed, 103 insertions(+), 6 deletions(-) create mode 100644 source/glest_game/global/metrics.cpp diff --git a/source/glest_game/global/metrics.cpp b/source/glest_game/global/metrics.cpp new file mode 100644 index 00000000..4a3c16f7 --- /dev/null +++ b/source/glest_game/global/metrics.cpp @@ -0,0 +1,89 @@ +// ============================================================== +// This file is part of Glest (www.glest.org) +// +// Copyright (C) 2001-2008 Martio Figueroa +// +// You can redistribute this code and/or modify it under +// the terms of the GNU General Public License as published +// by the Free Software Foundation; either version 2 of the +// License, or (at your option) any later version +// ============================================================== + +#include "metrics.h" + +#include "element_type.h" +#include +#include "leak_dumper.h" + +using namespace std; + +namespace Glest{ namespace Game{ + + +// ===================================================== +// class Metrics +// ===================================================== + +Metrics::Metrics(){ + Config &config= Config::getInstance(); + + virtualW= 1000; + virtualH= 750; + + screenW= config.getInt("ScreenWidth"); + screenH= config.getInt("ScreenHeight"); + + minimapX= 10; + minimapY= 750-128-30+16; + minimapW= 128; + minimapH= 128; + + displayX= 800; + displayY= 250; + displayW= 128; + displayH= 480; +} + +const Metrics &Metrics::getInstance(){ + static const Metrics metrics; + return metrics; +} + +float Metrics::getAspectRatio() const{ + if(screenH == 0) { + throw runtime_error("div by 0 screenH == 0"); + } + return static_cast(screenW)/screenH; +} + +int Metrics::toVirtualX(int w) const{ + if(screenW == 0) { + throw runtime_error("div by 0 screenW == 0"); + } + return w*virtualW/screenW; +} + +int Metrics::toVirtualY(int h) const{ + if(screenH == 0) { + throw runtime_error("div by 0 screenH == 0"); + } + return h*virtualH/screenH; +} + +bool Metrics::isInDisplay(int x, int y) const{ + return + x > displayX && + y > displayY && + x < displayX+displayW && + y < displayY+displayH; +} + +bool Metrics::isInMinimap(int x, int y) const{ + return + x > minimapX && + y > minimapY && + x < minimapX+minimapW && + y < minimapY+minimapH; +} + +}}// end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index 11886400..f1218d46 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -932,14 +932,22 @@ void Renderer::renderSelectionQuad(){ } Vec2i computeCenteredPos(const string &text, const Font2D *font, int x, int y){ - Vec2i textPos; - + if(font == NULL) { + throw runtime_error("font == NULL"); + } const Metrics &metrics= Metrics::getInstance(); const FontMetrics *fontMetrics= font->getMetrics(); - textPos= Vec2i( - x-metrics.toVirtualX(static_cast(fontMetrics->getTextWidth(text)/2.f)), - y-metrics.toVirtualY(static_cast(fontMetrics->getHeight()/2.f))); + if(fontMetrics == NULL) { + throw runtime_error("fontMetrics == NULL"); + } + + int virtualX = (fontMetrics->getTextWidth(text) > 0 ? static_cast(fontMetrics->getTextWidth(text)/2.f) : 5); + int virtualY = (fontMetrics->getHeight() > 0 ? static_cast(fontMetrics->getHeight()/2.f) : 5); + + Vec2i textPos( + x-metrics.toVirtualX(virtualX), + y-metrics.toVirtualY(virtualY)); return textPos; } diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index aad14432..44e4773d 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -240,7 +240,7 @@ void MenuStateConnectedGame::mouseClick(int x, int y, MouseButton mouseButton){ NetworkManager &networkManager= NetworkManager::getInstance(); ClientInterface* clientInterface= networkManager.getClientInterface(); - if (!settingsReceivedFromServer) return; + //if (!settingsReceivedFromServer) return; if(buttonDisconnect.mouseClick(x,y)){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line %d]\n",__FILE__,__FUNCTION__,__LINE__);