Basic Plugin Example for C/C++ #767
Replies: 2 comments
-
Here is another example of a plugin that calls PASS:TEXT inside the cpp plugin to display HELLO WORLD. Inside the plugin folder create a new folder called "lovr-cppbindings" Inside the new folder create two files. The first file is "CMakeLists.txt": project(lovr-cppbindings)
cmake_minimum_required(VERSION 3.5.0)
add_library(cppbindings MODULE cppbindings.cpp)
set_target_properties(cppbindings PROPERTIES PREFIX "") The second file is "cppbindings.cpp": extern "C"
{
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
#include <string>
//////////////////////////////////////////////////////////
void pass_text(lua_State *L, std::string text, int wrap, float halign, float valign)
{
lua_getglobal(L, "pass_text");
if (lua_isfunction(L, -1))
{
lua_pushstring(L, text.c_str()); // count 1
lua_pushnumber(L, float(wrap)); // 2
lua_pushnumber(L, float(halign)); // 3
lua_pushnumber(L, float(valign)); // 4: this is the 4 used in the next line
lua_pcall(L, 4, 0, 0);
}
}
/////////////////////////////////////////////////////////
int load(lua_State *L)
{
lua_pushstring(L, "load");
return 1;
}
int update(lua_State *L)
{
lua_pushstring(L, "update");
return 1;
}
int draw(lua_State *L)
{
lua_pushstring(L, "draw");
pass_text(L, "Hello World.", 0, 1.7f, -5);
return 1;
}
///////////////////////////////////////////////////////////
extern "C"
{
int luaopen_cppbindings(lua_State* L)
{
const struct luaL_Reg cmds[] = {
{"load", load},
{"update", update},
{"draw", draw},
{NULL, NULL}
};
luaL_newlib(L, cmds);
return 1;
}
} Build "LOVR" and use this as the "main.lua" file: local cppbinding = require 'cppbindings'
local result
local passlink
local dtlink
function pass_text(Text, Wrap, halign, valign)
passlink:text(Text, Wrap, halign, valign)
end
function lovr.load()
result = cppbinding.load()
end
function lovr.update(dt)
dtlink = dt
result = cppbinding.update()
end
function lovr.draw(pass)
passlink = pass
result = cppbinding.draw()
end |
Beta Was this translation helpful? Give feedback.
-
Nice! A small tip to make multi-line codeblocks more readable: Use three backticks (```) surrounding the piece of code:
it'll look like this:
You can even put a language after the first three backticks, like -- start this codeblock with ```lua
function lovr.draw(pass)
pass:text(a, 0, 1.7, -5)
end |
Beta Was this translation helpful? Give feedback.
-
Here is a barebone example of creating a plugin for LOVR.
Inside the plugin folder create a new folder called "lovr-cppbase"
Inside the new folder create two files. The first file is "CMakeLists.txt":
The second file is "cppbase.cpp":
Build "LOVR" and use this as the "main.lua" file:
Beta Was this translation helpful? Give feedback.
All reactions