-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (108 loc) · 2.75 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
119
120
121
122
package main
import (
"embed"
"fmt"
"github.com/kallahir/solitaire/board"
"github.com/kallahir/solitaire/card"
"github.com/kallahir/solitaire/renderwindow"
"github.com/kallahir/solitaire/utils"
"github.com/veandco/go-sdl2/sdl"
)
//go:embed resources/cards
var fs embed.FS
func main() {
fmt.Println("Welcome to Solitaire!")
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()
rw, err := renderwindow.New("Solitaire", board.NumberOfColumns*card.Width+(board.NumberOfColumns+1)*card.HSpacing, 1024)
if err != nil {
panic(err)
}
defer rw.CleanUp()
resources, err := fs.ReadDir("resources/cards")
if err != nil {
panic(err)
}
textures := make(map[string]*sdl.Texture)
for _, file := range resources {
if file.IsDir() {
continue
}
texture, err := rw.LoadTextureFromEmbedFS(fs, fmt.Sprintf("resources/cards/%s", file.Name()))
if err != nil {
panic(err)
}
fileName, err := utils.RemoveFileExtension(file.Name())
if err != nil {
panic(err)
}
textures[fileName] = texture
}
game := board.New(rw, textures)
var x, y int32
for game.IsRunning {
for event := sdl.WaitEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
fmt.Println("Closing Solitaire!")
game.IsRunning = false
case *sdl.MouseMotionEvent:
x, y = t.X, t.Y
case *sdl.MouseButtonEvent:
switch {
case t.Button == sdl.BUTTON_LEFT:
game.HandleClick(t.X, t.Y, t.State)
case t.Button == sdl.BUTTON_RIGHT && t.State == sdl.PRESSED:
game.HandleRightClick(t.X, t.Y)
}
}
}
rw.Clear()
game.Render(rw, x, y)
if game.IsOver() {
switch GetUserInput(rw.Window) {
case -1:
fmt.Println("No selection...")
case 1:
fmt.Println("Restarting game...")
game = board.New(rw, textures)
case 2:
fmt.Println("Closing Solitaire!")
game.IsRunning = false
}
}
rw.Display()
sdl.Delay(16)
}
}
func GetUserInput(w *sdl.Window) int32 {
buttons := []sdl.MessageBoxButtonData{
{Flags: sdl.MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT, ButtonID: 1, Text: "Yes"},
{Flags: sdl.MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT, ButtonID: 2, Text: "Quit"},
}
colorScheme := sdl.MessageBoxColorScheme{
Colors: [5]sdl.MessageBoxColor{
{R: 255, G: 0, B: 0},
{R: 0, G: 255, B: 0},
{R: 255, G: 255, B: 0},
{R: 0, G: 0, B: 255},
{R: 255, G: 0, B: 255},
},
}
messageboxdata := sdl.MessageBoxData{
Flags: sdl.MESSAGEBOX_INFORMATION,
Window: w,
Title: "Congratulations!\nYou Won!",
Message: "Do you want to play another match?",
Buttons: buttons,
ColorScheme: &colorScheme,
}
var buttonid int32
var err error
if buttonid, err = sdl.ShowMessageBox(&messageboxdata); err != nil {
panic(err)
}
return buttonid
}