Add lua to project for testing, add test.lua script
My lua test in here so far just outputs a message to cout. I will expand this for something later. Add defines.cpp for lua scripts and other items.
This commit is contained in:
parent
b0555158b4
commit
eab83c296c
1
lua_scripts/test.lua
Normal file
1
lua_scripts/test.lua
Normal file
@ -0,0 +1 @@
|
||||
--print_msg("Test in Cout ImGui C++")
|
18
src/Main.cpp
18
src/Main.cpp
@ -20,6 +20,8 @@ int main(int, char**)
|
||||
#include <iostream>
|
||||
#include <windows.h>
|
||||
|
||||
#include "defines.h"
|
||||
|
||||
// Main code
|
||||
// I moved the DirectX9 code into the test/directx9_test.cpp file,
|
||||
|
||||
@ -34,6 +36,15 @@ int main(int, char**)
|
||||
// New
|
||||
#include <shellapi.h>
|
||||
|
||||
#ifdef LUA_TEST
|
||||
#include "lua_test.h"
|
||||
//extern "C" {
|
||||
//#include <lauxlib.h>
|
||||
//#include <lua.h>
|
||||
//#include <lualib.h>
|
||||
//}
|
||||
#endif
|
||||
|
||||
|
||||
// https://stackoverflow.com/questions/14954876/how-to-create-a-message-box-with-user-defined-buttons-in-c
|
||||
/*
|
||||
@ -57,6 +68,13 @@ typedef VOID(*DLLPROC) (LPTSTR);
|
||||
int main(int, char**)
|
||||
{
|
||||
#define _TEST1
|
||||
// Load lua
|
||||
#ifdef LUA_TEST
|
||||
LuaTest luaTest = LuaTest();
|
||||
luaTest.InitLua();
|
||||
|
||||
#endif //LUA_TEST
|
||||
|
||||
// Load a test dll
|
||||
// Get path to the dll, use current path
|
||||
// https://stackoverflow.com/questions/61382829/dynamically-use-a-dll-from-a-console-application
|
||||
|
143
src/test/lua_test.cpp
Normal file
143
src/test/lua_test.cpp
Normal file
@ -0,0 +1,143 @@
|
||||
#include "lua_test.h"
|
||||
|
||||
#include <string>
|
||||
#include <iostream>
|
||||
|
||||
/// <summary>
|
||||
/// Set the lua state, and load the lua libraries
|
||||
/// </summary>
|
||||
LuaTest::LuaTest() {
|
||||
m_luaVM = luaL_newstate();
|
||||
|
||||
// Load LUA libraries, TODO is this needed in here?
|
||||
luaopen_base(m_luaVM);
|
||||
luaopen_math(m_luaVM);
|
||||
luaopen_string(m_luaVM);
|
||||
luaopen_table(m_luaVM);
|
||||
luaopen_debug(m_luaVM);
|
||||
luaopen_utf8(m_luaVM);
|
||||
luaopen_os(m_luaVM);
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Check lua globals, made this into a function
|
||||
/// </summary>
|
||||
/// <param name="L"></param>
|
||||
/// <param name="r"></param>
|
||||
/// <returns></returns>
|
||||
bool
|
||||
LuaTest::CheckLua(lua_State* L, int r)
|
||||
{
|
||||
// Check if the value is valid
|
||||
if (r != LUA_OK) {
|
||||
std::string errorMsg = lua_tostring(L, -1);
|
||||
std::cout << errorMsg << std::endl;
|
||||
return false;
|
||||
} /*else {
|
||||
std::cout << errorMsg << std::endl;
|
||||
}*/
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// All functions used in lua scripts need to be in 'extern "C" {}' blocks
|
||||
// When defining a lua function in here, make it like this:
|
||||
/*
|
||||
int FunctionNameLua(lua_State* L)
|
||||
|
||||
// The return can be how many variables you want this to use.
|
||||
// For the LogMessageToConsoleLua function, it has a return value of 1 because it
|
||||
// only takes the string from the lua file like this:
|
||||
// print_msg("This will display in cout")
|
||||
|
||||
return 1;
|
||||
|
||||
// If this has multiple variables needed, such as for coordinates in a game
|
||||
// You can change this to 3 for an X, Y, Z
|
||||
// set_coordinates(x, y, z);
|
||||
return 3;
|
||||
|
||||
|
||||
*
|
||||
*/
|
||||
extern "C"
|
||||
{
|
||||
/// <summary>
|
||||
/// Log a message to cout using lua
|
||||
/// </summary>
|
||||
/// <param name="L"></param>
|
||||
/// <returns></returns>
|
||||
int LogMessageToConsoleLua(lua_State* L)
|
||||
{
|
||||
std::string messageToLog = (std::string)lua_tostring(L, 1);
|
||||
std::cout << messageToLog << std::endl;
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// I had to add this to the class to fix it.
|
||||
/// I got this working, I had to add lua_State *L and get the lua state
|
||||
/// To move this back into the init function if needed:
|
||||
/// Rename the 'L' variable in each lua_register to 'm_luaVM'
|
||||
/// </summary>
|
||||
void
|
||||
LuaTest::RegisterLuaFunctions(lua_State* L)
|
||||
{
|
||||
// Register functions like this
|
||||
lua_register(L, "print_msg", LogMessageToConsoleLua);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// TODO Setup events to run, I may need to add a delay to some of this.
|
||||
/// This would go in the main loop if I decide to use events in this ImGui test.
|
||||
/// </summary>
|
||||
#ifdef LUA_EVENT_TEST
|
||||
void
|
||||
LuaTest::LuaEvents()
|
||||
{
|
||||
Defines defines = Defines();
|
||||
LuaTest luaTest = LuaTest();
|
||||
|
||||
luaTest.RegisterLuaFunctions(m_luaVM);
|
||||
|
||||
if (CheckLua(m_luaVM, luaL_dofile(m_luaVM, defines.luaEventsScript.c_str())))
|
||||
{
|
||||
}
|
||||
else {
|
||||
std::cout << "The lua file " << defines.luaEventsScript << "Couldn't be loaded!" << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// This runs the init lua function, but it can be run multiple times
|
||||
/// </summary>
|
||||
void
|
||||
LuaTest::InitLua()
|
||||
{
|
||||
Defines defines = Defines();
|
||||
LuaTest luaTest = LuaTest();
|
||||
|
||||
|
||||
//lua_State *L = LuaTest::GetInstance().GetLuaState();
|
||||
|
||||
luaTest.RegisterLuaFunctions(m_luaVM);
|
||||
|
||||
// This seems to work for error handling if the file doesn't exist
|
||||
if (CheckLua(m_luaVM, luaL_dofile(m_luaVM, defines.luaScript.c_str())))
|
||||
{
|
||||
//std::cout << defines.luaScript << " exists!" << std::endl;
|
||||
}
|
||||
else {
|
||||
std::cout << defines.luaScript << " doesn't exist!" << std::endl;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
59
src/test/lua_test.h
Normal file
59
src/test/lua_test.h
Normal file
@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
|
||||
#include "defines.h"
|
||||
#ifdef LUA_TEST
|
||||
extern "C" {
|
||||
#include <lauxlib.h>
|
||||
#include <lua.h>
|
||||
#include <lualib.h>
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
class LuaTest
|
||||
{
|
||||
private:
|
||||
// Set the lua state
|
||||
lua_State* m_luaVM;
|
||||
|
||||
// Register the lua functions
|
||||
void RegisterLuaFunctions(lua_State* L);
|
||||
public:
|
||||
|
||||
LuaTest();
|
||||
void InitLua();
|
||||
|
||||
bool CheckLua(lua_State* L, int r);
|
||||
|
||||
// Run the custom event system I am working on in Lua
|
||||
#ifdef LUA_EVENT_TEST
|
||||
void LuaEvents();
|
||||
#endif
|
||||
|
||||
// These work for getting the m_luaVM variable in the private class above.
|
||||
|
||||
// Return the instance of LuaTest
|
||||
static LuaTest& GetInstance()
|
||||
{
|
||||
static LuaTest instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
// Get the lua state, can be used like this:
|
||||
// lua_State *L = LuaTest::GetInstance().GetLuaState();
|
||||
lua_State* GetLuaState()
|
||||
{
|
||||
return m_luaVM;
|
||||
}
|
||||
|
||||
// Close the lua vm
|
||||
~LuaTest()
|
||||
{
|
||||
if (m_luaVM)
|
||||
{
|
||||
lua_close(m_luaVM);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
9
src/util/defines.cpp
Normal file
9
src/util/defines.cpp
Normal file
@ -0,0 +1,9 @@
|
||||
#include "defines.h"
|
||||
#include "misc_util.h"
|
||||
#include "file_functions.h"
|
||||
|
||||
std::string currentDirectory = FileFunctions::CurrentDirectory();
|
||||
std::string luaScriptsDirectory = currentDirectory + "\\lua_scripts\\";
|
||||
|
||||
std::string Defines::luaScript = luaScriptsDirectory + "test.lua";
|
||||
std::string Defines::luaEventsScript = luaScriptsDirectory + "kcnet-events.lua";
|
@ -3,6 +3,16 @@
|
||||
#if _WIN32
|
||||
#include <Windows.h>
|
||||
#endif
|
||||
|
||||
#include <string.h>
|
||||
#include <iostream>
|
||||
|
||||
// Enable lua test
|
||||
#define LUA_TEST
|
||||
|
||||
// Enable the lua event test, this is disabled until I'm ready to use it.
|
||||
//#define LUA_EVENT_TEST
|
||||
|
||||
//
|
||||
|
||||
// TODO Fix this to work in the OpenGL test
|
||||
@ -22,6 +32,10 @@ public:
|
||||
const wchar_t* window_title = TITLE;
|
||||
const wchar_t* main_menu_title = MAIN_MENU_TITLE_;
|
||||
const char* imgui_window_name = IMGUI_WINDOW_NAME_;
|
||||
|
||||
// Lua script files
|
||||
static std::string luaScript;
|
||||
static std::string luaEventsScript;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user