4. Drawing Shapes and Sprites
We already drew a rectangle, but let's explore some other available Raylib drawing procedures.
package main
import rl "vendor:raylib"
main :: proc() {
rl.InitWindow(640, 360, "Program Video Games!")
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.ClearBackground(rl.BLACK)
rl.DrawCircle(200, 200, 25, rl.BLUE)
rl.DrawRing({200, 150}, 20, 40, 0, 270, 32, rl.GREEN)
rl.DrawTriangle({300, 300}, {340, 300}, {320, 240}, rl.YELLOW)
rl.DrawPoly({400, 310}, 6, 100, 0, {0, 255, 255, 128})
rl.DrawRectangle(400, 310, 50, 50, rl.RED)
rl.EndDrawing()
}
}
This simple program shows just some of the shape drawing capabilities of Raylib.
More importantly, it shows that the order of drawing matters and that transparency is supported out of the box.
You'll notice, when you run the program, that the cyan hexagon is partially covering the yellow triangle, making it green.
As well as that, the red rectangle is drawn last, so it totally covers part of the hexagon.
Bringing it back to what's most useful for us, let's take a look at drawing sprites.
We'll start with drawing static sprites and expand to fully animated ones later in the course.
package main
import rl "vendor:raylib"
main :: proc() {
rl.InitWindow(1280, 720, "Program Video Games!")
texture_background := rl.LoadTexture("assets/textures/szadi_set5_background1.png")
texture_decoration := rl.LoadTexture("assets/textures/szadi_set5_decorative1.png")
camera := rl.Camera2D{zoom = 2}
for !rl.WindowShouldClose() {
rl.BeginDrawing()
rl.BeginMode2D(camera)
rl.ClearBackground(rl.BLACK)
rl.DrawTexture(texture_background, 0, 0, rl.WHITE)
rl.DrawTextureRec(texture_decoration, {0, 0, 32, 32}, {100, 100}, rl.WHITE)
rl.DrawTextureRec(texture_decoration, {32, 32, 32, 32}, {200, 100}, rl.WHITE)
rl.EndMode2D()
rl.EndDrawing()
}
}