diff --git a/game/game.go b/game/game.go index 51d9be3..0fbd955 100644 --- a/game/game.go +++ b/game/game.go @@ -23,26 +23,26 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu rotation_speed := 0.01 speed := 0.02 - g.OnUpdate(func() error { + g.OnUpdate(func(delta float64) error { if inputHandler.IsKeyJustReleased(sdl.SCANCODE_SPACE) { rotation_speed = -rotation_speed } if inputHandler.IsKeyDown(sdl.SCANCODE_D) { - g.Position.X += speed + g.Position.X += speed * delta } if inputHandler.IsKeyDown(sdl.SCANCODE_A) { - g.Position.X -= speed + g.Position.X -= speed * delta } if inputHandler.IsKeyDown(sdl.SCANCODE_S) { - g.Position.Y += speed + g.Position.Y += speed * delta } if inputHandler.IsKeyDown(sdl.SCANCODE_W) { - g.Position.Y -= speed + g.Position.Y -= speed * delta } - g.Rotation += rotation_speed + g.Rotation += rotation_speed * delta return nil }) @@ -51,7 +51,7 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu controller.SetProcessMode(node.ActiveProcessMode) defer root.AddChild(&controller) - controller.OnUpdate(func() error { + controller.OnUpdate(func(float64) error { if inputHandler.IsKeyJustPressed(sdl.SCANCODE_ESCAPE) { if g.IsProcessing() { g.SetProcessMode(node.PausedProcessMode) diff --git a/go.mod b/go.mod index a5895d0..5fab34f 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module github.com/manleydev/golang-game-framework go 1.18 -require github.com/veandco/go-sdl2 v0.4.21 +require github.com/veandco/go-sdl2 v0.4.25 diff --git a/go.sum b/go.sum index e3e4666..78fd645 100644 --- a/go.sum +++ b/go.sum @@ -2,3 +2,5 @@ github.com/veandco/go-sdl2 v0.4.21 h1:85AtFYv3vSPZvyOOxkJ03sHoJe8ESjPSZLr0KueQtV github.com/veandco/go-sdl2 v0.4.21/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY= github.com/veandco/go-sdl2 v0.4.24 h1:J+OCnPp0yfas4DAG13e3kIgC84mNxWGa3gpWYxrQQfI= github.com/veandco/go-sdl2 v0.4.24/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY= +github.com/veandco/go-sdl2 v0.4.25 h1:J5ac3KKOccp/0xGJA1PaNYKPUcZm19IxhDGs8lJofPI= +github.com/veandco/go-sdl2 v0.4.25/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY= diff --git a/input/input_handler.go b/input/input_handler.go index cd4627a..704b1be 100644 --- a/input/input_handler.go +++ b/input/input_handler.go @@ -4,5 +4,5 @@ type InputHandler interface { IsKeyDown(uint) bool IsKeyJustPressed(uint) bool IsKeyJustReleased(uint) bool - Update() error + Update(delta float64) error } diff --git a/main b/main new file mode 100755 index 0000000..ef94caf Binary files /dev/null and b/main differ diff --git a/main.go b/main.go index d19598f..d159a3b 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "log" + "time" "github.com/manleydev/golang-game-framework/game" "github.com/manleydev/golang-game-framework/input" @@ -27,16 +28,18 @@ func main() { game.Run(&root, renderer, input) running := true + var lastTimestamp int64 = time.Now().UnixMicro() + var delta float64 = 0.0 for running { - if err := root.Update(); err != nil { + if err := root.Update(delta); err != nil { log.Fatal("Update: ", err) } - if err := input.Update(); err != nil { + if err := input.Update(delta); err != nil { log.Fatal("Input Update: ", err) } - if err := renderer.Update(); err != nil { + if err := renderer.Update(delta); err != nil { log.Fatal("Renderer Update: ", err) } @@ -47,5 +50,9 @@ func main() { log.Fatal("Renderer Draw: ", err) } + newTimestamp := time.Now().UnixMicro() + delta = float64(lastTimestamp) / float64(newTimestamp) + lastTimestamp = newTimestamp + } } diff --git a/node/camera2d.go b/node/camera2d.go index 151123e..a359335 100644 --- a/node/camera2d.go +++ b/node/camera2d.go @@ -13,8 +13,8 @@ type Camera2D struct { renderer *rendering.Renderer2D } -func (camera *Camera2D) Update() error { - if err := camera.Node2D.Update(); err != nil { +func (camera *Camera2D) Update(delta float64) error { + if err := camera.Node2D.Update(delta); err != nil { return err } diff --git a/node/inode.go b/node/inode.go index 8c0c99b..34fa387 100644 --- a/node/inode.go +++ b/node/inode.go @@ -10,7 +10,7 @@ const ( type INode interface { GetName() string - Update() error + Update(delta float64) error ready(INode) error IsProcessing() bool GetProcessMode() ProcessMode diff --git a/node/node.go b/node/node.go index 6843c9d..536236b 100644 --- a/node/node.go +++ b/node/node.go @@ -14,7 +14,7 @@ type Node struct { processMode ProcessMode onReadyMethods []func() error - onUpdateMethods []func() error + onUpdateMethods []func(float64) error onDraw2dMethods []func(rendering.Renderer2D) error } @@ -36,16 +36,16 @@ func (node *Node) OnReady(callback func() error) { node.onReadyMethods = append(node.onReadyMethods, callback) } -func (node *Node) Update() error { +func (node *Node) Update(delta float64) error { for _, child := range node.children { - if err := child.Update(); err != nil { + if err := child.Update(delta); err != nil { return err } } if node.IsProcessing() { for _, updateMethod := range node.onUpdateMethods { - if err := updateMethod(); err != nil { + if err := updateMethod(delta); err != nil { return err } } @@ -54,7 +54,7 @@ func (node *Node) Update() error { return nil } -func (node *Node) OnUpdate(callback func() error) { +func (node *Node) OnUpdate(callback func(float64) error) { if callback == nil { return } diff --git a/rendering/render2d.go b/rendering/render2d.go index 855f343..b83b4b4 100644 --- a/rendering/render2d.go +++ b/rendering/render2d.go @@ -8,7 +8,7 @@ type Renderer2D interface { DrawTexture2D(DrawTexture2DInput) error Draw() error - Update() error + Update(delta float64) error Destroy() SetCamera(*Camera2D) error } diff --git a/sdl/input_handler.go b/sdl/input_handler.go index cf529ae..7313190 100644 --- a/sdl/input_handler.go +++ b/sdl/input_handler.go @@ -37,7 +37,7 @@ func (input *SdlInputHandler) IsKeyJustReleased(key uint) bool { return false } -func (input *SdlInputHandler) Update() error { +func (input *SdlInputHandler) Update(delta float64) error { copy(input.lastKeyState, input.keyState) input.keyState = sdl.GetKeyboardState() diff --git a/sdl/render2d.go b/sdl/render2d.go index f889bf2..6028ff0 100644 --- a/sdl/render2d.go +++ b/sdl/render2d.go @@ -69,7 +69,7 @@ func (w *SdlRenderer2D) SetCamera(camera *rendering.Camera2D) error { return nil } -func (w *SdlRenderer2D) Update() error { +func (w *SdlRenderer2D) Update(delta float64) error { for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() { switch event.(type) { case *sdl.QuitEvent: