Callbacks
Overview
At the beginning, Scrupp runs a Lua file which is chosen depending on the command line arguments. Read "Getting Started" for details. This file can run other Lua files, load various media files, define functions and so on. It has to define a table with the name 'main' which contains the callbacks.
Callbacks
Render Callback
- render(delta)
- You need to provide a render callback, otherwise Scrupp will not work properly. It is executed in every frame (unless the update callback returns true). Scrupp calls it with delta as the only argument. This parameter gives the time in milliseconds which has passed since the last frame. This parameter can be used to update the state of the application. The rendering of images should happen in this function.
Optional Callbacks
- update(delta)
- This callback can be used to update the internal state of your application (e.g. the position of images). It is executed prior to the render callback (see above). If this callback returns true, the execution of render(delta) is skipped in this frame and the graphics are not redrawn. If this callback does not exist or returns nothing, nil or false then render(delta) is called. If the window needs to be redrawn, e.g. after it was resized by the user, render(delta) is called regardless of the return value of the update callback.
- resized(width, height)
- If a resizable window is created and it is resized, this function will be executed. It gets the new window width and height as arguments. This function should call scrupp.init() with these new values as arguments. Otherwise, it's not possible to render to new areas of the window.
- keypressed(key, utf8_string)
- If a key is pressed, this function will be executed. Read the section about Keyboard usage for details and an example.
- keyreleased(key)
- If a key is released, this function will be executed. Its only argument is the key constant. Read the section about Keyboard usage for details and an example.
- mousepressed(x, y, button)
- If a mouse button (including the mouse wheel) is pressed, this function will be executed. Read the section about Mouse usage for details and an example.
- mousereleased(x, y, button)
- If a mouse button (including the mouse wheel) is released, this function will be executed. Read the section about Mouse usage for details and an example.
Example
-- create a resizable window
scrupp.init("Callback Test", 600, 400, 32, false, true)
local image = scrupp.addImage("path_to_image_file")
-- mouse cursor coordinates
local x, y
main = {
update = function(dt)
local skip = true
local newX, newY = scrupp.getMousePos()
if newX ~= x or newY ~= y then
x, y = newX, newY
skip = false
end
return skip
end,
render = function(dt)
image:render(x, y)
end,
resized = function(width, height)
scrupp.init("Resized!", width, height, 32, false, true)
end,
mousepressed = function(x, y, button)
print(button .." button pressed at "..x..", "..y)
end,
mousereleased = function(x, y, button)
print(button .." button released at "..x..", "..y)
end,
keypressed = function(k)
if k == "ESCAPE" then
scrupp.exit()
end
print("key '" .. k .. "' pressed")
end,
keyreleased = function(k)
print("key '" .. k .. "' released")
end
}