lua-users home
lua-l archive

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


Hello everyone!

I'm trying to make a game with Lua and Allegro, a game graphics/sound/input/etc. library.

Allegro has a struct called "BITMAP", which I'm trying to share with my Lua scripts.

Here's the current code:

--------------------------------------
int BITMAP_load(lua_State *L)
{
	const char *file = luaL_optstring(L, 1, "\0");
	
	BITMAP *bmp = (BITMAP*)lua_newuserdata(L, sizeof(BITMAP));
	BITMAP *bmp2 = load_bitmap(file, 0);
	
	if (bmp2 == NULL) error("Cannot load image!");
	
	memcpy(bmp, bmp2, sizeof(BITMAP));
	
	luaL_getmetatable(L, "Bitmap");
	lua_setmetatable(L, -2);
	
	destroy_bitmap(bmp2);
	
	return 1;
}
--------------------------------------

Am I doing it right? Doesn't seem like it. Here's my GDB output:
(hope it's clear enough, I stripped some print statements)

--------------------------------------
(gdb) run
Starting program: E:\Projects\C\LUATES~1\bin/luatest.exe

Breakpoint 1, BITMAP_load (L=0x3d9cf0) at bitmap_Lua.c:70
70              const char *file = luaL_optstring(L, 1, "\0");
(gdb) step
72              BITMAP *bmp = (BITMAP*)lua_newuserdata(L, sizeof(BITMAP));
(gdb) step
73              BITMAP *bmp2 = load_bitmap(file, 0);
(gdb) step
75              if (bmp2 == NULL) error("Cannot load image!");
(gdb) step
77              memcpy(bmp, bmp2, sizeof(BITMAP));
(gdb)
79              luaL_getmetatable(L, "Bitmap");
(gdb) step
80              lua_setmetatable(L, -2);
(gdb)
82              destroy_bitmap(bmp2);
(gdb)
84              return 1;
(gdb) step
85      }
(gdb)
0x00412cd7 in luaD_precall ()
(gdb)
Single stepping until exit from function luaD_precall,
which has no line number information.
0x0040e7cb in luaV_execute ()
(gdb)
Single stepping until exit from function luaV_execute,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x67a258be in _end__ ()
(gdb) q
The program is running.  Exit anyway? (y or n) y
--------------------------------------

Erm, help? Thanks in advance! ;-)