-
Notifications
You must be signed in to change notification settings - Fork 68
/
main.go
153 lines (130 loc) · 3.27 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
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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"time"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/libretro/ludo/audio"
"github.com/libretro/ludo/core"
"github.com/libretro/ludo/history"
"github.com/libretro/ludo/input"
"github.com/libretro/ludo/menu"
ntf "github.com/libretro/ludo/notifications"
"github.com/libretro/ludo/playlists"
"github.com/libretro/ludo/savefiles"
"github.com/libretro/ludo/scanner"
"github.com/libretro/ludo/settings"
"github.com/libretro/ludo/state"
"github.com/libretro/ludo/video"
)
func init() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
}
var frame = 0
func runLoop(vid *video.Video, m *menu.Menu) {
var currTime time.Time
prevTime := time.Now()
for !vid.Window.ShouldClose() {
currTime = time.Now()
dt := float32(currTime.Sub(prevTime)) / 1000000000
glfw.PollEvents()
m.ProcessHotkeys()
ntf.Process(dt)
vid.ResizeViewport()
m.UpdatePalette()
input.Poll()
if !state.MenuActive {
if state.CoreRunning {
state.Core.Run()
if state.Core.FrameTimeCallback != nil {
state.Core.FrameTimeCallback.Callback(state.Core.FrameTimeCallback.Reference)
}
if state.Core.AudioCallback != nil {
state.Core.AudioCallback.Callback()
}
}
vid.Render()
frame++
if frame%600 == 0 { // save sram about every 10 sec
savefiles.SaveSRAM()
}
} else {
m.Update(dt)
vid.Render()
m.Render(dt)
}
m.RenderNotifications()
if state.FastForward {
glfw.SwapInterval(0)
} else {
glfw.SwapInterval(1)
}
vid.Window.SwapBuffers()
prevTime = currTime
}
}
func main() {
err := settings.Load()
if err != nil {
log.Println("[Settings]: Loading failed:", err)
log.Println("[Settings]: Using default settings")
}
// ExitOnError causes flags to quit after displaying help.
// (--help counts as an error)
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
// customize help message
flag.CommandLine.Usage = func() {
fmt.Printf("Usage: %s [OPTIONS] [content]\n", os.Args[0])
fmt.Printf("Options:\n")
flag.PrintDefaults()
}
// set arguments
flag.StringVar(&state.CorePath, "L", "", "Path to the libretro core")
flag.BoolVar(&state.Verbose, "v", false, "Verbose logs")
flag.BoolVar(&state.LudOS, "ludos", false, "Expose the features related to LudOS")
flag.Parse()
args := flag.Args()
var gamePath string
if len(args) > 0 {
gamePath = args[0]
}
if err := glfw.Init(); err != nil {
log.Fatalln("Failed to initialize glfw", err)
}
defer glfw.Terminate()
state.DB, err = scanner.LoadDB(settings.Current.DatabaseDirectory)
if err != nil {
log.Println("Can't load game database:", err)
}
playlists.Load()
history.Load()
vid := video.Init(settings.Current.VideoFullscreen)
audio.Init()
m := menu.Init(vid)
core.Init(vid)
input.Init(vid)
if len(state.CorePath) > 0 {
err := core.Load(state.CorePath)
if err == nil {
if len(gamePath) > 0 {
err := core.LoadGame(gamePath)
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Menu", err.Error())
} else {
m.WarpToQuickMenu()
}
}
} else {
ntf.DisplayAndLog(ntf.Error, "Menu", err.Error())
}
}
// No game running? display the menu
state.MenuActive = !state.CoreRunning
runLoop(vid, m)
// Unload and deinit in the core.
core.Unload()
}