-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtictacgo.go
117 lines (94 loc) · 2.3 KB
/
tictacgo.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
package main
import (
"fmt"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
"os"
)
const (
screenWidth = 600 // must be a square
screenHeight = 600
frameRate = 30
)
func main() {
setup()
}
func setup() {
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
fmt.Println("initializing SDL:", err)
return
}
defer sdl.Quit()
window, err := sdl.CreateWindow(
"Tic Tac Go",
sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
screenWidth, screenHeight, sdl.WINDOW_OPENGL)
if err != nil {
fmt.Println("initializing window:", err)
return
}
defer window.Destroy()
renderer, err := sdl.CreateRenderer(window, -1, sdl.RENDERER_ACCELERATED)
if err != nil {
fmt.Println("initializing renderer:", err)
return
}
defer renderer.Destroy()
img.Init(img.INIT_JPG | img.INIT_PNG)
textures := loadGophers(renderer)
b := newBoard(screenWidth, renderer, textures)
b.drawBoard()
running := true
for running {
for event := sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch t := event.(type) {
case *sdl.QuitEvent:
println("Quit")
running = false
break
case *sdl.MouseButtonEvent:
gameOver, winner := b.handleMouseButtonEvent(t)
b.drawBoard()
if gameOver {
running = false
if winner == 0 {
fmt.Printf("It's a draw!!!\n")
} else {
fmt.Printf("Player %d wins!!!\n", winner)
}
break
}
}
}
sdl.Delay(1000 / frameRate)
}
}
func loadGophers(renderer *sdl.Renderer) []*sdl.Texture {
img1 := make(chan *sdl.Surface, 1)
img2 := make(chan *sdl.Surface, 1)
go loadImage("assets/gopher1.png", img1)
go loadImage("assets/gopher2.png", img2)
gopher1 := <-img1
gopher2 := <-img2
gopher1Texture, err := renderer.CreateTextureFromSurface(gopher1)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
os.Exit(5)
}
gopher2Texture, err := renderer.CreateTextureFromSurface(gopher2)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create texture: %s\n", err)
os.Exit(5)
}
gopher1Texture.Unlock()
gopher2Texture.Unlock()
return []*sdl.Texture{gopher1Texture, gopher2Texture}
}
func loadImage(path string, res chan *sdl.Surface) {
image, err := img.Load(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to gopher2 PNG: %s\n", err)
os.Exit(4)
}
res <- image
}