phi/main.go

185 lines
3.6 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) {
}
2019-03-16 22:15:26 +03:00
func (n *PhiEditor) init(conf *cfg.TomlConfig) {
mainView := buff.NewView(int(1280.0*cfg.ScaleFactor), int(720.0*cfg.ScaleFactor), conf)
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 = conf.Editor.LoadedFont
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
windowConfig := strife.DefaultConfig()
windowConfig.Accelerated = config.Render.Accelerated
windowConfig.Alias = config.Render.Aliased
windowConfig.VerticalSync = config.Render.VerticalSync
2019-03-16 22:15:26 +03:00
ww, wh := float32(640.0), float32(360.0)
dpi, defDpi := strife.GetDisplayDPI(0)
cfg.ScaleFactor = float64(dpi / defDpi)
scaledWidth := int((ww * dpi) / defDpi)
scaledHeight := int((wh * dpi) / defDpi)
window := strife.SetupRenderWindow(scaledWidth, scaledHeight, windowConfig)
window.AllowHighDPI()
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:
2019-03-16 22:23:58 +03:00
fmt.Println("window resize is unimplemented: size", event.Width, event.Height)
default:
editor.handleEvent(evt)
}
})
if err := window.Create(); err != nil {
panic(err)
}
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.IconDirPath, 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.AlwaysRender {
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)
2019-03-16 22:15:26 +03:00
ctx.Text(fmt.Sprintf("fps: %d, ups %d", fps, ups), int(scaledWidth-256), int(scaledHeight-128))
2018-02-28 16:23:07 +03:00
ctx.Display()
2019-03-16 22:15:26 +03:00
frames++
2018-02-28 16:23:07 +03:00
}
2019-03-16 22:15:26 +03:00
updates++
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.ThrottleCpuUsage {
2018-02-28 16:23:07 +03:00
// 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
}