• (转)LuaPlus测试代码


    #include "stdafx.h"
    #include <windows.h>
    #include <conio.h>
    #include <ctype.h>

    #define LUA_FILE "Plus.lua"

    #include "LuaPlus.h"
    using namespace LuaPlus;

    LuaStateOwner g_LusState(true);

    void CPrint(const char* sMsg)
    {
        printf(sMsg);
        OutputDebugString(sMsg);
    }

    int L_CPrint(LuaState* pState)
    {
        OutputDebugString("Table Function\r\n");

        LuaStack args(pState);
        int iArgs = args.Count();
        const char* sMsg = args[1].GetString();
        int iTimes = args[2].GetInteger();
        for (int i=0;i<iTimes;i++)
        {
            CPrint(sMsg);
        }
        return iTimes;
    }

    class CLogger
    {
    public:
        CLogger() : m_sVal("Value\r\n"){};
        ~CLogger(){};

    public:
        void LogMsg(const char* sMsg)
        {
            CPrint(sMsg);
            CPrint(m_sVal);
        }

        static void S_LogMsg(const char* sMsg)
        {
            CPrint(sMsg);
        }

        void SetVal(const char* sVal)
        {
            m_sVal = (char*)sVal;
        }

        void SP_LogMsg(const char* sMsg)
        {
            OutputDebugString(m_sVal);
            OutputDebugString(sMsg);
        }

    public:
        char* m_sVal;
    };

    void DoTest()   
    {
        int iRet = g_LusState->DoFile(LUA_FILE);

        //获取变量
        LuaObject varObj = g_LusState->GetGlobal("Variable");
        int iVal = varObj.GetInteger();

        LuaObject wstrObj = g_LusState->GetGlobal("WStr");
        const lua_WChar* pWS = wstrObj.GetWString();
        char ansi[7] = {pWS[0],pWS[1],pWS[2],pWS[3],pWS[4],pWS[5],0};
        wchar_t name[10] = {0};
        MultiByteToWideChar(CP_ACP,0,ansi,-1,name,10);


        //添加变量
        g_LusState->GetGlobals().SetNumber("TmpVar",123);
        iVal = g_LusState->GetGlobal("TmpVar").GetInteger();

        //使用Lua函数
        LuaFunction<int> luaAdd = g_LusState->GetGlobal("LuaAdd");
        iVal = luaAdd(2,5);

        //注册C函数
        g_LusState->GetGlobals().RegisterDirect("CPrint",CLogger::S_LogMsg);

        //Lua函数调用C函数
        LuaFunction<void> luaCPrint = g_LusState->GetGlobal("LuaCPrint");
        luaCPrint("lua run c function\r\n");

        //注册C++类成员函数
        CLogger logger;
        logger.m_sVal = "abcdefg";
        g_LusState->GetGlobals().RegisterDirect("LogMsg", logger, &CLogger::LogMsg);
        g_LusState->GetGlobals().RegisterObjectDirect("SP_LogMsg", (const CLogger*)0, &CLogger::SP_LogMsg);

        //Lua调用类成员函数
        LuaFunction<void> luaClassLog1 = g_LusState->GetGlobal("LuaClassPrint");
        luaClassLog1("lua run c++ function---1\r\n");

        LuaFunction<void> luaClassLog2 = g_LusState->GetGlobal("SP_LuaClassPrint");
        luaClassLog2("lua run c++ function---2\r\n");

        //使用Lua的Table
        LuaObject table = g_LusState->GetGlobals().CreateTable("GameApi");
        table.SetInteger("Ref", 1);
        table.SetString("Day", "2009-11-02");
        table.Register("Run",L_CPrint);

        //Lua 使用Table的函数
        LuaFunction<int> luaTbl = g_LusState->GetGlobal("LuaTbl");
        iVal = luaTbl("LuaTbl Run...\r\n",2);

        //获取Table的一项
        LuaObject tbl = g_LusState->GetGlobal("GameApi");
        const char* sDay = tbl.GetByName("Day").GetString();

        //Table遍历
        for (LuaTableIterator it( tbl ); it; it.Next() )
        {
            const char* key   = it.GetKey().GetString();
            LuaObject val = it.GetValue();
            if (val.IsNumber())
            {
                printf("Key: %s --- NumberValue: %f\r\n",key,val.GetFloat());
            }
            else if (val.IsString())
            {
                printf("Key: %s --- StringValue: %s\r\n",key,val.GetString());
            }
            else if (val.IsFunction())
            {
                printf("Key: %s --- Function\r\n",key);
            }
            else if (val.IsCFunction())
            {
                printf("Key: %s --- CFunction\r\n",key);
            }
        }
    }

    int _tmain(int argc, _TCHAR* argv[])
    {
        LuaStateOwner state;
        state->DoString("print('Hello World\\n')");

        printf("Begin Test LuaPlus Library......\r\n");
       
        DoTest();

        printf("End Test LuaPlus Library......\r\n");

        _getch();
        return 0;
    }

    Plus.lua内容如下:
    Variable = 12345
    WStr     = L"LuaPlus测试"
    print("luaplus do file\r\n")

    function LuaAdd(x,y)
        return x + y + TmpVar
    end

    function LuaCPrint(sMsg)
        CPrint(sMsg)
    end

    function LuaClassPrint(sMsg)
        LogMsg(sMsg)
    end

    function LuaTbl(sMsg,iTimes)
        CPrint(GameApi.Day)
        CPrint(string.format("%d",GameApi.Ref))
        GameApi.Run(sMsg,iTimes)
    end

    function SP_LuaClassPrint(sMsg)
        SP_LogMsg(sMsg)
    end

  • 相关阅读:
    C++友元
    C++类与static
    C++const
    reinterpret_cast应用
    学习软件工程课的心得上
    学习软件工程课的心得下
    项目总结报告之假如历史重新再来一次
    人月神话读后感
    团队任务考核
    冲刺周期会议十一
  • 原文地址:https://www.cnblogs.com/lancidie/p/1935800.html
Copyright © 2020-2023  润新知