luaplayer.org
September 08, 2010, 03:27:33 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: IMPORTANT: Please read the following post http://luaplayer.org/forums/index.php?topic=693
 
   Home   Help Search Login Register  
Pages: [1]
  Print  
Author Topic: [Lua] FaT3oYCG's PGE Lua Libraries  (Read 1324 times)
FaT3oYCG
Full Member
***
Posts: 139


I don't argue, I simply express my own opinion!

fatboycg@hotmail.co.uk fatboycg09
View Profile WWW Email
« on: January 08, 2010, 01:34:57 PM »

I am in the process of creating libraries for most of the PGE functions that you usually need more than one line for to carry out a task that you may require, to use these libraries the usual method is to simply require them at the top of your code.
I learned how to generate some of this code thanks to other developers on this site so credit where credit is due for them. I can't remember exactly what code came from which users so I will just thank anyone that believes any of their code has been used as a basis.

The code snippets will follow in the following format:
file_name.lua
Code
GeSHi (lua):
-- Code of the file, to use the functions or include the functionality place
-- require "file_name" at the top of the code (excluding the file extension).
 
Created by GeSHI 1.0.7.20
Description:
A short description if given.

script.lua
Code
GeSHi (lua):
require "protected_launcher"
 
Created by GeSHI 1.0.7.20
Description:
A simple script file that is loaded by the PGE EBOOT simply used for launching the protected loader script.

short_names.lua
Code
GeSHi (lua):
for key, value in pairs(pge) do
_G[key] = value
end
 
Created by GeSHI 1.0.7.20
Description:
Allows you to use short names for the built in PGE functions, for example instead of typing pge.delay(1000000) you can simply use delay(1000000) to complete the same task.

message_dialogue.lua
Code
GeSHi (lua):
require "short_names"
 
function messageDialogue(message, options)
if utils.msginit(message, options or 0) then
local status
 
while running() do
gfx.startdrawing()
gfx.clearscreen()
gfx.enddrawing()
 
status = utils.msgupdate()
 
if status ~= PGE_UTILS_DIALOG_RUNNING then
break
end
 
gfx.swapbuffers()
end
 
return status
end
end
 
Created by GeSHI 1.0.7.20
Description:
Allows you to use a one line function to display the message dialogue.

protected_launcher.lua
Code
GeSHi (lua):
require "short_names"
require "message_dialogue"
 
local function errorReporter(errorMessage)
local fileNameEnd = string.find(errorMessage, ".", 1, true)
local fileName = string.sub(errorMessage, 0, fileNameEnd - 1)
local errorLineStart = string.find(errorMessage, ":")
local errorLineEnd = string.find(errorMessage, ":", errorLineStart + 1)
local errorLine = string.sub(errorMessage, errorLineStart + 1, errorLineEnd - 1)
local formattedErrorMessage = string.sub(errorMessage, errorLineEnd + 1, string.len(errorMessage))
messageDialogue("An error has occoured." .. "\r\n" ..
"\r\n" ..
"Please report the following information:" .. "\r\n"..
"File: " .. fileName .. "\r\n" ..
"Line: " .. errorLine .. "\r\n" ..
"Cause: " .. formattedErrorMessage .. "\r\n" ..
"\r\n" ..
"Press Circle to restart."
  )
end
 
local environment = {}
setmetatable(environment, { __index = _G })
 
while running() do
local script, scriptMessage = loadfile("main.lua")
 
if script then
setfenv(script, environment)
 
if xpcall(script, errorReporter) then
break
end
 
for key, value in pairs(environment) do
environment[key] = nil
end
 
for key, value in pairs(package.loaded) do
package.loaded[key] = nil
end
 
collectgarbage('collect')
else
errorReporter(scriptMessage)
end
end
 
Created by GeSHI 1.0.7.20
Description:
A protected launching environment for your script, if you use this then you will require no need to make your global variables local any more. You will still need to define local variables though. The file launches your script which you should call main.lua but you are able to change this by editing the library.

print_to_file.lua
Code
GeSHi (lua):
function print(...)
local f = file.open("print output.txt", PGE_FILE_CREATE + PGE_FILE_RDWR + PGE_FILE_APPEND)
for key, value in ipairs(arg) do
f:write(value .. "\r\n")
end
f:close()
end
 
Created by GeSHI 1.0.7.20
Description:
Overwrites the usual print function to write to a file instead named "print output.txt" which you can change, this is usefule if you need to test a script and can't make use of the print functions normal functionality.

states.lua
Code
GeSHi (lua):
states = {}
 
function states.init()
states.groups = {}
states.group = 1
states.state = 1
end
 
function states.setGroup(groupID)
if type(groupID) == "number"then
states.group = groupID
else
for i = 1, #states.groups do
if states.groups[i].name == groupID then
states.group = i
end
end
end
end
 
function states.getGroup()
return states.group
end
 
function states.addGroup(groupName)
local groupTable = states.groups
 
table.insert(states.groups, { name = groupName, states = {} })
end
 
function states.setState(stateID)
local group = states.getGroup()
local stateTable = states.groups[group].states
 
if type(stateID) == "number" then
states.state = stateID
else
for i = 1, #stateTable do
if stateTable[i].name == stateID then
states.state = i
end
end
end
end
 
function states.getState()
return states.state
end
 
function states.addState(stateName, codeFile)
local group = states.getGroup()
local stateTable = states.groups[group].states
 
table.insert(stateTable, { name = stateName, code = loadfile(codeFile) })
end
 
function states.run()
local group = states.getGroup()
local stateTable = states.groups[group].states
 
for i = 1, #stateTable do
if states.getState() == i then
stateTable[i].code()
end
end
end
 
Created by GeSHI 1.0.7.20
Description:
Provides an easy to use state system to mnage different parts of your code, it includes state groups so that you can easily make states inside other states in a different group.

counter.lua
Code
GeSHi (lua):
counter = { status = 0, count = 0 }
 
function counter.create()
local newCounter = {}
setmetatable(newCounter, counter)
counter.__index = counter
return newCounter
end
 
function counter:start()
self.status = 1
end
 
function counter:stop()
self.status = 0
self.count = 0
end
 
function counter:pause()
self.status = 2
end
 
function counter:set(value)
value = value or 0
self.count = value
end
 
function counter:get()
return self.count
end
 
function counter:update()
if self.status == 1 then
self.count = self.count + 1
end
end
 
Created by GeSHI 1.0.7.20
Description:
Provides some functions for managing a counter, a timer is different than a counter as a counter is based on loop cycles.

fps.lua
Code
GeSHi (lua):
require "short_names"
require "counter"
 
fps = { frames = 0, counter = counter.create(), timer = timer.create() }
 
fps.counter:start()
 
function fps.calculate()
if fps.timer:totaltime() >= 1 then
fps.timer = timer.create()
fps.frames = fps.counter:get()
fps.counter:set()
else
fps.counter:update()
end
 
return fps.frames
end
 
Created by GeSHI 1.0.7.20
Description:
Calculates the number of frames that the PSP is displaying every second.

If you can suggest any improvements to these scripts then please feel free and if valid I will attempt to implement them, if you have an idea for a library that I could make then also please feel free to suggest it, if you feel that I have used your code or code that is very similar to yours and have a problem with that then please feel free to contact me via the private messaging service on these forums, any other manner will be ignored or not viewed.

INITIAL POST: Added: script.lua, short_names.lua, message_dialogue.lua and protected_launcher.lua
UPDATE 1: Added: print_to_file.lua
UPDATE 2: Added: states.lua and counter.lua
UPDATE 3: Modified: counter.lua
UPDATE 4: Added: fps.lua
UPDATE 5: Modified: protected_launcher.lua
UPDATE 6: Modified: print_to_file.lua

Thanks,

Craig.
« Last Edit: January 09, 2010, 09:45:40 AM by FaT3oYCG » Logged

FaT3oYCG
TheUnderminer
Sr. Member
****
Posts: 443


View Profile
« Reply #1 on: January 09, 2010, 04:00:27 AM »

Very helpful snippets and nicely documented. Although Im not sure about the fps counter. I would say it returns how many microsecs it took to render a frame, not give a framerate, returning 40000 instead of 25. I think it should be
fps.frames = 1000000 / fps.counter:get()
Logged
FaT3oYCG
Full Member
***
Posts: 139


I don't argue, I simply express my own opinion!

fatboycg@hotmail.co.uk fatboycg09
View Profile WWW Email
« Reply #2 on: January 09, 2010, 09:35:01 AM »

Hmm, it gives me 60?

My logic was that the counter is incremented by one every loop cycle or frame as we call it and the timer counts up to 1 and is then reset hence the counter should have counted up to 60 usually, how did you get that result? I am running it on the most basic file you can get that only includes the fps counter in it.
Logged

FaT3oYCG
TheUnderminer
Sr. Member
****
Posts: 443


View Profile
« Reply #3 on: January 09, 2010, 12:50:45 PM »

Never mind my previous post. I never even tried your script. I just figured wrong I guess.
Logged
FaT3oYCG
Full Member
***
Posts: 139


I don't argue, I simply express my own opinion!

fatboycg@hotmail.co.uk fatboycg09
View Profile WWW Email
« Reply #4 on: January 09, 2010, 02:26:45 PM »

Lol, nope it works as expected, I do test every piece of code before I post it up here.
Logged

FaT3oYCG
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.9 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!