-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
122 lines (103 loc) · 2.32 KB
/
main.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
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"runtime"
"strings"
"github.com/FryDay/chip8/chip8"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.2/glfw"
)
var (
screenWidth float32 = 1280
screenHeight float32 = 640
zoom float32
romPtr = flag.String("rom", "", "ROM to load (Required)")
widthPtr = flag.Int("width", 1280, "Screen width (Optional)")
)
func init() {
flag.StringVar(romPtr, "r", "", "Same as -rom")
flag.IntVar(widthPtr, "w", 1280, "Same as -width")
runtime.LockOSThread()
}
func main() {
flag.Parse()
if *romPtr == "" {
flag.PrintDefaults()
os.Exit(1)
}
if _, err := os.Stat("./roms/" + strings.ToUpper(*romPtr)); os.IsNotExist(err) {
fmt.Println(*romPtr, "is not a valid ROM")
os.Exit(1)
}
if float32(*widthPtr) != screenWidth {
screenWidth = float32(*widthPtr)
screenHeight = screenWidth / 2
}
err := glfw.Init()
if err != nil {
panic(err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
window, err := glfw.CreateWindow(int(screenWidth), int(screenHeight), "CHIP-8", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
panic(err)
}
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
gl.Ortho(0.0, float64(screenWidth), float64(screenHeight), 0.0, 1.0, -1.0)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
zoom = screenWidth / 64
chip8 := chip8.Chip8{}
//input
chip8.Initialize()
rom, _ := ioutil.ReadFile("./roms/" + strings.ToUpper(*romPtr))
chip8.LoadROM(rom)
for !window.ShouldClose() {
chip8.Cycle()
if chip8.Draw {
render(chip8.Display[:])
window.SwapBuffers()
chip8.Draw = false
}
glfw.PollEvents()
// set keys
}
}
func render(d []byte) {
var row float32
var col float32
var colZoom float32
var rowZoom float32
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Begin(gl.QUADS)
for i := range d {
if col > 63 {
row++
col = 0
}
if d[i] == 1 {
colZoom = col * zoom
rowZoom = row * zoom
gl.Vertex2f(colZoom, rowZoom)
gl.Vertex2f(colZoom, rowZoom+zoom)
gl.Vertex2f(colZoom+zoom, rowZoom+zoom)
gl.Vertex2f(colZoom+zoom, rowZoom)
}
col++
}
gl.End()
}