2024-07-18 04:23:16 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.solsynth.dev/highland/codingland/pkg/internal/entities"
|
2024-07-18 05:59:06 +00:00
|
|
|
"git.solsynth.dev/highland/codingland/pkg/internal/land"
|
2024-07-18 13:03:21 +00:00
|
|
|
"git.solsynth.dev/highland/codingland/pkg/internal/land/ui"
|
2024-07-18 09:21:25 +00:00
|
|
|
"git.solsynth.dev/highland/codingland/pkg/internal/tiles"
|
2024-07-18 13:03:21 +00:00
|
|
|
"github.com/veandco/go-sdl2/ttf"
|
2024-07-18 04:23:16 +00:00
|
|
|
"log"
|
|
|
|
"runtime"
|
2024-07-18 06:47:41 +00:00
|
|
|
"time"
|
2024-07-18 04:23:16 +00:00
|
|
|
|
|
|
|
"github.com/veandco/go-sdl2/sdl"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-07-18 06:47:41 +00:00
|
|
|
windowWidth = 720
|
2024-07-18 04:23:16 +00:00
|
|
|
windowHeight = 480
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
runtime.LockOSThread()
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
|
|
|
|
log.Fatalf("Failed to initialize SDL: %s", err)
|
|
|
|
}
|
|
|
|
defer sdl.Quit()
|
|
|
|
|
2024-07-18 13:03:21 +00:00
|
|
|
if err := ttf.Init(); err != nil {
|
|
|
|
log.Fatalf("Failed to initialize SDL ttf: %s", err)
|
|
|
|
}
|
|
|
|
defer ttf.Quit()
|
|
|
|
|
2024-07-18 04:23:16 +00:00
|
|
|
window, err := sdl.CreateWindow("CodingLand", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, windowWidth, windowHeight, sdl.WINDOW_SHOWN)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create window: %s", err)
|
|
|
|
}
|
|
|
|
defer window.Destroy()
|
|
|
|
|
|
|
|
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED|sdl.RENDERER_PRESENTVSYNC)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to create renderer: %s", err)
|
|
|
|
}
|
|
|
|
defer renderer.Destroy()
|
|
|
|
|
2024-07-18 14:59:03 +00:00
|
|
|
font, err := ttf.OpenFont("assets/fonts/NotoSans.ttf", 14)
|
2024-07-18 13:03:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Failed to open font: %s", err)
|
2024-07-18 14:59:03 +00:00
|
|
|
} else {
|
|
|
|
font.SetStyle(ttf.STYLE_BOLD)
|
2024-07-18 13:03:21 +00:00
|
|
|
}
|
|
|
|
defer font.Close()
|
|
|
|
|
2024-07-18 06:47:41 +00:00
|
|
|
root := land.NewRootObject()
|
2024-07-18 05:59:06 +00:00
|
|
|
root.AddChild(&entities.Player{
|
2024-07-18 06:47:41 +00:00
|
|
|
Position: land.Vector2D{X: windowWidth/2 - 25, Y: windowHeight/2 - 25},
|
|
|
|
Size: land.Vector2D{X: 50, Y: 50},
|
2024-07-18 05:59:06 +00:00
|
|
|
})
|
2024-07-18 09:21:25 +00:00
|
|
|
root.AddChild(&tiles.Tile{
|
2024-07-18 09:46:36 +00:00
|
|
|
Position: land.Vector2D{X: 50, Y: 50},
|
2024-07-18 09:21:25 +00:00
|
|
|
Size: land.Vector2D{X: 50, Y: 50},
|
|
|
|
})
|
2024-07-19 02:55:05 +00:00
|
|
|
root.AddOverlay(&ui.ButtonWidget{
|
2024-07-18 14:59:03 +00:00
|
|
|
BaseWidget: land.BaseWidget{
|
2024-07-19 02:55:05 +00:00
|
|
|
IsVisible: true,
|
|
|
|
Position: land.Vector2D{X: windowWidth/2 - 60, Y: windowHeight/2 - 25},
|
|
|
|
Size: land.Vector2D{X: 120, Y: 50},
|
2024-07-18 14:59:03 +00:00
|
|
|
},
|
|
|
|
Label: "Hello, World!",
|
|
|
|
Font: font,
|
|
|
|
OnClick: func(_ land.Vector2D) {
|
|
|
|
log.Println("你惊扰了古希腊掌控按钮的神")
|
|
|
|
},
|
|
|
|
})
|
2024-07-18 04:23:16 +00:00
|
|
|
|
2024-07-18 06:47:41 +00:00
|
|
|
// 10ms delay use be 100tps average
|
|
|
|
go root.RunEventLoop(10 * time.Millisecond)
|
|
|
|
|
2024-07-18 04:23:16 +00:00
|
|
|
running := true
|
|
|
|
for running {
|
|
|
|
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
|
|
|
|
switch event.(type) {
|
|
|
|
case *sdl.QuitEvent:
|
|
|
|
running = false
|
2024-07-18 14:59:03 +00:00
|
|
|
default:
|
2024-07-18 09:46:36 +00:00
|
|
|
root.HandleUserEvent(event)
|
2024-07-18 04:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-18 14:59:03 +00:00
|
|
|
// Draw the entire object tree
|
2024-07-18 05:59:06 +00:00
|
|
|
root.Draw(renderer)
|
2024-07-18 04:23:16 +00:00
|
|
|
|
2024-07-18 14:59:03 +00:00
|
|
|
// Show on the screen
|
2024-07-18 04:23:16 +00:00
|
|
|
renderer.Present()
|
|
|
|
}
|
|
|
|
}
|