/
main.go
118 lines (88 loc) · 2.48 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Eggdrop is an SDL game written for the purposes of learning Go.
package main
import (
"fmt"
"runtime"
"github.com/beejjorgensen/eggdrop/gamecontext"
"github.com/beejjorgensen/eggdrop/gamemanager"
"github.com/beejjorgensen/eggdrop/introstate"
"github.com/beejjorgensen/eggdrop/playstate"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/ttf"
)
func init() {
// SDL wants to be on the main thread, OR ELSE!
runtime.LockOSThread()
}
func sdlInit() {
sdl.Init(sdl.INIT_EVERYTHING)
// init the image subsystem
imgFlags := img.INIT_PNG | img.INIT_JPG
if imgFlags != img.Init(imgFlags) {
panic(fmt.Sprintf("Error initializing img: %v\n", img.GetError()))
}
// init the TTF subsystem
if err := ttf.Init(); err != nil {
panic(fmt.Sprintf("Error initializing ttf: %v\n", err))
}
}
func createMainWindow() {
var err error
var mainWindow *sdl.Window
gc := gamecontext.GContext
gc.WindowWidth = 800
gc.WindowHeight = 600
mainWindow, err = sdl.CreateWindow("Eggdrop!", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED, gc.WindowWidth, gc.WindowHeight, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
gc.MainSurface, err = mainWindow.GetSurface()
if err != nil {
panic(err)
}
gc.PixelFormatEnum, err = mainWindow.GetPixelFormat()
if err != nil {
panic(err)
}
gc.PixelFormat, err = sdl.AllocFormat(uint(gc.PixelFormatEnum)) // TODO why the cast? Seems to work?
if err != nil {
panic(err)
}
gc.MainWindow = mainWindow
}
func main() {
sdlInit()
gm := gamemanager.GGameManager
gc := gamecontext.GContext
createMainWindow()
defer gc.MainWindow.Destroy()
intro := &introstate.IntroState{}
play := &playstate.PlayState{}
gm.RegisterMode(gamemanager.GameModeIntro, intro)
gm.RegisterMode(gamemanager.GameModePlay, play)
done := false
// Keep handy for use in the loop
mainWindow := gc.MainWindow
mainWindowSurface := gc.MainSurface
gm.SetMode(gamemanager.GameModeIntro)
gm.SetEventMode(gamemanager.GameManagerEventDriven)
for done == false {
event := gm.GetNextEvent()
for ; event != nil; event = sdl.PollEvent() {
done = done || gm.HandleEvent(&event)
switch event := event.(type) {
case *sdl.WindowEvent:
//fmt.Printf("Window: %#v\n", event)
//fmt.Printf("Event: %t %t\n", event.Event, sdl.WINDOWEVENT_CLOSE)
if event.Event == sdl.WINDOWEVENT_CLOSE {
done = done || true
}
}
}
gm.Render(mainWindowSurface)
mainWindow.UpdateSurface()
gm.DelayToNextFrame()
}
sdl.Quit()
}