-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
90 lines (84 loc) · 1.99 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"github.com/20zinnm/smasteroids/assets"
"github.com/20zinnm/smasteroids/scenes"
"github.com/20zinnm/smasteroids/sprites"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
"math"
"math/rand"
"time"
)
//go:generate packr
func init() {
rand.Seed(int64(time.Now().Nanosecond()))
}
func run() {
var monitor = pixelgl.PrimaryMonitor()
for _, m := range pixelgl.Monitors() {
xp, _ := m.PhysicalSize()
xo, _ := monitor.PhysicalSize()
if xp > xo {
monitor = m
}
}
width, height := monitor.Size()
cfg := pixelgl.WindowConfig{
Title: "SMasteroids",
Bounds: pixel.R(0, 0, width, height),
VSync: true,
Undecorated: true,
Monitor: monitor,
Icon: []pixel.Picture{assets.Icon},
}
win, err := pixelgl.NewWindow(cfg)
defer win.Destroy()
if err != nil {
panic(err)
}
win.SetSmooth(true)
win.SetCursorVisible(false)
win.MouseScroll()
// initialize sprites
sprites.Init()
// set up canvas bounds
//scenes.GameBounds = win.Bounds().Moved(win.Bounds().Center().Scaled(-1))
CenterWindow(win)
scenes.TransitionTo(scenes.NewMainscreenScene())
tickDuration := time.Duration(math.Floor((1.0/60.0)*math.Pow10(9))) * time.Nanosecond
ticker := time.NewTicker(tickDuration)
defer ticker.Stop()
for !win.Closed() {
for _, m := range pixelgl.Monitors() {
xwidth, _ := m.Size()
if xwidth > width {
monitor = m
win.SetMonitor(monitor)
width, height = monitor.Size()
win.SetBounds(pixel.R(0, 0, width, height), )
win.SetMatrix(pixel.IM.Scaled(pixel.ZV, width/scenes.GameBounds.W()))
}
}
win.Clear(colornames.Black)
scenes.Render(win)
if win.Pressed(pixelgl.KeyEscape) {
win.SetClosed(true)
}
win.Update()
<-ticker.C // wait for next tick
}
}
func CenterWindow(win *pixelgl.Window) {
x, y := pixelgl.PrimaryMonitor().Size()
width, height := win.Bounds().Size().XY()
win.SetPos(
pixel.V(
x/2-width/2,
y/2-height/2,
),
)
}
func main() {
pixelgl.Run(run)
}