mirror of
https://github.com/yeslayla/golang-game-framework.git
synced 2025-01-14 04:53:32 +01:00
Add delta
value between frames
This commit is contained in:
parent
987ef9502f
commit
9ccd2cf2cb
14
game/game.go
14
game/game.go
@ -23,26 +23,26 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu
|
|||||||
|
|
||||||
rotation_speed := 0.01
|
rotation_speed := 0.01
|
||||||
speed := 0.02
|
speed := 0.02
|
||||||
g.OnUpdate(func() error {
|
g.OnUpdate(func(delta float64) error {
|
||||||
|
|
||||||
if inputHandler.IsKeyJustReleased(sdl.SCANCODE_SPACE) {
|
if inputHandler.IsKeyJustReleased(sdl.SCANCODE_SPACE) {
|
||||||
rotation_speed = -rotation_speed
|
rotation_speed = -rotation_speed
|
||||||
}
|
}
|
||||||
|
|
||||||
if inputHandler.IsKeyDown(sdl.SCANCODE_D) {
|
if inputHandler.IsKeyDown(sdl.SCANCODE_D) {
|
||||||
g.Position.X += speed
|
g.Position.X += speed * delta
|
||||||
}
|
}
|
||||||
if inputHandler.IsKeyDown(sdl.SCANCODE_A) {
|
if inputHandler.IsKeyDown(sdl.SCANCODE_A) {
|
||||||
g.Position.X -= speed
|
g.Position.X -= speed * delta
|
||||||
}
|
}
|
||||||
if inputHandler.IsKeyDown(sdl.SCANCODE_S) {
|
if inputHandler.IsKeyDown(sdl.SCANCODE_S) {
|
||||||
g.Position.Y += speed
|
g.Position.Y += speed * delta
|
||||||
}
|
}
|
||||||
if inputHandler.IsKeyDown(sdl.SCANCODE_W) {
|
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
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ func Run(root *node.Node, renderer rendering.Renderer2D, inputHandler input.Inpu
|
|||||||
controller.SetProcessMode(node.ActiveProcessMode)
|
controller.SetProcessMode(node.ActiveProcessMode)
|
||||||
defer root.AddChild(&controller)
|
defer root.AddChild(&controller)
|
||||||
|
|
||||||
controller.OnUpdate(func() error {
|
controller.OnUpdate(func(float64) error {
|
||||||
if inputHandler.IsKeyJustPressed(sdl.SCANCODE_ESCAPE) {
|
if inputHandler.IsKeyJustPressed(sdl.SCANCODE_ESCAPE) {
|
||||||
if g.IsProcessing() {
|
if g.IsProcessing() {
|
||||||
g.SetProcessMode(node.PausedProcessMode)
|
g.SetProcessMode(node.PausedProcessMode)
|
||||||
|
2
go.mod
2
go.mod
@ -2,4 +2,4 @@ module github.com/manleydev/golang-game-framework
|
|||||||
|
|
||||||
go 1.18
|
go 1.18
|
||||||
|
|
||||||
require github.com/veandco/go-sdl2 v0.4.21
|
require github.com/veandco/go-sdl2 v0.4.25
|
||||||
|
2
go.sum
2
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.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 h1:J+OCnPp0yfas4DAG13e3kIgC84mNxWGa3gpWYxrQQfI=
|
||||||
github.com/veandco/go-sdl2 v0.4.24/go.mod h1:OROqMhHD43nT4/i9crJukyVecjPNYYuCofep6SNiAjY=
|
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=
|
||||||
|
@ -4,5 +4,5 @@ type InputHandler interface {
|
|||||||
IsKeyDown(uint) bool
|
IsKeyDown(uint) bool
|
||||||
IsKeyJustPressed(uint) bool
|
IsKeyJustPressed(uint) bool
|
||||||
IsKeyJustReleased(uint) bool
|
IsKeyJustReleased(uint) bool
|
||||||
Update() error
|
Update(delta float64) error
|
||||||
}
|
}
|
||||||
|
13
main.go
13
main.go
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/manleydev/golang-game-framework/game"
|
"github.com/manleydev/golang-game-framework/game"
|
||||||
"github.com/manleydev/golang-game-framework/input"
|
"github.com/manleydev/golang-game-framework/input"
|
||||||
@ -27,16 +28,18 @@ func main() {
|
|||||||
game.Run(&root, renderer, input)
|
game.Run(&root, renderer, input)
|
||||||
|
|
||||||
running := true
|
running := true
|
||||||
|
var lastTimestamp int64 = time.Now().UnixMicro()
|
||||||
|
var delta float64 = 0.0
|
||||||
for running {
|
for running {
|
||||||
|
|
||||||
if err := root.Update(); err != nil {
|
if err := root.Update(delta); err != nil {
|
||||||
log.Fatal("Update: ", err)
|
log.Fatal("Update: ", err)
|
||||||
}
|
}
|
||||||
if err := input.Update(); err != nil {
|
if err := input.Update(delta); err != nil {
|
||||||
log.Fatal("Input Update: ", err)
|
log.Fatal("Input Update: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := renderer.Update(); err != nil {
|
if err := renderer.Update(delta); err != nil {
|
||||||
log.Fatal("Renderer Update: ", err)
|
log.Fatal("Renderer Update: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,5 +50,9 @@ func main() {
|
|||||||
log.Fatal("Renderer Draw: ", err)
|
log.Fatal("Renderer Draw: ", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
newTimestamp := time.Now().UnixMicro()
|
||||||
|
delta = float64(lastTimestamp) / float64(newTimestamp)
|
||||||
|
lastTimestamp = newTimestamp
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,8 +13,8 @@ type Camera2D struct {
|
|||||||
renderer *rendering.Renderer2D
|
renderer *rendering.Renderer2D
|
||||||
}
|
}
|
||||||
|
|
||||||
func (camera *Camera2D) Update() error {
|
func (camera *Camera2D) Update(delta float64) error {
|
||||||
if err := camera.Node2D.Update(); err != nil {
|
if err := camera.Node2D.Update(delta); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ const (
|
|||||||
|
|
||||||
type INode interface {
|
type INode interface {
|
||||||
GetName() string
|
GetName() string
|
||||||
Update() error
|
Update(delta float64) error
|
||||||
ready(INode) error
|
ready(INode) error
|
||||||
IsProcessing() bool
|
IsProcessing() bool
|
||||||
GetProcessMode() ProcessMode
|
GetProcessMode() ProcessMode
|
||||||
|
10
node/node.go
10
node/node.go
@ -14,7 +14,7 @@ type Node struct {
|
|||||||
processMode ProcessMode
|
processMode ProcessMode
|
||||||
|
|
||||||
onReadyMethods []func() error
|
onReadyMethods []func() error
|
||||||
onUpdateMethods []func() error
|
onUpdateMethods []func(float64) error
|
||||||
onDraw2dMethods []func(rendering.Renderer2D) error
|
onDraw2dMethods []func(rendering.Renderer2D) error
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -36,16 +36,16 @@ func (node *Node) OnReady(callback func() error) {
|
|||||||
node.onReadyMethods = append(node.onReadyMethods, callback)
|
node.onReadyMethods = append(node.onReadyMethods, callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *Node) Update() error {
|
func (node *Node) Update(delta float64) error {
|
||||||
for _, child := range node.children {
|
for _, child := range node.children {
|
||||||
if err := child.Update(); err != nil {
|
if err := child.Update(delta); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if node.IsProcessing() {
|
if node.IsProcessing() {
|
||||||
for _, updateMethod := range node.onUpdateMethods {
|
for _, updateMethod := range node.onUpdateMethods {
|
||||||
if err := updateMethod(); err != nil {
|
if err := updateMethod(delta); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,7 +54,7 @@ func (node *Node) Update() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *Node) OnUpdate(callback func() error) {
|
func (node *Node) OnUpdate(callback func(float64) error) {
|
||||||
if callback == nil {
|
if callback == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ type Renderer2D interface {
|
|||||||
DrawTexture2D(DrawTexture2DInput) error
|
DrawTexture2D(DrawTexture2DInput) error
|
||||||
|
|
||||||
Draw() error
|
Draw() error
|
||||||
Update() error
|
Update(delta float64) error
|
||||||
Destroy()
|
Destroy()
|
||||||
SetCamera(*Camera2D) error
|
SetCamera(*Camera2D) error
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ func (input *SdlInputHandler) IsKeyJustReleased(key uint) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (input *SdlInputHandler) Update() error {
|
func (input *SdlInputHandler) Update(delta float64) error {
|
||||||
copy(input.lastKeyState, input.keyState)
|
copy(input.lastKeyState, input.keyState)
|
||||||
input.keyState = sdl.GetKeyboardState()
|
input.keyState = sdl.GetKeyboardState()
|
||||||
|
|
||||||
|
@ -69,7 +69,7 @@ func (w *SdlRenderer2D) SetCamera(camera *rendering.Camera2D) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *SdlRenderer2D) Update() error {
|
func (w *SdlRenderer2D) Update(delta float64) error {
|
||||||
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
||||||
switch event.(type) {
|
switch event.(type) {
|
||||||
case *sdl.QuitEvent:
|
case *sdl.QuitEvent:
|
||||||
|
Loading…
Reference in New Issue
Block a user