Games are called carts like cartridges for old console systems.
A cart is made up of a few callbacks. You don't have to implement them all, but whatever you do implement will be called by the host. It's just a zip file, with main.wasm
in the root.
Here is one, now:
Source: flappybird (opens in a new tab)
all together
We'll get to more detail in a bit, but here is a quick lil example:
# This scope is shared between functions. It's an image. All asset-pointers are uint32.
var logo:uint32
# called when your cart loads
proc load() {.null0.} =
# put logo.png file, in your cart, in assets/
logo = load_image("assets/logo.png")
# called on every frame
proc update(time:uint) {.null0.} =
# clear the screen
clear_background(BLACK)
# draw the logo
draw_image(logo, 64, 47)
# called when the cart is unloaded
proc unload() {.null0.} =
echo "Ok, bye."
You can find more examples here (opens in a new tab).
lifecycle
These functions get called at different times in the life of your cart.
load()
This function will kick things off. It's called once when the cart first loads, and in most languages, you can put stuff in the top-level of your game, instead. In C, you can also use main()
which makes it look more "regular" and makes it so you don't need any special build-flags (to disable main
.)
proc load() {.null0.} =
# do you setup stuff here
discard
update()
This function is called on every frame. Use it to update things. It receives the elapsed time in milliseconds, as an int. You can use this to animate things.
proc update(time:uint) {.null0.} =
# do your update stuff here
discard
unload()
This function is called when the cart exits.
proc unload() {.null0.} =
# do your shutdown stuff here
discard
input
All input is routed through these, so you can respond to it. Learn more here.
buttonDown()
This is called whenever a button or directional is pressed on the virtual gamepad.
proc buttonDown(button: Button) {.null0.} =
# handle your input here
discard
buttonUp()
This is called whenever a button or directional is un-pressed on the virtual gamepad.
proc buttonUp(button: Button) {.null0.} =
# handle your input here
discard