forked from ricktimmis/gobandit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (97 loc) · 2.9 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
package main
import (
"fmt"
"github.com/spf13/viper"
"github.com/veandco/go-sdl2/sdl"
"github.com/veandco/go-sdl2/ttf"
)
// CONFIGURATION GLOBALS & CONSTANTS
// Window dimensions
var width = int32(900)
var height = int32(600)
func main() {
// FIXME Implement loadable configuration from a local .cfg file
viper.SetDefault("FilePath", "./resources/tile_images/Fruits/")
viper.SetDefault("FontFile", "./resources/fonts/open-sans/OpenSans-Regular.ttf")
// SDL Pointer initialisation
var window *sdl.Window
var renderer *sdl.Renderer
var surface *sdl.Surface
var event sdl.Event
// FIXME - I think this is redundant, but we need to fix up error management, and returns errors up the
// the chain of functional calls - Thanks Dave Cheney
var err error
if err := sdl.Init(sdl.INIT_EVERYTHING); err != nil {
panic(err)
}
defer sdl.Quit()
/*
Remember Window->Surface->Renderer
SDL Creates a Window and within in it is a Surface. We get a handle to the Surface using GetSurface
Create a renderer for that surface. Use that Renderer to build the scene, and then update the Surface
simples right ?
*/
window, err = sdl.CreateWindow("Go Bandit", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
width, height, sdl.WINDOW_SHOWN)
if err != nil {
panic(err)
}
defer window.Destroy()
// Initialise a ttf font renderer
if err := ttf.Init(); err != nil {
panic(err)
}
defer ttf.Quit()
font, err := ttf.OpenFont((viper.GetString("FontFile")), 320)
if err != nil {
panic(err)
}
// Board is a model of the game, it holds a tileset for each column
var board = new(Board)
// Initialise Score, which saves us from having to pass the board pointer each time we call
// the checkScore function - see scene.go
var score = new(Score)
score.init(board)
// Initialise the Board
ti := new(Tile) // FIXME This is coupling to tiles.go, and it must not
board.Init(3, 4, ti)
// Control is where the action is.... more to come here
var controller = control{
false,
board,
score,
window,
font,
renderer,
surface,
}
// Initialise the controller
controller.init()
// Game Main loop
running := true
for running {
window.UpdateSurface() //<- I think we need to do Clear, and Present instead
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
println("Quit")
running = false
break
case *sdl.KeyboardEvent:
//fmt.Printf("[%d ms] Keyboard\ttype:%d\tsym:%c\tmodifiers:%d\tstate:%d\trepeat:%d\n",
// event., event.Type, t.Keysym.Sym, t.Keysym.Mod, t.State, t.Repeat)
fmt.Printf("KEY PRESSED\n")
err = controller.spin()
if err != nil {
fmt.Errorf("Hmm something went wrong : %v", err)
}
case *sdl.MouseButtonEvent:
fmt.Printf("MOUSE CLICK -- Current Score = %d\n", board.score)
err = controller.spin()
if err != nil {
fmt.Errorf("Hmm something went wrong : %v", err)
}
}
}
}
}