/
gameview.go
134 lines (105 loc) · 2.72 KB
/
gameview.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
package simpleui
import (
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.1/glfw"
"image"
// "image/color"
// "fmt"
// "log"
//"nes"
// "nes/sql"
)
type CustomViewIF interface {
View
OnKey(glfw.Key)
TextureBuff() []uint8
Title() string
}
const padding = 0
type GameView struct {
director *Director
customView CustomViewIF
texture uint32
ppuScreen *image.RGBA
}
func NewGameView(director *Director, customView CustomViewIF) View {
texture := createGLTexture()
screen := image.NewRGBA(image.Rect(0, 0, width, height))
return &GameView{director:director,
customView:customView,
texture:texture,
ppuScreen: screen}
}
func (view *GameView) Enter() {
gl.ClearColor(0, 0, 0, 1)
view.director.window.SetKeyCallback(view.onKey)
if view.customView != nil {
view.director.SetTitle(view.customView.Title() )
view.customView.Enter()
}
}
func (view *GameView) Exit() {
view.director.window.SetKeyCallback(nil)
if view.customView != nil {
view.customView.Exit()
}
}
// qibinyi, main update
func (view *GameView) Update(t, dt float64) {
if dt > 1 {
dt = 0
}
/*
window := view.director.window
if joystickReset(glfw.Joystick1) {
view.director.ShowMenu()
}
if joystickReset(glfw.Joystick2) {
view.director.ShowMenu()
}
if readKey(window, glfw.KeyEscape) {
view.director.ShowMenu()
}
//*/
if view.customView != nil {
view.customView.Update(t, dt)
copy(view.ppuScreen.Pix, view.customView.TextureBuff())
}
// console.DebugSingleFrame(true)
gl.BindTexture(gl.TEXTURE_2D, view.texture)
setTexture(view.ppuScreen)
drawBuffer(view.director.window)
gl.BindTexture(gl.TEXTURE_2D, 0)
}
func (view *GameView) onKey(window *glfw.Window,
key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
if action == glfw.Press {
if view.customView != nil {
view.customView.OnKey(key)
}
}
}
func drawBuffer(window *glfw.Window) {
w, h := window.GetFramebufferSize()
s1 := float32(w) / float32(width)
s2 := float32(h) / float32(height)
f := float32(1 - padding)
var x, y float32
if s1 >= s2 {
x = f * s2 / s1
y = f
} else {
x = f
y = f * s1 / s2
}
gl.Begin(gl.QUADS)
gl.TexCoord2f(0, 1)
gl.Vertex2f(-x, -y)
gl.TexCoord2f(1, 1)
gl.Vertex2f(x, -y)
gl.TexCoord2f(1, 0)
gl.Vertex2f(x, y)
gl.TexCoord2f(0, 0)
gl.Vertex2f(-x, y)
gl.End()
}