[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Newbie... trying to bring interactivity in C application using LUA...!!
- From: Subhash_Patil <Subhash_Patil@...>
- Date: Fri, 31 Jul 2009 19:05:05 +0530
Thanks for the help.
I am clear what to do now.
I am developing a C application.
To bring some interactivity (through my Gui functions which is again in C), I was learning LUA.
I was looking for a sample glue code for a C function and how call that glue/ C function from my application.
This link is also good for the beginners to start up with.
http://www.gamedev.net/reference/articles/article1932.asp
Best Regards,
Subhash
-----Original Message-----
From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-bounces@bazar2.conectiva.com.br] On Behalf Of Martin
Sent: Friday, July 31, 2009 5:29 PM
To: Lua list
Subject: Re: Newbie... trying to bring interactivity in C application using LUA...!!
On Thu, Jul 30, 2009 at 08:45:05PM +0530, Subhash_Patil wrote:
> I get all the prints in the Lua_main function. I see all the prints
> on the console but not the rectangle.
>
> Can you help me out in this regard.
In your example there is too many errors, including some unrelated to
Lua, and you can not really expect it to be functional.
As first step I would recommend to add a option (switch) to your
compiler let it print warnings on all errors. I use gcc -Wall.
Let me first point some errors in C.
In your function:
> SCSL_Draw_Rect(lua_State*L)
> {
> GUI_Color_t asvDrawColor = {31, 31, 0};
>
> int i,j[3];
> int n = lua_gettop(L);
>
> for (i = 1; i <= n; i++){
> if (!lua_isnumber(L, i)){
> lua_pushstring(L, "incorrect argument");
> lua_error(L);
> }
> j[(i-1)] = lua_tonumber(L, i);
>
> printf("from stack %d\n",j[(i-1)]);
> }
> ML_GUI_DrawRectangle(j[1], j[2], j[3], &asvDrawColor);
> }
> SCSL_Draw_Rect has no declared return type, and no return value, so I
> guess it defaults to 0 as an int. Assuming that lua_pushcfunction with
> a NULL parameter doesn't throw an error, I'd expect lua_call to throw.
It very poor practise to not include return value in function
definition. It default to int but it is always better to be
explicit. I would even recommend to declare it as:
LUALIB_API int SCSL_Draw_Rect(lua_State*L)
And when you do not end function with
return 0;
it does not default to returning zero. If return statement is omitted
returned value is undefined (it can be anything - including zero).
Therefore you must always end function with return statement.
You define variable j as array of 3 elements but subsequently you pack
in it as many numbers as your Lua function gets arguments (it may be
any number of arguments). I guess you want to use 4 argument in this
case so you should define j as
int j[4]
and accept only first 4 argument from Lua stack. If you force
buffer overflow you can not expect anything good to happen!
Also I guess ML_GUI_DrawRectangle function need 5 arguments, but
you are passing it only 4. Maybe you want to write
ML_GUI_DrawRectangle(j[0], j[1], j[2], j[3], &asvDrawColor);
I would write this function something like:
LUALIB_API int SCSL_Draw_Rect(lua_State*L)
{
GUI_Color_t asvDrawColor = {31, 31, 0};
int j[4];
int n = lua_gettop(L);
if (n != 4) {
luaL_error(L, "wrong number of argument passed (expected 4, got %d)", n);
}
for (; n; n--) {
j[(n-1)] = luaL_checkint(L, n);
}
ML_GUI_DrawRectangle(j[0], j[1], j[2], j[3], &asvDrawColor);
return 0;
}
Now to your Lua_main function
> Int Lua_main( void )
> {
> int error;
> lua_State *L = lua_open(); /* opens Lua */
> luaL_Reg Creg;
>
> lua_Integer n;
> luaL_openlibs(L);
>
> printf("luaL_openlibs\n");
> lua_pushcfunction(L, SCSL_Draw_Rect(L));
> printf("lua_pushcfunction\n");
>
> /* n = 0;
> lua_pushinteger(L, n);*/
> n = 100;
> lua_pushinteger(L, n);
> n = 100;
> printf("lua_pushinteger\n");
> lua_pushinteger(L, n);
> n = 300;
> lua_pushinteger(L, n);
> n = 300;
> lua_pushinteger(L, n);
> printf("lua_call\n");
> lua_call( L, 5, 0);
> printf("lua_call\n");
> lua_close(L);
>
> return 0;
> }
As Duncan Cross and Jerome Vuarand said you should use
lua_pushcfunction(L, SCSL_Draw_Rect);
and
lua_call( L, 4, 0);
instead of
lua_pushcfunction(L, SCSL_Draw_Rect(L));
and
lua_call( L, 5, 0);
Since this is for testing only I would personally drop all
lua_pushfunction, lua_pushinteger and lua_call and instead write
if (luaL_dostring(L, "SCSL_Draw_Rect(100, 100, 300, 300)") {
const char *err = luaL_optstring(L, -1, NULL);
if (err) {
puts(err);
lua_pop(L, 1);
}
}
Of course this assume that you have registered SCSL_Draw_Rect as Lua
function which can not be seen from your snippet. But there is
> luaL_Reg Creg;
so I assume you know what it is for and you use it.
I do fully agree with Jerome that the code as you passed in here is so
bad that symptoms you describe are the only one that shows.
> I get all the prints in the Lua_main function. I see all the prints
> on the console but not the rectangle.
Also it in not clear where you use this function
> Int Gui_Draw_Rectangle( x,y,Width,Height, /*(Gui_color_t *)color*/){
> Gui_color_t color; // I have had coded this for testing LUA; I am not comfortable with tables yet
You could look at wiki for many helpful article. Some of them
http://lua-users.org/wiki/SimpleLuaApiExample)
http://lua-users.org/wiki/BindingCodeToLua
http://lua-users.org/wiki/UserDataExample
http://lua-users.org/wiki/UserDataWithPointerExample
http://lua-users.org/wiki/UserDataEnvironmentExamples
http://lua-users.org/wiki/BindingWithMembersAndMethods
You should really activate all warning with your compiler and try to
solve them.
Martin
DISCLAIMER:
This email (including any attachments) is intended for the sole use of the intended recipient/s and may contain material that is CONFIDENTIAL AND PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or distribution or forwarding of any or all of the contents in this message is STRICTLY PROHIBITED. If you are not the intended recipient, please contact the sender by email and delete all copies; your cooperation in this regard is appreciated.