-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
92 lines (77 loc) · 1.81 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
// +build !js
//Methodology cribbed from this https://medium.com/@ben.mcclelland/an-adventure-into-cgo-calling-go-code-with-c-b20aa6637e75
//todo: determine how the ALE included its games; might need to zip rom files and game states to be viable
package main
//#include <stdio.h>
//#include <errno.h>
//#include <stdlib.h>
import "C"
import (
"./nes"
"bytes"
"strconv"
)
// todo: see if there is a "more elegant" way to retain this reference
var handle []*nes.EmulatorInterface
//export NewNintendo
func NewNintendo(filename *C.char, displayMode *C.char, frameSkipRate C.int) (rc int) {
options := &nes.Options{
Region: "NTSC",
Display: C.GoString(displayMode),
}
//nameString := C.GoString(filename)
h := nes.NewEmulatorInterface(C.GoString(filename), int(frameSkipRate), options)
handle = append(handle, h)
return len(handle) - 1
}
//export RunNintendo
func RunNintendo() {
emu := handle[0]
emu.Start()
}
//export GetObservation
func GetObservation() *C.char {
emu := handle[0]
output := emu.Observe()
var buffer bytes.Buffer
for i := 0; i < len(output); i++ {
buffer.WriteString(strconv.Itoa(int(output[i])))
if i != len(output)-1 {
buffer.WriteString(",")
}
}
return C.CString(buffer.String())
}
//export TakeAction
func TakeAction(btn int) C.float {
emu := handle[0]
//reward := emu.Act(btn)
return C.float(emu.Act(btn))
}
//export IsGameOver
func IsGameOver() bool {
emu := handle[0]
//gameOver := emu.IsGameOver()
return emu.IsGameOver()
}
//export CloseEmulator
func CloseEmulator() {
emu := handle[0]
emu.Close()
}
//export Reset
func Reset() {
emu := handle[0]
emu.Reset()
}
//export OpenToStart
func OpenToStart() {
emu := handle[0]
emu.OpenToStart()
}
//export EndRecording
func EndRecording(filename *C.char) {
emu := handle[0]
emu.EndRecording(C.GoString(filename))
}
func main() {}