H1.1: Create a skeleton application in LuaI'll start with the code, then explain it below.
local App = Service(Core.EventListener) function App:initialize() -- initialize your application here system:setWindowTitle("My Application") end function App:onTimeEvent(event) -- update your scene here -- render your scene here render:start() render:finish() system:swapBuffers() end function App:onKeyEvent(event) if (event.pressed and event.code == event.ESCAPE) then core:quit() end end io = IO.IOService() system = System.SystemService() render = Render.RenderService() collide = Collider.CollisionService() physics = Physics.PhysicsService() StdIO.Register() core:addEventListener(App()) From the top:
local App = Service(Core.EventListener) Nearly everything in F4 is a service, and applications are no exception. This statement creates a new service interface, based on "Core.EventListener" to represent your new application. An EventListener is able to receive and respond to messages from the main event loop.
function App:initialize()
-- initialize your application here
system:setWindowTitle("My Application")
end
function App:onTimeEvent(event)
-- update your scene here
-- render your scene here
render:start()
render:finish()
system:swapBuffers()
end
A TimeEvent is sent to your application whenever there are no other events to be processed. This is where you will want to update the state of your game and render a new frame of animation.
function App:onKeyEvent(event)
if (event.pressed and event.code == event.ESCAPE) then
core:quit()
end
end
A KeyEvent is just one kind of user input event that your application can receive: ClickEvent, MotionEvent, AxisEvent, and ButtonEvent are also possible. Or, you may find it more useful to map these low-level input events to higher-level game events using the ActionMapper, located in the StdLib package. This sample KeyEvent handler waits for the user to press the [Esc] key, then quits the application.
io = IO.IOService() system = System.SystemService() render = Render.RenderService() collide = Collider.CollisionService() physics = Physics.PhysicsService() These statements start up the F4 engine subsystems and assigns them to global script variables for quick access later. You only need to start the subsystems which you actually intend to use in your application.
StdIO.Register()
The StdIO package contains reader/writers for common file formats. This statement registers each of the recognized file extensions with the IO service, so it will know what to do with a command like
core:addEventListener(App())
There are actually two things going on here. First, the call to
|