Browse by type

A Lua instance addon
Copyright (c) Dan Wilcox 2011-2023
BSD Simplified License.
For information on usage and redistribution, and for a DISCLAIMER OF ALL WARRANTIES, see the file, "LICENSE.txt," in this distribution.
See https://github.com/danomatika/ofxLua and the Openframeworks Forum post for documentation
If you want to use Lua + openFrameworks without building your own app, check out loaf: danomatika.com/code/loaf.
This project has been supported by the CMU Frank-Ratchey STUDIO for Creative Inquiry, the DU Emergent Digital Practices Program, and my time at the ZKM | Hertz-Lab.
ofxLua is an openFrameworks addon for running a Lua embedded scripting interpreter within an openFrameworks application. Using the SWIG (Simple Wrapper and Interface Generator) tool, C++ functions and classes can be bound to the Lua api allowing them to be called within a Lua script. This is useful in separating the upper level logic from the lower level application and is utilized in numerous video games and applications.
In addition, ofxLua provides bindings for the openFrameworks API.
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode for a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. It is used to generate the C++ bindings which wrap the openFrameworks API for Lua.
(Optional) LuaJIT is a Just-In-Time Compiler (JIT) for the Lua programming language. It implements the Lua API but is optimized for performance over the standard Lua distribution. It is recommended to use LuaJIT when speed is a concern and it is enabled on embedded Linux in addon_config.mk for this reason.
openFrameworks is a cross platform open source toolkit for creative coding in C++.
Those coming from a Javascript background may ask "Why Lua? Why not Javascript? I like Javascript!"
Lua has a long history of use as a embedded scripting language and, as such, is both smaller & much easier to embed on pretty much every platform. The compiled language adds less than 500kb to your app binary versus many Mbs for Javascript or Python. Speed-wise, the base Lua interpreter is oftentimes faster than either language due to it's simplicity and using LuaJIT may add a speed increase of many orders of magnitude. As an embedded language, Lua makes it relatively easy to bind C/C++ functions & objects to it's scripting environment.
For these reasons, Lua has been used for game development for many years:
Those coming from an embedded computing or game development background are probably familiar with Lua while those coming from design and/or web development are used to Javascript. In many ways, both languages share a number of similarities and the rest is due to simple syntax or design differences. When it comes down to it, no one language or environment is better than another, they just have different focuses and design backgrounds. Do not dismiss Lua because you are unfamiliar with it as not every nail needs the same hammer.
Lua is not scary, trust me :)
See:
To use ofxLua, first you need to download and install openFrameworks. The examples are developed against the latest release version of openFrameworks on http://openframeworks.cc/download.
Currently, ofxLua is being developed on macOS and has been tested on macOS, iOS, & Linux. Android should work but has not been tested extensively.
Place ofxLua within a folder in the apps folder of the OF directory tree:
openframeworks/addons/ofxLua
The easiest way to do this is via cloning with git:
cd openframeworks/addons/
git clone git://github.com/danomatika/ofxLua.git
You'll need to checkout the swig-openframeworks submodule as well using:
cd ofxLua
git submodule init
git submodule update
The master branch of ofxLua will work with the current stable version of openFrameworks and can be considered relatively stable.
Previous versions are tagged using Semantic Versioning with the updates to newer versions of openFrameworks and Lua noted in the changelog, CHANGES.txt. You can select the tag in the Github "Current Branch" menu or clone and check it out using git.
If you want to use ofxLua with a previous version of openFrameworks, find the tag corresponding to your OF version by looking at the changelog or releases. Note that the ofxLua tag and OF version do not match.
For example,
git clone git://github.com/danomatika/ofxLua.git
cd ofxLua
git checkout 1.3.0
will checkout a version that's compatible with OF 0.11.0.
For embedded Linux (arm, Raspberry Pi, etc), LuaJIT is used for better performance. Make sure you have the luajit-5.1 development package installed.
If you run into compilation issues in Visual Studio (ie. "unresolved external symbol" errors with "_lua_*" library functions), you may need to tell the VS to build the Lua library using the C compiler instead of the C++ compiler:
In the Solution Explorer:
If there is an alternate solution to this issue in the addons_config.mk, please let us know!
Contributed by Zach Lee
In Android Studio, the addon_config.mk exclude flags may not have an effect when using the OF ProjectGenerator app, and the following platform-specific bindings folders may need to be manually removed from the AS project:
Next, to make Android Studio read the C flags from the addon_config.mk file, add the following code below cppFlags.addAll(addonCppFlags(abi, ofRoot())) in build.gradle file:
CFlags.addAll(addonCppFlags(abi, ofRoot()))
After the fix, the ofxLua project should hopefully build and run on an Android device.
The example projects are located in the luaExample & luaExampleIOS folders.
Project files for the examples are not included so you will need to generate the project files for your operating system and development environment using the OF ProjectGenerator which is included with the openFrameworks distribution.
To (re)generate project files for an existing project:
If everything went Ok, you should now be able to open the generated project and build/run the example.
Open the Xcode project, select the "luaExample Debug" scheme, and hit "Run".
Open the Code::Blocks .cbp and hit F9 to build. Optionally, you can build the example with the Makefile.
To build and run it on the terminal:
make
make run
Simply select ofxLua from the available addons in the ProjectGenerator before generating a new project.
To develop your own project based on ofxLua, simply copy an example project and rename it. You probably want to put it in your apps folder, for example, after copying:
openFrameworks/addons/ofxLua/example/ => openFrameworks/apps/myApps/example/
It must be 3 levels down in the openframeworks folder structure.
Then after renaming:
openFrameworks/apps/myApps/myLuaProject/
On Mac, rename the project in Xcode (do not rename the .xcodeproj file in Finder!): Long click on the project name in the project tree.
Select ofxLua and other addons used by your project from the available addons in the ProjectGenerator, select the parent folder of your project, and set the exact name of the existing project in the text box. This will overwrite the existing project files with new ones that now include ofxLua.
Note: you will lose any custom settings you've added manually to your project.
If you want to add ofxLua to another project, you need to make sure you add the following src files:
openFrameworks/addons/ofxLua/src/ofxLua.h
openFrameworks/addons/ofxLua/src/ofxLua.cpp
openFrameworks/addons/ofxLua/src/ofxLuaFileWriter.h
openFrameworks/addons/ofxLua/src/ofxLuaFileWriter.cpp
and optionally
openFrameworks/addons/ofxLua/src/bindings/ofBindings.h
openFrameworks/addons/ofxLua/src/bindings/YOURPLATFORM/ofBindings.cpp
openFrameworks/addons/ofxLua/src/bindings/glmBindings.h
You also need to add the Lua library files in the libs directory:
openFrameworks/addons/ofxLua/libs/lua
src/bindings/desktop for iOSOn older macOS versions (pre 10.8), a header file which is included with the OS contains some macros which conflict with several lua macros. They can be renamed by setting this CFLAG:
-D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0
SWIG generated bindings for the OF API can be found in src/bindings. Currently it covers most of the api while leaving out base classes. More specific documentation may come at a future date, but for now check the example scripts on usage.
There is a main "of" module and functions, classes, constants, & enums are renamed:
begin & end: these are lua keywords, so functions with this name are renamed in the following classes:Base classes, deprecations, variable arguments (...), ofThread, ofPtr, ofMutex, & ofScopedLock are ignored for now.
Functions that return a std::vector return a wrapped std::vector in Lua. As with Lua tables, indexes start at 1.
As of OF 0.10.0, there is also a "glm" module for the glm types and math functions. Note that the OF math types cannot be implicitly cast to glm types in Lua as they are in C++, so you need to use special conversion functions:
-- error!
local v = of.Vec2f(100, 100)
of.drawRectangle(v, 20, 20) -- needs a glm::vec2
-- convert
of.drawRectangle(v:vec2(), 20, 20) -- ofVec2f -> glm::vec2
-- or use the ofVec2f attributes directly
of.drawRectangle(v.x, v.y, 20, 20)
It looks as those ofVec*, ofMatrix*, and ofQuaternion may be deprecated in the future, so it's probably best to transition to using glm::vec*, glm::mat*, and glm::quat over time:
-- using glm::vec2
local v = glm.vec2(100, 100)
of.drawRectangle(v, 20, 20)
See swig/README.txt for details.
The basic string and math functions are provided by built-in Lua libraries:
Other standard Lua libraries are: table, io, and os.
OF uses integers for the key event values so comparisons can be made using character literals in C++:
if(key == 'f') {
ofToggleFullscreen();
}
else if(key == OF_KEY_UP) {
ofLog() << "up pressed";
}
Lua does not have character literals so 'f' is treated as a string. In order to do the same comparison, convert 'f' to a number using the string library string.byte function:
if key == string.byte("f") then
of.toggleFullscreen()
elseif key == of.KEY_UP then
print("up pressed")
end
Calling class member functions requires using a : character -> image:draw(20, 100, 100, 100)
Calling class member variables require
$ claude mcp add ofxLua \
-- python -m otcore.mcp_server <graph>