delta time!

This commit is contained in:
Morrígan 2025-06-26 22:32:55 +02:00
parent 22d1469b0f
commit 266adb051e
Signed by: morrigan
GPG key ID: CACB010F463A77D0
2 changed files with 22 additions and 8 deletions

View file

@ -11,6 +11,8 @@ Game :: struct {
render: ^sdl3.Renderer,
units: [dynamic]Unit,
player: Unit,
time: u64,
delta: f32,
input_state: map[sdl3.Scancode]bool,
}
@ -30,10 +32,16 @@ main :: proc() {
append(&units, Unit{faction = Faction.Allied, position = Position{x = 500, y = 500}, hp = 100})
append(&units, Unit{faction = Faction.Enemy, position = Position{x = 700, y = 700}, hp = 100})
input_state := make(map[sdl3.Scancode]bool)
time := sdl3.GetTicksNS()
game := Game {
render = render,
units = units,
player = Unit{faction = Faction.Player, position = Position{x = 255, y = 255}, hp = 100},
time = time,
delta = 0.0,
input_state = input_state,
}
@ -65,4 +73,9 @@ RenderGame :: proc(game: ^Game) {
RenderUnit(game.player, game)
sdl3.RenderPresent(game.render)
new_time := sdl3.GetTicksNS()
delta_ns := new_time - game.time
game.time = new_time
game.delta = f32(delta_ns) / f32(sdl3.NS_PER_SECOND)
}

View file

@ -3,18 +3,19 @@ package main
import "core:fmt"
import "vendor:sdl3"
SPEED: f32 : 200
HandleInput :: proc(game: ^Game, event: sdl3.Event) {
effect := event.type == sdl3.EventType.KEY_DOWN ? true : false
game.input_state[event.key.scancode] = effect
}
MovePlayer :: proc(game: ^Game) {
x: f32 = game.input_state[sdl3.Scancode.W] ? 1 : 0
x = game.input_state[sdl3.Scancode.S] ? -1 : 0
y: f32 = game.input_state[sdl3.Scancode.A] ? 1 : 0
y = game.input_state[sdl3.Scancode.D] ? -1 : 0
fmt.println(x, y)
game.player.position.y += y
game.player.position.x += x
y: f32 = game.input_state[sdl3.Scancode.W] ? -SPEED : 0
y += game.input_state[sdl3.Scancode.S] ? SPEED : 0
x: f32 = game.input_state[sdl3.Scancode.A] ? -SPEED : 0
x += game.input_state[sdl3.Scancode.D] ? SPEED : 0
game.player.position.y += y * game.delta
game.player.position.x += x * game.delta
}