movement and sprite flipping
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package engine
|
||||||
|
|
||||||
|
import sdl "vendor:sdl3"
|
||||||
|
|
||||||
|
Key :: enum {
|
||||||
|
A,
|
||||||
|
D,
|
||||||
|
Left,
|
||||||
|
Right,
|
||||||
|
}
|
||||||
|
|
||||||
|
key_down :: proc(key: Key) -> bool {
|
||||||
|
keys := sdl.GetKeyboardState(nil)
|
||||||
|
if keys == nil do return false
|
||||||
|
|
||||||
|
scan_code: sdl.Scancode
|
||||||
|
switch key {
|
||||||
|
case .A:
|
||||||
|
scan_code = .A
|
||||||
|
case .D:
|
||||||
|
scan_code = .D
|
||||||
|
case .Left:
|
||||||
|
scan_code = .LEFT
|
||||||
|
case .Right:
|
||||||
|
scan_code = .RIGHT
|
||||||
|
}
|
||||||
|
return keys[scan_code]
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ Sprite :: struct {
|
|||||||
clip: string,
|
clip: string,
|
||||||
frame: int,
|
frame: int,
|
||||||
time: f32,
|
time: f32,
|
||||||
|
flip_x: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
spawn_sprite :: proc(
|
spawn_sprite :: proc(
|
||||||
@@ -119,6 +120,7 @@ draw_sprite :: proc(app: ^App, sprite: ^Sprite) {
|
|||||||
v0 := f32(rect[1]) / tex_h
|
v0 := f32(rect[1]) / tex_h
|
||||||
u1 := f32(rect[0] + rect[2]) / tex_w
|
u1 := f32(rect[0] + rect[2]) / tex_w
|
||||||
v1 := f32(rect[1] + rect[3]) / tex_h
|
v1 := f32(rect[1] + rect[3]) / tex_h
|
||||||
|
if sprite.flip_x do u0, u1 = u1, u0
|
||||||
|
|
||||||
// Two triangles: (0,1,2) and (0,2,3)
|
// Two triangles: (0,1,2) and (0,2,3)
|
||||||
verts := [6]Vertex {
|
verts := [6]Vertex {
|
||||||
@@ -196,3 +198,8 @@ set_sprite_clip :: proc(sprite: ^Sprite, clip: string) {
|
|||||||
sprite_quad_origin :: proc(position: Vec2, size: Vec2, pivot: [2]f32) -> Vec2 {
|
sprite_quad_origin :: proc(position: Vec2, size: Vec2, pivot: [2]f32) -> Vec2 {
|
||||||
return {position.x - size.x * pivot[0], position.y - size.y * pivot[1]}
|
return {position.x - size.x * pivot[0], position.y - size.y * pivot[1]}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
set_sprite_flip_x :: proc(sprite: ^Sprite, flip: bool) {
|
||||||
|
if sprite == nil do return
|
||||||
|
sprite.flip_x = flip
|
||||||
|
}
|
||||||
|
|||||||
@@ -15,12 +15,30 @@ main :: proc() {
|
|||||||
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
toad := eng.spawn_sprite(&data, {400, 500}, "idle", 0)
|
||||||
|
|
||||||
last := eng.now_seconds()
|
last := eng.now_seconds()
|
||||||
|
SPEED :: f32(200)
|
||||||
|
|
||||||
for eng.events() {
|
for eng.events() {
|
||||||
now := eng.now_seconds()
|
now := eng.now_seconds()
|
||||||
dt := f32(now - last)
|
dt := f32(now - last)
|
||||||
last = now
|
last = now
|
||||||
|
|
||||||
|
left := eng.key_down(.A) || eng.key_down(.Left)
|
||||||
|
right := eng.key_down(.D) || eng.key_down(.Right)
|
||||||
|
|
||||||
|
if left || right {
|
||||||
|
eng.set_sprite_clip(&toad, "walk")
|
||||||
|
if left {
|
||||||
|
toad.position.x -= SPEED * dt
|
||||||
|
toad.flip_x = true
|
||||||
|
}
|
||||||
|
|
||||||
|
if right {
|
||||||
|
toad.position.x += SPEED * dt
|
||||||
|
toad.flip_x = false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
eng.set_sprite_clip(&toad, "idle")
|
||||||
|
}
|
||||||
eng.update_sprite(&toad, dt)
|
eng.update_sprite(&toad, dt)
|
||||||
|
|
||||||
eng.begin_frame(&app)
|
eng.begin_frame(&app)
|
||||||
|
|||||||
Reference in New Issue
Block a user