forked from nwidger/nintengo
/
azul3d_video.go
475 lines (399 loc) · 10.5 KB
/
azul3d_video.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
// +build !sdl,!js
package nes
import (
"azul3d.org/engine/gfx"
"azul3d.org/engine/gfx/camera"
"azul3d.org/engine/gfx/window"
"azul3d.org/engine/keyboard"
"azul3d.org/engine/lmath"
"image"
"image/color"
"math"
)
var Azul3DPalette []color.RGBA = []color.RGBA{
color.RGBA{0x66, 0x66, 0x66, 0xff},
color.RGBA{0x00, 0x2A, 0x88, 0xff},
color.RGBA{0x14, 0x12, 0xA7, 0xff},
color.RGBA{0x3B, 0x00, 0xA4, 0xff},
color.RGBA{0x5C, 0x00, 0x7E, 0xff},
color.RGBA{0x6E, 0x00, 0x40, 0xff},
color.RGBA{0x6C, 0x06, 0x00, 0xff},
color.RGBA{0x56, 0x1D, 0x00, 0xff},
color.RGBA{0x33, 0x35, 0x00, 0xff},
color.RGBA{0x0B, 0x48, 0x00, 0xff},
color.RGBA{0x00, 0x52, 0x00, 0xff},
color.RGBA{0x00, 0x4F, 0x08, 0xff},
color.RGBA{0x00, 0x40, 0x4D, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0xAD, 0xAD, 0xAD, 0xff},
color.RGBA{0x15, 0x5F, 0xD9, 0xff},
color.RGBA{0x42, 0x40, 0xFF, 0xff},
color.RGBA{0x75, 0x27, 0xFE, 0xff},
color.RGBA{0xA0, 0x1A, 0xCC, 0xff},
color.RGBA{0xB7, 0x1E, 0x7B, 0xff},
color.RGBA{0xB5, 0x31, 0x20, 0xff},
color.RGBA{0x99, 0x4E, 0x00, 0xff},
color.RGBA{0x6B, 0x6D, 0x00, 0xff},
color.RGBA{0x38, 0x87, 0x00, 0xff},
color.RGBA{0x0C, 0x93, 0x00, 0xff},
color.RGBA{0x00, 0x8F, 0x32, 0xff},
color.RGBA{0x00, 0x7C, 0x8D, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0xFF, 0xFE, 0xFF, 0xff},
color.RGBA{0x64, 0xB0, 0xFF, 0xff},
color.RGBA{0x92, 0x90, 0xFF, 0xff},
color.RGBA{0xC6, 0x76, 0xFF, 0xff},
color.RGBA{0xF3, 0x6A, 0xFF, 0xff},
color.RGBA{0xFE, 0x6E, 0xCC, 0xff},
color.RGBA{0xFE, 0x81, 0x70, 0xff},
color.RGBA{0xEA, 0x9E, 0x22, 0xff},
color.RGBA{0xBC, 0xBE, 0x00, 0xff},
color.RGBA{0x88, 0xD8, 0x00, 0xff},
color.RGBA{0x5C, 0xE4, 0x30, 0xff},
color.RGBA{0x45, 0xE0, 0x82, 0xff},
color.RGBA{0x48, 0xCD, 0xDE, 0xff},
color.RGBA{0x4F, 0x4F, 0x4F, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0xFF, 0xFE, 0xFF, 0xff},
color.RGBA{0xC0, 0xDF, 0xFF, 0xff},
color.RGBA{0xD3, 0xD2, 0xFF, 0xff},
color.RGBA{0xE8, 0xC8, 0xFF, 0xff},
color.RGBA{0xFB, 0xC2, 0xFF, 0xff},
color.RGBA{0xFE, 0xC4, 0xEA, 0xff},
color.RGBA{0xFE, 0xCC, 0xC5, 0xff},
color.RGBA{0xF7, 0xD8, 0xA5, 0xff},
color.RGBA{0xE4, 0xE5, 0x94, 0xff},
color.RGBA{0xCF, 0xEF, 0x96, 0xff},
color.RGBA{0xBD, 0xF4, 0xAB, 0xff},
color.RGBA{0xB3, 0xF3, 0xCC, 0xff},
color.RGBA{0xB5, 0xEB, 0xF2, 0xff},
color.RGBA{0xB8, 0xB8, 0xB8, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
color.RGBA{0x00, 0x00, 0x00, 0xff},
}
type Azul3DVideo struct {
input chan []uint8
width, height int
palette []color.RGBA
events chan Event
overscan bool
caption string
fps float64
}
func NewVideo(caption string, events chan Event, fps float64) (video *Azul3DVideo, err error) {
video = &Azul3DVideo{
input: make(chan []uint8, 128),
events: events,
palette: Azul3DPalette,
overscan: true,
caption: caption,
fps: fps,
}
return
}
func convertColor(c color.Color) gfx.Color {
r, g, b, a := c.RGBA()
return gfx.Color{
float32(r) / float32(math.MaxUint16),
float32(g) / float32(math.MaxUint16),
float32(b) / float32(math.MaxUint16),
float32(a) / float32(math.MaxUint16),
}
}
func (video *Azul3DVideo) Events() chan Event {
return video.events
}
func (video *Azul3DVideo) Input() chan []uint8 {
return video.input
}
func (video *Azul3DVideo) frameWidth() int {
width := 256
if video.overscan {
width -= 16
}
return width
}
func (video *Azul3DVideo) frameHeight() int {
height := 240
if video.overscan {
height -= 16
}
return height
}
var glslVert = []byte(`
#version 120
attribute vec3 Vertex;
attribute vec2 TexCoord0;
uniform mat4 MVP;
uniform vec3 shift;
uniform vec3 scale;
varying vec2 tc;
void main()
{
tc = scale.xy * TexCoord0;
tc += shift.xy;
gl_Position = MVP * vec4(Vertex, 1.0);
}
`)
var glslFrag = []byte(`
#version 120
varying vec2 tc;
uniform sampler2D Texture0; // palette indices
uniform sampler2D Texture1; // palette
void main() {
vec4 t = texture2D(Texture0, tc);
gl_FragColor = texture2D(Texture1, vec2((t.r*256.0)/64.0, 0));
}
`)
func (video *Azul3DVideo) handleInput(ev keyboard.ButtonEvent, w *window.Window) (running bool) {
var event Event
setSize := func(width, height int) {
props := (*w).Props()
props.SetSize(width, height)
(*w).Request(props)
}
running = true
if ev.State == keyboard.Down {
switch ev.Key {
case keyboard.Tilde:
video.overscan = !video.overscan
case keyboard.One:
setSize(256, 240)
case keyboard.Two:
setSize(512, 480)
case keyboard.Three:
setSize(768, 720)
case keyboard.Four:
setSize(1024, 960)
case keyboard.Five:
setSize(2560, 1440)
case keyboard.P:
event = &PauseEvent{}
case keyboard.Q:
running = false
event = &QuitEvent{}
case keyboard.L:
event = &SavePatternTablesEvent{}
case keyboard.R:
event = &ResetEvent{}
case keyboard.S:
event = &RecordEvent{}
case keyboard.D:
event = &StopEvent{}
case keyboard.NumAdd:
event = &AudioRecordEvent{}
case keyboard.NumSubtract:
event = &AudioStopEvent{}
case keyboard.O:
event = &CPUDecodeEvent{}
case keyboard.I:
event = &PPUDecodeEvent{}
case keyboard.Nine:
event = &ShowBackgroundEvent{}
case keyboard.Zero:
event = &ShowSpritesEvent{}
case keyboard.F1:
event = &SaveStateEvent{}
case keyboard.F5:
event = &LoadStateEvent{}
case keyboard.F8:
event = &FPSEvent{2.}
case keyboard.F9:
event = &FPSEvent{1.}
case keyboard.F10:
event = &FPSEvent{.75}
case keyboard.F11:
event = &FPSEvent{.5}
case keyboard.F12:
event = &FPSEvent{.25}
case keyboard.NumZero:
event = &MuteEvent{}
case keyboard.NumOne:
event = &MutePulse1Event{}
case keyboard.NumTwo:
event = &MutePulse2Event{}
case keyboard.NumThree:
event = &MuteTriangleEvent{}
case keyboard.NumFour:
event = &MuteNoiseEvent{}
case keyboard.NumFive:
event = &MuteDMCEvent{}
}
}
if event == nil {
button := One
switch ev.Key {
case keyboard.Z:
button = A
case keyboard.X:
button = B
case keyboard.Enter:
button = Start
case keyboard.RightShift:
button = Select
case keyboard.ArrowUp:
button = Up
case keyboard.ArrowDown:
button = Down
case keyboard.ArrowLeft:
button = Left
case keyboard.ArrowRight:
button = Right
}
event = &ControllerEvent{
Button: button,
Down: ev.State == keyboard.Down,
}
}
if event != nil {
video.events <- event
}
return
}
func (video *Azul3DVideo) Run() {
colors := []uint8{}
running := true
gfxLoop := func(w window.Window, d gfx.Device) {
// Create a simple shader.
shader := gfx.NewShader("SimpleShader")
shader.GLSL = &gfx.GLSLSources{Vertex: glslVert, Fragment: glslFrag}
// Create a new orthograhpic camera
cam := camera.NewOrtho(d.Bounds())
// Move the camera two units back
cam.SetPos(lmath.Vec3{0, -2, 0})
// Create a card mesh.
cardMesh := gfx.NewMesh()
cardMesh.Vertices = []gfx.Vec3{
// Left triangle.
{-1, 0, 1}, // Left-Top
{-1, 0, -1}, // Left-Bottom
{1, 0, -1}, // Right-Bottom
// Right triangle.
{-1, 0, 1}, // Left-Top
{1, 0, -1}, // Right-Bottom
{1, 0, 1}, // Right-Top
}
cardMesh.TexCoords = []gfx.TexCoordSet{
{
Slice: []gfx.TexCoord{
// Left triangle.
{0, 0},
{0, 1},
{1, 1},
// Right triangle.
{0, 0},
{1, 1},
{1, 0},
},
},
}
palette := gfx.NewTexture()
palette.MinFilter = gfx.Nearest
palette.MagFilter = gfx.Nearest
paletteSrc := image.NewRGBA(image.Rect(0, 0, 64, 2))
for x, c := range Azul3DPalette {
paletteSrc.SetRGBA(x, 0, c)
}
palette.Source = paletteSrc
// Create a card object.
card := gfx.NewObject()
card.State = gfx.NewState()
card.AlphaMode = gfx.AlphaToCoverage
card.Shader = shader
card.Textures = []*gfx.Texture{nil, palette}
card.Meshes = []*gfx.Mesh{cardMesh}
img := image.NewRGBA(image.Rect(0, 0, 256, 256))
updateTex := func() {
for i, c := range colors {
img.Pix[i<<2] = c
}
var cropPx float32 = 0.0
if video.overscan {
cropPx = 8.0
}
var btmPx float32 = (16.0 - cropPx) / 256.0
nx := 1.0 / float32(img.Bounds().Dx())
ny := 1.0 / float32(img.Bounds().Dy())
scale := gfx.Vec3{
X: 1.0 - (nx * cropPx * 2),
Y: 1.0 - (ny * cropPx * 2) - btmPx,
}
shift := gfx.Vec3{
X: nx * cropPx,
Y: ny * cropPx,
}
shader.Inputs["scale"] = scale
shader.Inputs["shift"] = shift
// Create new texture and ask the renderer to load it. We don't use DXT
// compression because those textures cannot be downloaded.
tex := gfx.NewTexture()
tex.Source = img
tex.MinFilter = gfx.Nearest
tex.MagFilter = gfx.Nearest
onLoad := make(chan *gfx.Texture, 1)
d.LoadTexture(tex, onLoad)
<-onLoad
// Swap the texture with the old one on the card.
card.Textures[0] = tex
}
updateTex()
go func() {
// Create a channel of events
events := make(chan window.Event, 256)
// Have the window notify out channel whenever events occur.
w.Notify(events, window.KeyboardButtonEvents)
for running {
select {
case colors = <-video.input:
// We drop any pending frames and grab the most recent one. This is
// because frame display is tied to the runProcessors loop and can
// cause audio stuttering.
frameDrop:
for {
select {
case colors = <-video.input:
default:
break frameDrop
}
}
// Update the texture using the most recent frame.
updateTex()
}
// Handle eaach pending event
window.Poll(events, func(e window.Event) {
switch ev := e.(type) {
case keyboard.ButtonEvent:
running = video.handleInput(ev, &w)
}
})
}
}()
for running {
// Center the card in the window
b := d.Bounds()
card.SetPos(lmath.Vec3{float64(b.Dx()) / 2.0, 0, float64(b.Dy()) / 2.0})
// Scale the card to fit the window
s := float64(b.Dy()) / 2.0
card.SetScale(lmath.Vec3{s, s, s})
// clear the entire area (empty rectangle means "the whole area").
d.Clear(d.Bounds(), gfx.Color{0, 0, 0, 1})
d.ClearDepth(d.Bounds(), 1.0)
// Draw the textured card.
d.Draw(d.Bounds(), card, cam)
// Render the frame
d.Render()
}
}
props := window.NewProps()
props.SetSize(512, 480)
props.SetTitle("nintengo - " + video.caption + " - {FPS}")
window.Run(gfxLoop, props)
}
func (video *Azul3DVideo) SetCaption(caption string) {
video.caption = caption
}