-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathim.go
173 lines (124 loc) · 4.74 KB
/
im.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
package im
import (
"github.com/go-gl/gl/v3.3-core/gl"
glm "github.com/go-gl/mathgl/mgl32"
"github.com/micahke/mango/res"
"github.com/micahke/mango/util"
"github.com/micahke/mango/util/color"
"github.com/micahke/mango/util/loaders"
)
type IMMEDIATE_MODE struct {
// These deal with scripts
scene interface{}
sceneActivated bool
// This deals with camera
projectionMatrix glm.Mat4
viewMatrix glm.Mat4
// Renderers
quadRenderer *QuadRenderer
spriteRenderer *SpriteRenderer
circleRenderer *CircleRenderer
lineRenderer *LineRenderer
textRenderer *TextRenderer
textBatcher *TextBatcher
pixelRenderer *PixelRenderer
quadBatcher *QuadBatcher
}
func Init() *IMMEDIATE_MODE {
im_mode := new(IMMEDIATE_MODE)
InitTextureCache()
InitFontAtlas()
im_mode.viewMatrix = glm.Ident4()
return im_mode
}
// Starts a new frame in IMMEDIATE MODE
// This function should only be used internally by the engine
func (im *IMMEDIATE_MODE) NewFrame(deltaTime float64) {
// Poll for events
// glfw.PollEvents()
scene, _ := im.scene.(IM_SCRIPT)
if !im.sceneActivated {
scene.Init()
im.sceneActivated = true
}
scene.Update()
// RIP: forgot about the rendering order
// To do: make this more general
im.quadBatcher.FlushBatch(im.projectionMatrix, im.viewMatrix)
scene.Draw()
im.textBatcher.FlushBatch(im.projectionMatrix, im.viewMatrix)
}
func (im *IMMEDIATE_MODE) ConnectScene(i interface{}) {
// Pass in whatever interface we get
im.scene = i
}
func (im *IMMEDIATE_MODE) InitProjectionMatrix(width, height float32) {
im.projectionMatrix = glm.Ortho(0, width, 0, height, -1.0, 1.0)
// Also set up renderers here because OpenGL has just been set up
im.setupRenderers()
}
func (im *IMMEDIATE_MODE) setupRenderers() {
im.quadRenderer = InitQuadRenderer()
im.spriteRenderer = InitSpriteRenderer()
im.circleRenderer = InitCircleRenderer()
im.lineRenderer = InitLineRenderer()
// Load the bitmap font before we start any text rendering
fontData, _ := res.LoadEngineResource("BitmapFont.png")
loaders.LoadImageFromData("BitmapFont.png", fontData)
im.textRenderer = InitTextRenderer()
im.textBatcher = InitTextBatcher()
im.pixelRenderer = InitPixelRenderer()
im.quadBatcher = InitQuadBatcher()
}
func (im *IMMEDIATE_MODE) SetBackgroundColor(color color.Color) {
colorVec := color.Vec4
gl.ClearColor(colorVec[0], colorVec[1], colorVec[2], colorVec[3])
}
// Draw a quad to the screen
func (im *IMMEDIATE_MODE) DrawRect(x, y, width, height float32) {
im.quadRenderer.RenderQuad(x, y, width, height, color.WHITE, im.projectionMatrix, im.viewMatrix)
}
// Draw a filled rectangle to the screen
func (im *IMMEDIATE_MODE) FillRect(x, y, width, height float32, color color.Color) {
im.quadRenderer.RenderQuad(x, y, width, height, color, im.projectionMatrix, im.viewMatrix)
}
// Draw a sprite to the screen
func (im *IMMEDIATE_MODE) DrawSprite(x, y, width, height float32, texturePath string) {
im.spriteRenderer.RenderSprite(x, y, width, height, texturePath, im.projectionMatrix, im.viewMatrix)
}
// Draw a UV sprite to the screen defined by a UV spritemap
func (im *IMMEDIATE_MODE) DrawUVSprite(x, y, width, height float32, texturePath string, uv util.UVSpriteMap) {
im.spriteRenderer.RenderUVSprite(x, y, width, height, texturePath, uv, im.projectionMatrix, im.viewMatrix)
}
// Draw a circle to the screen
// Centered at bottom left
func (im *IMMEDIATE_MODE) DrawCircle(x, y, width, height float32, color color.Color) {
im.circleRenderer.RenderCircle(x, y, width, height, color.Vec4, im.projectionMatrix, im.viewMatrix)
}
// Draw a line to the screen
func (im *IMMEDIATE_MODE) DrawGLine(x1, y1, x2, y2 float32, color color.Color, thickness float32) {
p1 := glm.Vec2{x1, y1}
p2 := glm.Vec2{x2, y2}
im.lineRenderer.RenderLine(p1, p2, color, thickness, im.projectionMatrix, im.viewMatrix)
}
func (im *IMMEDIATE_MODE) DrawLine(x0, y0, x1, y1, size int) {
linePoints := im.lineRenderer.GenerateBresenhamPoints(x0, y0, x1, y1, size)
// TODO: fix unecessary conversions between float32 and int
im.DrawPixels(linePoints, float32(size))
}
// Draw text within the world space
// This is not batch optimized
func (im *IMMEDIATE_MODE) DrawWorldText(x, y, size float32, text string) {
im.textRenderer.RenderText(x, y, size, text, im.projectionMatrix, im.viewMatrix)
}
// Draw text to the screen layer
// This is batch renedered and optimized
func (im *IMMEDIATE_MODE) DrawText(text string, x, y float32) {
im.textBatcher.AddText(text, x, y, im.projectionMatrix, im.viewMatrix)
}
func (im *IMMEDIATE_MODE) DrawPixels(buffer []float32, size float32) {
im.pixelRenderer.DrawPixels(buffer, 2*size, im.projectionMatrix, im.viewMatrix)
}
func (im *IMMEDIATE_MODE) DrawQuad(quad Quad) {
im.quadBatcher.AddQuad(quad, im.projectionMatrix, im.viewMatrix)
}