Changeset 918 – HoverRace

Changeset 918

Show
Ignore:
Timestamp:
03/14/10 03:49:25 (5 months ago)
Author:
zoogie
Message:

Now using Luabind!

  • The function "on_init" in the system environment is now part of the "Game" object (C++ peer: GamePeer), an instance of which is added to the environment as "game". So, instead of "on_init(...)" use "game:on_init(...)".
  • The function "get_on_init" is not available yet.
  • When executing callbacks we now use Script::Core::CallAndPrint() and handle exceptions to avoid a panic.
  • There is a memory leak somewhere I'm tracking down. :)
Location:
trunk
Files:
2 added
6 modified

Legend:

Unmodified
Added
Removed
  • trunk/client/client.vcproj

    r897 r918  
    6969                Name="VCLinkerTool" 
    7070                AdditionalOptions="/MACHINE:I386" 
    71                 AdditionalDependencies="wsock32.lib winmm.lib version.lib comctl32.lib libcurld.lib LiteUnzip.lib intl.lib lua5.1D.lib OIS_d.lib" 
     71                AdditionalDependencies="wsock32.lib winmm.lib version.lib comctl32.lib libcurld.lib LiteUnzip.lib intl.lib lua5.1D.lib luabindD.lib OIS_d.lib" 
    7272                OutputFile="..\Debug\HoverRace.exe" 
    7373                SuppressStartupBanner="true" 
     
    152152            <Tool 
    153153                Name="VCLinkerTool" 
    154                 AdditionalDependencies="wsock32.lib winmm.lib version.lib comctl32.lib libcurl.lib LiteUnzip.lib intl.lib lua5.1.lib OIS.lib" 
     154                AdditionalDependencies="wsock32.lib winmm.lib version.lib comctl32.lib libcurl.lib LiteUnzip.lib intl.lib lua5.1.lib luabind.lib OIS.lib" 
    155155                OutputFile=".\..\Release\HoverRace.exe" 
    156156                LinkIncremental="1" 
     
    294294            </File> 
    295295            <File 
     296                RelativePath=".\Game2\GamePeer.cpp" 
     297                > 
     298            </File> 
     299            <File 
     300                RelativePath=".\Game2\GamePeer.h" 
     301                > 
     302            </File> 
     303            <File 
    296304                RelativePath=".\Game2\HighConsole.cpp" 
    297305                > 
     
    669677                        Description="$(InputFileName)" 
    670678                        CommandLine="copy &quot;$(InputPath)&quot; $(OutDir)&#x0D;&#x0A;" 
     679                        Outputs="$(OutDir)\$(InputFileName)" 
     680                    /> 
     681                </FileConfiguration> 
     682            </File> 
     683            <File 
     684                RelativePath="..\lib\luabind.dll" 
     685                > 
     686                <FileConfiguration 
     687                    Name="Debug|Win32" 
     688                    ExcludedFromBuild="true" 
     689                    > 
     690                    <Tool 
     691                        Name="VCCustomBuildTool" 
     692                        Description="$(InputFileName)" 
     693                        CommandLine="copy &quot;$(InputPath)&quot; $(OutDir)" 
     694                        Outputs="$(OutDir)\$(InputFileName)" 
     695                    /> 
     696                </FileConfiguration> 
     697                <FileConfiguration 
     698                    Name="Release|Win32" 
     699                    > 
     700                    <Tool 
     701                        Name="VCCustomBuildTool" 
     702                        Description="$(InputFileName)" 
     703                        CommandLine="copy &quot;$(InputPath)&quot; $(OutDir)" 
     704                        Outputs="$(OutDir)\$(InputFileName)" 
     705                    /> 
     706                </FileConfiguration> 
     707            </File> 
     708            <File 
     709                RelativePath="..\lib\luabindD.dll" 
     710                > 
     711                <FileConfiguration 
     712                    Name="Debug|Win32" 
     713                    > 
     714                    <Tool 
     715                        Name="VCCustomBuildTool" 
     716                        Description="$(InputFileName)" 
     717                        CommandLine="copy &quot;$(InputPath)&quot; $(OutDir)" 
     718                        Outputs="$(OutDir)\$(InputFileName)" 
     719                    /> 
     720                </FileConfiguration> 
     721                <FileConfiguration 
     722                    Name="Release|Win32" 
     723                    ExcludedFromBuild="true" 
     724                    > 
     725                    <Tool 
     726                        Name="VCCustomBuildTool" 
     727                        Description="$(InputFileName)" 
     728                        CommandLine="copy &quot;$(InputPath)&quot; $(OutDir)" 
    671729                        Outputs="$(OutDir)\$(InputFileName)" 
    672730                    /> 
  • trunk/client/Game2/GameApp.cpp

    r910 r918  
    2828#include "FirstChoiceDialog.h" 
    2929#include "FullscreenTest.h" 
     30#include "GamePeer.h" 
    3031#include "Controller.h" 
    3132#include "resource.h" 
     
    570571 
    571572MR_GameApp::MR_GameApp(HINSTANCE pInstance, bool safeMode) : 
    572     introMovie(NULL), scripting(NULL), sysConsole(NULL) 
     573    introMovie(NULL), scripting(NULL), gamePeer(NULL), sysConsole(NULL) 
    573574{ 
    574575    This = this; 
     
    613614        delete sysConsole; 
    614615        sysConsole = NULL; 
     616    } 
     617    if (gamePeer != NULL) { 
     618        delete gamePeer; 
     619        gamePeer = NULL; 
    615620    } 
    616621    if (scripting != NULL) { 
     
    11421147    // This allows the script to modify the configuration (e.g. for unit tests). 
    11431148    scripting = new Script::Core(); 
    1144     sysConsole = new SysConsole(scripting); 
     1149    GamePeer::Register(scripting); 
     1150    gamePeer = new GamePeer(scripting); 
     1151    sysConsole = new SysConsole(scripting, gamePeer); 
    11451152    if (!initScript.empty()) { 
    11461153        sysConsole->RunScript(initScript); 
     
    20072014         */ 
    20082015        case MR_WM_ON_INIT: 
    2009             This->sysConsole->OnInit(); 
     2016            This->gamePeer->OnInit(); 
    20102017            break; 
    20112018 
  • trunk/client/Game2/GameApp.h

    r904 r918  
    3232    namespace Client { 
    3333        class FullscreenTest; 
     34        class GamePeer; 
    3435        class HighConsole; 
    3536        class IntroMovie; 
     
    8788        HoverRace::Client::FullscreenTest *fullscreenTest; 
    8889        HoverRace::Script::Core *scripting; 
     90        HoverRace::Client::GamePeer *gamePeer; 
    8991        HoverRace::Client::SysConsole *sysConsole; 
    9092        MR_ClientSession *mCurrentSession; 
  • trunk/client/Game2/SysConsole.cpp

    r904 r918  
    2828#include <boost/filesystem/fstream.hpp> 
    2929#include <boost/format.hpp> 
     30 
     31#include <luabind/luabind.hpp> 
     32 
     33#include "GamePeer.h" 
    3034 
    3135#include "SysConsole.h" 
     
    6872namespace Client { 
    6973 
    70 SysConsole::SysConsole(Script::Core *scripting) : 
    71     SUPER(scripting), outHandle(scripting->AddOutput(boost::make_shared<LogStream>())) 
     74SysConsole::SysConsole(Script::Core *scripting, GamePeer *gamePeer) : 
     75    SUPER(scripting), gamePeer(gamePeer), 
     76    outHandle(scripting->AddOutput(boost::make_shared<LogStream>())) 
    7277{ 
    73     lua_State *state = scripting->GetState(); 
    74  
    75     scripting->PrintStack(); 
    76  
    77     // Initial table for callbacks. 
    78     lua_newtable(state); 
    79     onInitRef = luaL_ref(scripting->GetState(), LUA_REGISTRYINDEX); 
    8078} 
    8179 
    8280SysConsole::~SysConsole() 
    8381{ 
    84     Script::Core *scripting = GetScripting(); 
    85     luaL_unref(scripting->GetState(), LUA_REGISTRYINDEX, onInitRef); 
    86     scripting->RemoveOutput(outHandle); 
     82    GetScripting()->RemoveOutput(outHandle); 
    8783} 
    8884 
    8985void SysConsole::InitEnv() 
    9086{ 
     87    using namespace luabind; 
     88 
    9189    Script::Core *scripting = GetScripting(); 
    92     lua_State *state = scripting->GetState(); 
     90    lua_State *L = scripting->GetState(); 
    9391 
    9492    // Start with the standard global environment. 
    9593    CopyGlobals(); 
    9694 
     95    object env(from_stack(L, -1)); 
     96    env["game"] = gamePeer; 
     97 
     98    /* 
    9799    lua_pushlightuserdata(state, this);  // table this 
    98100    lua_pushcclosure(state, SysConsole::LOnInit, 1);  // table fn 
     
    106108    lua_insert(state, -2);  // table str fn 
    107109    lua_rawset(state, -3);  // table 
     110    */ 
    108111} 
    109112 
     
    152155} 
    153156 
    154 /** 
    155  * Executes all "on_init" callbacks. 
    156  */ 
    157 void SysConsole::OnInit() 
    158 { 
    159     lua_State *state = GetScripting()->GetState(); 
    160  
    161     lua_rawgeti(state, LUA_REGISTRYINDEX, onInitRef);  // table 
    162  
    163     lua_pushnil(state);  // table nil 
    164     while (lua_next(state, -2) != 0) { 
    165         // table key fn 
    166         lua_call(state, 0, 0);  // table key 
    167     } 
    168     // table 
    169     lua_pop(state, 1); 
    170 } 
    171  
    172 int SysConsole::LOnInit(lua_State *state) 
    173 { 
    174     // function on_init(f) 
    175     // Register a callback function for when the game starts up. 
    176     SysConsole *self = static_cast<SysConsole*>(lua_touserdata(state, lua_upvalueindex(1))); 
    177  
    178     if (!lua_isfunction(state, 1)) { 
    179         lua_pushstring(state, "on_init requires a function."); 
    180         return lua_error(state); 
    181     } 
    182  
    183     lua_rawgeti(state, LUA_REGISTRYINDEX, self->onInitRef);  // table 
    184  
    185     lua_pushinteger(state, lua_objlen(state, -1) + 1); // table key 
    186     lua_pushvalue(state, 1); // table key fn 
    187     lua_settable(state, -3); // table 
    188  
    189     lua_pop(state, 1); 
    190  
    191     return 0; 
    192 } 
    193  
    194 int SysConsole::LGetOnInit(lua_State *state) 
    195 { 
    196     // function get_on_init() 
    197     // Returns the table of on_init callbacks (for debugging purposes). 
    198     SysConsole *self = static_cast<SysConsole*>(lua_touserdata(state, lua_upvalueindex(1))); 
    199  
    200     lua_rawgeti(state, LUA_REGISTRYINDEX, self->onInitRef);  // table 
    201  
    202     return 1; 
    203 } 
    204  
    205157}  // namespace Client 
    206158}  // namespace HoverRace 
  • trunk/client/Game2/SysConsole.h

    r904 r918  
    2727 
    2828namespace HoverRace { 
     29    namespace Client { 
     30        class GamePeer; 
     31    } 
    2932    namespace Script { 
    3033        class Core; 
     
    4043 
    4144    public: 
    42         SysConsole(Script::Core *scripting); 
     45        SysConsole(Script::Core *scripting, GamePeer *gamePeer); 
    4346        virtual ~SysConsole(); 
    4447 
     
    5356        void RunScript(const std::string &filename); 
    5457 
    55     public: 
    56         void OnInit(); 
    57  
    5858    private: 
    59         static int LOnInit(lua_State *state); 
    60         static int LGetOnInit(lua_State *state); 
    61  
    62     private: 
     59        GamePeer *gamePeer; 
    6360        Script::Core::OutHandle outHandle; 
    64         int onInitRef; 
    6561}; 
    6662 
  • trunk/include/config-win32.h

    r871 r918  
    1616#define HAVE_LUA 1 
    1717 
     18#define HAVE_LUABIND 1 
     19#define LUABIND_DYNAMIC_LINK 1 
     20 
    1821// Only enable DirectShow support if a recent Windows SDK is installed. 
    1922#if defined(VER_PRODUCTBUILD) && VER_PRODUCTBUILD >= 6001