How to manipulate Images in LUA Scripts

BlitzMax Forums/BlitzMax Programming/How to manipulate Images in LUA Scripts

Snatcher(Posted 2010) [#1]
Hi to all,

I'm using LUA with Blitzmax to implement a luaplayer.. but when I´ll try to implement the image layer i´m having some troubles .. here´s my code:

'---------------------------------------------------------------------------------
SuperStrict

Import brl.max2d

Local L:Byte Ptr
L=luaL_newstate()
luaL_openlibs(L)

Function LUA_INIT_SCREEN( L:Byte Ptr )
	Local resWidth :Int = luaL_checkinteger(L, 1)
	Local resHeight:Int = luaL_checkinteger(L, 2)
	Graphics resWidth , resHeight 
EndFunction

Function LUA_LOAD_IMAGE:Int (L:Byte Ptr)
	Local imageFile:String  = luaL_checkstring(L,1)
	Local imageObject:TImage
	imageObject = LoadImage(imageFile)
	lua_pushlightuserdata(L, imageObject)
EndFunction

Function LUA_DRAW_IMAGE:Int (L:Byte Ptr)
  Local posX  			:Int = luaL_checkinteger(L,1)
  Local posY  			:Int = luaL_checkinteger(L,2)
  Local imageParameter :TImage = luaL_checkudata(L,3)
  DrawImage(imageParameter,posX,posY,0)
End Function

lua_register(L, "INIT_SCREEN"  , LUA_INIT_SCREEN)
lua_register(L, "LOAD_IMAGE"   , LUA_LOAD_IMAGE)
lua_register(L, "DRAW_IMAGE"  , LUA_DRAW_IMAGE)

Local strInitialization:String
strInitialization="dofile('main.lua')~n"

If luaL_dostring(L,strInitialization)
	Print "Error: "+lua_tostring(L,-1)
Else
	Print "Everything is fine."
EndIf

lua_close(L)



The LUA script is:
-- -----------------------------------------------------------------------
-- main.lua
INIT_SCREEN(640,480,32)
img = LOAD_IMAGE("image.png")
if tst == null then print "Error" end
for x=1,10000 do
   for y=1,10000 do
   end
end



Well.. the graphic windows open, the LOAD_IMAGE command execute, but the img variable in LUA are nill

Anyone can give some help ?

thanks


Tommo(Posted 2010) [#2]
You need to tell lua how many values this function will return.
Function LUA_LOAD_IMAGE:Int (L:Byte Ptr)
	Local imageFile:String  = luaL_checkstring(L,1)
	Local imageObject:TImage
	imageObject = LoadImage(imageFile)
	lua_pushlightuserdata(L, imageObject)
	return 1 'add this
EndFunction


However, your code still won't work, because imageObject will get collected as garbage some time later.
Just try LUGI, it will save you a bunch of time.


Snatcher(Posted 2010) [#3]
Thanks Tommo, now that I have load something in img variable in LUA, the probelm is to draw the image :) I don't understand how to get the img data im img variable to puit um a local TImage variable to draw the image:

Function LUA_DRAW_IMAGE:Int (L:Byte Ptr)
  Local posX  			:Int = luaL_checkinteger(L,1)
  Local posY  			:Int = luaL_checkinteger(L,2)
  Local imgPointer:Byte Ptr = luaL_checkudata(L,3)
  DrawImage(imageParameter,posX,posY,0)
End Function


but when I´ll compile .. blitz give me a missing function parameter tname ..


What´s LUGI ???

thks


N(Posted 2010) [#4]
What's LUGI ???

LuGI is the Lua interop module I wrote. It, in my opinion at least, greatly simplifies adding Lua bindings to most code, especially if you consider this while designing your code early on (although, as a decent example of late integration, the Leadwerks engine had LuGI working well not long after they started using it, so really all you need to do is design your code well in general).

Documentation of sorts is in the wiki section of the project page.