forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

lua strings in my c program..

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development
View previous topic :: View next topic  
Author Message
SephZero



Joined: 29 Apr 2006
Posts: 5

PostPosted: Fri May 19, 2006 6:57 am    Post subject: lua strings in my c program.. Reply with quote

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?
Back to top
View user's profile Send private message
SSpeare



Joined: 23 May 2006
Posts: 63

PostPosted: Wed May 24, 2006 12:06 am    Post subject: Reply with quote

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:
// Create lua VM
lua_State *g_luaVM = lua_open();
// Open standard libs (optional)
luaL_openlibs(g_luaVM);


To load a file simply:

Code:
if (luaL_dofile(g_luaVM, "main.lua"))
{
  printf("Executing main.lua failed.\n");
}


I usually load a file that looks something like this:

Code:
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:
executeLuaFunc("update");


With that being defined as:
Code:
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.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PS2 Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group