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.
Can I use C++ code in my game?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
Re: Can I use C++ code in my game?
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.
-
- Prole
- Posts: 8
- Joined: Sat Mar 10, 2012 8:31 pm
Re: Can I use C++ code in my game?
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?
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?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.
- OmarShehata
- Party member
- Posts: 259
- Joined: Tue May 29, 2012 6:46 pm
- Location: Egypt
- Contact:
Re: Can I use C++ code in my game?
That would be really nice to know, if everyone can write that up!SNK1337 wrote: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?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.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: Can I use C++ code in my game?
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Can I use C++ code in my game?
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.
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.
When I write def I mean function.
Re: Can I use C++ code in my game?
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.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.
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.
- kikito
- Inner party member
- Posts: 3153
- Joined: Sat Oct 03, 2009 5:22 pm
- Location: Madrid, Spain
- Contact:
Re: Can I use C++ code in my game?
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.
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.
When I write def I mean function.
Re: Can I use C++ code in my game?
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?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.
Who is online
Users browsing this forum: Google [Bot] and 7 guests