-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.go
347 lines (323 loc) · 10.8 KB
/
render.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"strconv"
"log"
"github.com/veandco/go-sdl2/sdl"
gfx "github.com/veandco/go-sdl2/sdl_gfx"
"fmt"
)
const (
WindowWidth = 800
WindowHeight = 600
WorldWidth = 3200
WorldHeight = 2400
MaxZoom = 8
MinZoom = 1
DefaultZoom = 2
DefaultX = 0
DefaultY = 0
ZoomStep = 0.25
DiskWorldRadius = 1000
DiskWorldX = WorldWidth/2
DiskWorldY = WorldHeight/2
FontSize = 8
CameraMoveSpeed = 100
ProvenceMenuWidth = 320
ProvenceMenuHeight = 200
TopBarHeight = 26
)
type Camera struct {
x int16
y int16
ZoomLevel int8
}
func (c *Camera) MaxX() int16 {
zoom := float64(c.ZoomLevel) * ZoomStep
cameraWidth := int16(WindowWidth/zoom)
return WorldWidth-cameraWidth
}
func (c *Camera) MaxY() int16 {
zoom := float64(c.ZoomLevel) * ZoomStep
cameraHeight := int16(WindowHeight/zoom)
return WorldHeight-cameraHeight
}
func (c *Camera) ZoomIn() bool {
if c.ZoomLevel < MaxZoom {
c.ZoomLevel++
return true
}
return false
}
func (c *Camera) ZoomOut() bool {
if c.ZoomLevel > MinZoom {
c.ZoomLevel--
return true
}
return false
}
func (c *Camera) correct() {
maxX := c.MaxX()
if c.x > maxX {
c.x = maxX
}
maxY := c.MaxY()
if c.y > maxY {
c.y = maxY
}
if c.x < 0 {
c.x = 0
}
if c.y < 0 {
c.y = 0
}
}
func (c *Camera) ConvertXY(x, y int16) (int16, int16) {
zoom := float64(c.ZoomLevel) * ZoomStep
xScaled := int16(zoom*float64(x - c.x))
yScaled := int16(zoom*float64(y - c.y))
return xScaled, yScaled
}
func (c *Camera) ConvertR(r int16) int16 {
zoom := float64(c.ZoomLevel) * ZoomStep
return int16 (zoom * float64(r))
}
func (c *Camera) ConvertToWorld(x, y int32) (int32, int32) {
zoom := float64(c.ZoomLevel) * ZoomStep
xScaled := int32(float64(x)/zoom) + int32(c.x)
yScaled := int32(float64(y)/zoom) + int32(c.y)
return xScaled, yScaled
}
func NewCamera() *Camera {
c := new(Camera)
c.x = DefaultX
c.y = DefaultY
c.ZoomLevel = DefaultZoom
return c
}
type polygon struct {
vx []int16
vy []int16
namex int16
namey int16
}
var polygonMap map[string]*polygon
func drawProvence(p *Provence, r *sdl.Renderer) error {
poly := polygonMap[p.Tag]
if poly == nil {
return fmt.Errorf("Provence tag \"%s\" not in polygon map", p.Tag)
}
vertexCount := len(poly.vx)
vx := make([]int16, vertexCount)
vy := make([]int16, vertexCount)
for i, _ := range(vx) {
vx[i], vy[i] = GameCam.ConvertXY(poly.vx[i], poly.vy[i])
}
ok := gfx.FilledPolygonColor(r, vx, vy, p.Colour)
if !ok {
return fmt.Errorf("Error drawing provence \"%s\"", p.Tag)
}
if GameCam.ZoomLevel > 2 {
nameX, nameY := GameCam.ConvertXY(poly.namex, poly.namey)
ok = gfx.StringColor(r, int(nameX), int(nameY), p.Name, colourProvenceName)
if !ok {
return fmt.Errorf("Error drawing name for provence \"%s\"", p.Tag)
}
cx, cy := p.Centre()
symX, symY := GameCam.ConvertXY(cx, cy)
var colour sdl.Color
if p.Owner != nil {
colour = p.Owner.Colour
} else {
colour = colourBlack
}
sym := p.Level.Symbol()
if sym == nil {
log.Panicf("No symbol found for provence level \"%v\" (%d)", p.Level, int(p.Level))
}
symX -= int16(sym.Width/2)
symY -= int16(sym.Height/2)
box := sdl.Rect{int32(symX), int32(symY), int32(sym.Width), int32(sym.Height)}
r.SetDrawColor(colour.R, colour.G, colour.B, colour.A)
r.FillRect(&box)
r.Copy(sym.Texture, nil, &box)
}
return nil
}
func drawWorld(r *sdl.Renderer) error {
r.SetDrawColor(0,0,0,255)
r.Clear()
circleX, circleY := GameCam.ConvertXY(DiskWorldX, DiskWorldY)
circleR := GameCam.ConvertR(DiskWorldRadius)
gfx.FilledCircleColor(r, int(circleX), int(circleY), int(circleR), colourWorldBackground)
for _, p := range(Provences) {
err := drawProvence(p, r)
if err != nil {
log.Panicln(err)
}
}
//gfx.CircleColor(r, int(circleX), int(circleY), int(circleR), colourRed)
drawButton(r, ButtonEndTurn, WindowWidth-ButtonCentreX, WindowHeight-ButtonCentreY)
return nil
}
func drawButton(r *sdl.Renderer, ID ButtonID, x, y int32) {
buttonRect := sdl.Rect{0, 0, ButtonWidth, ButtonHeight}
x_mod := x - ButtonWidth/2
y_mod := y - ButtonHeight/2
targetRect := sdl.Rect{x_mod, y_mod, ButtonWidth, ButtonHeight}
button := buttons[ID]
if button == nil {
log.Panicf("Invalid Button ID %d\n", int(ID))
}
r.Copy(button, &buttonRect, &targetRect)
}
func drawMenu(r *sdl.Renderer) {
MenuTop := int32(WindowHeight/2-MenuHeight/2)
menuBackground := sdl.Rect{WindowWidth/2-MenuWidth/2, MenuTop, MenuWidth, MenuHeight}
r.SetDrawColor(colourMenuBackground.R, colourMenuBackground.G, colourMenuBackground.B, colourMenuBackground.A)
r.FillRect(&menuBackground)
y := MenuTop + 2*ButtonHeight
drawButton(r, ButtonResume, WindowWidth/2, y)
y += 2*ButtonHeight
drawButton(r, ButtonNewGame, WindowWidth/2, y)
y += 2*ButtonHeight
drawButton(r, ButtonQuit, WindowWidth/2, y)
}
const (
TopBarValueOffset = 10
TopBarSeperator = 50
TopBarTextOffset = 3
)
func drawTopMenu(r *sdl.Renderer) {
PlayerNation := Nations["PLA"]
if PlayerNation == nil {
return
}
topBarBackground := sdl.Rect{0, 0, WindowWidth, TopBarHeight}
r.SetDrawColor(colourTopBarBackground.R, colourTopBarBackground.G, colourTopBarBackground.B, colourTopBarBackground.A)
r.FillRect(&topBarBackground)
x := int32(15)
var sym *Symbol
var drawRect sdl.Rect
sym = SymbolMap[SymbolFood]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Food), colourWhite)
x += TopBarSeperator
sym = SymbolMap[SymbolMetal]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Metal), colourWhite)
x += TopBarSeperator
x += TopBarSeperator
sym = SymbolMap[SymbolSword]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Equipment[Sword]), colourWhite)
x += TopBarSeperator
sym = SymbolMap[SymbolPike]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Equipment[Pike]), colourWhite)
x += TopBarSeperator
sym = SymbolMap[SymbolCrossbow]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Equipment[Crossbow]), colourWhite)
x += TopBarSeperator
sym = SymbolMap[SymbolBow]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Equipment[Bow]), colourWhite)
x += TopBarSeperator
sym = SymbolMap[SymbolLance]
drawRect = sdl.Rect{x, int32(TopBarHeight/2-sym.Height/2), int32(sym.Width), int32(sym.Height) }
r.Copy(sym.Texture, nil, &drawRect)
x += sym.Width + TopBarValueOffset
gfx.StringColor(r, int(x), TopBarHeight/2 - TopBarTextOffset, strconv.Itoa(PlayerNation.Equipment[Lance]), colourWhite)
}
const (
ProvenceMenuNameOffsetX = 20
ProvenceMenuNameOffsetY = 15
ProvenceMenuOwnerOffsetX = 20
ProvenceMenuOwnerOffsetY = 35
ProvenceMenuPopOffsetX = 20
ProvenceMenuPopOffsetY = 45
ProvenceMenuMetalOffsetX = 20
ProvenceMenuMetalOffsetY = 55
ProvenceMenuLevelOffsetX = 20
ProvenceMenuLevelOffsetY = 25
ProvenceMenuArmiesOffsetX = 20
ProvenceMenuArmiesOffsetY = 65
ProvenceMenuBuildOffsetX = 20
ProvenceMenuBuildOffsetY = 75
ProvenceMenuTrainOffsetX = 20
ProvenceMenuTrainOffsetY = 85
)
func drawProvenceMenu(r *sdl.Renderer) {
MenuTop := WindowHeight - ProvenceMenuHeight
r.SetDrawColor(colourProvenceMenuBackground.R, colourProvenceMenuBackground.G, colourProvenceMenuBackground.B, colourProvenceMenuBackground.A)
menuBackground := sdl.Rect{0, int32(MenuTop), ProvenceMenuWidth, ProvenceMenuHeight}
r.FillRect(&menuBackground)
gfx.StringColor(r, ProvenceMenuNameOffsetX, MenuTop + ProvenceMenuNameOffsetY, SelectedProvence.Name, colourWhite)
popString := fmt.Sprintf("Population: %d", SelectedProvence.Population)
gfx.StringColor(r, ProvenceMenuPopOffsetX, MenuTop + ProvenceMenuPopOffsetY, popString, colourWhite)
metalString := fmt.Sprintf("Unmined Metal: %d", SelectedProvence.MetalCount)
gfx.StringColor(r, ProvenceMenuMetalOffsetX, MenuTop + ProvenceMenuMetalOffsetY, metalString, colourWhite)
levelString := fmt.Sprintf("Provence Level: %v", SelectedProvence.Level)
gfx.StringColor(r, ProvenceMenuLevelOffsetX, MenuTop + ProvenceMenuLevelOffsetY, levelString, colourWhite)
var ownerName string
if SelectedProvence.Owner != nil {
ownerName = SelectedProvence.Owner.Name
} else {
ownerName = "Unowned"
}
ownerString := fmt.Sprintf("Owner: %s", ownerName)
gfx.StringColor(r, ProvenceMenuOwnerOffsetX, MenuTop + ProvenceMenuOwnerOffsetY, ownerString, colourWhite)
player := Nations["PLA"]
if player == nil {
// stop here
return
}
if player != SelectedProvence.Owner {
// stop here
return
}
// Terrible efficency, but in a HURRY
localFriendlies := len(FilterArmiesPresent(player, SelectedProvence))
armiesString := fmt.Sprintf("Armies: %d", localFriendlies)
gfx.StringColor(r, ProvenceMenuArmiesOffsetX, MenuTop + ProvenceMenuArmiesOffsetY, armiesString, colourWhite)
ProvenceOrders := ProvincialOrders(player.OrderQueue, SelectedProvence)
var trainOrder *TrainOrder
var buildOrder *BuildOrder
for _, order := range(ProvenceOrders) {
switch o := order.(type) {
case *BuildOrder:
buildOrder = o
case *TrainOrder:
trainOrder = o
}
}
var buildString string
var trainString string
if buildOrder != nil {
buildString = fmt.Sprintf("Building: %v (%d remaining)", buildOrder.itemType, buildOrder.count)
} else {
buildString = "Nothing Being Built"
}
gfx.StringColor(r, ProvenceMenuBuildOffsetX, MenuTop + ProvenceMenuBuildOffsetY, buildString, colourWhite)
if trainOrder != nil {
trainString = fmt.Sprintf("TTraining: %v (%d remaining)", trainOrder.unitType, buildOrder.count)
} else {
trainString = "No troops being trained"
}
gfx.StringColor(r, ProvenceMenuTrainOffsetX, MenuTop + ProvenceMenuTrainOffsetY, trainString, colourWhite)
drawButton(r, ButtonArmies, ButtonArmiesX + ButtonWidth/2, ButtonArmiesY + ButtonHeight/2)
drawButton(r, ButtonBuild, ButtonBuildX + ButtonWidth/2, ButtonBuildY + ButtonHeight/2)
drawButton(r, ButtonTrain, ButtonTrainX + ButtonWidth/2, ButtonTrainY + ButtonHeight/2)
}