phi/main.go

175 lines
3.2 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"
2018-05-06 18:13:29 +03:00
"path/filepath"
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-06-22 13:36:52 +03:00
"github.com/felixangell/phi/buff"
2018-04-22 20:56:47 +03:00
"github.com/felixangell/phi/cfg"
"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
2018-06-22 13:36:52 +03:00
mainView *buff.BufferView
}
2018-06-09 12:58:06 +03:00
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) {
2018-06-23 01:36:08 +03:00
mainView := buff.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() {
2018-05-07 14:04:25 +03:00
runtime.LockOSThread()
2016-11-24 12:31:14 +03:00
config := cfg.Setup()
2016-11-14 11:14:53 +03:00
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}
2018-02-19 02:13:48 +03:00
window.HandleEvents(func(evt strife.StrifeEvent) {
2018-06-09 12:58:06 +03:00
switch event := evt.(type) {
case *strife.CloseEvent:
window.Close()
2018-06-09 12:58:06 +03:00
case *strife.WindowResizeEvent:
editor.resize(event.Width, event.Height)
default:
editor.handleEvent(evt)
}
})
window.Create()
2016-11-24 12:31:14 +03:00
{
2018-05-06 18:13:29 +03:00
size := 16
2016-11-24 12:31:14 +03:00
switch runtime.GOOS {
case "windows":
2018-05-06 18:13:29 +03:00
size = 64
2016-11-24 12:31:14 +03:00
case "darwin":
2018-05-06 18:13:29 +03:00
size = 512
2016-11-24 12:31:14 +03:00
case "linux":
2018-05-06 18:13:29 +03:00
size = 96
2016-11-24 12:31:14 +03:00
default:
2018-02-19 02:15:21 +03:00
log.Println("unrecognized runtime ", runtime.GOOS)
2016-11-24 12:31:14 +03:00
}
2018-05-06 18:13:29 +03:00
iconFile := fmt.Sprintf("icon%d.png", size)
icon, err := strife.LoadImage(filepath.Join(cfg.ICON_DIR_PATH, iconFile))
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.Text(fmt.Sprintf("fps: %d, ups %d", fps, ups), ww-256, wh-128)
2018-02-28 16:23:07 +03:00
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
}