在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):danomatika/ofxLua开源软件地址(OpenSource Url):https://github.com/danomatika/ofxLua开源编程语言(OpenSource Language):C++ 94.4%开源软件介绍(OpenSource Introduction):ofxLuaA Lua instance addon Copyright (c) Dan Wilcox 2011-2020 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. DescriptionofxLua 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 openFrameworks is a cross platform open source toolkit for creative coding in C++. Why not Javascript?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: Build RequirementsTo 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. Installation and BuildPlace ofxLua within a folder in the apps folder of the OF directory tree:
The easiest way to do this is via cloning with git:
You'll need to checkout the swig-openframeworks submodule as well using:
Which version to use?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,
will checkout a version that's compatible with OF 0.11.0. DependenciesFor embedded Linux (arm, Raspberry Pi, etc), LuaJIT is used for better performance. Make sure you have the luajit-5.1 development package installed. Visual StudioIf 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! Android StudioContributed by Zach Lee In Android Studio, the
Next, to make Android Studio read the C flags from the
After the fix, the ofxLua project should hopefully build and run on an Android device. Running the Example ProjectsThe example projects are located in the 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. macOSOpen the Xcode project, select the "luaExample Debug" scheme, and hit "Run". LinuxOpen 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:
How to Create a New ofxLua ProjectProjectGeneratorSimply select ofxLua from the available addons in the ProjectGenerator before generating a new project. Manual MethodTo 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:
It must be 3 levels down in the openframeworks folder structure. Then after renaming:
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. Adding ofxLua to an Existing ProjectProjectGeneratorSelect 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. Manual MethodIf you want to add ofxLua to another project, you need to make sure you add the following src files:
and optionally
You also need to add the Lua library files in the libs directory:
For Xcode:
On 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:
OF API BindingsSWIG generated bindings for the OF API can be found in Basic documentation:There is a main "of" module and functions, classes, constants, & enums are renamed:
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. glmAs 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:
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:
See Math & StringThe basic string and math functions are provided by built-in Lua libraries: Other standard Lua libraries are: table, io, and os. Comparing Key ValuesOF uses integers for the key event values so comparisons can be made using character literals in C++:
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:
Working with Class InstancesCalling class member functions requires using a : character -> image:draw(20, 100, 100, 100) Calling class member variables requires using a . character -> print(image.width) Mixing up : and . is probably the most common pitfall when coming from C++ to Lua:
DetailsTo see the detailed differences with the OF C++ API run the following:
To see work to be done on the bindings run:
ClassesSimple Lua class support is provided by the class() function from the Lua Users wiki:
This implementation allows for inheritance and usage is as follows:
Checking TypesLua comes with the built-in For more detailed type info for objects wrapped by SWIG, use the special
will print the following:
Making Your Own BindingsSWIG InterfaceCreate a SWIG interface file (*.i) with includes headers for the functions and classes which you want to bind. You then run SWIG with this file to generate the *.cpp wrapper. It could be as simple as the following:. MyCode.h, your custom code:
MyBindings.i, your custom SWIG interface file:
That's it, SWIG will handle the rest! Of course this is a simple example but there are lots more options for specific bindings settings such as generating properties from getters & setters, etc. Generate .cpp WrapperNow call SWIG to generate your .cpp wrapper:
Make sure to add search paths to headers used by your code (aka the "-I" line in the command above). If all went well, SWIG will have generated the Opening Your Lua LibraryYou will need to open your new Lua library provided by the SWIG-generated .cpp file in order to use it in your lua state. SWIG creates a "luaopen" C function using your module name which, in this case, will be "luaopen_my". This function needs to be defined in C++ in order to be used, so add it to the top of the .cpp file where you initialize your ofxLua object:
Then call this function after initing ofxLua:
Using Your Lua LibraryIf everything is working, you should be able to call your bindings in Lua using your new "my" module:
See the SWIG interface file in If you end up having lots of custom code to bind, it's recommended to create multiple SWIG interface files which are included into a single *.i using the %include command. Do not create separate files with the same module name, only set the module in the main file as SWIG is designed for 1 module per main interface. Do not open issues or bug reports if the problem is in writing your own bindings as this is all handled by SWIG. Be sure to search online for similar errors with "swig" as part of your search. More likely than not, it's an issue with your bindings and not with ofxLua. Debugging Symbols and SyntaxWhen writing bindings, it's often helpful to see what C++ symbols (classes, functions, etc) are being bound to Lua by SWIG. You have SWIG generate a list of language symbols it sees while processing using the
This file can also be used as the basis for generating a list of keywords for autocompletion. For instance, the
This generates a Lua ModulesFor platforms that support it, an ofxLua project can load Lua dynamic modules via Lua's For info on building your own Lua modules, see Lua requireLua's
scriptA.lua:
require will fail since the current working directory is not The easiest fix for this is to change the current working directory of the app to the directory of the script which is calling require. Setting the changeDir argument to true when calling ofxLua::doScript() will change the directory for you:
This will not effect the OF data path. Building with LuaJITBuilding with LuaJIT instead of Lua simply requires ignoring the Lua sources in This is handled automatically in the for embedded Linux in the ofxLua addon_config.mk. For macOS, install Luajit using Homebrew:
Uncomment the commented osx lines in the ofxLua/addon_config.mk and (re)generate your project using the OF ProjecGenerator. Now build. DevelopingYou can help develop ofxLua on GitHub: https://github.com/danomatika/ofxLua Create an account, clone or fork the repo, then request a push/merge. If you find any bugs or suggestions please log them to GitHub as well. Known IssuesofxLuaBindings.cpp fails to compile on Linux ARM (RPI 3B+)(Contributed by Steven Noreyko) The single OF API ofxLuaBindings.cpp bindings file generated by SWIG is relatively large. Some embedded systems may not have enough memory to build it completely resulting in strange errors with the compiler always stopping on
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论