CodingLand/pkg/internal/land/root.go
2024-07-18 14:47:41 +08:00

58 lines
937 B
Go

package land
import (
"github.com/veandco/go-sdl2/sdl"
"time"
)
type RootObject struct {
BaseObject
Analyzer *PerformanceAnalyzer
}
func NewRootObject() *RootObject {
in := &RootObject{
Analyzer: &PerformanceAnalyzer{},
}
go in.Analyzer.KeepResetting(1 * time.Second)
return in
}
func (p *RootObject) RunEventLoop(tickDuration time.Duration) {
for {
startTime := time.Now()
p.Update()
elapsed := time.Since(startTime)
hangDuration := tickDuration - elapsed
if hangDuration > 0 {
time.Sleep(hangDuration)
}
}
}
func (p *RootObject) Update() {
p.BaseObject.Update()
p.Analyzer.Tick()
}
func (p *RootObject) Draw(pen *sdl.Renderer) {
// Render background and clear previous state
pen.SetDrawColor(77, 77, 77, 255)
pen.Clear()
// Render each child
for _, child := range p.Children {
if drawableChild, ok := child.(DrawableObject); ok {
drawableChild.Draw(pen)
}
}
p.Analyzer.Draw()
}