script.cpp

Go to the documentation of this file.
00001 
00002 //            Copyright (C) 2004-2007 by The Allacrost Project
00003 //                         All Rights Reserved
00004 //
00005 // This code is licensed under the GNU GPL version 2. It is free software
00006 // and you may modify it and/or redistribute it under the terms of this license.
00007 // See http://www.gnu.org/copyleft/gpl.html for details.
00009 
00018 #include <iostream>
00019 #include <stdarg.h>
00020 
00021 #include "script.h"
00022 
00023 using namespace std;
00024 using namespace luabind;
00025 
00026 using namespace hoa_utils;
00027 using namespace hoa_script::private_script;
00028 
00029 template<> hoa_script::GameScript* Singleton<hoa_script::GameScript>::_singleton_reference = NULL;
00030 
00031 namespace hoa_script {
00032 
00033 GameScript* ScriptManager = NULL;
00034 bool SCRIPT_DEBUG = false;
00035 
00036 //-----------------------------------------------------------------------------
00037 // GameScript Class Functions
00038 //-----------------------------------------------------------------------------
00039 
00040 GameScript::GameScript() {
00041   if (SCRIPT_DEBUG) cout << "SCRIPT: GameScript constructor invoked." << endl;
00042 
00043   // Initialize Lua and LuaBind
00044   _global_state = lua_open();
00045   luabind::open(_global_state);
00046   lua_baselibopen(_global_state);
00047   lua_iolibopen(_global_state);
00048   lua_strlibopen(_global_state);
00049   lua_mathlibopen(_global_state);
00050 }
00051 
00052 
00053 
00054 GameScript::~GameScript() {
00055   if (SCRIPT_DEBUG) cout << "SCRIPT: GameScript destructor invoked." << endl;
00056 
00057   _open_files.clear();
00058   lua_close(_global_state);
00059   _global_state = NULL;
00060 }
00061 
00062 
00063 
00064 bool GameScript::SingletonInitialize() {
00065   // TODO: Open the user setting's file and apply those settings
00066   return true;
00067 }
00068 
00069 
00070 
00071 void GameScript::HandleLuaError(luabind::error& err) {
00072   lua_State *state = err.state();
00073   cerr << "SCRIPT ERROR: a run-time Lua error has occured with the following error message:\n  " << endl;
00074   cerr << lua_tostring(state, lua_gettop(state)) << endl;
00075   lua_pop(state, 1);
00076 }
00077 
00078 
00079 
00080 void GameScript::HandleCastError(luabind::cast_failed& err) {
00081   cerr << "SCRIPT ERROR: the return value of a Lua function call could not be successfully converted "
00082     << "to the specified C++ type: " << err.what() << endl;
00083 }
00084 
00085 
00086 
00087 void GameScript::_AddOpenFile(ScriptDescriptor* sd) {
00088   // NOTE: Function assumes that the file is not already open
00089   _open_files.insert(make_pair(sd->_filename, sd));
00090 }
00091 
00092 
00093 
00094 void GameScript::_RemoveOpenFile(ScriptDescriptor* sd) {
00095   // NOTE: Function assumes that the ScriptDescriptor file is already open
00096   _open_files.erase(sd->_filename);
00097 }
00098 
00099 
00100 
00101 bool GameScript::IsFileOpen(const std::string& filename) {
00102   return false; // TEMP: working on resolving the issue with files being opened multiple times
00103 
00104   if (_open_files.find(filename) != _open_files.end()) {
00105     return true;
00106   }
00107   return false;
00108 }
00109 
00110 } // namespace hoa_script

Generated on Fri Jul 6 23:11:22 2007 for Hero of Allacrost by  doxygen 1.5.1