/* ********************************************************
 * Example, how to build a standalone GUI application from
 * a lua program using mnoofltk.
 * The lua file is referenced in the makefile.
 * ********************************************************/

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <assert.h>
  
typedef lua_State luaState_t;
static const luaL_reg lualibs[] = {
  { "base",       luaopen_base },
  { "table",      luaopen_table },
  { "io",         luaopen_io },
  { "string",     luaopen_string },
  { "math",       luaopen_math },
  { "debug",      luaopen_debug },
  { "loadlib",    luaopen_loadlib },
  { NULL,         NULL }
};

static void openlualibs(luaState_t *l){
  const luaL_reg *lib;
  
  for (lib = lualibs; lib->func != NULL; lib++) {
    lib->func(l);
    lua_settop(l, 0);
  }
}

/* ********************************************************
 * Function that generates mnoofltk bindings and puts the
 * result on the stack
 * ********************************************************/
int mnoofltk_api_init(luaState_t* l);

int main(void){
  luaState_t* L = lua_open(); assert(L);
  openlualibs(L);
  
  /* ********************************************************
   * Initialize mnoofltk C level functions.
   * In precompiled mode, the library expects the result in 
   * a global variable with the name of the API init function.
   * (the content is actually a table)
   * ********************************************************/
  mnoofltk_api_init(L);
  lua_setglobal(L, "mnoofltk_api_init");
  
  /* ********************************************************
   * Load precompiled lua code. This must contain (in the 
   * correct order to satisfy dependencies between the files
   * themselves!):
   * - dependencies of mnoofltk.lua
   * - mnoofltk.lua itself
   * - the application 
   * See the makefile for details.
   * ********************************************************/
#include "generated.h"
  
  return 0;
}