-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargon.go
142 lines (114 loc) · 3 KB
/
argon.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
// Argon Graphics Engine
package argon
import (
"errors"
"fmt"
"log"
"os"
"reflect"
gl "github.com/chsc/gogl/gl43"
glfw "github.com/go-gl/glfw3"
)
type Graphics struct {
width, height int
window *glfw.Window
drawMap map[reflect.Type]Renderer
}
var (
//TODO TEMP Remove when ortho uniform becomes external to renderer
Width, Height int
)
func init() {
log.Println("argon.go here")
}
func (this *Graphics) Draw(element interface{}) {
renderer, ok := this.drawMap[reflect.TypeOf(element)]
if !ok {
fmt.Fprintf(os.Stderr, "argon: No renderer registered for %s\n", reflect.TypeOf(element))
return
}
renderer.Draw(element)
}
func (this *Graphics) RegisterRenderer(element interface{}, renderer Renderer) {
this.drawMap[reflect.TypeOf(element)] = renderer
}
func NewGraphics(width, height int, fullscreen bool) (*Graphics, error) {
//Error callback
glfw.SetErrorCallback(glfwErrorCallback)
//glfw
if !glfw.Init() {
fmt.Fprintf(os.Stderr, "glfw: Unable to Init\n")
return nil, errors.New("glfw: Unable to Init")
}
//-hints
glfw.WindowHint(glfw.Resizable, 0)
//Fullscreen param
var monitor *glfw.Monitor = nil
var err error
if fullscreen {
if monitor, err = glfw.GetPrimaryMonitor(); err != nil {
fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
return nil, err
}
}
//Window
var window *glfw.Window
if window, err = glfw.CreateWindow(width, height, "Argon", monitor, nil); err != nil {
fmt.Fprintf(os.Stderr, "glfw: %s\n", err)
return nil, err
}
//Context
window.MakeContextCurrent()
glfw.SwapInterval(1)
//opengl
if err := gl.Init(); err != nil {
fmt.Fprintf(os.Stderr, "gl: %s\n", err)
return nil, err
}
//-initial state
gl.Viewport(0, 0, gl.Sizei(width), gl.Sizei(height))
gl.ClearColor(0.4, 0.4, 0.4, 1.0)
gl.Enable(gl.BLEND)
gl.BlendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA)
//-log version
// glMajor, glMinor, glRev := glfw.GLVersion()
// log.Printf("%d %d %d", glMajor, glMinor, glRev)
log.Println(gl.GoStringUb(gl.GetString(gl.VERSION)))
//TODO TEMP Remove when ortho uniform becomes external to renderer
Width, Height = width, height
//Graphics Struct
graphics := new(Graphics)
graphics.width, graphics.height = width, height
graphics.window = window
graphics.drawMap = make(map[reflect.Type]Renderer)
return graphics, nil
}
func (this *Graphics) Destroy() {
//TODO TEMP Remove when ortho uniform becomes external to renderer
Width, Height = 0, 0
this.width, this.height = 0, 0
//TODO Free all images, and stuffs?
this.window.Destroy()
glfw.Terminate()
}
func (this *Graphics) Cls() {
//TODO Other buffers
gl.Clear(gl.COLOR_BUFFER_BIT)
}
func (this *Graphics) Flip() {
//TODO Timing management and vsync?
this.window.SwapBuffers()
glfw.PollEvents()
}
func (this *Graphics) ShouldClose() bool {
return this.window.ShouldClose()
}
func (this *Graphics) Width() int {
return this.width
}
func (this *Graphics) Height() int {
return this.height
}
func glfwErrorCallback(err glfw.ErrorCode, desc string) {
fmt.Printf("%v: %v\n", err, desc)
}