phi/main.go

159 lines
2.8 KiB
Go
Raw Normal View History

2016-11-14 01:10:03 +03:00
package main
import (
2016-11-14 10:24:19 +03:00
"fmt"
"io/ioutil"
2018-02-19 02:15:21 +03:00
"log"
"os"
2016-11-28 17:28:31 +03:00
"runtime"
2018-02-28 16:23:07 +03:00
"time"
2016-11-28 17:28:31 +03:00
2018-04-22 20:56:47 +03:00
"github.com/felixangell/phi/cfg"
"github.com/felixangell/phi/gui"
"github.com/felixangell/strife"
2016-11-14 01:10:03 +03:00
)
2016-11-14 09:16:37 +03:00
const (
PRINT_FPS bool = true
2016-11-14 09:16:37 +03:00
)
type PhiEditor struct {
running bool
defaultFont *strife.Font
mainView *gui.View
}
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
2016-11-14 01:10:03 +03:00
}
func (n *PhiEditor) dispose() {
}
func (n *PhiEditor) update() bool {
return n.mainView.OnUpdate()
2016-11-14 01:10:03 +03:00
}
func (n *PhiEditor) render(ctx *strife.Renderer) {
ctx.SetFont(n.defaultFont)
n.mainView.OnRender(ctx)
2016-11-14 01:10:03 +03:00
}
func main() {
2016-11-24 12:31:14 +03:00
config := cfg.Setup()
2016-11-14 11:14:53 +03:00
ww, wh := 1280, 720
window := strife.SetupRenderWindow(ww, wh, strife.DefaultConfig())
window.SetTitle("Hello world!")
window.SetResizable(true)
editor := &PhiEditor{running: true}
2018-02-19 02:13:48 +03:00
window.HandleEvents(func(evt strife.StrifeEvent) {
switch evt.(type) {
case *strife.CloseEvent:
window.Close()
default:
editor.handleEvent(evt)
}
})
window.Create()
2016-11-24 12:31:14 +03:00
{
size := "16"
switch runtime.GOOS {
case "windows":
size = "256"
case "darwin":
size = "512"
case "linux":
size = "96"
default:
2018-02-19 02:15:21 +03:00
log.Println("unrecognized runtime ", runtime.GOOS)
2016-11-24 12:31:14 +03:00
}
icon, err := strife.LoadImage("./res/icons/icon" + size + ".png")
2016-11-24 12:31:14 +03:00
if err != nil {
log.Println("Failed to load icon ", err.Error())
} else {
window.SetIconImage(icon)
defer icon.Destroy()
2016-11-24 12:31:14 +03:00
}
}
2016-11-14 11:14:53 +03:00
editor.init(&config)
2016-11-14 10:24:19 +03:00
lastDebugRender := time.Now()
2018-02-28 16:23:07 +03:00
frames, updates := 0, 0
fps, ups := frames, updates
2016-11-14 10:24:19 +03:00
2018-02-19 02:13:48 +03:00
ctx := window.GetRenderContext()
2018-02-28 16:23:07 +03:00
ctx.Clear()
editor.render(ctx)
2018-02-28 16:23:07 +03:00
ctx.Display()
for {
window.PollEvents()
if window.CloseRequested() {
break
}
shouldRender := editor.update()
if shouldRender || config.Render.Always_Render {
2018-02-28 16:23:07 +03:00
ctx.Clear()
editor.render(ctx)
2018-02-19 02:13:48 +03:00
2018-02-28 16:23:07 +03:00
// 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
2016-11-14 10:24:19 +03:00
if time.Now().Sub(lastDebugRender) >= time.Second {
lastDebugRender = time.Now()
2018-02-28 16:23:07 +03:00
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)
2016-11-14 10:24:19 +03:00
}
}
editor.dispose()
2016-11-14 01:10:03 +03:00
}