Creating your very own Lua Player…

Want to know how LuaHM7 was created? Or even the old Lua Player? Or do you only want to create your own? Well today I’m going to show you how!

Requirements

  • PSP’s SDK (Use Google to find one, it’s everywhere)
  • Lua Library
This is the basics to creating your own Lua Player, it by no means pretends to be a replacement for existing Lua players out there. We can only load a script and use the controls, but if you know C you will be able to add your own functions. Okay, let’s go with the main code:
Main.c 

// Lua test player for Wololo
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspdisplay.h>
#include <pspctrl.h>
#include <stdlib.h>
#include <string.h>
#include “lua.h”
#include “lualib.h”
#include “lauxlib.h”

/* Define the module info section */
PSP_MODULE_INFO(“LUATEST”, 0, 1, 1);

/* Define the main thread’s attribute value (optional) */
PSP_MAIN_THREAD_ATTR(THREAD_ATTR_USER | THREAD_ATTR_VFPU);

/* Define printf, just to make typing easier */
#define printf pspDebugScreenPrintf

/* Exit callback */
int exit_callback(void)
{
sceKernelExitGame();

return 0;
}

/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;

cbid = sceKernelCreateCallback(“Exit Callback”, (void *) exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);

sceKernelSleepThreadCB();

return 0;
}

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
int thid = 0;

thid = sceKernelCreateThread(“update_thread”, CallbackThread, 0×11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}

return thid;
}

int lua_ctrlRead(lua_State *L)
{
// number of passed arguments
int argc = lua_gettop(L);
if (argc != 0) return luaL_error(L, “wrong number of arguments”);

SceCtrlData pad;
sceCtrlReadBufferPositive(&pad, 1);

// push return value (multiple return values are possible)
lua_pushnumber(L, (int)pad.Buttons);

// number of returned arguments
return 1;
}

#define CHECK_CTRL(name, bit) \
int name(lua_State *L) \
{ \
int argc = lua_gettop(L); \
if (argc != 1) return luaL_error(L, “wrong number of arguments”); \
\
/* get first argument as int */ \
int argument = luaL_checkint(L, 1); \
\
/* push result */ \
lua_pushboolean(L, (argument & bit) == bit); \
return 1; \
}

CHECK_CTRL(lua_isCtrlSelect, PSP_CTRL_SELECT)
CHECK_CTRL(lua_isCtrlStart, PSP_CTRL_START)
CHECK_CTRL(lua_isCtrlUp, PSP_CTRL_UP)
CHECK_CTRL(lua_isCtrlRight, PSP_CTRL_RIGHT)
CHECK_CTRL(lua_isCtrlDown, PSP_CTRL_DOWN)
CHECK_CTRL(lua_isCtrlLeft, PSP_CTRL_LEFT)
CHECK_CTRL(lua_isCtrlLTrigger, PSP_CTRL_LTRIGGER)
CHECK_CTRL(lua_isCtrlRTrigger, PSP_CTRL_RTRIGGER)
CHECK_CTRL(lua_isCtrlTriangle, PSP_CTRL_TRIANGLE)
CHECK_CTRL(lua_isCtrlCircle, PSP_CTRL_CIRCLE)
CHECK_CTRL(lua_isCtrlCross, PSP_CTRL_CROSS)
CHECK_CTRL(lua_isCtrlSquare, PSP_CTRL_SQUARE)
CHECK_CTRL(lua_isCtrlHome, PSP_CTRL_HOME)
CHECK_CTRL(lua_isCtrlHold, PSP_CTRL_HOLD)

int lua_waitVblankStart(lua_State *L)
{
if (lua_gettop(L) != 0) return luaL_error(L, “wrong number of arguments”);
sceDisplayWaitVblankStart();
return 0;
}

// print all arguments, converted to strings
int lua_print(lua_State *L)
{
int argc = lua_gettop(L);
int n;
for (n=1; n return 0;
}

int main(int argc, char** argv)
{
pspDebugScreenInit();
SetupCallbacks();

// script file must be in the same directory where the program started
const char* scriptName = “script.lua”;
char* scriptFilename = (char*) malloc(strlen(argv[0]) + strlen(scriptName));
strcpy(scriptFilename, argv[0]);
char* end = strrchr(scriptFilename, ‘/’);
end++;
*end = 0;
strcat(scriptFilename, scriptName);

// init Lua and load all libraries
lua_State *L = lua_open();
luaL_openlibs(L);

// register our own functions
lua_register(L, “ctrlRead”, lua_ctrlRead);
lua_register(L, “isCtrlSelect”, lua_isCtrlSelect);
lua_register(L, “isCtrlStart”, lua_isCtrlStart);
lua_register(L, “isCtrlUp”, lua_isCtrlUp);
lua_register(L, “isCtrlRight”, lua_isCtrlRight);
lua_register(L, “isCtrlDown”, lua_isCtrlDown);
lua_register(L, “isCtrlLeft”, lua_isCtrlLeft);
lua_register(L, “isCtrlUp”, lua_isCtrlUp);
lua_register(L, “isCtrlLTrigger”, lua_isCtrlLTrigger);
lua_register(L, “isCtrlRTrigger”, lua_isCtrlRTrigger);
lua_register(L, “isCtrlTriangle”, lua_isCtrlTriangle);
lua_register(L, “isCtrlCircle”, lua_isCtrlCircle);
lua_register(L, “isCtrlCross”, lua_isCtrlCross);
lua_register(L, “isCtrlSquare”, lua_isCtrlSquare);
lua_register(L, “isCtrlHome”, lua_isCtrlHome);
lua_register(L, “isCtrlHold”, lua_isCtrlHold);
lua_register(L, “print”, lua_print);
lua_register(L, “waitVblankStart”, lua_waitVblankStart);

// load script
int status = luaL_loadfile(L, scriptFilename);

// call script
if (status == 0) status = lua_pcall(L, 0, LUA_MULTRET, 0);

// show error, if any
if (status != 0) {
printf(“error: %s\n”, lua_tostring(L, -1));
lua_pop(L, 1); // remove error message
}

// cleanup
lua_close(L);
free(scriptFilename);

sceKernelExitGame();

return 0;
}

And here is the Makefile:

TARGET = luatest
OBJS = main.o

INCDIR =
CFLAGS = -O2 -G0 -Wall
CXXFLAGS = $(CFLAGS) -fno-exceptions -fno-rtti
ASFLAGS = $(CFLAGS)

LIBDIR =
LIBS = -llua -lm
LDFLAGS =

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = Lua test program

PSPSDK=$(shell psp-config –pspsdk-path)
include $(PSPSDK)/lib/build.mak

Hope you understood everything, I know the PSP Scene is not that active now, but who knows, maybe one of you will be the start to an optimized PSVita Lua player? If you have any doubts or anything, please comment!

  1. JMQMOfficials’s avatar

    #1 :D

    Reply

    1. Viral_Weaponry’s avatar

      bah!

      Reply

  2. JMQMOfficials’s avatar

    xD Very nice.

    Reply

  3. RodPin’s avatar

    Great article, m.rr! Hope that someday we´ll have great code writers for Vita as we had on PSP.

    Reply

  4. memkou’s avatar

    C :) i love writing programs in C. ill try this when i have free time

    Reply

  5. oO Flowzilla Oo’s avatar

    Nice tut

    Reply

Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>