lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

... but this is a really VERY complex question. This is somehow the
base of any graphical system, e. g. you might have a look at
MS-Windows GDI functions or GDI++ ... just the problem with Windows
software is, that many example programs there are written in C++, and
then many things somehow "work by witchcraft", as long as you do not
understand the class system completely (GDI++ unfortunately nearly
impossible to program by C, you have to use C++...).

But combining your question with performance issues from the beginning
is also I think too hard for a "start point"... better start first
with simple examples (but the task must be very clear then..), then
get this working, and then try to optimize this further if you
want/can/do ... (but do not forget Roberto's two very smart base rules
in from the Lua Gem's book concerning optimization: Rule no. 1: Don't
do it, and Rule No. 2: Still don't do it :) ).

On Wed, Nov 10, 2021 at 9:56 AM Patrick Kurth <> wrote:
> Hello list,
> I'm writing an application that deals with shapes, so there are objects
> such as rectangles, polygons, paths etc. These shapes also manage
> points which make up the shape. In the beginning I implemented
> everything as lua tables, so something like
> local rectangle = {
>     local points = {
>         bl = { x = -10, y = -10 },
>         tr = { x =  10, y =  10 }
>     }
> }
> with an appropriate set of metatables for object methods.
> At some point I recognized some performance issues, so I started to
> implement some of this stuff in C (for instance, points are now
> userdata). Furthermore, I have C code that does not need to interact
> with lua, so it is a little bit silly to use the lua memory managment
> unit for that.
> I started to change the entire object system to write everything in C,
> but I failed at figuring out how to do this structure. My main problem
> is that, occasingly, I have to pass objects to lua. What is the best
> way to represent a structure such as my rectangle example in C? The
> definition in C is something like this:
> struct point
> {
>     int x, y;
> };
> struct rectangle
> {
>     point bl;
>     point tr;
> };
> What is the best approach here regarding memory and lifetime managment?
> In short: How to do nested userdata?
> My current Idea would be to use a proxy (userdata) which encapsulates
> the C data and is resolved on the C side.
> Thank you, kind regards,
> Patrick