2020-05-23 01:11:46 +08:00
|
|
|
#include "LuaEngine.h"
|
|
|
|
#include <string>
|
|
|
|
#include <iostream>
|
2020-05-29 01:39:49 +08:00
|
|
|
#include <direct.h>
|
2020-05-23 01:11:46 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
string LoadFile(const string& filename)
|
|
|
|
{
|
|
|
|
string temp;
|
|
|
|
int ret;
|
2020-06-24 18:47:46 +08:00
|
|
|
char buff[1024] = { 0 };
|
2020-05-23 01:11:46 +08:00
|
|
|
SDL_RWops* io = SDL_RWFromFile(filename.c_str(), "rb");
|
|
|
|
if (!io)
|
|
|
|
{
|
|
|
|
SDL_Log("SDL_RWFromFile: %s\n", SDL_GetError());
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
while ((ret=SDL_RWread(io, buff, 1, 1024)))
|
|
|
|
{
|
|
|
|
temp.append(buff, ret);
|
2020-06-24 18:47:46 +08:00
|
|
|
memset(buff, 0, 1024);
|
2020-05-23 01:11:46 +08:00
|
|
|
}
|
|
|
|
SDL_RWclose(io);
|
|
|
|
return temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
|
|
|
InitEngine();
|
|
|
|
lua_State* L = luaL_newstate();
|
|
|
|
luaL_openlibs(L);
|
|
|
|
InitLuaEngine(L);
|
2020-05-29 01:39:49 +08:00
|
|
|
_chdir("game");
|
2020-05-31 18:03:21 +08:00
|
|
|
string code = LoadFile("app.lua");
|
2020-06-24 18:47:46 +08:00
|
|
|
if (luaL_loadbufferx(L, code.c_str(), code.size(), "ProgramMain", "t"))
|
2020-05-23 01:11:46 +08:00
|
|
|
{
|
2020-06-24 18:47:46 +08:00
|
|
|
size_t errlen;
|
|
|
|
const char* err = lua_tolstring(L, -1, &errlen);
|
|
|
|
string errmsg(err, errlen);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[LuaAppSyntaxError] %s", errmsg.c_str());
|
2020-05-23 01:11:46 +08:00
|
|
|
}
|
|
|
|
else if(lua_pcall(L, 0, LUA_MULTRET, 0))
|
|
|
|
{
|
2020-06-24 18:47:46 +08:00
|
|
|
size_t errlen;
|
|
|
|
const char* err = lua_tolstring(L, -1, &errlen);
|
|
|
|
string errmsg(err, errlen);
|
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "[LuaAppRuntimeError] %s", errmsg.c_str());
|
2020-05-23 01:11:46 +08:00
|
|
|
}
|
|
|
|
lua_close(L);
|
|
|
|
CloseEngine();
|
|
|
|
return 0;
|
|
|
|
}
|