[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: about luaffi
- From: Martin Krpan <wtxnh-lua@...>
- Date: Sat, 15 Sep 2012 23:15:17 +0200
On Thu, Sep 13, 2012 at 07:08:32PM -0400, James McKaskill wrote:
> On Thu, Sep 13, 2012 at 6:57 PM, Martin Krpan <wtxnh-lua@yahoo.com.au> wrote:
> > How can I pass table (for example) to my Lua callback function?
>
> You need to create a struct e.g. ffi.new('struct {int x; int y;}',
> {x=2, y=3}). Otherwise the ffi has no idea how to marshal it. I must
> admit the error could be better. It's actually erroring on a different
> problem that comes up when you try and set a pointer directly to a
> table, the ffi struct will be immediately gc'd.
>
> > And how do I dereference void* pointer back to lua table in callback function?
>
> You cast back to a ffi struct and then manually unpack.
That works for table as mentioned above. I guess if table contains
another table, or function, or even some userdata such conversion is not
possible? Anyway I recalled that I can pass any data to callback
function with an upvalue or global variable, so this is not big deal.
I got another error when I tried to use SDL_ttf library.
There is a function with this signature:
SDL_Surface * TTF_RenderUTF8_Solid(TTF_Font *font, const char *text, SDL_Color fg);
typedef struct SDL_Color {
Uint8 r;
Uint8 g;
Uint8 b;
Uint8 unused;
} SDL_Color;
When I call that function this error shows:
lua: ttt.lua:24: NYI: call arg type
stack traceback:
[C]: ?
ttt.lua:24: in main chunk
[C]: ?
Example script run fine with luajit2. I guess that there is not much
that can be done as it seems that something is Not Yet Implemented.
Bye
Martin
*** ttt.lua ***
local ffi = require 'ffi'
if jit then NULL = nil else NULL = ffi.C.NULL end
local sdl = require 'inc/sdl'
local SDL = sdl.SDL
local TTF = sdl.TTF
SDL.SDL_Init(SDL.SDL_INIT_EVERYTHING);
local screen = SDL.SDL_SetVideoMode(320,
240,
16,
SDL.SDL_SWSURFACE);
TTF.TTF_Init()
local fn_name = 'fonts/comic.ttf'
local font = TTF.TTF_OpenFont(fn_name, 32)
if (font == NULL) then
print('Loading font error:', ffi.string(SDL.SDL_GetError()))
end
local text = "Hello world!"
local fg = ffi.new('SDL_Color')
fg.r=47
fg.g=140
fg.b=180
pic_a = TTF.TTF_RenderUTF8_Solid(font, text, fg) -- this is line 24
local event=ffi.new('SDL_Event')
local rect=ffi.new('SDL_Rect')
local format = screen.format
local color = SDL.SDL_MapRGB(format, 57, 22, 28)
SDL.SDL_FillRect(screen, nil, color)
rect.x=10
rect.y=0
sdl.BlitSurface(pic_a, nil, screen, rect)
SDL.SDL_Flip(screen)
SDL.SDL_Delay(2000)
TTF.TTF_Quit()
SDL.SDL_Quit()
*** end of ttt.lua ***