Optimization and ready jump to godot

This commit is contained in:
LittleSheep 2024-07-19 10:55:05 +08:00
parent 3fd38c73b6
commit b67353c1ae
6 changed files with 36 additions and 25 deletions

View File

@ -36,8 +36,6 @@ func (p *Player) Update() {
if keys[sdl.SCANCODE_RIGHT] == 1 || keys[sdl.SCANCODE_D] == 1 {
p.Move(1, 0)
}
p.BaseObject.Update()
}
func (p *Player) Move(deltaX, deltaY float64) {

View File

@ -19,12 +19,7 @@ type BaseObject struct {
func (p *BaseObject) Create() {}
func (p *BaseObject) Update() {
// Update each child
for _, child := range p.Children {
child.Update()
}
}
func (p *BaseObject) Update() {}
func (p *BaseObject) AddChild(child Object) {
p.Children = append(p.Children, child)

View File

@ -5,6 +5,7 @@ import "github.com/veandco/go-sdl2/sdl"
type BaseWidget struct {
BaseObject
IsVisible bool
Position Vector2D
Size Vector2D
}

View File

@ -6,7 +6,8 @@ import (
)
type RootObject struct {
BaseObject
Children []Object
Overlays []Object
Analyzer *PerformanceAnalyzer
}
@ -21,6 +22,14 @@ func NewRootObject() *RootObject {
return in
}
func (p *RootObject) AddChild(child Object) {
p.Children = append(p.Children, child)
}
func (p *RootObject) AddOverlay(overlay Object) {
p.Overlays = append(p.Overlays, overlay)
}
func (p *RootObject) RunEventLoop(tickDuration time.Duration) {
for {
startTime := time.Now()
@ -45,7 +54,12 @@ func (p *RootObject) ForEachChildren(cb func(child Object)) {
}
}
caller(p)
for _, child := range p.Children {
caller(child)
}
for _, child := range p.Overlays {
caller(child)
}
}
func (p *RootObject) HandleUserEvent(event sdl.Event) {
@ -77,7 +91,9 @@ func (p *RootObject) HandleUserEvent(event sdl.Event) {
}
func (p *RootObject) Update() {
p.BaseObject.Update()
p.ForEachChildren(func(child Object) {
child.Update()
})
// Check collision
p.ForEachChildren(func(child Object) {
@ -110,10 +126,6 @@ func (p *RootObject) Draw(pen *sdl.Renderer) {
// Render each child
p.ForEachChildren(func(child Object) {
if child == p {
// Skip the current to prevent infinite drawing
return
}
if drawableChild, ok := child.(DrawableObject); ok {
drawableChild.Draw(pen)
}

View File

@ -2,7 +2,6 @@ package ui
import (
"git.solsynth.dev/highland/codingland/pkg/internal/land"
"git.solsynth.dev/highland/codingland/pkg/internal/land/renderer"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
"log"
@ -20,6 +19,10 @@ type ButtonWidget struct {
}
func (p *ButtonWidget) Draw(pen *sdl.Renderer) {
if !p.BaseWidget.IsVisible {
return
}
var borderColor, bgColor sdl.Color
if p.isClicking {
borderColor = sdl.Color{R: 200, G: 0, B: 0, A: 255}
@ -35,18 +38,19 @@ func (p *ButtonWidget) Draw(pen *sdl.Renderer) {
var borderWidth int32 = 2
pen.SetDrawColor(borderColor.R, borderColor.G, borderColor.B, borderColor.A)
renderer.FillRoundedRect(pen, &sdl.Rect{
pen.FillRect(&sdl.Rect{
X: int32(p.Position.X),
Y: int32(p.Position.Y),
W: int32(p.Size.X),
H: int32(p.Size.Y),
}, borderColor, 16)
renderer.FillRoundedRect(pen, &sdl.Rect{
})
pen.SetDrawColor(bgColor.R, bgColor.G, bgColor.B, bgColor.A)
pen.FillRect(&sdl.Rect{
X: int32(p.Position.X) + borderWidth/2,
Y: int32(p.Position.Y) + borderWidth/2,
W: int32(p.Size.X) - borderWidth,
H: int32(p.Size.Y) - borderWidth,
}, bgColor, 16)
})
surface, err := p.Font.RenderUTF8Blended(p.Label, sdl.Color{R: 255, G: 255, B: 255, A: 255})
if err != nil {

View File

@ -62,8 +62,9 @@ func main() {
Position: land.Vector2D{X: 50, Y: 50},
Size: land.Vector2D{X: 50, Y: 50},
})
root.AddChild(&ui.ButtonWidget{
root.AddOverlay(&ui.ButtonWidget{
BaseWidget: land.BaseWidget{
IsVisible: true,
Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25},
Size: land.Vector2D{X: 120, Y: 50},
},