Using TkLua Library

TkLua is a C library that must be linked to the embedding program. Besides tklua, we link the tolua, lua and lualib libraries. We also need to link the tcl and tk libraries. And, if we are planning to instantiate OpenGL canvases, we also need to link the gllua and the OpenGL (or Mesa) libraries.

Below is a simplified version of the code distributed with TkLua that executes TkLua scripts.

/*
** tklua.c
** Execute tkLua scripts
*/

#include <stdlib.h>

#include "lua.h"
#include "lualib.h"
#include "tklua.h"

int main (int argc, char *argv[])
{
 int i;
 tkluaCreate();
 tkluaGLCanvas();
 glluaBindGL();
 glluaBindGLU();

 for (i=1; i<argc; i++)
  lua_dofile (argv[i]);

 tkluaMainLoop();

 return 0;
}

We initialize the TkLua library calling:

TkLua* tkluaCreate (void);

which creates and returns a TkLua context. We may create many TkLua contexts but only one is active. If we have multiple contexts, we switch between them by calling:

TkLua* tkluaSetCurrent (TkLua* context);

which returns the previously current context.The last created context is automatically turned active.

If we also intend to create OpenGL canvases, we initialize them with:

int tkluaGLCanvas (void);

Finally, note that, after creating the dialogues (executing Lua codes), we invoke tkluaMainLoop to pass the control to Tk.

Timers and Idle function

From C, we can create and delete timers using the functions:

TkLuaTimer* tkluaCreateTimer (int milliseconds, void (*func) (void*), void* data);
void        tkluaDeleteTimer (TkLuaTimer* timer);

Also, we set an idle function by calling:

void tkluaIdleFunc (void (*func) (void));

A previously set idle function is cancelled by specifying NULL to the above function.


Topics


Last update: November 1998 by W. Celes.