/
main.go
141 lines (121 loc) · 2.76 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package main
import (
"sdl"
"fmt"
"blokus"
)
var running = true
var screen *sdl.Surface
var cellSize int
var offsetX int
var offsetY int
const margin = 4
const borderWidth = 1
const boardStart = margin + borderWidth
const gridSize = 2*margin + 2*borderWidth + blokus.BoardSize
const emptyCellColor = 0x0
func main() {
fmt.Println("loaded", len(blokus.Tiles), "shapes")
blokus.Board[2][4] = blokus.Red
if sdl.Init(sdl.INIT_EVERYTHING) != 0 {
panic(sdl.GetError())
}
resize(640, 480)
sdl.WM_SetCaption("Blokus", "")
for running {
e := &sdl.Event{}
for e.Poll() {
switch e.Type {
case sdl.QUIT:
running = false
case sdl.KEYDOWN:
keyDown(e.Keyboard())
case sdl.KEYUP:
keyUp(e.Keyboard())
case sdl.MOUSEBUTTONDOWN:
mouseDown(e.MouseButton())
case sdl.VIDEORESIZE:
r := e.Resize()
resize(int(r.W), int(r.H))
default:
}
if !running {
break
}
}
draw()
sdl.Delay(25)
}
sdl.Quit()
}
func resize(width int, height int) {
screen = sdl.SetVideoMode(width, height, 32, sdl.RESIZABLE)
if screen == nil {
panic(sdl.GetError())
}
gridSizePixels := min(width, height)
cellSize = gridSizePixels / gridSize
offsetX = (width - gridSizePixels) / 2
offsetY = (height - gridSizePixels) / 2
}
func min(a int, b int) int {
if a < b {
return a
}
return b
}
func stop() {
running = false
}
func draw() {
screen.FillRect(nil, 0xa0a0a0)
drawBorder()
screen.Flip()
}
func drawBorder() {
for y := 0; y < blokus.BoardSize; y++ {
for x := 0; x < blokus.BoardSize; x++ {
var color uint32
switch blokus.Board[x][y] {
case blokus.Empty:
color = 0x0
case blokus.Blue:
color = 0x0000ff
case blokus.Yellow:
color = 0xffff00
case blokus.Red:
color = 0xff0000
case blokus.Green:
color = 0x00ff00
}
screen.FillRect(rect(pixelX(x)+1, pixelY(y)+1, cellSize-2, cellSize-2), color)
}
}
}
func pixelX(cellX int) int {
return offsetX + cellSize*(boardStart+cellX)
}
func pixelY(cellY int) int {
return offsetY + cellSize*(boardStart+cellY)
}
func rect(x int, y int, w int, h int) *sdl.Rect {
return &sdl.Rect{int16(x), int16(y), uint16(w), uint16(h)}
}
func keyUp(k *sdl.KeyboardEvent) {
//debugKeyboardEvent(k)
}
func keyDown(k *sdl.KeyboardEvent) {
//debugKeyboardEvent(k)
if k.Keysym.Sym == 27 {
stop()
}
}
func debugKeyboardEvent(k *sdl.KeyboardEvent) {
println("")
println(k.Keysym.Sym, ": ", sdl.GetKeyName(sdl.Key(k.Keysym.Sym)))
fmt.Printf("Type: %02x Which: %02x State: %02x Pad: %02x\n", k.Type, k.Which, k.State, k.Pad0[0])
fmt.Printf("Scancode: %02x Sym: %08x Mod: %04x Unicode: %04x\n", k.Keysym.Scancode, k.Keysym.Sym, k.Keysym.Mod, k.Keysym.Unicode)
}
func mouseDown(mouseButton *sdl.MouseButtonEvent) {
println("Click:", mouseButton.Button, mouseButton.X, mouseButton.Y)
}