GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Sep 20, 2014 3:54 am

All times are UTC




Post new topic Reply to topic  [ 1 post ] 

Did you find these code snippets usefull?
Yes 60%  60%  [ 3 ]
No 40%  40%  [ 2 ]
Total votes : 5
Author Message
 Post subject: How to use Lua with C.
PostPosted: Thu Oct 23, 2008 5:57 pm 
Offline
GTK+ Guru

Joined: Thu Jun 21, 2007 1:52 pm
Posts: 198
Location: Wilkes Barre Pa
About Lua
Lua is the most popular scripting language for game development, has been described as the de facto standard for game scripting, and is the language of Baldur's Gate, Escape from Monkey Island, FarCry, Grim Fandango, Homeworld 2, Illarion, Impossible Creatures, Psychonauts, The Sims, and World of Warcraft. Adobe, Disney, Electronic Arts, Intel, LucasArts, Microsoft, NASA, Olivetti, and Philips all use Lua heavily.

How to call a C Function from Lua

luaavg.c
Code:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* gcc -o luaavg luaavg.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

/* the Lua interpreter */
lua_State* L;

static int average(lua_State *L)
{
   /* get number of arguments */
   int n = lua_gettop(L);
   double sum = 0;
   int i;

   /* loop through each argument */
   for (i = 1; i <= n; i++)
   {
      /* total the arguments */
      sum += lua_tonumber(L, i);
   }

   /* push the average */
   lua_pushnumber(L, sum / n);

   /* push the sum */
   lua_pushnumber(L, sum);

   /* return the number of results */
   return 2;
}

int main ( int argc, char *argv[] )
{
   /* initialize Lua */
   L = lua_open();

   /* load Lua base libraries */
   luaL_openlibs(L);

   /* register our function */
   lua_register(L, "average", average);

   /* run the script */
   luaL_dofile(L, "avg.lua");

   /* cleanup Lua */
   lua_close(L);

   /* pause */
   printf( "Press enter to exit..." );

   getchar();

   return 0;
}



avg.lua
[code=]
-- call a C function

avg, sum = average(10, 20, 30, 40, 50)
print("The average is ", avg)
print("The sum is ", sum)

avg, sum = average(1, 2, 3, 4, 5)
print("The average is ", avg)
print("The sum is ", sum)
[/code]


How to call a Lua Function from C

luadd.c
Code:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* gcc -o luadd luadd.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

/* the Lua interpreter */
lua_State* L;

int luaadd ( int x, int y )
{
   int sum;

   /* the function name */
   lua_getglobal(L, "add");

   /* the first argument */
   lua_pushnumber(L, x);

   /* the second argument */
   lua_pushnumber(L, y);

   /* call the function with 2 arguments, return 1 result */

   lua_call(L, 2, 1);

   /* get the result */
   sum = (int)lua_tointeger(L, -1);

   lua_pop(L, 1);

   return sum;
}

int main ( int argc, char *argv[] )
{
   int sum;

   /* initialize Lua */
   L = lua_open();

   /* load Lua base libraries */
   luaL_openlibs(L);

   /* load the script */
   luaL_dofile(L, "add.lua");

   /* call the add function */
   sum = luaadd( 10, 15 );

   /* print the result */
   printf( "The sum is %d\n", sum );

   /* cleanup Lua */
   lua_close(L);

   /* pause */
   printf( "Press enter to exit..." );

   getchar();

   return 0;
}


add.lua
[code=]
-- add two numbers
function add ( x, y )
return x + y
end
[/code]


How to execute a Lua Script from C.

luatest.c
Code:
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"

/* gcc -o luatest luatest.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

int main()
{
    int s=0;
    lua_State *L = lua_open();

    // load the libs
    luaL_openlibs(L);

    //run a Lua scrip here
    luaL_dofile(L,"foo.lua");

    printf("\nAllright we are back in C.\n");

    lua_close(L);

    return 0;
}


foo.lua
[code=]
io.write("Please enter your name: ")
name = io.read() -- read input from user
print ("Hi " .. name .. ", did you know we are in lua right now?")
[/code]


How to get a Varible value from Lua to C.

test.c
Code:
#include <stdio.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <string.h>

/* gcc -o test test.c -I/usr/local/include -L/usr/local/lib -llua -lm -ldl */

int main()
{
   lua_State *L = lua_open();
   
   if(luaL_loadfile(L,"settings.lua") || lua_pcall(L,0,0,0))
      printf("Error failed to load %s",lua_tostring(L,-1));
   else
   {
         
       lua_getglobal(L,"screenWidth");
            const int screenWidth = lua_tonumber(L,-1);
            printf("Screen Width = %d \n", screenWidth);
           
       lua_getglobal(L,"appName");
            const char *appName = luaL_checkstring(L, -1);    
            printf("Screen Name = %s \n", appName);

   }
   
   lua_close(L);

   /* If we got this far, everything worked */
   printf("Success!\n");

   return 0;
}


settings.lua
[code=]
appName = "Firefox"
screenWidth = 400
[/code]

How to make a Lua Module for a C function using Cinvoke.

libtest.c
Code:
int sum(int a, int b)
{
    return (a+b);
}


gcc -fPIC -c libtest.c
gcc -shared libtest.o -o libtest.so

echo $HOME
mkdir $HOME/example

cp libtest.so $HOME/example
export LD_LIBRARY_PATH=$HOME/example

[code=]
require("cinvoke_lua")

libtest = clibrary.new("libtest.so")
sum = libtest:get_function(Cint, "sum", Cint, Cint)
print(sum(5, 10))
[/code]

How to make a Lua Module for a C function using Swig.

How to Build Dynamic Module:
ath.c
Code:
#include <stdio.h>
#include "ath.h"

int add(int x, int y) {
    return x+y;
}


ath.h
[code=]extern int add(int x, int y);[/code]

ath.i
[code=]
/* ath.i */
%module ath
%{
extern int add(int x, int y);
%}
extern int add(int x, int y);
[/code]

Build:
[code=]
swig -lua ath.i
gcc -I/usr/include/lua -c ath_wrap.c -o ath_wrap.o -llua -lm -ldl
gcc -c ath.c -o ath.o
gcc ­shared ­I/usr/include/lua ­L/usr/lib/lua ath_wrap.o ath.o ­o ath.so -llua -lm -ldl
[/code]

Usage Example:
[code=]
require("ath")
print(ath.add(10, 80))
[/code]

or

How to Build Statically Linked with Swig:

ath.c
[code=]
#include <stdio.h>
#include "ath.h"

int add(int x, int y) {
return x+y;
}
[/code]

ath.h
[code=]extern int add(int x, int y);[/code]

ath.i
[code=]
/* ath.i */
%module ath
%{
extern int add(int x, int y);
%}
extern int add(int x, int y);
[/code]

min.c
Code:
#include <stdio.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "ath.h"

extern int luaopen_ath(lua_State* L); // declare the wrapped module

int main(int argc,char* argv[]) {
   
lua_State *L;

if (argc<2)
{
     printf("%s: <filename.lua>\n",argv[0]);
     return 0;
}

L=lua_open();
luaopen_base(L);   // load basic libs (eg. print)
luaopen_ath(L);   // load the wrappered module

if (luaL_loadfile(L,argv[1])==0) {
     lua_pcall(L,0,0,0);
}

return 0;
}


test.lua
Code:
print(ath.add(10, 80))


Build:
[code=]
swig -lua ath.i
gcc -I/usr/include/lua -c min.c -o min.o
gcc -I/usr/include/lua -c ath_wrap.c -o ath_wrap.o -llua -lm -ldl
gcc -c ath.c -o ath.o
gcc -I/usr/include/lua -L/usr/lib/lua min.o ath.o ath_wrap.o -o my_lua -llua -lm -ldl
[/code]

Usage Example:
[code=]
./my_lua test.lua
[/code]

Please report any broken code so i can fix it.
Thanks Caracal


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group