- added caching for computeVisibleQuad

- fixed bug when in chat mode and the deub view toggle key was intercepted
- added less than operators
This commit is contained in:
Mark Vejvoda 2010-07-29 05:24:51 +00:00
parent d8e41495d0
commit 8feaebb503
10 changed files with 83 additions and 41 deletions

View File

@ -124,6 +124,7 @@ public:
virtual void mouseMove(int x, int y, const MouseState *mouseState);
void quitGame();
virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); }
private:
//render

View File

@ -16,12 +16,13 @@
#include "config.h"
#include "game_constants.h"
#include "xml_parser.h"
#include "conversion.h"
#include "leak_dumper.h"
using namespace Shared::Graphics;
using Shared::Xml::XmlNode;
using namespace Shared::Util;
namespace Glest { namespace Game {
@ -29,9 +30,9 @@ namespace Glest { namespace Game {
// class GameCamera
// =====================================================
// ================== PUBLIC =====================
static std::map<float, std::map<float, std::map<Vec3f, Quad2i> > > cacheVisibleQuad;
static std::map<std::string, Quad2i> cacheVisibleQuad;
// ================== PUBLIC =====================
const float GameCamera::startingVAng= -60.f;
const float GameCamera::startingHAng= 0.f;
@ -42,31 +43,13 @@ const float GameCamera::centerOffsetZ= 8.0f;
// ================= Constructor =================
class quadCacheLookup {
public:
quadCacheLookup(float fov,float hAng,Vec3f pos) {
this->fov = fov;
this->hAng = hAng;
this->pos = pos;
}
std::string getString() const {
std::ostringstream streamOut;
streamOut << fov << "_" << hAng << "_" << pos.getString();
return streamOut.str();
}
float fov;
float hAng;
Vec3f pos;
};
GameCamera::GameCamera() : pos(0.f, defaultHeight, 0.f),
destPos(0.f, defaultHeight, 0.f), destAng(startingVAng, startingHAng) {
Config &config = Config::getInstance();
state= sGame;
cacheVisibleQuad.clear();
MaxVisibleQuadItemCache = config.getInt("MaxVisibleQuadItemCache",intToStr(-1).c_str());
//config
speed= 15.f / GameConstants::cameraFps;
@ -89,6 +72,10 @@ GameCamera::GameCamera() : pos(0.f, defaultHeight, 0.f),
fov = Config::getInstance().getFloat("CameraFov","45");
}
GameCamera::~GameCamera() {
cacheVisibleQuad.clear();
}
void GameCamera::init(int limitX, int limitY){
this->limitX= limitX;
this->limitY= limitY;
@ -165,14 +152,19 @@ void GameCamera::update(){
}
Quad2i GameCamera::computeVisibleQuad() const {
//
//quadCacheLookup lookup(fov, hAng, pos);
//string lookupKey = lookup.getString();
//std::map<std::string, Quad2i>::const_iterator iterFind = cacheVisibleQuad.find(lookupKey);
//if(iterFind != cacheVisibleQuad.end()) {
// return iterFind->second;
//}
//
if(MaxVisibleQuadItemCache != 0) {
std::map<float, std::map<float, std::map<Vec3f, Quad2i> > >::const_iterator iterFind = cacheVisibleQuad.find(fov);
if(iterFind != cacheVisibleQuad.end()) {
std::map<float, std::map<Vec3f, Quad2i> >::const_iterator iterFind2 = iterFind->second.find(hAng);
if(iterFind2 != iterFind->second.end()) {
std::map<Vec3f, Quad2i>::const_iterator iterFind3 = iterFind2->second.find(pos);
if(iterFind3 != iterFind2->second.end()) {
return iterFind3->second;
}
}
}
}
float nearDist = 20.f;
float dist = pos.y > 20.f ? pos.y * 1.2f : 20.f;
@ -199,18 +191,31 @@ Quad2i GameCamera::computeVisibleQuad() const {
Vec2i p4(static_cast<int>(p.x + v2.x * farDist), static_cast<int>(p.y + v2.y * farDist));
if (hAng >= 135 && hAng <= 225) {
if(MaxVisibleQuadItemCache != 0 &&
(MaxVisibleQuadItemCache < 0 || cacheVisibleQuad[fov][hAng].size() <= MaxVisibleQuadItemCache)) {
cacheVisibleQuad[fov][hAng][pos] = Quad2i(p1, p2, p3, p4);
}
return Quad2i(p1, p2, p3, p4);
}
if (hAng >= 45 && hAng <= 135) {
if(MaxVisibleQuadItemCache != 0 &&
(MaxVisibleQuadItemCache < 0 || cacheVisibleQuad[fov][hAng].size() <= MaxVisibleQuadItemCache)) {
cacheVisibleQuad[fov][hAng][pos] = Quad2i(p3, p1, p4, p2);
}
return Quad2i(p3, p1, p4, p2);
}
if (hAng >= 225 && hAng <= 315) {
if(MaxVisibleQuadItemCache != 0 &&
(MaxVisibleQuadItemCache < 0 || cacheVisibleQuad[fov][hAng].size() <= MaxVisibleQuadItemCache)) {
cacheVisibleQuad[fov][hAng][pos] = Quad2i(p2, p4, p1, p3);
}
return Quad2i(p2, p4, p1, p3);
}
//cacheVisibleQuad[lookupKey] = Quad2i(p4, p3, p2, p1);
//cacheVisibleQuad.insert(std::make_pair(lookupKey,Quad2i(p4, p3, p2, p1)));
//return cacheVisibleQuad[lookupKey];
if(MaxVisibleQuadItemCache != 0 &&
(MaxVisibleQuadItemCache < 0 || cacheVisibleQuad[fov][hAng].size() <= MaxVisibleQuadItemCache)) {
cacheVisibleQuad[fov][hAng][pos] = Quad2i(p4, p3, p2, p1);
}
return Quad2i(p4, p3, p2, p1);
}

View File

@ -37,7 +37,7 @@ class Config;
/// A basic camera that holds information about the game view
// =====================================================
class GameCamera{
class GameCamera {
public:
static const float startingVAng;
static const float startingHAng;
@ -83,8 +83,11 @@ private:
float maxVAng;
float fov;
int MaxVisibleQuadItemCache;
public:
GameCamera();
~GameCamera();
void init(int limitX, int limitY);

View File

@ -23,7 +23,6 @@
#include "platform_util.h"
#include "platform_main.h"
#include "network_interface.h"
//#include "sound_renderer.h"
#include "ImageReaders.h"
#include "renderer.h"
#include "simple_threads.h"
@ -290,11 +289,15 @@ void MainWindow::eventKeyDown(char key){
}
}
Config &configKeys = Config::getInstance(std::pair<ConfigType,ConfigType>(cfgMainKeys,cfgUserKeys));
if(key == configKeys.getCharKey("HotKeyShowDebug")) {
Renderer &renderer= Renderer::getInstance();
bool showDebugUI = renderer.getShowDebugUI();
renderer.setShowDebugUI(!showDebugUI);
if(program != NULL && program->isInSpecialKeyCaptureEvent() == false) {
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);
Config &configKeys = Config::getInstance(std::pair<ConfigType,ConfigType>(cfgMainKeys,cfgUserKeys));
if(key == configKeys.getCharKey("HotKeyShowDebug")) {
Renderer &renderer= Renderer::getInstance();
bool showDebugUI = renderer.getShowDebugUI();
renderer.setShowDebugUI(!showDebugUI);
}
}
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__);

View File

@ -75,6 +75,7 @@ public:
virtual void keyPress(char c){};
virtual void setStartXY(int X,int Y) { startX=X; startY=Y; }
virtual void restoreToStartXY() { SDL_WarpMouse(startX, startY); }
virtual bool isInSpecialKeyCaptureEvent() { return false; }
};
// ===============================
@ -151,6 +152,7 @@ public:
void eventMouseMove(int x, int y, const MouseState *ms);
void renderProgramMsgBox();
bool isInSpecialKeyCaptureEvent() { return (programState != NULL ? programState->isInSpecialKeyCaptureEvent() : false); }
private:

View File

@ -87,6 +87,8 @@ public:
virtual void keyPress(char c);
virtual void keyUp(char key);
virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); }
private:
bool hasNetworkGameSettings();

View File

@ -115,6 +115,7 @@ public:
virtual void simpleTask();
virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); }
private:

View File

@ -73,6 +73,8 @@ public:
virtual void keyDown(char key);
virtual void keyPress(char c);
virtual bool isInSpecialKeyCaptureEvent() { return chatManager.getEditEnabled(); }
private:
void connectToServer();
virtual void DiscoveredServers(std::vector<string> serverList);

View File

@ -138,6 +138,22 @@ public:
p[3]/scalar);
}
bool operator <(const Quad2<T> &v) const {
if(p[0] < v.p[0]) {
return true;
}
if(p[1] < v.p[1]) {
return true;
}
if(p[2] < v.p[2]) {
return true;
}
if(p[3] < v.p[3]) {
return true;
}
return false;
}
Rect2<T> computeBoundingRect() const{
return Rect2i(
min(p[0].x, p[1].x),

View File

@ -261,12 +261,16 @@ public:
}
Vec3<T> operator -=(const Vec3<T> &v){
x-=v.x;
x-=v.x;
y-=v.y;
z-=v.z;
return *this;
}
bool operator <(const Vec3<T> &v) const {
return x < v.x || (x == v.x && y < v.y) || (x == v.x && y == v.y && z < v.z);
}
Vec3<T> lerp(T t, const Vec3<T> &v) const{
return *this + (v - *this) * t;
}
@ -455,6 +459,9 @@ public:
w-=w.z;
return *this;
}
bool operator <(const Vec4<T> &v) const {
return x < v.x || (x == v.x && y < v.y) || (x == v.x && y == v.y && z < v.z) || (x == v.x && y == v.y && z == v.z && w < v.w);
}
Vec4<T> lerp(T t, const Vec4<T> &v) const{
return *this + (v - *this) *t;