Can I use C++ code in my game?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
SNK1337
Prole
Posts: 16
Joined: Sun Jan 05, 2014 2:30 pm

Can I use C++ code in my game?

Post by SNK1337 » Wed Aug 20, 2014 2:00 pm

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.

User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: Can I use C++ code in my game?

Post by Plu » Wed Aug 20, 2014 2:28 pm

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.

DelishusCake
Prole
Posts: 8
Joined: Sat Mar 10, 2012 8:31 pm

Re: Can I use C++ code in my game?

Post by DelishusCake » Wed Aug 20, 2014 3:06 pm

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.

SNK1337
Prole
Posts: 16
Joined: Sun Jan 05, 2014 2:30 pm

Re: Can I use C++ code in my game?

Post by SNK1337 » Wed Aug 20, 2014 3:09 pm

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?

User avatar
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?

Post by OmarShehata » Wed Aug 20, 2014 10:36 pm

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!

User avatar
bartbes
Sex machine
Posts: 4944
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Can I use C++ code in my game?

Post by bartbes » Thu Aug 21, 2014 12:42 pm

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.

User avatar
kikito
Inner party member
Posts: 3152
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Can I use C++ code in my game?

Post by kikito » Thu Aug 21, 2014 1:50 pm

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.
When I write def I mean function.

SNK1337
Prole
Posts: 16
Joined: Sun Jan 05, 2014 2:30 pm

Re: Can I use C++ code in my game?

Post by SNK1337 » Sat Aug 23, 2014 1:06 pm

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! :D). The vector library was just an example, and I'd mostly like to include some C++ for training, the potential performance boost is secondary.

User avatar
kikito
Inner party member
Posts: 3152
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: Can I use C++ code in my game?

Post by kikito » Sat Aug 23, 2014 1:47 pm

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.
When I write def I mean function.

SNK1337
Prole
Posts: 16
Joined: Sun Jan 05, 2014 2:30 pm

Re: Can I use C++ code in my game?

Post by SNK1337 » Sat Aug 23, 2014 3:47 pm

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?

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 12 guests