From 1d4f47718ca34ceca940e45a279f7fe1288ed1cb Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Thu, 24 Jun 2010 01:23:18 +0000 Subject: [PATCH] - added many new things and fixed a number of bugs (too tried to mention them all) --- source/glest_game/game/game.cpp | 19 ++- source/glest_game/game/game.h | 1 + source/glest_game/game/stats.cpp | 1 + source/glest_game/game/stats.h | 8 + source/glest_game/graphics/renderer.cpp | 7 +- source/glest_game/graphics/renderer.h | 6 +- source/glest_game/main/battle_end.cpp | 20 ++- source/glest_game/main/main.cpp | 6 +- source/glest_game/main/program.cpp | 10 +- .../menu/menu_state_custom_game.cpp | 23 ++- .../menu/menu_state_masterserver.cpp | 2 +- source/glest_game/network/network_manager.cpp | 27 +++- .../glest_game/network/server_interface.cpp | 16 +- source/glest_game/network/server_interface.h | 3 + source/glest_game/world/world.cpp | 2 + .../include/graphics/gl/text_renderer_gl.h | 58 +++++++ .../include/graphics/text_renderer.h | 52 ++++++ .../include/platform/posix/socket.h | 9 +- .../shared_lib/include/platform/sdl/window.h | 4 + .../sources/graphics/gl/text_renderer_gl.cpp | 150 ++++++++++++++++++ .../platform/common/platform_common.cpp | 2 +- .../sources/platform/posix/socket.cpp | 34 +++- .../sources/platform/sdl/window.cpp | 31 ++-- 23 files changed, 444 insertions(+), 47 deletions(-) create mode 100644 source/shared_lib/include/graphics/gl/text_renderer_gl.h create mode 100644 source/shared_lib/include/graphics/text_renderer.h create mode 100644 source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp diff --git a/source/glest_game/game/game.cpp b/source/glest_game/game/game.cpp index b2371771e..4b5ac94d5 100644 --- a/source/glest_game/game/game.cpp +++ b/source/glest_game/game/game.cpp @@ -41,7 +41,7 @@ Game *thisGamePtr = NULL; // ===================== PUBLIC ======================== Game::Game(Program *program, const GameSettings *gameSettings): - ProgramState(program), lastMousePos(0) + ProgramState(program), lastMousePos(0), isFirstRender(true) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -328,6 +328,7 @@ void Game::init() chatManager.init(&console, world.getThisTeamIndex()); console.clearStoredLines(); + const Vec2i &v= map->getStartLocation(world.getThisFaction()->getStartLocationIndex()); gameCamera.init(map->getW(), map->getH()); gameCamera.setPos(Vec2f(v.x, v.y)); @@ -544,6 +545,15 @@ void Game::updateCamera(){ void Game::render() { //SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps); + // Ensure the camera starts in the right position + if(isFirstRender == true) { + isFirstRender = false; + Map *map= world.getMap(); + const Vec2i &v= map->getStartLocation(world.getThisFaction()->getStartLocationIndex()); + gameCamera.init(map->getW(), map->getH()); + gameCamera.setPos(Vec2f(v.x, v.y)); + } + renderFps++; renderWorker(); //SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d renderFps = %d\n",__FILE__,__FUNCTION__,__LINE__,renderFps); @@ -1017,7 +1027,12 @@ void Game::keyPress(char c){ void Game::quitGame(){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); - program->setState(new BattleEnd(program, world.getStats())); + + Stats stats = *(world.getStats()); + + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + + program->setState(new BattleEnd(program, &stats)); } // ==================== PRIVATE ==================== diff --git a/source/glest_game/game/game.h b/source/glest_game/game/game.h index ff4c2da85..0397ce8ca 100644 --- a/source/glest_game/game/game.h +++ b/source/glest_game/game/game.h @@ -83,6 +83,7 @@ private: Vec2i lastMousePos; time_t lastRenderLog2d; DisplayMessageFunction originalDisplayMsgCallback; + bool isFirstRender; public: Game(Program *program, const GameSettings *gameSettings); diff --git a/source/glest_game/game/stats.cpp b/source/glest_game/game/stats.cpp index 753c45275..6de47a780 100644 --- a/source/glest_game/game/stats.cpp +++ b/source/glest_game/game/stats.cpp @@ -22,6 +22,7 @@ PlayerStats::PlayerStats(){ deaths= 0; unitsProduced= 0; resourcesHarvested= 0; + playerName = ""; } // ===================================================== diff --git a/source/glest_game/game/stats.h b/source/glest_game/game/stats.h index 7f3fcb81e..81f36c486 100644 --- a/source/glest_game/game/stats.h +++ b/source/glest_game/game/stats.h @@ -16,8 +16,10 @@ #include "game_constants.h" #include "faction.h" +#include "vec.h" using std::string; +using namespace Shared::Graphics; namespace Glest{ namespace Game{ @@ -32,6 +34,8 @@ struct PlayerStats{ int deaths; int unitsProduced; int resourcesHarvested; + string playerName; + Vec3f playerColor; }; // ===================================================== @@ -63,6 +67,8 @@ public: int getDeaths(int factionIndex) const {return playerStats[factionIndex].deaths;} int getUnitsProduced(int factionIndex) const {return playerStats[factionIndex].unitsProduced;} int getResourcesHarvested(int factionIndex) const {return playerStats[factionIndex].resourcesHarvested;} + string getPlayerName(int factionIndex) const {return playerStats[factionIndex].playerName;} + Vec3f getPlayerColor(int factionIndex) const { return playerStats[factionIndex].playerColor;} void setDescription(const string& description) {this->description = description;} void setFactionTypeName(int playerIndex, const string& factionTypeName) {playerStats[playerIndex].factionTypeName= factionTypeName;} @@ -73,6 +79,8 @@ public: void die(int diedFactionIndex); void produce(int producerFactionIndex); void harvest(int harvesterFactionIndex, int amount); + void setPlayerName(int playerIndex, string value) {playerStats[playerIndex].playerName = value; } + void setPlayerColor(int playerIndex, Vec3f value) {playerStats[playerIndex].playerColor = value; } }; }}//end namespace diff --git a/source/glest_game/graphics/renderer.cpp b/source/glest_game/graphics/renderer.cpp index a5a2a5172..70fa18f2c 100644 --- a/source/glest_game/graphics/renderer.cpp +++ b/source/glest_game/graphics/renderer.cpp @@ -163,7 +163,9 @@ Renderer::Renderer(){ FactoryRepository &fr= FactoryRepository::getInstance(); Config &config= Config::getInstance(); - maxConsoleLines= Config::getInstance().getInt("ConsoleMaxLines"); + no2DMouseRendering = config.getBool("No2DMouseRendering","false"); + maxConsoleLines= config.getInt("ConsoleMaxLines"); + gi.setFactory(fr.getGraphicsFactory(config.getString("FactoryGraphics"))); GraphicsFactory *graphicsFactory= GraphicsInterface::getInstance().getFactory(); @@ -549,6 +551,9 @@ void Renderer::computeVisibleQuad(){ // ======================================= void Renderer::renderMouse2d(int x, int y, int anim, float fade){ + if(no2DMouseRendering == true) { + return; + } float color1, color2; float fadeFactor= fade+1.f; diff --git a/source/glest_game/graphics/renderer.h b/source/glest_game/graphics/renderer.h index 38d0fe653..b8ea620ad 100644 --- a/source/glest_game/graphics/renderer.h +++ b/source/glest_game/graphics/renderer.h @@ -261,6 +261,8 @@ private: bool allowRenderUnitTitles; std::vector > renderUnitTitleList; + bool no2DMouseRendering; + private: Renderer(); ~Renderer(); @@ -270,7 +272,6 @@ public: void reinitAll(); - //init void init(); void initGame(const Game *game); @@ -385,6 +386,9 @@ public: void setPhotoMode(bool value) { photoMode = value; } + bool getNo2DMouseRendering() const { return no2DMouseRendering; } + void setNo2DMouseRendering(bool value) { no2DMouseRendering = value; } + private: //private misc float computeSunAngle(float time); diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 2570b1a0e..be73b70a5 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -34,10 +34,11 @@ namespace Glest{ namespace Game{ // class BattleEnd // ===================================================== -BattleEnd::BattleEnd(Program *program, const Stats *stats): -ProgramState(program) { - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - this->stats= *stats; +BattleEnd::BattleEnd(Program *program, const Stats *stats): ProgramState(program) { + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] stats = %p\n",__FILE__,__FUNCTION__,__LINE__,stats); + if(stats != NULL) { + this->stats= *stats; + } SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } @@ -102,7 +103,14 @@ void BattleEnd::render(){ assert(false); }; - textRenderer->render((lang.get("Player")+" "+intToStr(i+1)).c_str(), textX, bm+400); + Vec3f color = stats.getPlayerColor(i); + + if(stats.getPlayerName(i) != "") { + textRenderer->render(stats.getPlayerName(i).c_str(), textX, bm+400, false, color); + } + else { + textRenderer->render((lang.get("Player")+" "+intToStr(i+1)).c_str(), textX, bm+400,false, color); + } textRenderer->render(stats.getVictory(i)? lang.get("Victory").c_str(): lang.get("Defeat").c_str(), textX, bm+360); textRenderer->render(controlString, textX, bm+320); textRenderer->render(stats.getFactionTypeName(i), textX, bm+280); @@ -146,13 +154,11 @@ void BattleEnd::render(){ void BattleEnd::keyDown(char key){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); program->setState(new MainMenu(program)); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } void BattleEnd::mouseDownLeft(int x, int y){ SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); program->setState(new MainMenu(program)); - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); } }}//end namespace diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index 3b49a11ea..7e9c4afd1 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -373,7 +373,9 @@ int glestMain(int argc, char** argv){ std::pair("glestkeys.ini","glestuserkeys.ini"), std::pair(true,false)); - showCursor(false); + if(config.getBool("No2DMouseRendering","false") == false) { + showCursor(false); + } if(config.getBool("noTeamColors","false") == true) { MeshCallbackTeamColor::noTeamColors = true; @@ -391,6 +393,8 @@ int glestMain(int argc, char** argv){ mainWindow= new MainWindow(program); + mainWindow->setUseDefaultCursorOnly(config.getBool("No2DMouseRendering","false")); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); //parse command line diff --git a/source/glest_game/main/program.cpp b/source/glest_game/main/program.cpp index ab356bbb9..49f84e712 100644 --- a/source/glest_game/main/program.cpp +++ b/source/glest_game/main/program.cpp @@ -296,7 +296,10 @@ void Program::setState(ProgramState *programState, bool cleanupOldState) updateCameraTimer.reset(); fpsTimer.reset(); - showCursor(false); + Config &config = Config::getInstance(); + if(config.getBool("No2DMouseRendering","false") == false) { + showCursor(false); + } sleep(0); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -484,7 +487,10 @@ void Program::showMessage(const char *msg) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d]\n",__FILE__,__FUNCTION__,__LINE__); - showCursor(false); + Config &config = Config::getInstance(); + if(config.getBool("No2DMouseRendering","false") == false) { + showCursor(false); + } //MainWindow *mainWindow= new MainWindow(this); init(this->window,false); diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 431ef7fd0..fc0e69807 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -200,7 +200,7 @@ MenuStateCustomGame::MenuStateCustomGame(Program *program, MainMenu *mainMenu, b listBoxEnableServerControlledAI.init(700, networkPos, 80); listBoxEnableServerControlledAI.pushBackItem(lang.get("Yes")); listBoxEnableServerControlledAI.pushBackItem(lang.get("No")); - listBoxEnableServerControlledAI.setSelectedItemIndex(1); + listBoxEnableServerControlledAI.setSelectedItemIndex(0); @@ -741,7 +741,7 @@ void MenuStateCustomGame::update() { if(EndsWith(masterServererErrorToShow, "wrong router setup") == true) { - masterServererErrorToShow=lang.get("wrong router setup"); + masterServererErrorToShow=lang.get("WrongRouterSetup"); } showMasterserverError=false; mainMessageBoxState=1; @@ -1508,7 +1508,24 @@ void MenuStateCustomGame::updateNetworkSlots() { if(serverInterface->getSlot(i) == NULL && listBoxControls[i].getSelectedItemIndex() == ctNetwork) { - serverInterface->addSlot(i); + try { + serverInterface->addSlot(i); + } + catch(const std::exception &ex) { + char szBuf[1024]=""; + sprintf(szBuf,"In [%s::%s %d] error [%s]\n",__FILE__,__FUNCTION__,__LINE__,ex.what()); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"%s",szBuf); + showGeneralError=true; + if(serverInterface->isPortBound() == false) { + generalErrorToShow = Lang::getInstance().get("ErrorBindingPort") + " : " + intToStr(serverInterface->getBindPort()); + } + else { + generalErrorToShow = ex.what(); + } + + // Revert network to CPU + listBoxControls[i].setSelectedItemIndex(2); + } } if(serverInterface->getSlot(i) != NULL && listBoxControls[i].getSelectedItemIndex() != ctNetwork) { diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 466dda020..cb6f1b69d 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -169,7 +169,7 @@ MenuStateMasterserver::MenuStateMasterserver(Program *program, MainMenu *mainMen listBoxAutoRefresh.pushBackItem("10 s"); listBoxAutoRefresh.pushBackItem("20 s"); listBoxAutoRefresh.pushBackItem("30 s"); - listBoxAutoRefresh.setSelectedItemIndex(0); + listBoxAutoRefresh.setSelectedItemIndex(1); SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); diff --git a/source/glest_game/network/network_manager.cpp b/source/glest_game/network/network_manager.cpp index dfdff065c..5b984de7a 100644 --- a/source/glest_game/network/network_manager.cpp +++ b/source/glest_game/network/network_manager.cpp @@ -1,7 +1,7 @@ // ============================================================== // This file is part of Glest (www.glest.org) // -// Copyright (C) 2001-2008 Martiņo Figueroa +// 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 @@ -10,8 +10,11 @@ // ============================================================== #include "network_manager.h" +#include "util.h" #include "leak_dumper.h" +using namespace Shared::Util; + namespace Glest{ namespace Game{ // ===================================================== @@ -23,13 +26,18 @@ NetworkManager &NetworkManager::getInstance(){ return networkManager; } -NetworkManager::NetworkManager(){ +NetworkManager::NetworkManager() { + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + gameNetworkInterface= NULL; networkRole= nrIdle; + + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); } -void NetworkManager::init(NetworkRole networkRole) -{ +void NetworkManager::init(NetworkRole networkRole) { + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + assert(gameNetworkInterface==NULL); this->networkRole = networkRole; @@ -37,16 +45,21 @@ void NetworkManager::init(NetworkRole networkRole) if(networkRole==nrServer){ gameNetworkInterface = new ServerInterface(); } - else - { + else { gameNetworkInterface = new ClientInterface(); } + + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); } -void NetworkManager::end(){ +void NetworkManager::end() { + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); + delete gameNetworkInterface; gameNetworkInterface= NULL; networkRole= nrIdle; + + SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s] Line: %d this->networkRole = %d\n",__FILE__,__FUNCTION__,__LINE__,this->networkRole); } void NetworkManager::update(){ diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index f255130a7..551b2c1b4 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -61,7 +61,8 @@ ServerInterface::ServerInterface(){ switchSetupRequests[i]= NULL; } serverSocket.setBlock(false); - serverSocket.bind(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str())); + //serverSocket.bind(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str())); + serverSocket.setBindPort(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str())); } ServerInterface::~ServerInterface(){ @@ -85,6 +86,10 @@ void ServerInterface::addSlot(int playerIndex){ assert(playerIndex>=0 && playerIndex &socketTriggeredList); + bool isPortBound() const { return serverSocket.isPortBound(); } + int getBindPort() const { return serverSocket.getBindPort(); } + public: Mutex * getServerSynchAccessor() { return &serverSynchAccessor; } diff --git a/source/glest_game/world/world.cpp b/source/glest_game/world/world.cpp index a6a4a030b..ae756e312 100644 --- a/source/glest_game/world/world.cpp +++ b/source/glest_game/world/world.cpp @@ -651,6 +651,8 @@ void World::initFactionTypes(GameSettings *gs){ stats.setTeam(i, gs->getTeam(i)); stats.setFactionTypeName(i, formatString(gs->getFactionTypeName(i))); stats.setControl(i, gs->getFactionControl(i)); + stats.setPlayerName(i,gs->getNetworkPlayerName(i)); + stats.setPlayerColor(i,getFaction(i)->getTexture()->getPixmap()->getPixel3f(0, 0)); } thisTeamIndex= getFaction(thisFactionIndex)->getTeam(); diff --git a/source/shared_lib/include/graphics/gl/text_renderer_gl.h b/source/shared_lib/include/graphics/gl/text_renderer_gl.h new file mode 100644 index 000000000..2dafb1b3d --- /dev/null +++ b/source/shared_lib/include/graphics/gl/text_renderer_gl.h @@ -0,0 +1,58 @@ +// ============================================================== +// This file is part of Glest Shared Library (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 +// ============================================================== + +#ifndef _SHARED_GRAPHICS_GL_TEXTRENDERERGL_H_ +#define _SHARED_GRAPHICS_GL_TEXTRENDERERGL_H_ + +#include "text_renderer.h" + +namespace Shared{ namespace Graphics{ namespace Gl{ + +class Font2DGl; +class Font3DGl; + +// ===================================================== +// class TextRenderer2DGl +// ===================================================== + +class TextRenderer2DGl: public TextRenderer2D{ +private: + const Font2DGl *font; + bool rendering; + +public: + TextRenderer2DGl(); + + virtual void begin(const Font2D *font); + virtual void render(const string &text, int x, int y, bool centered, Vec3f color); + virtual void end(); +}; + +// ===================================================== +// class TextRenderer3DGl +// ===================================================== + +class TextRenderer3DGl: public TextRenderer3D{ +private: + const Font3DGl *font; + bool rendering; + +public: + TextRenderer3DGl(); + + virtual void begin(const Font3D *font); + virtual void render(const string &text, float x, float y, float size, bool centered); + virtual void end(); +}; + +}}}//end namespace + +#endif diff --git a/source/shared_lib/include/graphics/text_renderer.h b/source/shared_lib/include/graphics/text_renderer.h new file mode 100644 index 000000000..8b4c4780f --- /dev/null +++ b/source/shared_lib/include/graphics/text_renderer.h @@ -0,0 +1,52 @@ +// ============================================================== +// This file is part of Glest Shared Library (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 +// ============================================================== + +#ifndef _SHARED_GRAPHICS_TEXTRENDERER_H_ +#define _SHARED_GRAPHICS_TEXTRENDERER_H_ + +#include + +#include "vec.h" +#include "font.h" + +using std::string; + +namespace Shared{ namespace Graphics{ + +// ===================================================== +// class TextRenderer2D +// ===================================================== + +class TextRenderer2D{ +public: + virtual ~TextRenderer2D(){}; + + virtual void begin(const Font2D *font)= 0; + virtual void render(const string &text, int x, int y, bool centered= false,Vec3f color=Vec3f(-1.0))= 0; + virtual void end()= 0; +}; + +// ===================================================== +// class TextRenderer3D +// ===================================================== + +class TextRenderer3D{ +public: + virtual ~TextRenderer3D(){}; + + virtual void begin(const Font3D *font)= 0; + virtual void render(const string &text, float x, float y, float size, bool centered= false)= 0; + virtual void end()= 0; +}; + +}}//end namespace + +#endif diff --git a/source/shared_lib/include/platform/posix/socket.h b/source/shared_lib/include/platform/posix/socket.h index 30dcbbee5..b7b209eb6 100644 --- a/source/shared_lib/include/platform/posix/socket.h +++ b/source/shared_lib/include/platform/posix/socket.h @@ -113,7 +113,7 @@ public: static bool hasDataToRead(std::map &socketTriggeredList); static bool hasDataToRead(PLATFORM_SOCKET socket); bool hasDataToRead(); - void disconnectSocket(); + virtual void disconnectSocket(); PLATFORM_SOCKET getSocketId() const { return sock; } @@ -194,8 +194,15 @@ public: Socket *accept(); void stopBroadCastThread(); + void setBindPort(int port) { boundPort = port; } + int getBindPort() const { return boundPort; } + bool isPortBound() const { return portBound; } + + virtual void disconnectSocket(); + protected: + bool portBound; int boundPort; BroadCastSocketThread *broadCastThread; void startBroadCastThread(); diff --git a/source/shared_lib/include/platform/sdl/window.h b/source/shared_lib/include/platform/sdl/window.h index cd1375a87..1d1a17a48 100644 --- a/source/shared_lib/include/platform/sdl/window.h +++ b/source/shared_lib/include/platform/sdl/window.h @@ -132,6 +132,7 @@ private: protected: int w, h; static bool isActive; + static bool no2DMouseRendering; public: static bool handleEvent(); @@ -170,6 +171,9 @@ public: void destroy(); void minimize(); + static void setUseDefaultCursorOnly(bool value) { no2DMouseRendering = value; } + static bool getUseDefaultCursorOnly() { return no2DMouseRendering; } + protected: virtual void eventCreate(){} virtual void eventMouseDown(int x, int y, MouseButton mouseButton){} diff --git a/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp b/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp new file mode 100644 index 000000000..61b1897ea --- /dev/null +++ b/source/shared_lib/sources/graphics/gl/text_renderer_gl.cpp @@ -0,0 +1,150 @@ +// ============================================================== +// This file is part of Glest Shared Library (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 "text_renderer_gl.h" + +#include "opengl.h" +#include "font_gl.h" +#include "leak_dumper.h" + +namespace Shared{ namespace Graphics{ namespace Gl{ + +// ===================================================== +// class TextRenderer2DGl +// ===================================================== + +TextRenderer2DGl::TextRenderer2DGl(){ + rendering= false; +} + +void TextRenderer2DGl::begin(const Font2D *font){ + assert(!rendering); + rendering= true; + + this->font= static_cast(font); +} + +void TextRenderer2DGl::render(const string &text, int x, int y, bool centered, Vec3f color) { + assert(rendering); + + assertGl(); + + if(color.x >= 0) { + glPushAttrib(GL_CURRENT_BIT); + glColor3fv(color.ptr()); + } + + int line=0; + int size= font->getSize(); + const unsigned char *utext= reinterpret_cast(text.c_str()); + + Vec2f rasterPos; + const FontMetrics *metrics= font->getMetrics(); + if(centered){ + rasterPos.x= x-metrics->getTextWidth(text)/2.f; + rasterPos.y= y+metrics->getHeight()/2.f; + } + else{ + rasterPos= Vec2f(static_cast(x), static_cast(y)); + } + glRasterPos2f(rasterPos.x, rasterPos.y); + + for (int i=0; utext[i]!='\0'; ++i) { + switch(utext[i]){ + case '\t': + rasterPos= Vec2f((rasterPos.x/size+3.f)*size, y-(size+1.f)*line); + glRasterPos2f(rasterPos.x, rasterPos.y); + break; + case '\n': + line++; + rasterPos= Vec2f(static_cast(x), y-(metrics->getHeight()*2.f)*line); + glRasterPos2f(rasterPos.x, rasterPos.y); + break; + default: + glCallList(font->getHandle()+utext[i]); + } + } + + if(color.x >= 0) { + glPopAttrib(); + } + assertGl(); +} + +void TextRenderer2DGl::end(){ + assert(rendering); + rendering= false; +} + +// ===================================================== +// class TextRenderer3DGl +// ===================================================== + +TextRenderer3DGl::TextRenderer3DGl(){ + rendering= false; +} + +void TextRenderer3DGl::begin(const Font3D *font){ + assert(!rendering); + rendering= true; + + this->font= static_cast(font); + + assertGl(); + + //load color + glPushAttrib(GL_TRANSFORM_BIT); + + assertGl(); +} + +void TextRenderer3DGl::render(const string &text, float x, float y, float size, bool centered){ + assert(rendering); + + assertGl(); + + const unsigned char *utext= reinterpret_cast(text.c_str()); + + glMatrixMode(GL_MODELVIEW); + glPushMatrix(); + glPushAttrib(GL_POLYGON_BIT); + float scale= size/10.f; + if(centered){ + const FontMetrics *metrics= font->getMetrics(); + glTranslatef(x-scale*metrics->getTextWidth(text)/2.f, y-scale*metrics->getHeight()/2.f, 0); + } + else{ + glTranslatef(x-scale, y-scale, 0); + } + glScalef(scale, scale, scale); + + for (int i=0; utext[i]!='\0'; ++i) { + glCallList(font->getHandle()+utext[i]); + } + + glPopMatrix(); + glPopAttrib(); + + assertGl(); +} + +void TextRenderer3DGl::end(){ + assert(rendering); + rendering= false; + + assertGl(); + + glPopAttrib(); + + assertGl(); +} + +}}}//end namespace diff --git a/source/shared_lib/sources/platform/common/platform_common.cpp b/source/shared_lib/sources/platform/common/platform_common.cpp index f59568c49..921dbf6b7 100644 --- a/source/shared_lib/sources/platform/common/platform_common.cpp +++ b/source/shared_lib/sources/platform/common/platform_common.cpp @@ -784,7 +784,7 @@ void showCursor(bool b) { } SDL_ShowCursor(b ? SDL_ENABLE : SDL_DISABLE); if(b) { - SDL_WM_GrabInput(SDL_GRAB_OFF); + //SDL_WM_GrabInput(SDL_GRAB_OFF); //SDL_WarpMouse(x,y); } } diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 76dcf374f..6f1a34510 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -1465,6 +1465,7 @@ void BroadCastClientSocketThread::execute() { ServerSocket::ServerSocket() : Socket() { SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); + portBound = false; broadCastThread = NULL; SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__,__LINE__); @@ -1513,9 +1514,19 @@ bool ServerSocket::isBroadCastThreadRunning() { return isThreadRunning; } -void ServerSocket::bind(int port) -{ +void ServerSocket::bind(int port) { + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d port = %d, portBound = %d START\n",__FILE__,__FUNCTION__,__LINE__,port,portBound); + boundPort = port; + + if(isSocketValid() == false) { + sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); + if(isSocketValid() == false) { + throwException("Error creating socket"); + } + setBlock(false); + } + //sockaddr structure sockaddr_in addr; addr.sin_family= AF_INET; @@ -1540,6 +1551,14 @@ void ServerSocket::bind(int port) sprintf(szBuf, "Error binding socket sock = %d, err = %d, error = %s\n",sock,err,getLastSocketErrorFormattedText().c_str()); throw runtime_error(szBuf); } + portBound = true; + + SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d port = %d, portBound = %d END\n",__FILE__,__FUNCTION__,__LINE__,port,portBound); +} + +void ServerSocket::disconnectSocket() { + Socket::disconnectSocket(); + portBound = false; } void ServerSocket::listen(int connectionQueueSize) { @@ -1554,6 +1573,9 @@ void ServerSocket::listen(int connectionQueueSize) { throwException("Error creating socket"); } setBlock(false); + } + + if(portBound == false) { bind(boundPort); } @@ -1579,8 +1601,12 @@ void ServerSocket::listen(int connectionQueueSize) { } } -Socket *ServerSocket::accept() -{ +Socket *ServerSocket::accept() { + + if(isSocketValid() == false) { + throwException("socket is invalid!"); + } + struct sockaddr_in cli_addr; socklen_t clilen = sizeof(cli_addr); char client_host[100]=""; diff --git a/source/shared_lib/sources/platform/sdl/window.cpp b/source/shared_lib/sources/platform/sdl/window.cpp index a7f10e038..73e074a24 100644 --- a/source/shared_lib/sources/platform/sdl/window.cpp +++ b/source/shared_lib/sources/platform/sdl/window.cpp @@ -49,6 +49,7 @@ bool Window::isFullScreen = false; SDL_keysym Window::keystate; bool Window::isActive = false; +bool Window::no2DMouseRendering = false; // ========== PUBLIC ========== @@ -176,8 +177,10 @@ bool Window::handleEvent() { Window::isActive = true; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); - showCursor(!Window::isActive); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); + if(Window::isActive && Window::getUseDefaultCursorOnly() == false) { + showCursor(!Window::isActive); + } } // Check if the program has lost window focus else if (event.active.state == SDL_APPACTIVE) { @@ -188,8 +191,10 @@ bool Window::handleEvent() { Window::isActive = true; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); - showCursor(!Window::isActive); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); + if(Window::isActive && Window::getUseDefaultCursorOnly() == false) { + showCursor(!Window::isActive); + } } // Check if the program has lost window focus else if (event.active.state == SDL_APPMOUSEFOCUS) { @@ -200,8 +205,10 @@ bool Window::handleEvent() { Window::isActive = true; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); - showCursor(!Window::isActive); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive); + if(Window::isActive && Window::getUseDefaultCursorOnly() == false) { + showCursor(!Window::isActive); + } } else { if (event.active.gain == 0) { @@ -211,8 +218,10 @@ bool Window::handleEvent() { Window::isActive = true; } - SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d, event.active.state = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive,event.active.state); - showCursor(!Window::isActive); + SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] Window::isActive = %d, event.active.state = %d\n",__FILE__,__FUNCTION__,__LINE__,Window::isActive,event.active.state); + if(Window::isActive && Window::getUseDefaultCursorOnly() == false) { + showCursor(!Window::isActive); + } } } @@ -413,8 +422,10 @@ void Window::toggleFullscreen() { if(Window::isFullScreen == true) { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] Window::isFullScreen == true [%d]\n",__FILE__,__FUNCTION__,__LINE__,handle); - ShowWindow(handle, SW_MAXIMIZE); - showCursor(false); + ShowWindow(handle, SW_MAXIMIZE); + if(Window::isActive && Window::getUseDefaultCursorOnly() == false) { + showCursor(false); + } } else { SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s %d] Window::isFullScreen == false [%d]\n",__FILE__,__FUNCTION__,__LINE__,handle);