C++/C in Love2D?

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
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

C++/C in Love2D?

Post by louie999 »

Will it be possible to run C++/C(more preferably C++) with Love2D ? If so then how?

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: C++/C in Love2D?

Post by Nixola »

You can use C via LuaJIT's FFI lib, or use C++ if you make a library yourself and build it to a .so/dll/dylib file.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
louie999
Prole
Posts: 46
Joined: Fri Mar 06, 2015 9:01 am

Re: C++/C in Love2D?

Post by louie999 »

Ah ok, C is good too. Though I have another question, how can I call a lua function from C ? like call love.graphics.setColor from C, how will I be able to do it ?

Edit:
I tried this:

Code: Select all

ffi = require "ffi"

ffi.cdef[[ #include <stdio.h>
    int main()
	{
		printf("Hi");
	}
]]
But it resulted in this error:

Code: Select all

unexpected symbol near 'include'
o.O

Code: Select all

fun = true
school = true

function isItFun()
    if school then
       fun = false
    end
    if not fun then 
       me:explode()
    end
end
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: C++/C in Love2D?

Post by Nixola »

Apparently you don't include things in the FFI. By googling I found out that this is how you should be able to use printf:

Code: Select all

local ffi = require "ffi"

ffi.cdef[[
int printf(const char *fmt, ...);
]]

ffi.C.printf("Hi\n")
This link explains something (not much actually) about the FFI.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: C++/C in Love2D?

Post by T-Bone »

Why would you want this? At least for me, the big advantage of Löve is that you get to code in Lua. There are plenty of great C/C++ game frameworks out there.
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: C++/C in Love2D?

Post by Nixola »

Lua is great, but sometimes a C library does exactly what you want and it'd be a pity not to use it, or you may need pretty intensive math which is much faster in C. It has its uses. You don't use only C with the FFI, but you can easily use it along Lua.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
arampl
Party member
Posts: 248
Joined: Mon Oct 20, 2014 3:26 pm

Re: C++/C in Love2D?

Post by arampl »

louie999 wrote:Though I have another question, how can I call a lua function from C ? like call love.graphics.setColor from C, how will I be able to do it ?
T-Bone wrote:Why would you want this? At least for me, the big advantage of Löve is that you get to code in Lua. There are plenty of great C/C++ game frameworks out there.
+1. Functions like "love.graphics.setColor" is already written in C++ in LÖVE sources to be used from Lua. You want something like installing VirtualBox in Windows to install Linux with its own VirtualBox again.

If you talking about calling Lua functions from C/C++ in general then you should read about embedding Lua in PIL. It's not very complicated.
Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests