📜 ⬆️ ⬇️

Own scripting engine for games using C ++ and Lua (part 1)

Foreword


Perhaps you had such a moment that you wanted to write your own game engine, or you just wanted to know how to implement this, but for some reason you didn’t succeed.
Well, the topic is quite extensive, so I am starting a series of lessons on writing my 2D game engine, and believe me it will be no worse than the same Love2d, this is exactly the style that will be our engine.

What do you need?



How is everything arranged?


All game logic will be programmed in a file, for example, “main.lua”. The engine will read this file and execute the actions described in this file. Graphics output will be using SDL 2.0, physics - Box2D, audio - OpenAl, scripting - Lua. IDE - Microsoft Visual Studio of any version.

Draw a scheme




Getting started!


First you need to download:
')
  1. Lua
  2. SDL 2.0
  3. Box2D (you need to compile yourself)
  4. Openal

Download and move all files to a separate folder, for example - “Engine SDK”. We open MVS, we create the "empty" console application, further we add the file - "main.cpp".

Fill for now in this way:

int main() { return 0; } 

Compile, if compiled, go ahead. Click “Project → project properties”. Select "C / C ++ → General" and add additional include folders (specify the path where you extracted from the Lua archive). Specify the path to "include" Lua.



After that, go to “Linker → General” and add the path to the lib.



Apply and change "main.cpp"

 int main(int argc, char * argv[]) { return 0; } 

We compile, we go further. Next, we need to create a separate header file, which will be the main part of the engine. Add the file "Engine.h". And immediately fill it in this way.

 #include<iostream> #include<lua.hpp> #pragma comment(lib,"lua53") //      Lua 5.3. using namespace std; class Lua { private: lua_State * lua_state; public: void Init() //    . { lua_state = luaL_newstate(); static const luaL_Reg lualibs[] = { { "base", luaopen_base }, { "io", luaopen_io }, { "os",luaopen_os }, { "math",luaopen_math }, { "table",luaopen_table }, { "string",luaopen_string }, { "package",luaopen_package }, { NULL, NULL } }; for (const luaL_Reg *lib = lualibs; lib->func != NULL; lib++) { luaL_requiref(lua_state, lib->name, lib->func, 1); lua_settop(lua_state, 0); } } void Open(const char*filename) //     (main.lua) { luaL_openlibs(lua_state); if (luaL_dofile(lua_state, filename)) { const char*error = lua_tostring(lua_state, -1); } } void Close() //  { lua_close(lua_state); } void Reg_int(int value, char*name) { lua_pushinteger(lua_state, value); lua_setglobal(lua_state, name); } void Reg_double(double value, char*name) { lua_pushnumber(lua_state, value); lua_setglobal(lua_state, name); } void Reg_bool(bool value, char*name) { lua_pushboolean(lua_state, value); lua_setglobal(lua_state, name); } void Reg_string(char*value, char*name) { lua_pushstring(lua_state, value); lua_setglobal(lua_state, name); } void Reg_function(lua_CFunction value, const char*name) //    { lua_pushcfunction(lua_state, value); lua_setglobal(lua_state, name); } int Get_int(int index) //      { return (int)lua_tointeger(lua_state, index); } double Get_double(int index) { return lua_tonumber(lua_state, index); } char* Get_string(int index) { return (char*)lua_tostring(lua_state, index); } bool Get_bool(int index) { return lua_toboolean(lua_state, index); } void Return_int(int value) //      { lua_pushinteger(lua_state, value); } void Return_double(double value) { lua_pushnumber(lua_state, value); } void Return_string(char*value) { lua_pushstring(lua_state, value); } void Return_bool(int value) { lua_pushboolean(lua_state, value); } int Call_load() //    { lua_getglobal(lua_state, "Load"); lua_call(lua_state, 0, 1); return 0; } int Call_update() //     { lua_getglobal(lua_state, "Update"); lua_call(lua_state, 0, 1); return 0; } int Call_draw() { lua_getglobal(lua_state, "Draw"); //   "Update" lua_call(lua_state, 0, 1); return 0; } }; Lua lua;// lua  

We compile, if there are no errors, we go further, but if there is, then you have nakosyachili somewhere. Change the "main.cpp":

 include "Engine.h" int main(int argc, char * argv[]) { lua.Init(); lua.Open("main.lua"); lua.Call_load(); lua.Close(); return 0; } 

We compile, if without errors, we move on. Create a text file “main.lua” in the project folder. Fill it like this:

 function Load() print("Lua inited!") end function Update() end function Draw() end 

Compile, throw "lua5 * .dll" in the project folder, run, and Oppa! In the console, output "Lua inited!". In fact, we wrote a simple Lua interpreter. In the second part we proceed to the output graphics.

Source: https://habr.com/ru/post/330062/


All Articles