fixes to viewport, config tuning

This commit is contained in:
2019-11-01 15:03:52 +03:00
parent a91351d3dc
commit c6c6b6254d
11 changed files with 228 additions and 173 deletions

View File

@ -5,6 +5,7 @@ import (
"github.com/rs/zerolog/log"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap"
"lab.zaar.be/thefish/alchemyst-go/engine/gamemap/mapgens"
"lab.zaar.be/thefish/alchemyst-go/engine/types"
"lab.zaar.be/thefish/alchemyst-go/ui"
"lab.zaar.be/thefish/alchemyst-go/ui/mainwindow"
"lab.zaar.be/thefish/alchemyst-go/util"
@ -22,23 +23,6 @@ func init() {
runtime.LockOSThread()
}
type GameState struct {
mainfunc chan func()
exit chan struct{}
input chan string
rawInput chan int
}
// do runs f on the main thread.
func (*GameState) Do(f func()) {
done := make(chan struct{}, 1)
State.mainfunc <- func() {
f()
done <- struct{}{}
}
<-done
}
//we can run logic in separate goroutines
//
// go doSometing(State,...)
@ -52,11 +36,13 @@ func (*GameState) Do(f func()) {
// ...
// }
var State = GameState{
mainfunc: make(chan func()),
exit: make(chan struct{}, 1),
input: make(chan string, 1),
rawInput: make(chan int, 1),
var State = types.GameState{
Mainfunc: make(chan func()),
Exit: make(chan struct{}, 1),
Input: make(chan string, 1),
RawInput: make(chan int, 1),
FovRecompute: make(chan struct{},1),
Redraw: make(chan struct{},1),
}
func main() {
@ -72,35 +58,37 @@ func main() {
setupLayers(mw)
level := gamemap.NewLevel(mainCtx, "test", 1)
level = mapgens.DefaultGen(level)
level, rooms := mapgens.DefaultGen(gamemap.NewLevel(mainCtx, "test", 1))
vp := mainwindow.NewViewPort(40, 0, 60, 47, level, mw.GetLayer("base"))
vp.Render()
vp.PlayerCoords = rooms[0].Center
vp.Render(State)
go decodeInput(mainCtx, mw.GetLayer("base"))
go vp.Listen(State)
//but every call to bearlibterminal must be wrapped to closure and passed to mainfunc
var exit = false
for !exit {
select {
case State.rawInput <- ui.ReadKeyCode():
case State.RawInput <- ui.ReadKeyCode():
break
case pressed := <-State.input:
case pressed := <-State.Input:
mw.GetLayer("base").ClearArea(0, 3, 40, 1)
mw.GetLayer("base").Print(1, 3, "Key: "+pressed)
mw.GetLayer("base").Print(1, 6, "█")
break
//case f := <-State.mainfunc:
// f()
// break
case <-State.exit:
//case f := <-State.mainfunc:
// f()
// break
case <-State.Exit:
mainCtx.Logger().Warn().Msg("quitting NOW")
exit = true
break
// не оставляйте default в бесконесчном select {} - сожрет всё CPU
// не оставляйте default в бесконесчном select {} - сожрет всё CPU
default:
vp.Render()
vp.Render(State)
blt.Refresh()
}
@ -117,20 +105,20 @@ func setupLayers(mainwindow *mainwindow.MainWindow) {
func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
var exit = false
var waitForWCspam = true
for !exit{
for !exit {
select {
case keycode := <-State.rawInput:
case keycode := <-State.RawInput:
if keycode == blt.TK_NONE {
continue
}
if keycode == blt.TK_CLOSE && !waitForWCspam {
ctx.Logger().Warn().Msg("exiting on window close...")
State.exit <- struct{}{}
State.Exit <- struct{}{}
ctx.Logger().Warn().Msg("...done")
return
}
var pressed= ""
var isModifier, _= util.InArray(keycode, modifiers)
var pressed = ""
var isModifier, _ = util.InArray(keycode, modifiers)
if !isModifier {
pressed = ui.Scancodemap[keycode]
@ -147,6 +135,7 @@ func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
//global hotkeys
switch pressed {
//fixme testing only
case "F10":
State.Do(func() {
blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;")
@ -155,13 +144,15 @@ func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
fallthrough
case "Escape":
ctx.Logger().Info().Msg("exiting on quit command...")
State.exit <- struct{}{}
State.Exit <- struct{}{}
ctx.Logger().Info().Msg("...done")
exit = true
return
default:
waitForWCspam = false;
State.input <- pressed
if pressed != "" {
waitForWCspam = false;
State.Input <- pressed
}
}
}
}