some changes to how we use streflop:

- Removed all unneeded includes of cmath
- added proper wrapper classes for lua calls to toggle streflop back and forth
- some code cleanup
This commit is contained in:
Mark Vejvoda 2010-05-28 05:31:17 +00:00
parent f0da609e7f
commit 908b155d62
10 changed files with 119 additions and 151 deletions

View File

@ -23,6 +23,25 @@ using namespace Shared::Util;
namespace Glest{ namespace Game{
//
// This class wraps streflop for the Lua ScriptMAnager. We need to toggle the data type
// for streflop to use when calling into glest from LUA as streflop may corrupt some
// numeric values passed from Lua otherwise
//
class ScriptManager_STREFLOP_Wrapper {
public:
ScriptManager_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
~ScriptManager_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
}
};
// =====================================================
// class PlayerModifiers
// =====================================================
@ -131,6 +150,7 @@ void ScriptManager::onUnitDied(const Unit* unit){
// ========================== lua wrappers ===============================================
string ScriptManager::wrapString(const string &str, int wrapCount){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
string returnString;
@ -151,6 +171,8 @@ string ScriptManager::wrapString(const string &str, int wrapCount){
}
void ScriptManager::showMessage(const string &text, const string &header){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
Lang &lang= Lang::getInstance();
messageQueue.push(ScriptManagerMessage(text, header));
@ -164,88 +186,108 @@ void ScriptManager::clearDisplayText(){
}
void ScriptManager::setDisplayText(const string &text){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
displayText= wrapString(Lang::getInstance().getScenarioString(text), displayTextWrapCount);
}
void ScriptManager::setCameraPosition(const Vec2i &pos){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
gameCamera->centerXZ(pos.x, pos.y);
}
void ScriptManager::createUnit(const string &unitName, int factionIndex, Vec2i pos){
SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unitName.c_str(),factionIndex);
//SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] unit [%s] factionIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,unitName.c_str(),factionIndex);
ScriptManager_STREFLOP_Wrapper streflopWrapper;
world->createUnit(unitName, factionIndex, pos);
}
void ScriptManager::giveResource(const string &resourceName, int factionIndex, int amount){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
world->giveResource(resourceName, factionIndex, amount);
}
void ScriptManager::givePositionCommand(int unitId, const string &commandName, const Vec2i &pos){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
world->givePositionCommand(unitId, commandName, pos);
}
void ScriptManager::giveProductionCommand(int unitId, const string &producedName){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
world->giveProductionCommand(unitId, producedName);
}
void ScriptManager::giveUpgradeCommand(int unitId, const string &producedName){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
world->giveUpgradeCommand(unitId, producedName);
}
void ScriptManager::disableAi(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
playerModifiers[factionIndex].disableAi();
}
}
void ScriptManager::setPlayerAsWinner(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
playerModifiers[factionIndex].setAsWinner();
}
}
void ScriptManager::endGame(){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
gameOver= true;
}
Vec2i ScriptManager::getStartLocation(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getStartLocation(factionIndex);
}
Vec2i ScriptManager::getUnitPosition(int unitId){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getUnitPosition(unitId);
}
int ScriptManager::getUnitFaction(int unitId){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getUnitFactionIndex(unitId);
}
int ScriptManager::getResourceAmount(const string &resourceName, int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getResourceAmount(resourceName, factionIndex);
}
const string &ScriptManager::getLastCreatedUnitName(){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return lastCreatedUnitName;
}
int ScriptManager::getLastCreatedUnitId(){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return lastCreatedUnitId;
}
const string &ScriptManager::getLastDeadUnitName(){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return lastDeadUnitName;
}
int ScriptManager::getLastDeadUnitId(){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return lastDeadUnitId;
}
int ScriptManager::getUnitCount(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getUnitCount(factionIndex);
}
int ScriptManager::getUnitCountOfType(int factionIndex, const string &typeName){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getUnitCountOfType(factionIndex, typeName);
}

View File

@ -24,10 +24,7 @@
#include "opengl.h"
#include "faction.h"
#include "factory_repository.h"
#ifndef WIN32
#include <cmath>
#endif
#include <cstdlib>
#include "leak_dumper.h"
@ -541,8 +538,8 @@ void Renderer::renderMouse2d(int x, int y, int anim, float fade){
anim= anim*2-maxMouse2dAnim;
color2= (abs(anim*fadeFactor)/static_cast<float>(maxMouse2dAnim))/2.f+0.4f;
color1= (abs(anim*fadeFactor)/static_cast<float>(maxMouse2dAnim))/2.f+0.8f;
color2= (abs(anim*(int)fadeFactor)/static_cast<float>(maxMouse2dAnim))/2.f+0.4f;
color1= (abs(anim*(int)fadeFactor)/static_cast<float>(maxMouse2dAnim))/2.f+0.8f;
glPushAttrib(GL_CURRENT_BIT | GL_COLOR_BUFFER_BIT | GL_LINE_BIT);
glEnable(GL_BLEND);

View File

@ -121,7 +121,6 @@ Program::Program() {
programState= NULL;
singleton = this;
soundThreadManager = NULL;
loopThreadManager = NULL;
}
void Program::initNormal(WindowGl *window){
@ -167,10 +166,6 @@ Program::~Program(){
BaseThread::shutdownAndWait(soundThreadManager);
delete soundThreadManager;
soundThreadManager = NULL;
BaseThread::shutdownAndWait(loopThreadManager);
delete loopThreadManager;
loopThreadManager = NULL;
}
void Program::keyDown(char key){
@ -191,12 +186,7 @@ void Program::simpleTask() {
}
void Program::loop() {
if(loopThreadManager == NULL) {
loopWorker();
}
else {
loopThreadManager->setTaskSignalled(true);
}
loopWorker();
}
void Program::loopWorker() {
@ -407,11 +397,6 @@ void Program::init(WindowGl *window, bool initSound, bool toggleFullScreen){
}
}
BaseThread::shutdownAndWait(loopThreadManager);
delete loopThreadManager;
//loopThreadManager = new SimpleTaskThread(this,0,5,true);
//loopThreadManager->start();
NetworkInterface::setAllowGameDataSynchCheck(Config::getInstance().getBool("AllowGameDataSynchCheck","false"));
NetworkInterface::setAllowDownloadDataSynch(Config::getInstance().getBool("AllowDownloadDataSynch","false"));

View File

@ -110,7 +110,6 @@ private:
WindowGl *window;
static Program *singleton;
SimpleTaskThread *loopThreadManager;
public:
Program();

View File

@ -121,17 +121,18 @@ GameNetworkInterface::GameNetworkInterface(){
}
void GameNetworkInterface::requestCommand(const NetworkCommand *networkCommand, bool insertAtStart) {
Mutex *mutex = getServerSynchAccessor();
assert(networkCommand != NULL);
//Mutex *mutex = getServerSynchAccessor();
if(insertAtStart == false) {
if(mutex != NULL) mutex->p();
//if(mutex != NULL) mutex->p();
requestedCommands.push_back(*networkCommand);
if(mutex != NULL) mutex->v();
//if(mutex != NULL) mutex->v();
}
else {
if(mutex != NULL) mutex->p();
//if(mutex != NULL) mutex->p();
requestedCommands.insert(requestedCommands.begin(),*networkCommand);
if(mutex != NULL) mutex->v();
//if(mutex != NULL) mutex->v();
}
}

View File

@ -41,7 +41,6 @@ ServerInterface::ServerInterface(){
for(int i= 0; i<GameConstants::maxPlayers; ++i){
slots[i]= NULL;
switchSetupRequests[i]= NULL;
//slotThreads[i] = NULL;
}
serverSocket.setBlock(false);
serverSocket.bind(Config::getInstance().getInt("ServerPort",intToStr(GameConstants::serverPort).c_str()));
@ -51,10 +50,6 @@ ServerInterface::~ServerInterface(){
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] START\n",__FILE__,__FUNCTION__);
for(int i= 0; i<GameConstants::maxPlayers; ++i){
//BaseThread::shutdownAndWait(slotThreads[i]);
//delete slotThreads[i];
//slotThreads[i] = NULL;
delete slots[i];
slots[i]=NULL;
delete switchSetupRequests[i];
@ -76,9 +71,6 @@ void ServerInterface::addSlot(int playerIndex){
slots[playerIndex]= new ConnectionSlot(this, playerIndex);
updateListen();
//slotThreads[playerIndex] = new ConnectionSlotThread(this);
//slotThreads[playerIndex]->start();
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] END\n",__FILE__,__FUNCTION__);
}
@ -108,12 +100,6 @@ bool ServerInterface::switchSlot(int fromPlayerIndex,int toPlayerIndex){
}
void ServerInterface::removeSlot(int playerIndex) {
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex);
//BaseThread::shutdownAndWait(slotThreads[playerIndex]);
//delete slotThreads[playerIndex];
//slotThreads[playerIndex] = NULL;
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d playerIndex = %d\n",__FILE__,__FUNCTION__,__LINE__,playerIndex);
delete slots[playerIndex];
@ -650,7 +636,7 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){
SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s] Line: %d\n",__FILE__,__FUNCTION__,__LINE__);
serverSynchAccessor.p();
//serverSynchAccessor.p();
for(int i= 0; i<GameConstants::maxPlayers; ++i)
{
@ -667,7 +653,7 @@ bool ServerInterface::launchGame(const GameSettings* gameSettings){
}
}
serverSynchAccessor.v();
//serverSynchAccessor.v();
if(bOkToStart == true)
{
@ -749,37 +735,35 @@ void ServerInterface::updateListen() {
return;
}
serverSynchAccessor.p();
int openSlotCount= 0;
for(int i= 0; i<GameConstants::maxPlayers; ++i)
{
if(slots[i] != NULL && slots[i]->isConnected() == false)
{
for(int i= 0; i<GameConstants::maxPlayers; ++i) {
serverSynchAccessor.p();
bool isSlotOpen = (slots[i] != NULL && slots[i]->isConnected() == false);
serverSynchAccessor.v();
if(isSlotOpen == true) {
++openSlotCount;
}
}
serverSynchAccessor.p();
serverSocket.listen(openSlotCount);
serverSynchAccessor.v();
}
int ServerInterface::getOpenSlotCount() {
int openSlotCount= 0;
serverSynchAccessor.p();
for(int i= 0; i<GameConstants::maxPlayers; ++i) {
serverSynchAccessor.p();
bool isSlotOpen = (slots[i] != NULL && slots[i]->isConnected() == false);
serverSynchAccessor.v();
for(int i= 0; i<GameConstants::maxPlayers; ++i)
{
if(slots[i] != NULL && slots[i]->isConnected() == false)
{
if(isSlotOpen == true) {
++openSlotCount;
}
}
serverSynchAccessor.v();
return openSlotCount;
}

View File

@ -23,13 +23,7 @@
#include "skill_type.h"
#include "core_data.h"
#include "renderer.h"
#include "game.h"
#ifndef WIN32
#include <cmath>
#endif
#include "socket.h"
#include "leak_dumper.h"

View File

@ -12,8 +12,6 @@
#ifndef _SHARED_GRAPHICS_MATHUTIL_H_
#define _SHARED_GRAPHICS_MATHUTIL_H_
//#include <cmath>
#include "vec.h"
namespace Shared{ namespace Graphics{

View File

@ -12,8 +12,6 @@
#ifndef _SHARED_GRAPHICS_MATRIX_H_
#define _SHARED_GRAPHICS_MATRIX_H_
//#include <cmath>
#include "vec.h"
namespace Shared{ namespace Graphics{

View File

@ -22,11 +22,32 @@ using namespace Shared::Util;
namespace Shared{ namespace Lua{
//
// This class wraps streflop for LuaScript. We need to toggle the data type
// for streflop to use when calling into LUA as streflop may corrupt some
// numeric values passed from Lua otherwise
//
class Lua_STREFLOP_Wrapper {
public:
Lua_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
}
~Lua_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
};
// =====================================================
// class LuaScript
// =====================================================
LuaScript::LuaScript(){
LuaScript::LuaScript() {
Lua_STREFLOP_Wrapper streflopWrapper;
luaState= luaL_newstate();
luaL_openlibs(luaState);
@ -38,73 +59,51 @@ LuaScript::LuaScript(){
argumentCount= -1;
}
LuaScript::~LuaScript(){
LuaScript::~LuaScript() {
Lua_STREFLOP_Wrapper streflopWrapper;
lua_close(luaState);
}
void LuaScript::loadCode(const string &code, const string &name){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
int errorCode= luaL_loadbuffer(luaState, code.c_str(), code.size(), name.c_str());
if(errorCode!=0){
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
throw runtime_error("Error loading lua code: " + errorToString(errorCode));
}
//run code
errorCode= lua_pcall(luaState, 0, 0, 0)!=0;
if(errorCode!=0){
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
throw runtime_error("Error initializing lua: " + errorToString(errorCode));
}
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
void LuaScript::beginCall(const string& functionName){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
lua_getglobal(luaState, functionName.c_str());
argumentCount= 0;
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
void LuaScript::endCall(){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
lua_pcall(luaState, argumentCount, 0, 0);
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
lua_pushcfunction(luaState, luaFunction);
Lua_STREFLOP_Wrapper streflopWrapper;
lua_pushcfunction(luaState, luaFunction);
lua_setglobal(luaState, functionName.c_str());
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
string LuaScript::errorToString(int errorCode){
string LuaScript::errorToString(int errorCode) {
Lua_STREFLOP_Wrapper streflopWrapper;
string error;
switch(errorCode){
case LUA_ERRSYNTAX:
error+= "Syntax error";
@ -132,34 +131,25 @@ string LuaScript::errorToString(int errorCode){
// =====================================================
LuaArguments::LuaArguments(lua_State *luaState){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
this->luaState= luaState;
returnCount= 0;
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
int LuaArguments::getInt(int argumentIndex) const{
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
if(!lua_isnumber(luaState, argumentIndex)){
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
if(!lua_isnumber(luaState, argumentIndex)) {
throwLuaError("Can not get int from Lua state");
}
int result = luaL_checkint(luaState, argumentIndex);
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
return result;
}
string LuaArguments::getString(int argumentIndex) const{
Lua_STREFLOP_Wrapper streflopWrapper;
if(!lua_isstring(luaState, argumentIndex)){
throwLuaError("Can not get string from Lua state");
}
@ -167,22 +157,15 @@ string LuaArguments::getString(int argumentIndex) const{
}
Vec2i LuaArguments::getVec2i(int argumentIndex) const{
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
Vec2i v;
if(!lua_istable(luaState, argumentIndex)){
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
throwLuaError("Can not get vec2i from Lua state, value on the stack is not a table");
}
if(luaL_getn(luaState, argumentIndex)!=2){
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
throwLuaError("Can not get vec2i from Lua state, array size not 2");
}
@ -194,32 +177,26 @@ Vec2i LuaArguments::getVec2i(int argumentIndex) const{
v.y= luaL_checkint(luaState, argumentIndex);
lua_pop(luaState, 1);
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
return v;
}
void LuaArguments::returnInt(int value){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
++returnCount;
lua_pushinteger(luaState, value);
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
void LuaArguments::returnString(const string &value){
Lua_STREFLOP_Wrapper streflopWrapper;
++returnCount;
lua_pushstring(luaState, value.c_str());
}
void LuaArguments::returnVec2i(const Vec2i &value){
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
++returnCount;
lua_newtable(luaState);
@ -229,15 +206,11 @@ void LuaArguments::returnVec2i(const Vec2i &value){
lua_pushnumber(luaState, value.y);
lua_rawseti(luaState, -2, 2);
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
}
void LuaArguments::throwLuaError(const string &message) const{
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
#endif
Lua_STREFLOP_Wrapper streflopWrapper;
string stackString;
int stackSize = lua_gettop(luaState);
@ -260,9 +233,6 @@ void LuaArguments::throwLuaError(const string &message) const{
stackString+= "\n";
}
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
#endif
throw runtime_error("Lua error: " + message + "\n\nLua Stack:\n" + stackString);
}