2020-12-30
(Previous article: Jaylib - 2, Docs)
I’m learning Jaylib/Raylib myself as I write this, but here are a few morsels I can provide:
When you draw, coordinates are in pixels and start at the top left of your window. So, if you think of horizontal as the x-direction and vertical as the y-direction, then the origin [0, 0] is at the top left.
Raylib uses double-buffering, and your program runs in a loop that goes like:
check if looping should continue (if not, exits loop)
begin-drawing (starts you off with a new buffer)
clear screen
update the game/world
draw the game/world
end-drawing (swaps buffers)
To keep organized, it’s useful to separate out your functions into init-game
, update-game
, and draw-game
(see below).
So that your game loop doesn’t speed out of control, you first call (set-target-fps 60)
so it knows when enough is enough.
Here’s an example of how that all looks:
#!/usr/bin/env janet
import jaylib :as jl)
(
def screen-width 800)
(def screen-height 400)
(
defn init-game
(
[]
(jl/init-window screen-width
screen-height"Title Goes Here")
60)
(jl/set-target-fps
(jl/hide-cursor))
defn update-game
(
[]comment "Nothing to update at the moment."))
(
defn draw-game
(
[]let [[x y] (jl/get-mouse-position)]
(40 :lime :red)))
(jl/draw-circle-gradient x y
defn main
(
[& args]
(init-game)while (not (jl/window-should-close))
(
(jl/begin-drawing)0 0 0])
(jl/clear-background [
(update-game)
(draw-game)
(jl/end-drawing)) (jl/close-window))
(Please excuse the temporary lack of proper Janet syntax-highlighting here.)
Running this will produce a window where the colorful circle follows your mouse around:
Some comments on that code:
jl/
” prefix on the jaylib functions.init-game
, and possibly update them as needed in update-game
. You’d also need to wrap your drawing code (inside draw-game
) with (begin-mode-2d my-camera)
and (end-mode-2d)
. (Possibly coming up in a future article here.)(begin-mode-3d my-3d-camera)
and (end-mode-3d)
.(Next up: Jaylib - 4, The Bouncing Square)
Notice any errors, glaring omissions, or just clumsy wording? Please email me at approximately <jgabriele©fastmail·fm>. Thank you!