forked from oakmound/oak
/
config.go
191 lines (161 loc) · 4.5 KB
/
config.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
package oak
import (
"encoding/json"
"io"
"github.com/oakmound/oak/fileutil"
"github.com/oakmound/oak/dlog"
)
var (
// SetupConfig is the config struct read from at initialization time
// when oak starts. When oak.Init() is called, the variables behind
// SetupConfig are passed to their appropriate places in the engine, and
// afterword the variable is unused.
SetupConfig Config
// These are the default settings of a project. Anything within SetupConfig
// that is set to its zero value will not overwrite these settings.
conf = Config{
Assets{"assets/", "audio/", "images/", "font/"},
Debug{"", "ERROR"},
Screen{480, 640, 1},
Font{"none", 12.0, 72.0, "", "white"},
60,
60,
"English",
"Oak Window",
false,
false,
false,
}
)
// Config stores initialization settings for oak.
type Config struct {
Assets Assets `json:"assets"`
Debug Debug `json:"debug"`
Screen Screen `json:"screen"`
Font Font `json:"font"`
FrameRate int `json:"frameRate"`
DrawFrameRate int `json:"drawFrameRate"`
Language string `json:"language"`
Title string `json:"title"`
BatchLoad bool `json:"batchLoad"`
GestureSupport bool `json:"gestureSupport"`
// DisableKeyhold is deprecated. Keyhold functionality
// no longer has a significant performance impact and so can't
// be disabled.
DisableKeyhold bool `json:"disableKeyHold"`
}
// Assets is a json type storing paths to different asset folders
type Assets struct {
AssetPath string `json:"assetPath"`
AudioPath string `json:"audioPath"`
ImagePath string `json:"imagePath"`
FontPath string `json:"fontPath"`
}
// Debug is a json type storing the starting debug filter and level
type Debug struct {
Filter string `json:"filter"`
Level string `json:"level"`
}
// Screen is a json type storing the starting screen width and height
type Screen struct {
Height int `json:"height"`
Width int `json:"width"`
Scale int `json:"scale"`
}
// Font is a json type storing the default font settings
type Font struct {
Hinting string `json:"hinting"`
Size float64 `json:"size"`
DPI float64 `json:"dpi"`
File string `json:"file"`
Color string `json:"color"`
}
// LoadConf loads a config file, that could exist inside
// oak's binary data storage (see fileutil), to SetupConfig
func LoadConf(filePath string) error {
r, err := fileutil.Open(filePath)
if err != nil {
dlog.Warn(err)
return err
}
defer r.Close()
err = LoadConfData(r)
dlog.Info(SetupConfig)
return err
}
// LoadConfData takes in an io.Reader and decodes it to SetupConfig
func LoadConfData(r io.Reader) error {
return json.NewDecoder(r).Decode(&SetupConfig)
}
func initConfAssets() {
if SetupConfig.Assets.AssetPath != "" {
conf.Assets.AssetPath = SetupConfig.Assets.AssetPath
}
if SetupConfig.Assets.ImagePath != "" {
conf.Assets.ImagePath = SetupConfig.Assets.ImagePath
}
if SetupConfig.Assets.AudioPath != "" {
conf.Assets.AudioPath = SetupConfig.Assets.AudioPath
}
if SetupConfig.Assets.FontPath != "" {
conf.Assets.FontPath = SetupConfig.Assets.FontPath
}
}
func initConfDebug() {
if SetupConfig.Debug.Filter != "" {
conf.Debug.Filter = SetupConfig.Debug.Filter
}
if SetupConfig.Debug.Level != "" {
conf.Debug.Level = SetupConfig.Debug.Level
}
}
func initConfScreen() {
if SetupConfig.Screen.Width != 0 {
conf.Screen.Width = SetupConfig.Screen.Width
}
if SetupConfig.Screen.Height != 0 {
conf.Screen.Height = SetupConfig.Screen.Height
}
if SetupConfig.Screen.Scale != 0 {
conf.Screen.Scale = SetupConfig.Screen.Scale
}
}
func initConfFont() {
if SetupConfig.Font.Hinting != "" {
conf.Font.Hinting = SetupConfig.Font.Hinting
}
if SetupConfig.Font.Size != 0 {
conf.Font.Size = SetupConfig.Font.Size
}
if SetupConfig.Font.DPI != 0 {
conf.Font.DPI = SetupConfig.Font.DPI
}
if SetupConfig.Font.File != "" {
conf.Font.File = SetupConfig.Font.File
}
if SetupConfig.Font.Color != "" {
conf.Font.Color = SetupConfig.Font.Color
}
}
func initConf() {
initConfAssets()
initConfDebug()
initConfScreen()
initConfFont()
if SetupConfig.FrameRate != 0 {
conf.FrameRate = SetupConfig.FrameRate
}
if SetupConfig.DrawFrameRate != 0 {
conf.DrawFrameRate = SetupConfig.DrawFrameRate
}
if SetupConfig.Language != "" {
conf.Language = SetupConfig.Language
}
if SetupConfig.Title != "" {
conf.Title = SetupConfig.Title
}
conf.BatchLoad = SetupConfig.BatchLoad
conf.GestureSupport = SetupConfig.GestureSupport
conf.DisableKeyhold = SetupConfig.DisableKeyhold
dlog.Error(conf)
}