zoukankan      html  css  js  c++  java
  • 封装LuaEngine

    1, Q:为什么没有用Luaplus或是其他的Binder?
        A: nice question! Luaplus不能满足我在linux下使用,其他的太庞大,我需要一个轻量级的。

    2,Q:是原创吗?
       A:NO, thanks to Matthew Harmon matt@matthewharmon.com,我按照自己的需求做了封装。

    3,Q:它的优点是什么?
       A:主要是它简单,基本能满足和C/C++交互的需求。它使用了lua_newthread来管理每一个脚本(性能有问题吗?)。它支持
           事件的resume,另外把LuaDebugger也封装进来了,方便调试。

     贴下接口头文件:
    #ifndef    ILUAENGINE_H
    #define    ILUAENGINE_H

    #define GTC_LP        0x0000    // 指针
    #define GTC_INT        0x0001    // 整型
    #define GTC_DOUBLE    0x0002    // 浮点型
    #define GTC_STRING    0x0003    // 字符串

    // 脚本数据类型定义
    enum
    {
        SD_NUMBER 
    = 0,    // 数字类型
        SD_STRING,        // 字符串类型
        SD_TABLE,        //
    }
    ;

    struct SSDTable
    {
        
    int nNum;
        
    void* pValue;
    }
    ;

    // 脚本参数对象
    struct SScriptParamObj
    {
        
    int nType;            // 参数类型, SD_NUMBER 或者 SD_STRING

        union UScriptParam    
    // 参数值
        {
            
    int        nNumber;        // 数字
            char    szString[64];    // 字符串
            SSDTable stTable;

        }
     unValue;

        SScriptParamObj()
        
    {
            memset(
    this0sizeof(*this));
        }


        
    void operator = (int nValue)
        
    {
            nType 
    = SD_NUMBER;
            unValue.nNumber 
    = nValue;
        }


        
    void operator = (char *str)
        
    {
            nType 
    = SD_STRING;
            unValue.szString[
    0= 0;

            
    if (str != NULL)
            
    {
                strncpy(unValue.szString, str, 
    sizeof(unValue.szString));
            }

        }


        
    void operator = ( SSDTable pT )
        
    {
            nType 
    = SD_TABLE;
            unValue.stTable.nNum 
    = pT.nNum;
            unValue.stTable.pValue 
    = (void *)pT.pValue;    
        }


    }
    ;

    struct ILuaScript
    {    
        
    /**//*
         *    @Param: szFileName - 脚本文件名
         *    @Return: NULL
         *    @Description: 调用一个脚本
         
    */

        
    virtual bool CallFile(const char *szFileName) = 0;
        
        
    /**//*
         *    @Param: szFuncName - 函数名 pIn, nInNum - 输入参数列表指针以及个数
         *    @Return: pRet, nRetNum - 返回参数列表指针以及个数
         *    @Description: 调用一个函数
         
    */

        
    virtual bool CallFunction(const char *szFuncName, SScriptParamObj *pIn, 
                            
    int nInNum,    SScriptParamObj *pRet, int nRetNum) = 0;
        
        
    /**//*
         *    @Param: szString - 字符串指针
         *    @Return: NULL
         *    @Description: 调用一个字符串
         
    */

        
    virtual bool CallString(const char *szString) = 0;

        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual void ShowLuaCallStack() = 0;    
    }
    ;

    struct ILuaManager
    {
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual bool InitManager(IGameWorld* pGameWorld) = 0;
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual ILuaScript* CreateScript() = 0;
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual void Update(float fElapse) = 0;
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: 注意如果是自己卸载一个script,请自己
                          手工管理这个script的指针,manager析构
                          的时候,会自动析构所有的script
         
    */

        
    virtual void UnlinkScript(ILuaScript* pScript) = 0;
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual ILuaScript* GetScript(const char* szScriptName) = 0;
        
    /**//*
         *    @Param: NULL
         *    @Return: NULL
         *    @Description: NULL
         
    */

        
    virtual void Release() = 0;
    }
    ;
    #endif


    另外,还有一个高手写的binder,针对c++的object做了很好的封装,但我没有采用,因为我想要灵活一点的。
    链接在下面,大家可以对比一下,给出评论
    http://www.codeproject.com/KB/cpp/luaincpp.aspx

    原文地址:

    http://www.cppblog.com/zuhd/archive/2010/05/04/114346.html

  • 相关阅读:
    android界面横屏和竖屏的切换
    google 提供webrtc 的实例使用 turnserver的方式
    如何使官方提供的AppRTCDemo 运行在自己搭建的server(官方提供的apprtc)上(官方的server源码)
    android在全屏下第一次触摸屏幕没有触发事件
    ubuntu常用命令记录集
    python 一个包中的文件调用另外一个包文件 实例
    python-插入排序
    phantomjs submit click
    python socket.error: [Errno 10054] 解决方法
    python-快速排序,两种方法→易理解
  • 原文地址:https://www.cnblogs.com/byfei/p/3112148.html
Copyright © 2011-2022 走看看