- added more LUA support and fixed parameter passing of Vec2i types with streflop

New LUA methods:
DisplayFormattedText
getAiEnabled
getHungerEnabled
startPerformanceTimer
endPerformanceTimer
getPerformanceTimerResults
This commit is contained in:
Mark Vejvoda 2010-08-28 20:52:25 +00:00
parent dc5bf0f850
commit 4d372c12bc
6 changed files with 374 additions and 5 deletions

View File

@ -670,6 +670,21 @@ void Game::tick(){
avgRenderFps = (avgRenderFps + renderFps) / 2;
}
if(captureAvgTestStatus == true) {
if(updateFpsAvgTest == -1) {
updateFpsAvgTest = updateFps;
}
else {
updateFpsAvgTest = (updateFpsAvgTest + updateFps) / 2;
}
if(renderFpsAvgTest == -1) {
renderFpsAvgTest = renderFps;
}
else {
renderFpsAvgTest = (renderFpsAvgTest + renderFps) / 2;
}
}
lastUpdateFps= updateFps;
lastRenderFps= renderFps;
updateFps= 0;
@ -1652,4 +1667,19 @@ void Game::showErrorMessageBox(const string &text, const string &header, bool to
}
}
void Game::startPerformanceTimer() {
captureAvgTestStatus = true;
updateFpsAvgTest = -1;
renderFpsAvgTest = -1;
}
void Game::endPerformanceTimer() {
captureAvgTestStatus = false;
}
Vec2i Game::getPerformanceTimerResults() {
Vec2i results(this->updateFpsAvgTest,this->renderFpsAvgTest);
return results;
}
}}//end namespace

View File

@ -91,6 +91,10 @@ private:
int original_updateFps;
int original_cameraFps;
bool captureAvgTestStatus;
int updateFpsAvgTest;
int renderFpsAvgTest;
public:
Game(Program *program, const GameSettings *gameSettings);
~Game();
@ -135,6 +139,10 @@ public:
Stats quitGame();
static void exitGameState(Program *program, Stats &endStats);
void startPerformanceTimer();
void endPerformanceTimer();
Vec2i getPerformanceTimerResults();
private:
//render
void render3d();

View File

@ -14,6 +14,7 @@
#include "world.h"
#include "lang.h"
#include "game_camera.h"
#include "game.h"
#include "leak_dumper.h"
@ -32,12 +33,12 @@ class ScriptManager_STREFLOP_Wrapper {
public:
ScriptManager_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Simple>();
//streflop_init<streflop::Simple>();
#endif
}
~ScriptManager_STREFLOP_Wrapper() {
#ifdef USE_STREFLOP
streflop_init<streflop::Double>();
//streflop_init<streflop::Double>();
#endif
}
};
@ -71,6 +72,7 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){
//register functions
luaScript.registerFunction(showMessage, "showMessage");
luaScript.registerFunction(setDisplayText, "setDisplayText");
luaScript.registerFunction(DisplayFormattedText, "DisplayFormattedText");
luaScript.registerFunction(clearDisplayText, "clearDisplayText");
luaScript.registerFunction(setCameraPosition, "setCameraPosition");
luaScript.registerFunction(createUnit, "createUnit");
@ -81,11 +83,17 @@ void ScriptManager::init(World* world, GameCamera *gameCamera){
luaScript.registerFunction(giveUpgradeCommand, "giveUpgradeCommand");
luaScript.registerFunction(disableAi, "disableAi");
luaScript.registerFunction(enableAi, "enableAi");
luaScript.registerFunction(getAiEnabled, "getAiEnabled");
luaScript.registerFunction(disableHunger, "disableHunger");
luaScript.registerFunction(enableHunger, "enableHunger");
luaScript.registerFunction(getHungerEnabled, "getHungerEnabled");
luaScript.registerFunction(setPlayerAsWinner, "setPlayerAsWinner");
luaScript.registerFunction(endGame, "endGame");
luaScript.registerFunction(startPerformanceTimer, "startPerformanceTimer");
luaScript.registerFunction(endPerformanceTimer, "endPerformanceTimer");
luaScript.registerFunction(getPerformanceTimerResults, "getPerformanceTimerResults");
luaScript.registerFunction(getStartLocation, "startLocation");
luaScript.registerFunction(getUnitPosition, "unitPosition");
luaScript.registerFunction(getUnitFaction, "unitFaction");
@ -195,6 +203,22 @@ void ScriptManager::setDisplayText(const string &text){
displayText= wrapString(Lang::getInstance().getScenarioString(text), displayTextWrapCount);
}
void ScriptManager::DisplayFormattedText(const char *fmt, ...) {
ScriptManager_STREFLOP_Wrapper streflopWrapper;
va_list argList;
va_start(argList, fmt);
const int max_debug_buffer_size = 8096;
char szBuf[max_debug_buffer_size]="";
vsnprintf(szBuf,max_debug_buffer_size-1,fmt, argList);
//displayText= wrapString(Lang::getInstance().getScenarioString(text), displayTextWrapCount);
displayText=szBuf;
va_end(argList);
}
void ScriptManager::setCameraPosition(const Vec2i &pos){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
gameCamera->centerXZ(pos.x, pos.y);
@ -245,6 +269,13 @@ void ScriptManager::enableAi(int factionIndex){
}
}
bool ScriptManager::getAiEnabled(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
return playerModifiers[factionIndex].getAiEnabled();
}
}
void ScriptManager::disableHunger(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
@ -259,6 +290,13 @@ void ScriptManager::enableHunger(int factionIndex){
}
}
bool ScriptManager::getHungerEnabled(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
return playerModifiers[factionIndex].getHungerEnabled();
}
}
void ScriptManager::setPlayerAsWinner(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(factionIndex<GameConstants::maxPlayers){
@ -271,6 +309,32 @@ void ScriptManager::endGame(){
gameOver= true;
}
void ScriptManager::startPerformanceTimer() {
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(world->getGame() == NULL) {
throw runtime_error("world->getGame() == NULL");
}
world->getGame()->startPerformanceTimer();
}
void ScriptManager::endPerformanceTimer() {
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(world->getGame() == NULL) {
throw runtime_error("world->getGame() == NULL");
}
world->getGame()->endPerformanceTimer();
}
Vec2i ScriptManager::getPerformanceTimerResults() {
ScriptManager_STREFLOP_Wrapper streflopWrapper;
if(world->getGame() == NULL) {
throw runtime_error("world->getGame() == NULL");
}
return world->getGame()->getPerformanceTimerResults();
}
Vec2i ScriptManager::getStartLocation(int factionIndex){
ScriptManager_STREFLOP_Wrapper streflopWrapper;
return world->getStartLocation(factionIndex);
@ -411,6 +475,13 @@ int ScriptManager::enableAi(LuaHandle* luaHandle){
return luaArguments.getReturnCount();
}
int ScriptManager::getAiEnabled(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
bool result = thisScriptManager->getAiEnabled(luaArguments.getInt(-1));
luaArguments.returnInt(result);
return luaArguments.getReturnCount();
}
int ScriptManager::disableHunger(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
thisScriptManager->disableHunger(luaArguments.getInt(-1));
@ -423,6 +494,13 @@ int ScriptManager::enableHunger(LuaHandle* luaHandle){
return luaArguments.getReturnCount();
}
int ScriptManager::getHungerEnabled(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
bool result = thisScriptManager->getHungerEnabled(luaArguments.getInt(-1));
luaArguments.returnInt(result);
return luaArguments.getReturnCount();
}
int ScriptManager::setPlayerAsWinner(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
thisScriptManager->setPlayerAsWinner(luaArguments.getInt(-1));
@ -435,6 +513,25 @@ int ScriptManager::endGame(LuaHandle* luaHandle){
return luaArguments.getReturnCount();
}
int ScriptManager::startPerformanceTimer(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
thisScriptManager->startPerformanceTimer();
return luaArguments.getReturnCount();
}
int ScriptManager::endPerformanceTimer(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
thisScriptManager->endPerformanceTimer();
return luaArguments.getReturnCount();
}
int ScriptManager::getPerformanceTimerResults(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
Vec2i results= thisScriptManager->getPerformanceTimerResults();
luaArguments.returnVec2i(results);
return luaArguments.getReturnCount();
}
int ScriptManager::getStartLocation(LuaHandle* luaHandle){
LuaArguments luaArguments(luaHandle);
Vec2i pos= thisScriptManager->getStartLocation(luaArguments.getInt(-1));
@ -498,4 +595,112 @@ int ScriptManager::getUnitCountOfType(LuaHandle* luaHandle){
return luaArguments.getReturnCount();
}
int ScriptManager::DisplayFormattedText(LuaHandle* luaHandle) {
//const char *ret;
//lua_lock(luaHandle);
//luaC_checkGC(luaHandle);
const int max_args_allowed = 8;
int args = lua_gettop(luaHandle);
if(lua_checkstack(luaHandle, args+1)) {
LuaArguments luaArguments(luaHandle);
string fmt = luaArguments.getString(-args);
//va_list argList;
//va_start(argList, fmt.c_str() );
printf("\nDisplayFormattedText args = %d!\n",args);
if(args == 1) {
thisScriptManager->DisplayFormattedText(fmt.c_str());
}
else if(args == 2) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1));
}
else if(args == 3) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2));
}
else if(args == 4) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2),
luaArguments.getGenericData(-args+3));
}
else if(args == 5) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2),
luaArguments.getGenericData(-args+3),
luaArguments.getGenericData(-args+4));
}
else if(args == 6) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2),
luaArguments.getGenericData(-args+3),
luaArguments.getGenericData(-args+4),
luaArguments.getGenericData(-args+5));
}
else if(args == 7) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2),
luaArguments.getGenericData(-args+3),
luaArguments.getGenericData(-args+4),
luaArguments.getGenericData(-args+5),
luaArguments.getGenericData(-args+6));
}
else if(args == max_args_allowed) {
thisScriptManager->DisplayFormattedText(fmt.c_str(),
luaArguments.getGenericData(-args+1),
luaArguments.getGenericData(-args+2),
luaArguments.getGenericData(-args+3),
luaArguments.getGenericData(-args+4),
luaArguments.getGenericData(-args+5),
luaArguments.getGenericData(-args+6),
luaArguments.getGenericData(-args+7));
}
else {
char szBuf[1024]="";
sprintf(szBuf,"Invalid parameter count in method [%s] args = %d [argument count must be between 1 and %d]",__FUNCTION__,args,max_args_allowed);
throw runtime_error(szBuf);
}
//va_end(argList);
}
//lua_unlock(luaHandle);
return 1;
/*
int args=lua_gettop(luaHandle);
if(lua_checkstack(luaHandle, args+1))
{
va_list argList;
int i;
//lua_getfield(luaHandle, LUA_GLOBALSINDEX, "print");
for(i = 1;i <= args; i++) {
lua_pushvalue(luaHandle, i);
}
lua_call(luaHandle, args, 0);
}
else
{
return luaL_error(luaHandle, "cannot grow stack");
}
*/
/*
luax_getfunction(L, mod, fn);
for (int i = 0; i < n; i++) {
lua_pushvalue(L, idxs[i]); // The arguments.
}
lua_call(L, n, 1); // Call the function, n args, one return value.
lua_replace(L, idxs[0]); // Replace the initial argument with the new object.
return 0;
*/
}
}}//end namespace

View File

@ -135,6 +135,7 @@ private:
void showMessage(const string &text, const string &header);
void clearDisplayText();
void setDisplayText(const string &text);
void DisplayFormattedText(const char *fmt,...);
void setCameraPosition(const Vec2i &pos);
void createUnit(const string &unitName, int factionIndex, Vec2i pos);
void giveResource(const string &resourceName, int factionIndex, int amount);
@ -146,9 +147,17 @@ private:
void enableAi(int factionIndex);
void disableHunger(int factionIndex);
void enableHunger(int factionIndex);
bool getAiEnabled(int factionIndex);
bool getHungerEnabled(int factionIndex);
void setPlayerAsWinner(int factionIndex);
void endGame();
void startPerformanceTimer();
void endPerformanceTimer();
Vec2i getPerformanceTimerResults();
//wrappers, queries
Vec2i getStartLocation(int factionIndex);
Vec2i getUnitPosition(int unitId);
@ -164,6 +173,7 @@ private:
//callbacks, commands
static int showMessage(LuaHandle* luaHandle);
static int setDisplayText(LuaHandle* luaHandle);
static int DisplayFormattedText(LuaHandle* luaHandle);
static int clearDisplayText(LuaHandle* luaHandle);
static int setCameraPosition(LuaHandle* luaHandle);
static int createUnit(LuaHandle* luaHandle);
@ -176,9 +186,17 @@ private:
static int enableAi(LuaHandle* luaHandle);
static int disableHunger(LuaHandle* luaHandle);
static int enableHunger(LuaHandle* luaHandle);
static int getAiEnabled(LuaHandle* luaHandle);
static int getHungerEnabled(LuaHandle* luaHandle);
static int setPlayerAsWinner(LuaHandle* luaHandle);
static int endGame(LuaHandle* luaHandle);
static int startPerformanceTimer(LuaHandle* luaHandle);
static int endPerformanceTimer(LuaHandle* luaHandle);
static int getPerformanceTimerResults(LuaHandle* luaHandle);
//callbacks, queries
static int getStartLocation(LuaHandle* luaHandle);
static int getUnitPosition(LuaHandle* luaHandle);

View File

@ -0,0 +1,82 @@
// ==============================================================
// 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_LUA_LUASCRIPT_H_
#define _SHARED_LUA_LUASCRIPT_H_
#include <string>
#include <lua.hpp>
#include <vec.h>
using std::string;
using Shared::Graphics::Vec2i;
namespace Shared{ namespace Lua{
typedef lua_State LuaHandle;
typedef int(*LuaFunction)(LuaHandle*);
// =====================================================
// class LuaScript
// =====================================================
class LuaScript{
private:
LuaHandle *luaState;
int argumentCount;
public:
LuaScript();
~LuaScript();
void loadCode(const string &code, const string &name);
void beginCall(const string& functionName);
void endCall();
void registerFunction(LuaFunction luaFunction, const string &functionName);
private:
string errorToString(int errorCode);
};
// =====================================================
// class LuaArguments
// =====================================================
class LuaArguments{
private:
lua_State *luaState;
int returnCount;
public:
LuaArguments(lua_State *luaState);
int getInt(int argumentIndex) const;
string getString(int argumentIndex) const;
void * getGenericData(int argumentIndex) const;
Vec2i getVec2i(int argumentIndex) const;
int getReturnCount() const {return returnCount;}
void returnInt(int value);
void returnString(const string &value);
void returnVec2i(const Vec2i &value);
private:
void throwLuaError(const string &message) const;
};
}}//end namespace
#endif

View File

@ -14,7 +14,7 @@
#include <stdexcept>
#include "conversion.h"
#include "util.h"
#include "leak_dumper.h"
using namespace std;
@ -83,6 +83,8 @@ void LuaScript::loadCode(const string &code, const string &name){
void LuaScript::beginCall(const string& functionName){
Lua_STREFLOP_Wrapper streflopWrapper;
SystemFlags::OutputDebug(SystemFlags::debugPerformance,"In [%s::%s Line: %d] functionName [%s]\n",__FILE__,__FUNCTION__,__LINE__,functionName.c_str());
lua_getglobal(luaState, functionName.c_str());
argumentCount= 0;
}
@ -93,7 +95,7 @@ void LuaScript::endCall(){
lua_pcall(luaState, argumentCount, 0, 0);
}
void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName){
void LuaScript::registerFunction(LuaFunction luaFunction, const string &functionName) {
Lua_STREFLOP_Wrapper streflopWrapper;
lua_pushcfunction(luaState, luaFunction);
@ -156,6 +158,30 @@ string LuaArguments::getString(int argumentIndex) const{
return luaL_checkstring(luaState, argumentIndex);
}
void * LuaArguments::getGenericData(int argumentIndex) const{
Lua_STREFLOP_Wrapper streflopWrapper;
if(lua_isstring(luaState, argumentIndex)) {
const char *result = luaL_checkstring(luaState, argumentIndex);
printf("\nGENERIC param %d is a string, %s!\n",argumentIndex,result);
return (void *)result;
}
//else if(lua_isnumber(luaState, argumentIndex)) {
// double result = luaL_checknumber(luaState, argumentIndex);
// printf("\nGENERIC param %d is a double, %f!\n",argumentIndex,result);
// return (void *)result;
//}
else if(lua_isnumber(luaState, argumentIndex)) {
int result = luaL_checkinteger(luaState, argumentIndex);
printf("\nGENERIC param %d is an int, %d!\n",argumentIndex,result);
return (void *)result;
}
else {
printf("\nGENERIC param %d is a NULL!\n",argumentIndex);
return NULL;
}
}
Vec2i LuaArguments::getVec2i(int argumentIndex) const{
Lua_STREFLOP_Wrapper streflopWrapper;
@ -195,7 +221,7 @@ void LuaArguments::returnString(const string &value){
}
void LuaArguments::returnVec2i(const Vec2i &value){
Lua_STREFLOP_Wrapper streflopWrapper;
//Lua_STREFLOP_Wrapper streflopWrapper;
++returnCount;