/
main.go
174 lines (138 loc) · 3.24 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"runtime"
"time"
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/strife"
)
const (
PRINT_FPS bool = true
)
type PhiEditor struct {
running bool
defaultFont *strife.Font
mainView *gui.View
}
func (n *PhiEditor) resize(w, h int) {
n.mainView.Resize(w, h)
}
func (n *PhiEditor) handleEvent(evt strife.StrifeEvent) {
}
func (n *PhiEditor) init(cfg *cfg.TomlConfig) {
mainView := gui.NewView(1280, 720, cfg)
args := os.Args
if len(args) > 1 {
// TODO check these are files
// that actually exist here?
for _, arg := range args[1:] {
mainView.AddBuffer().OpenFile(arg)
}
} else {
// we have no args, open up a scratch file
tempFile, err := ioutil.TempFile("", "phi-editor-")
if err != nil {
log.Println("Failed to create temp file", err.Error())
os.Exit(1)
}
mainView.AddBuffer().OpenFile(tempFile.Name())
}
n.mainView = mainView
n.defaultFont = cfg.Editor.Loaded_Font
}
func (n *PhiEditor) dispose() {
}
func (n *PhiEditor) update() bool {
return n.mainView.OnUpdate()
}
func (n *PhiEditor) render(ctx *strife.Renderer) {
ctx.SetFont(n.defaultFont)
n.mainView.OnRender(ctx)
}
func main() {
runtime.LockOSThread()
config := cfg.Setup()
ww, wh := 1280, 720
windowConfig := strife.DefaultConfig()
windowConfig.Accelerated = config.Render.Accelerated
windowConfig.Alias = config.Render.Aliased
windowConfig.VerticalSync = config.Render.Vertical_Sync
window := strife.SetupRenderWindow(ww, wh, windowConfig)
window.SetTitle("Hello world!")
window.SetResizable(true)
editor := &PhiEditor{running: true}
window.HandleEvents(func(evt strife.StrifeEvent) {
switch event := evt.(type) {
case *strife.CloseEvent:
window.Close()
case *strife.WindowResizeEvent:
editor.resize(event.Width, event.Height)
default:
editor.handleEvent(evt)
}
})
window.Create()
{
size := 16
switch runtime.GOOS {
case "windows":
size = 64
case "darwin":
size = 512
case "linux":
size = 96
default:
log.Println("unrecognized runtime ", runtime.GOOS)
}
iconFile := fmt.Sprintf("icon%d.png", size)
icon, err := strife.LoadImage(filepath.Join(cfg.ICON_DIR_PATH, iconFile))
if err != nil {
log.Println("Failed to load icon ", err.Error())
} else {
window.SetIconImage(icon)
defer icon.Destroy()
}
}
editor.init(&config)
lastDebugRender := time.Now()
frames, updates := 0, 0
fps, ups := frames, updates
ctx := window.GetRenderContext()
ctx.Clear()
editor.render(ctx)
ctx.Display()
for {
window.PollEvents()
if window.CloseRequested() {
break
}
shouldRender := editor.update()
if shouldRender || config.Render.Always_Render {
ctx.Clear()
editor.render(ctx)
// this is only printed on each
// render...
ctx.SetColor(strife.White)
ctx.String(fmt.Sprintf("fps: %d, ups %d", fps, ups), ww-256, wh-128)
ctx.Display()
frames += 1
}
updates += 1
if time.Now().Sub(lastDebugRender) >= time.Second {
lastDebugRender = time.Now()
fps, ups = frames, updates
frames, updates = 0, 0
}
if config.Render.Throttle_Cpu_Usage {
// todo put in the config how long
// we sleep for!
time.Sleep(16)
}
}
editor.dispose()
}