forked from libretro/ludo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
132 lines (111 loc) · 2.86 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
package main
import (
"flag"
"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/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()
}
func runLoop(vid *video.Video, m *menu.Menu) {
var currTime, prevTime time.Time
for !vid.Window.ShouldClose() {
currTime = time.Now()
dt := float32(currTime.Sub(prevTime)) / 1000000000
glfw.PollEvents()
m.ProcessHotkeys()
ntf.Process(dt)
vid.ResizeViewport()
if !state.Global.MenuActive {
if state.Global.CoreRunning {
state.Global.Core.Run()
if state.Global.Core.FrameTimeCallback != nil {
state.Global.Core.FrameTimeCallback.Callback(state.Global.Core.FrameTimeCallback.Reference)
}
if state.Global.Core.AudioCallback != nil {
state.Global.Core.AudioCallback.Callback()
}
}
vid.Render()
} else {
input.Poll()
m.Update(dt)
vid.Render()
m.Render(dt)
}
m.RenderNotifications()
if state.Global.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")
}
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
flag.StringVar(&state.Global.CorePath, "L", "", "Path to the libretro core")
flag.BoolVar(&state.Global.Verbose, "v", false, "Verbose logs")
flag.BoolVar(&state.Global.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.Global.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.Global.CorePath) > 0 {
err := core.Load(state.Global.CorePath)
if err != nil {
panic(err)
}
}
if len(gamePath) > 0 {
err := core.LoadGame(gamePath)
if err != nil {
ntf.DisplayAndLog(ntf.Error, "Menu", err.Error())
} else {
m.WarpToQuickMenu()
}
}
// No game running? display the menu
state.Global.MenuActive = !state.Global.CoreRunning
runLoop(vid, m)
// Unload and deinit in the core.
core.Unload()
}