PVG
5. Handling Input — Program Video Games

5. Handling Input

Raylib comes in for us in regards to handling input.

There are a bunch of Raylib procedures that let us do easy input handling for mouse, keyboard, and gamepad.

package main

import rl "vendor:raylib"
import "core:math/linalg"

WINDOW_WIDTH :: 1280
WINDOW_HEIGHT :: 720
ZOOM :: 2
BG_COLOR :: rl.Color{113, 202, 211, 255}

main :: proc() {
    rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Program Video Games!")

    texture_background := rl.LoadTexture("assets/textures/bg_clouds_back.png")
    texture_tileset := rl.LoadTexture("assets/textures/tileset.png")

    camera := rl.Camera2D{
        zoom = ZOOM,
    }

    pos: rl.Vector2

    for !rl.WindowShouldClose() {
        delta := rl.GetFrameTime()
        input_vector: rl.Vector2

        if rl.IsKeyDown(.LEFT) do input_vector.x -= 1
        if rl.IsKeyDown(.RIGHT) do input_vector.x += 1
        if rl.IsKeyDown(.UP) do input_vector.y -= 1
        if rl.IsKeyDown(.DOWN) do input_vector.y += 1

        input_vector = linalg.normalize0(input_vector)

        pos += input_vector * 100 * delta

        rl.BeginDrawing()
        rl.BeginMode2D(camera)
        rl.ClearBackground(BG_COLOR)

        rl.DrawTexture(texture_background, 0, 0, rl.WHITE)
        rl.DrawTextureRec(texture_tileset, {0, 0, 16, 16}, pos, rl.WHITE)

        rl.EndMode2D()
        rl.EndDrawing()
    }
}