Page 1 of 1

lua strings in my c program..

Posted: Fri May 19, 2006 6:57 am
by SephZero
Hi dudes..you're master..so i hope u can help me..

i have to do something like this..

i've written a program in C language..
now..if i press X i would to run the code of a .lua file that i have..how could i do it?

Posted: Wed May 24, 2006 12:06 am
by SSpeare
So... you have a C program that runs on the PS2 and you have a Lua environment set up? Have you initialized Lua? Using Lua on the PS2 is pretty straitforward, but there are some changes to get Lua to compile. If you need I can post a diff from Lua 5.1 to get it to work on the PS2.

Assuming you have Lua compiling and linking with your C program, you need to initialize it like this:

Code: Select all

// Create lua VM
lua_State *g_luaVM = lua_open();
// Open standard libs (optional)
luaL_openlibs(g_luaVM);
To load a file simply:

Code: Select all

if (luaL_dofile(g_luaVM, "main.lua"))
{
  printf("Executing main.lua failed.\n");
}
I usually load a file that looks something like this:

Code: Select all

objRot = createVector()
objPos = createVector()

localWorld = createMatrix()
worldView = createMatrix()
localScreen = createMatrix()
viewScreen = createMatrix()

pointCount = 36
cubePoints = createPointArray(pointCount)
cubePoints[0]  =  0 cubePoints[1]  =  1 cubePoints[2]  =  2
-- setup point array code removed...
cubePoints[33] = 21 cubePoints[34] = 22 cubePoints[35] = 23

vertexCount = 24
vertices = createVertexArray(vertexCount)
vertices[0*4] = 10 vertices[0*4+1] = 10 vertices[0*4+2] = 10 vertices[0*4+3]=1
-- setup vertex array code removed...
vertices[23*4]= 10 vertices[23*4+1]=-10 vertices[23*4+2]=-10 vertices[23*4+3]=1

colours = createVertexArray(vertexCount)
-- Setup Color area code removed...

function update()
  -- update object position based on user input
  objRot[0] = objRot[0] + (normJoyAxis(pad[1].RJOY_V) / 20)
  objRot[1] = objRot[1] + (normJoyAxis(pad[1].RJOY_H) / 20)
  objPos[0] = objPos[0] + normJoyAxis(pad[1].LJOY_H)
  objPos[1] = objPos[1] - normJoyAxis(pad[1].LJOY_V) 

  -- setup matrices for rendering
  create_local_world(localWorld, objPos, objRot)
  create_world_view(worldView, camPos, camRot)
  create_local_screen(localScreen, localWorld, worldView, viewScreen)

  -- queue an object for rendering
  queue_render_buffers(localScreen, vertexCount, vertices, colours, pointCount, cubePoints)

  vsync()
  render_queue()
end
And then in my main() loop I do:

Code: Select all

executeLuaFunc("update");
With that being defined as:

Code: Select all

void executeLuaFunc(const char *fname)
{
  lua_getglobal(g_luaVM, fname);
  if (lua_isfunction(g_luaVM, -1))
  {
    lua_pcall(g_luaVM, 0, 0, 0);
  } else {
    lua_pop(g_luaVM, 1);
  }
}
To get input from the pad I used the simple rpc/pad example from ps2sdk. I just use the pad state to update some variables in Lua.

The lua functions that I call are based on ps2sdk and can perform some simple rendering and setup. If you need more help, please post.