These is kind of a "mini wiki" per say. Just basic commands, definitions, functions, ect...
Look for animation, shooting,jumping and simple AI to be added next!
THIS WILL BE UPDATED FREQUENTLY, AND FEEL FREE TO POST ANYTHING YOU WOULD LIKE ADDED, Thanks
Escape Sequences
-- These commands can be used when you are in certain situation dealing with strings. For those of you who are not familiar with strings, a string is "a sequence of characters". Now, back to escape sequences. Say you want to print
"I printed text within quotes"
it wolud be impossible todo this without the escape sequence(s)
Code:
\" --double quote
\' --single quote
since all text must be put in quotes for it to print. So an example of the code for this is
Code:
screen:print("\"in quotes\"")
The rest of the escape sequences are as follows
\a --bell
\b --back space
\f --form feed
\n --newline
\r --carriage return
\t --horizontal tab
\v --vertical tab
\\ --backslash
\" --double quote
\' --single quote
\[ --left square bracket
\] --right square bracket
Menu
ok Im going to show you three different ways to make a menu. But there are other ways 
1.)
Code:
-- initialize black and white colors
black = Color.new(0, 0, 0)
white = Color.new(255, 255, 255)
index = 1 -- initialize index value to "1" (top of menu)
xIcon = 0 -- x coordinate of Icon: in this example, it remains at 0
yIcon = 0 -- y coordinate of Icon: it moves up and down by 15 pixels
-- create the offscreen image (where the menu options will be printed - NOT the icon)
offscreen = Image.createEmpty(480, 272)
offscreen:clear(black)
-- checks index of menu to perform an action based on user's choice
-- insert the code you want your menu choices to perform HERE.
function menuCode ()
screen:clear(white)
if index == 1 then
screen:print(0, 0, "You have chosen 'Play'.", black)
else if index == 2 then
screen:print(0, 0, "You have chosen 'Options'.", black)
else if index == 3 then
screen:print(0, 0, "You have chosen 'Exit'.", black)
end
end
end
screen.flip()
end
-- takes table values of 'menu' and prints the menu to offscreen
function printMenu ()
for i = 0, table.getn(menu)-1 do
offscreen:print(10, i*15, menu[i+1], white)
end
end
-- Direction: 1 = up , 2 = down
function moveIcon (direction)
screen:clear(black) -- reset/clear the screen every time the icon moves
screen:blit(0, 0, offscreen, false)
-- if the direction of the menu is up, make sure that position is valid and move the icon
-- also changes the index value of the menu, and sets new Y coordinate for the icon
if direction == 1 and index > 1 and index <= table.getn(menu) then
index = index - 1
yIcon = yIcon - 15
-- if the direction of the menu is down, make sure the position is valid and move the icon
-- also changes the index value of the menu, and sets new Y coordinate for the icon
else if direction == 2 and index >= 1 and index < table.getn(menu) then
index = index + 1
yIcon = yIcon + 15
end
end
-- prints the Icon to the screen
screen:fillRect (xIcon, yIcon, 5, 5, white)
screen.waitVblankStart(4)
screen.flip()
end
-- declare your menu and initialize values
menu = {}
menu[1] = "Play"
menu[2] = "Options"
menu[3] = "Exit"
-- call function to print menu onto screen
printMenu()
screen:blit(0, 0, offscreen, false) -- print offscreen
screen:fillRect(xIcon, yIcon, 5, 5, white) -- place/initialize icon onto screen
screen.flip()
while true do
pad = Controls.read()
if pad:start() then
break
end
if pad:down() then
-- calls moveIcon function with direction = 2 (DOWN)
moveIcon(2)
end
if pad:up() then
-- calls moveIcon function with direction = 1 (UP)
moveIcon(1)
end
if pad:cross() then
-- calls the actual code you want your menu choices to perform
menuCode()
end
screen.waitVblankStart()
end
2.)
Code:
red = Color.new(255,0,0)
black = Color.new(0,0,0)
white = Color.new(255,255,255)
menustatus = 1
while true do
screen:clear(black)
pad = Controls.read()
if pad:up() then
menustatus = menustatus - 1
screen.waitVblankStart(4)
end
if pad:down() then
menustatus = menustatus + 1
screen.waitVblankStart(4)
end
color={white, white, white}
screen:print(50, 50, "Play", color[1])
screen:print(50,60,"Options", color[2])
screen:print(50,70,"Exit", color[3])
color[menustatus]=red
if menustatus == 1 then
if pad:cross() then
--insert game code here
end
end
if menustatus == 2 then
if pad:cross() then
--insert options here
end
end
if menustatus == 3 then
if pad:cross() then
break
end
end
if menustatus <= 0 then
menustatus = 3
end
if menustatus => 4 then
menustatus = 1
end
screen.flip()
screen.waitVblankStart()
end
3.)
Code:
function menuoption(xpos,ypos,txt, cursor,num_cursor_char,co l,selcol,selcondition)
screen:print(xpos,ypos,tx t,col)
if selcondition then
screen:print(xpos-(8*num_cursor_char),ypos, cursor .. txt,selcol)
end
end
I suggest most use the second one as its the most understandable and more efficient then the first one. But the third is more advance but more efficient.
Functions
Functions are first-class values in Lua. That means that functions can be kept (or stored) in variables, be used inside another functions, and returned as results.
The following functions are built in functions specific to the PSP Lua player
Graphics
Code:
Font Font.load(filename)
Loads a true type font.
Code:
Font Font.createMonoSpaced()
Creates an instance of the built-in mono-spaced font.
Code:
Font Font.createProportional()
Creates an instance of the built-in proportional font.
Code:
Font Font:setCharSize(number width, number height, number dpiX, number dpiY)
Changes the size of the current font to the specified point size height and width (if width is 0, it will be calculated proportional to the height for every character). dpiX and dpiY is the resolution of the display (see the Freetype documentation for details).
Code:
Font Font:setPixelSizes(number width, number height)
Changes the size of the current font to the specified pixel size height and width (if width is 0, it will be calculated proportional to the height for every character). (see the Freetype documentation for details).
Code:
width, height Font:getTextSize(string)
Returns the width and height, which will be used, if the specified text is drawn with this font and the current font size.
Code:
Image Image.createEmpty(width, height)
Creates an empty image, initially cleared. Max width and height is 512.
Code:
Image Image.load( filename )
Loads an image. Currently JPEG and PNG files are supported.
Code:
nil image:blit(x, y, Image source, [sourcex, sourcey, width, height], [alpha = true])
Paints the image, which is specified as “source” to the image on which this function is called on (or to the double buffered screen, when using the “screen:blit(...)” variable, which is a special image). “sourcex” and “sourcey” is the position in the source image, from where a rectangle of the size width/height is copied.
Code:
nil image:clear([color = transparent-black])
Sets all pixels of an image to the specified color.
Code:
nil image:fillRect(x, y, width, height, [color = transparent-black])
Draws a filled rectangle.
Code:
nil image:drawLine(x0, y0, x1, y1, [color = black])
Draws a line. TODO: Clipping needs to be enhanced.
Code:
Color image:pixel(x, y) --get
Gets the color of a pixel. See for example this code to get the color of an pixel and assert the value of all color components:
image = Image.createEmpty(1, 1)
color = image
ixel(0, 0)
colors = color:colors()
assert(colors.r == 0)
assert(colors.g == 0)
assert(colors.b == 0)
assert(colors.a == 0)
Code:
nil image:pixel(x, y, Color) --set
Sets the color of a pixel.
Code:
nil image:print(x, y, text, [color = black])
Prints some text with a predefined fixed width font with 8×8 pixels per character.
Code:
nil image:fontPrint(font, x, y, text, [color = black])
Prints some text with the specified true type font.
Code:
Number image:width()
Returns the width of an image.
Code:
Number image:height()
Returns the height of an image.
Code:
nil Image:save( filename )
Saves an image to memory stick. Currently JPEG and PNG files are supported.
Code:
global Image screen
The special double buffered screen object, which has all methods of an image and some more, see below.
Code:
nil screen.flip() -- note the small s; this is a function of the screen
Flips the offscreen and onscreen screen.
Code:
nil screen.waitVblankStart([count])
Waits until vertical retrace starts.
Code:
Color Color.new(r, g, b, [a=255])
Creates a new color object with the specified RGBA values.
Code:
table[r,g,b,a] color:colors()
Returns the RGBA values of a color object.
Code:
Bool (Color a == Color b)
Color objects are equal, if all RGBA components are equal.
3D GU mapping
Note: The 3D API might change in future.
saves the current GE state for the normal 2D operations. Call this function first and then draw your scene. You can use the following functions:
Gu.clearColor, Gu.clearDepth, Gu.clear, Gum.matrixMode, Gum.loadIdentity, Gum.perspective, Gum.translate, Gum.rotateXYZ, Gu.texImage, Gu.texFunc, Gu.texEnvColor, Gu.texFilter, Gu.texScale, Gu.texOffset, Gu.ambientColor, Gu.enable, Gu.disable, Gu.blendFunc, Gu.light, Gu.lightAtt, Gu.lightColor, Gu.lightMode, Gu.lightSpot and Gum.drawArray. See the PSPSDK for documenation about the parameters, for example the pspgu.h. If there is a ScePspFVector3, write 3 numbers instead one parameter (this will be changed in a later version). Whereever a color is expected, use a Color object. For textures use an Image object.
Gum.drawArray is something special: It has just 3 parameters: “prim”, “vtype” and a table. With “prim” you specify which primitive should be used, for example Gu.TRIANGLES. “vtype” specifies the vertex format and the transformation operation. For example “Gu.COLOR_8888|Gu.VERTEX_32BITF|Gu.TRANSFORM_3D means, that your vertices has a color component and a 3 vertex coordinate components and is transformed before passed to the rasterizer. The table is then a list of vertex entries, where each vertex entry has the form (color, x, y, z). If you have specified GU_TEXTURE_32BITF, too, then one entry looks like this: (textureU, textureV, color, x, y, z). The order of the entries is defined in pspgu.h: textureU, textureV, color, normalX, normalY, normalZ, vertexX, vertexY, vertexZ. Indices, weights and multiple vertices per entry (which you’ll need for the morphing feature) currently are not supported.
After drawing anything, call Gu.end3d(), which flushs the display buffer and draws all pending 3D graphics and restores the 2D GE state. Don’t forget to call a screen:clear() before fullscreen 2D operations, because otherwise the z-buffer is not cleared and the blitting functions may not work correctly. If you need to blit 2D images in 3D mode, use textures and triangles.
See the 3D Cube example in the Applications folder of the distribution of Lua Player to see how to use the 3D functions and take a look at the GU example in the PSPSDK how to use all the other functions. Most of the time you can copy and paste the examples and just remove all those C fussinesses like “.0f”, semicolon, malloc etc. :-)
Controls
Code:
Controls Controls.read()
Code:
Bool controls:select()
Bool controls:start()
Bool controls:up()
Bool controls:right()
Bool controls:down()
Bool controls:left()
Bool controls:l()
Bool controls:r()
Bool controls:triangle()
Bool controls:circle()
Bool controls:cross()
Bool controls:square()
Bool controls:home()
Bool controls:hold()
Bool controls:note()
Code:
Number controls:analogX() -- ranges from -127 to 128.
Returns the current analog stick position in x direction.
Code:
Number controls:analogY() -- same
Returns the current analog stick position in y direction.
Code:
Bool (Controls a == Controls b) -- note! The analog stick is NOT considered when comparing because of analog fluctuations.
Compares two pad states.
Code:
Number controls:buttons() -- returns the bitmask like sceCtrlReadBufferPositive reads it
Constants for binary operations on buttons() result (for example “Controls.read():buttons() & Controls.startMask > 0” is the same as “Controls.read():start()”)
Code:
Number Controls.selectMask
Number Controls.startMask
Number Controls.upMask
Number Controls.rightMask
Number Controls.downMask
Number Controls.leftMask
Number Controls.ltriggerMask
Number Controls.rtriggerMask
Number Controls.triangleMask
Number Controls.circleMask
Number Controls.crossMask
Number Controls.squareMask
Number Controls.homeMask
Number Controls.holdMask
Number Controls.noteMask
Millisecond Timer
Code:
Timer Timer.new([startTime])
Creates a new Timer object, sets to 0 or startTime in milliseconds, if specified BUT don’t start ticking as yet.
Code:
Number Timer:start()
Starts to tick the timer (incrementing by one every millisecond), or resumes to tick after being stopped. Returns the current time() value. If the timer is running, it is the same as time().
Code:
Number Timer:time()
Returns in milliseconds since the timer started/resumed.
Code:
Number Timer:stop()
Stops the timer. Returns the current time(). Subsequent time() calls returns the value when stopped. If the timer is stopped, this call is the same as time().
Code:
Number Timer:reset([startTime])
Resets the timer to 0 by default or startTime in milliseconds and holds the timer. Returns the time before resetted to the new value.
System
Code:
String System.currentDirectory() -- get
Gets the current working directory.
Code:
String System.currentDirectory( path ) -- set, returns old path.
Sets the current working directory.
Code:
table System.listDirectory( [path] )
Lists the contents of the current working directory or the specified path. The result is a table of tables, where each table-entry has the string entry “name”, the number entry “size” and the boolean entry “directory”, which is set to true, when the current entry is a directory. See for example this code to print all files in the current working directory:
Code:
files = System.listDirectory()
for index, file in files do
print(file.name)
end
Code:
nil System.createDirectory(path)
Creates a new directory. The path is relative to the current directory, if not absolute.
Code:
nil System.removeDirectory(path)
Deletes a directory. The path is relative to the current directory, if not absolute.
Code:
nil System.removeFile(path)
Deletes a file. The path is relative to the current directory, if not absolute.
Code:
nil System.usbDiskModeActivate()
Activates the USB mode. Attention: When writing from USB to the memory stick, you must not write from within your Lua script to the memory stick, until you disable USB, otherwise the filesystem of your memory stick gets corrupted and you have to reformat your memmory stick.
Code:
nil System.usbDiskModeDeactivate()
Deactivates the USB mode.
Battery functions:
Code:
Bool System.powerIsPowerOnline()
Code:
Bool System.powerIsBatteryExist()
Code:
Bool System.powerIsBatteryCharging()
Code:
Number System.powerGetBatteryChargingStatus()
Code:
Bool System.powerIsLowBattery()
Code:
Number System.powerGetBatteryLifePercent()
Code:
Number System.powerGetBatteryLifeTime()
Code:
Number System.powerGetBatteryTemp()
Code:
Number System.powerGetBatteryVolt()
Code:
Number System.powerTick()
When called, it resets the internal power off counter, which prevents auto power off.
Code:
String System.md5sum(String)
Calculates the md5sum of a string. For example print(System.md5sum(io.input(”EBOOT.PBP”):read (”*a”))) prints the same digest as “md5sum EBOOT.PBP” on Unix.
Serial input/output functions:
nil System.sioInit(baudrate)Opens the SIO device and sets the baud rate. This needs some seconds to power up the UART.
Code:
System.sioWrite(string)
Writes the string to the SIO
Code:
string System.sioRead()
Reads all available data from the SIO. Returns an empty string, if no data was received.
Code:
System.sleep(number)
Pauses program execution for the specified time in milliseconds. It doesn’t affect any timer object.
IrDA functions:
Code:
nil System.irdaInit()
Opens the IrDA device. Call this to start the IrDA module.
Code:
System.irdaWrite(string)
Writes the string to the IrDA port.
Code:
string System.irdaRead()
Reads all available data from the IrDA port. Returns an empty string, if no data was received.
Code:
number System.getFreeMemory()
Gets the available free memory. You can use it like this:
print("about " .. System.getFreeMemory() / 1024 / 1024 .. " megabytes free memory available")
continued...
Bookmarks