/
camera.go
69 lines (56 loc) · 1.92 KB
/
camera.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
package main
import (
"github.com/go-gl/gl/v2.1/gl"
)
const (
leftBorder = 50
rightBorder = 50
bottomBorder = 150
topBorder = 100
gameW = 7 * 200
gameH = 7*tileYOffset + tileSlopeHeight
)
func newCamera() *camera { return &camera{} }
type camera struct {
WindowWidth, WindowHeight int
Left, Right, Top, Bottom float64
}
func (c *camera) windowToGame(x, y float64) (int, int) {
xPercent := x / float64(c.WindowWidth)
yPercent := y / float64(c.WindowHeight)
relX := c.Left + xPercent*(c.Right-c.Left)
relY := c.Top + yPercent*(c.Bottom-c.Top)
return int(relX + 0.5), int(relY + 0.5)
}
func (c *camera) gameToWindow(x, y int) (float64, float64) {
xPercent := (float64(x) - c.Left) / (c.Right - c.Left)
yPercent := (float64(y) - c.Top) / (c.Bottom - c.Top)
return xPercent * float64(c.WindowWidth), yPercent * float64(c.WindowHeight)
}
func (cam *camera) windowSizeChangedTo(width, height int) {
gl.Viewport(0, 0, int32(width), int32(height))
cam.WindowWidth, cam.WindowHeight = width, height
cam.recalcOrthoBorders()
}
func (cam *camera) recalcOrthoBorders() {
const totalW = gameW + leftBorder + rightBorder
const totalH = gameH + topBorder + bottomBorder
const totalRatio = float64(totalW) / totalH
windowRatio := float64(cam.WindowWidth) / float64(cam.WindowHeight)
var horizontalBorder, verticalBorder float64
if windowRatio > totalRatio {
// window is wider than game => borders left and right
horizontalBorder = (windowRatio*totalH - totalW) / 2
} else {
// window is higher than game => borders on top and bottom
verticalBorder = (totalW/windowRatio - totalH) / 2
}
cam.Left = -leftBorder - horizontalBorder
cam.Right = gameW + rightBorder + horizontalBorder
cam.Top = -topBorder - verticalBorder
cam.Bottom = gameH + bottomBorder + verticalBorder
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(cam.Left, cam.Right, cam.Bottom, cam.Top, -1, 1)
gl.MatrixMode(gl.MODELVIEW)
}