How much C++ really improves Love?
Posted: Sun Apr 11, 2021 10:42 pm
~We're (no) strangers to C++, you know the benefits but sure don't I
After this silly introduction, I want to say hello everybody: this is my first topic ever in this forum!
It's been a year since I discovered this awesome framework, and I decided to join the community one month ago(~Feb 2021) to work on the wiki's italian translation(cosider it my way to say thanks).
But let's jump into it!
A little bit of context
Lately, I've been working hard on a Graphic User Interface library+engine for Love. I went to the point where I coded all of the basic components (e.g. AABB class) in Lua.
I was ready to move on to the implementation of the library itself, but a critical dilemma came up to my mind.
When I figured out that C and C++ could be used to integrate Lua fairly easily, I really couldn't ignore this opportunity; being the project in a early stage of development, I have no excuses for not even considering the idea.
So I decided to dig deeper in the subject... keep in mind that I'm a total noob about how C/C++ developers actually work.
Slowly descending a hell of confusion
I decided to pick the AABB class as test case, since it will be used in many ways in the library.
The first feature to accomplish a this "improvement" I tried was LuaJit's FFI. I admit that this introduction here https://luajit.org/ext_ffi.html, really catched my interest.
My first approach was to simply put the viewport properties x,y,with,height inside a C struct and let the AABB use a instance of this c object.
Well that's cool, it means that now a AABB instance uses a lot less memory but... is it really the improvement I'm looking for?
I mean, this object must be fast at computing intersections with its siblings.
Playing with ffi I noticed that it can run custom C functions, but they must be defined in a shared library so the module can load them.
Well, I grabbed my rusty knowledge about C and tried coding the entire AABB type.
With no oop it was quite a pain, but I managed to code the set of functions needed with the help of pointers.
Good, now let's wrap this into a Lua class and test it.
Love2D ingloriously crashes.
Long story short, managing memory (using malloc(), free()) inside the C given to FFI is a big nono.
At this point my frustration was already building up, but that was ok, I had to try the last option yet: writing a C++ module that could be loaded directly from require.
And now we're coming to the epilogue
Thank you for your patience, I promise this is almost it.
Here my ignorance about how a compiler works, the differences between compilers etc. made me wasted a lot of time, building up frustration and desperation.
To build the shared libraries (.so files) to use in FFI I used g++, that was fine.
Understanding how to include Lua source on the other hand, with no clear tutorials was a nightmare; the worst thing is that everybody makes it like it is the easiest thing in the world.
Finally I installed Visual Studio since I have Windows on my machine, and found out this video by this blessed man.
https://youtu.be/4l5HdmPoynw.
After failing in the attempt to make a dll with the help of Visual Studio, I decided that the best thing to do was to ask directly to the Love community to stop this unhealthy race.
At this point you probably understood my condition, so it's time for the question.
1. Is it really worthy to write a module in C++, instead of Lua in the context of Love? If so, any good suggestions on how to actually do it?
2. Was I wrong on my considerations about FFI? Is a simple change like the first one I described for AABB actually a significant improvement?
Thank you again for your attention, if you need any information in particular feel free to ask!
After this silly introduction, I want to say hello everybody: this is my first topic ever in this forum!
It's been a year since I discovered this awesome framework, and I decided to join the community one month ago(~Feb 2021) to work on the wiki's italian translation(cosider it my way to say thanks).
But let's jump into it!
A little bit of context
Lately, I've been working hard on a Graphic User Interface library+engine for Love. I went to the point where I coded all of the basic components (e.g. AABB class) in Lua.
I was ready to move on to the implementation of the library itself, but a critical dilemma came up to my mind.
When I figured out that C and C++ could be used to integrate Lua fairly easily, I really couldn't ignore this opportunity; being the project in a early stage of development, I have no excuses for not even considering the idea.
So I decided to dig deeper in the subject... keep in mind that I'm a total noob about how C/C++ developers actually work.
Slowly descending a hell of confusion
I decided to pick the AABB class as test case, since it will be used in many ways in the library.
The first feature to accomplish a this "improvement" I tried was LuaJit's FFI. I admit that this introduction here https://luajit.org/ext_ffi.html, really catched my interest.
My first approach was to simply put the viewport properties x,y,with,height inside a C struct and let the AABB use a instance of this c object.
Well that's cool, it means that now a AABB instance uses a lot less memory but... is it really the improvement I'm looking for?
I mean, this object must be fast at computing intersections with its siblings.
Playing with ffi I noticed that it can run custom C functions, but they must be defined in a shared library so the module can load them.
Well, I grabbed my rusty knowledge about C and tried coding the entire AABB type.
With no oop it was quite a pain, but I managed to code the set of functions needed with the help of pointers.
Good, now let's wrap this into a Lua class and test it.
Love2D ingloriously crashes.
Long story short, managing memory (using malloc(), free()) inside the C given to FFI is a big nono.
At this point my frustration was already building up, but that was ok, I had to try the last option yet: writing a C++ module that could be loaded directly from require.
And now we're coming to the epilogue
Thank you for your patience, I promise this is almost it.
Here my ignorance about how a compiler works, the differences between compilers etc. made me wasted a lot of time, building up frustration and desperation.
To build the shared libraries (.so files) to use in FFI I used g++, that was fine.
Understanding how to include Lua source on the other hand, with no clear tutorials was a nightmare; the worst thing is that everybody makes it like it is the easiest thing in the world.
Finally I installed Visual Studio since I have Windows on my machine, and found out this video by this blessed man.
https://youtu.be/4l5HdmPoynw.
After failing in the attempt to make a dll with the help of Visual Studio, I decided that the best thing to do was to ask directly to the Love community to stop this unhealthy race.
At this point you probably understood my condition, so it's time for the question.
1. Is it really worthy to write a module in C++, instead of Lua in the context of Love? If so, any good suggestions on how to actually do it?
2. Was I wrong on my considerations about FFI? Is a simple change like the first one I described for AABB actually a significant improvement?
Thank you again for your attention, if you need any information in particular feel free to ask!