Page 1 of 1
Can I use C++ code in my game?
Posted: Wed Aug 20, 2014 2:00 pm
by SNK1337
Hi everyone,
I recently read a bit about how to write C++ code that is usable within Lua
here. I know that LÖVE itself is an example of C++ code that is used in Lua, however I have a question: Can LÖVE only handle pure Lua code, or could I, for example, write a vector library in C++ and use it within my Lua code, which I then open with LÖVE? If so, how can I include and use this C++ code?
Note: I don't intend to call any of LÖVE's functions in the C++ code, only create new functions to be used in my Lua code.
Re: Can I use C++ code in my game?
Posted: Wed Aug 20, 2014 2:28 pm
by Plu
Löve is merely a framework; in the end it's still running Lua, which means you have access to everything Lua can do. Including the option to run C++ code from libraries you make.
Re: Can I use C++ code in my game?
Posted: Wed Aug 20, 2014 3:06 pm
by DelishusCake
Of course you can! Just grab the love c++ source from
BitBucket and get coding! Build instructions are
on the wiki if you need help with dependencies or whatever.
Re: Can I use C++ code in my game?
Posted: Wed Aug 20, 2014 3:09 pm
by SNK1337
Plu wrote:Löve is merely a framework; in the end it's still running Lua, which means you have access to everything Lua can do. Including the option to run C++ code from libraries you make.
Alright! I'm very new to all this, so could you maybe give me a quick rundown of the whole process of writing a function in C++, getting it into the Lua code and running that with LÖVE?
Re: Can I use C++ code in my game?
Posted: Wed Aug 20, 2014 10:36 pm
by OmarShehata
SNK1337 wrote:Plu wrote:Löve is merely a framework; in the end it's still running Lua, which means you have access to everything Lua can do. Including the option to run C++ code from libraries you make.
Alright! I'm very new to all this, so could you maybe give me a quick rundown of the whole process of writing a function in C++, getting it into the Lua code and running that with LÖVE?
That would be really nice to know, if everyone can write that up!
Re: Can I use C++ code in my game?
Posted: Thu Aug 21, 2014 12:42 pm
by bartbes
I'd suggest against modifying love unless you need to interface with love's internals. And if you don't modify love, it's no different than using normal lua.
Re: Can I use C++ code in my game?
Posted: Thu Aug 21, 2014 1:50 pm
by kikito
This is slightly out of my knowledge zone, but since LuaJIT has
FFI you can probably implement a vector library in C directly on the .lua source of your game, without having to recompile LÖVE.
Or maybe do a hybrid implementation where some methods are implemented in C and others in Lua, using a C structure (passing the Lua -> C -> Lua barrier has a cost).
In any case, give a look at vrld's
vector-light.lua. It's a plain-lua implementation, but it should go pretty fast, especially in LuaJIT. At least use it as a benchmark for your implementation - multiply 10k vectors with both libs and see if yours is really faster.
Re: Can I use C++ code in my game?
Posted: Sat Aug 23, 2014 1:06 pm
by SNK1337
kikito wrote:This is slightly out of my knowledge zone, but since LuaJIT has
FFI you can probably implement a vector library in C directly on the .lua source of your game, without having to recompile LÖVE.
Or maybe do a hybrid implementation where some methods are implemented in C and others in Lua, using a C structure (passing the Lua -> C -> Lua barrier has a cost).
In any case, give a look at vrld's
vector-light.lua. It's a plain-lua implementation, but it should go pretty fast, especially in LuaJIT. At least use it as a benchmark for your implementation - multiply 10k vectors with both libs and see if yours is really faster.
FFI looks interesting, thanks for the link. Some things are still a bit unclear to me though, so I'd love if someone could just briefly write up the whole process of making a C++ function and using it in Lua with FFI. Also, could you expand on the hybrid implementation? I'm very new to this, and I think that's what I'd like to do, but I'm not sure if I completely understand.
By the way, I've already written a vector library in Lua which I'm very happy with (I can even use operators with vector tables!
). The vector library was just an example, and I'd mostly like to include some C++ for training, the potential performance boost is secondary.
Re: Can I use C++ code in my game?
Posted: Sat Aug 23, 2014 1:47 pm
by kikito
All I know is on the FFI link I sent. I have personally never used it.
Note that this won't allow you to "use C++". It will allow you to use (a subset of) C, as far as I know, but that's it.
Re: Can I use C++ code in my game?
Posted: Sat Aug 23, 2014 3:47 pm
by SNK1337
kikito wrote:All I know is on the FFI link I sent. I have personally never used it.
Note that this won't allow you to "use C++". It will allow you to use (a subset of) C, as far as I know, but that's it.
Oops, I mean C functions in the case of FFI. Regardless, could you go a bit into detail about the hybrid implementation you were talking about?