-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrol.go
288 lines (257 loc) · 7.63 KB
/
control.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
package main
import (
"fmt"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
"math/rand"
"os"
"strconv"
"time"
)
type control struct {
ready bool // Defines if GO button is lit or not
board *Board //FIXME - Switch this out for an interface
score *Score //FIXME - Switch this out for an interface
window *sdl.Window
font *ttf.Font
renderer *sdl.Renderer // Initialised here
surface *sdl.Surface // Initialised here
}
type controller interface {
spin()
hold()
nudge()
init()
}
func (c *control) init() {
var err error
// Instantiate the other window elements
c.surface, err = c.window.GetSurface()
if err != nil {
panic(err)
}
c.renderer, err = sdl.CreateSoftwareRenderer(c.surface)
if err != nil {
panic(err)
}
/* BACKGROUND IMAGE
FIXME This should be replaced with a folder traversing function that can load multiple backgrounds
once complete, then drawbackground can be refactored into drawboard too.
*/
bgimage := "./resources/backgrounds/dragon_of_the_north.jpg"
// Check file exists
_, err = os.Open(bgimage)
if err != nil {
panic(err)
}
c.drawbackground(bgimage)
c.drawboard()
return
}
// All the interesting functions are here !
// Implementation functions that do the work and the rendering
// This is the stuff you want to mess with to get different game behaviour
func (c *control) spin() error {
//err = renderer.Clear()
//if err != nil {
// panic(err)
//}
c.spinimation()
return nil
}
func (c *control) hold() {
return
}
func (c *control) nudge() {
return
}
// FIXME - Requires a file path to the background image
func (c *control) drawbackground(i string) error {
// Initialise pointer ;-)
var texture *sdl.Texture
/* Fixes issue #2 Crashes with segev null pointer Kubuntu 18.04
https://github.com/ricktimmis/gobandit/issues/2
Turns out that although SDL2 provides default src:nil dst:nil in renderer.Copy
on some systems the c.go lowlevel binding to C blows up. Probably because
src and dst use the unsafe.Pointer package. Declaring a rectangle layer the same
size as the window, and initialising it gives us a definite destination for the Copy.
3 hours it took me to debug that!!
*/
var dst = sdl.Rect{0, 0, 900, 600}
texture, err := img.LoadTexture(c.renderer, i)
if err != nil {
return err
}
err = c.renderer.Copy(texture, nil, &dst)
if err != nil {
return err
}
return err
}
func (c *control) drawboard() error {
// Work out positions sizes based upon Window Size and number
// of Columns and Rows in the board, iterate through the board applying face
// textures
// FIXME Refactor this int/int32 recasting, which will require board Cols and Rows to be int32,
colwidth := int32(int(width) / (c.board.Cols + 2))
rowheight := int32(int(height) / (c.board.Rows + 2))
x := colwidth
y := rowheight
for row := 0; row < (c.board.Rows); row++ {
for col := 0; col < (c.board.Cols); col++ {
var dst = sdl.Rect{x, y, colwidth, rowheight}
p := c.board.Tiles[row][col].GetImage()
texture, err := img.LoadTexture(c.renderer, p)
if err != nil {
return fmt.Errorf("could not load background image : %v", err)
}
err = c.renderer.Copy(texture, nil, &dst)
if err != nil {
return fmt.Errorf("could not render background image : %v", err)
}
x = x + colwidth
}
// Reset back to first column
x = colwidth
y = y + rowheight
}
/* Draw control buttons
*/
for col := 0; col < (c.board.Cols); col++ {
var dst = sdl.Rect{(x + 10), y, (colwidth - 20), rowheight}
// FIXME - Implement a range of buttons, including hold, nudge etc.
// Need to load these through a function so that we can involve
// logic here to decide if we are offering hold, nudge, or are
// holding, or nudging
button := "./resources/controller_assets/nudge_button.png"
texture, err := img.LoadTexture(c.renderer, button)
if err != nil {
return fmt.Errorf("could not load background image : %v", err)
}
err = c.renderer.Copy(texture, nil, &dst)
if err != nil {
return fmt.Errorf("could not render background image : %v", err)
}
x = x + colwidth
}
/* Draw Go button
This checks the controllers ready state and draws one of two buttons
FIXME - This is a horrible, hardcoding shortcut to get the code into release, as it
was required for a time bound magazine article...
*/
var dst = sdl.Rect{(width - 125), y, 80, 80}
var button = ""
if c.ready {
button = "./resources/controller_assets/go_button_rdy.png"
} else {
button = "./resources/controller_assets/go_button.png"
}
texture, err := img.LoadTexture(c.renderer, button)
if err != nil {
return fmt.Errorf("could not load background image : %v", err)
}
err = c.renderer.Copy(texture, nil, &dst)
if err != nil {
return fmt.Errorf("could not render background image : %v", err)
}
/* Draw Score Panel
Avoiding a fixed Score rectangle by calculating the Score window as a percentage of the window.
20% (1/5) of width and height
*/
wide := width / 3
high := height / 10
x = (width / 2) - (wide / 2)
y = (height / 25)
var scoreborder = sdl.Rect{x, y, wide, high}
var scorepanel = sdl.Rect{(x + 5), (y + 5), (wide - 10), (high - 10)}
c.renderer.SetDrawColor(255, 230, 15, 255)
c.renderer.FillRect(&scoreborder)
c.renderer.SetDrawColor(58, 58, 58, 255)
c.renderer.FillRect(&scorepanel)
// Score
scorestring := strconv.Itoa(int(c.board.score))
textcolor := sdl.Color{255, 230, 15, 20}
scoretextsurface, err := c.font.RenderUTF8Solid(scorestring, textcolor)
if err != nil {
return err
}
scoretexture, err := c.renderer.CreateTextureFromSurface(scoretextsurface)
if err != nil {
return err
}
if c.renderer.Copy(scoretexture, nil, &scorepanel); err != nil {
fmt.Errorf("Failed to render Score texture")
}
//if r.FillRect(&scorepanel); err !=nil {
// fmt.Errorf("Failed to fill Score panel")
//}
//RenderUTF8Solid(text string, color sdl.Color) (*sdl.Surface, error)
// Draw the controls
return nil
}
func (ctrl *control) playnext() error {
for c := 0; c < ctrl.board.Cols; c++ {
for r := 0; r < ctrl.board.Rows; r++ {
ctrl.board.Tiles[r][c].Next()
}
}
return nil
}
func (c *control) checkscore() (int, error) {
// Define rules here to evaluate board and generate a Score
// Rule 1 - Check each row for adjacent matching tile
rule1 := func(b *Board) int {
sum := 0
for r := 0; r != b.Rows; r++ {
for c := 0; c < (b.Cols - 1); c++ {
if (b.Tiles[r][c].GetFace()) == (b.Tiles[r][c+1].GetFace()) {
sum = sum + b.Tiles[r][c].GetValue()
fmt.Printf("Row %d Columns %d and %d Matching %s \n", r, c, c+1, (b.Tiles[r][c].GetFace()))
// FIXME Call or setup a display routine, to highlight the match
}
}
}
return sum
}
v, err := c.score.evaluate(rule1)
return v, err
}
func (c *control) spinimation() error {
//iterate := false
//doiteration := false
//iterations := 0
//start := 0
// FIXME This logic is messy and confusing
// Why? - to avoid constantly calling into the SDL library to update the surface, which
// caused the Window renderer to stop performing updates. I have used conditional logic
// to define which calls are made when.
rand.Seed(time.Now().UnixNano())
min := 5
max := 20
iterations := (rand.Intn(max-min+1) + min)
for i := 0; i < iterations; i++ {
c.ready = false // Defines if GO button is lit or not
c.playnext()
sdl.Delay(10)
c.drawboard()
err := c.window.UpdateSurface()
if err != nil {
return err
}
}
c.ready = true
v, err := c.checkscore()
if err != nil {
return err
}
if v > 0 {
fmt.Printf("Match found and scored %d \n", v)
}
c.board.ScoreAdd(v)
c.drawboard()
if c.window.UpdateSurface(); err != nil {
return err
}
return nil
}