From ffe658e90ef26b6737fb461bc5907279961e9064 Mon Sep 17 00:00:00 2001 From: "anton.gurov" Date: Wed, 30 Oct 2019 20:23:47 +0300 Subject: [PATCH] decouple main loop attempt --- cmd/game/main.go | 119 +++++++++++++++++------------- engine/gamemap/mapgens/default.go | 4 +- ui/mainwindow/layer.go | 6 ++ 3 files changed, 75 insertions(+), 54 deletions(-) diff --git a/cmd/game/main.go b/cmd/game/main.go index 631228d..b63d9a8 100644 --- a/cmd/game/main.go +++ b/cmd/game/main.go @@ -24,12 +24,14 @@ type GameState struct { mainfunc chan func() exit chan struct{} input chan string + rawInput chan int } var State = GameState{ mainfunc: make(chan func()), exit: make(chan struct{}, 1), - input: make(chan string, 1), + input: make(chan string, 5), + rawInput: make(chan int, 5), } func main() { @@ -96,14 +98,13 @@ Cursus in hac habitasse platea. Aliquet risus feugiat in ante metus dictum. Maec Sed euismod nisi porta lorem mollis aliquam ut porttitor leo. Ut tellus elementum sagittis vitae et leo duis ut diam. Elementum curabitur vitae nunc sed velit dignissim. Auctor elit sed vulputate mi sit. Consectetur adipiscing elit ut aliquam purus. Feugiat vivamus at augue eget arcu. Duis ut diam quam nulla porttitor massa id neque. Pharetra magna ac placerat vestibulum lectus mauris ultrices. Non sodales neque sodales ut etiam. Massa ultricies mi quis hendrerit dolor. Est sit amet facilisis magna etiam. Ornare suspendisse sed nisi lacus sed viverra tellus in. `) - menuLayer.WithColor("red").PutWithBackground(20,40,"Ы", "#aa1257d4") - menuLayer.WithColor("white").PutWithBackground(21,40,"Щ", "#cd31ed12") - menuLayer.WithColor("yellow").PutWithBackground(22,40,"Ц", "#efcccccc") - - menuLayer.WithColor("red").PutWithBackground(39,1,"Ы", "#aa1257d4") - menuLayer.WithColor("white").PutWithBackground(40,1,"Щ", "#cd31ed12") - menuLayer.WithColor("yellow").PutWithBackground(41,1,"Ц", "#efcccccc") + menuLayer.WithColor("red").PutWithBackground(20, 40, "Ы", "#aa1257d4") + menuLayer.WithColor("white").PutWithBackground(21, 40, "Щ", "#cd31ed12") + menuLayer.WithColor("yellow").PutWithBackground(22, 40, "Ц", "#efcccccc") + menuLayer.WithColor("red").PutWithBackground(39, 1, "Ы", "#aa1257d4") + menuLayer.WithColor("white").PutWithBackground(40, 1, "Щ", "#cd31ed12") + menuLayer.WithColor("yellow").PutWithBackground(41, 1, "Ц", "#efcccccc") bgLayer.WithColor("#77cfcfcf").NewWindow(45, 5, 40, 40).Splash() menuLayer.WithColor("#aaed26ca").NewWindow(45, 5, 40, 40).DoubleBordered("Transparent BG window test") @@ -122,57 +123,71 @@ Sed euismod nisi porta lorem mollis aliquam ut porttitor leo. Ut tellus elementu State.Do(initRender) - level := gamemap.NewLevel(ctx, "test", 1) level = mapgens.DefaultGen(level) vp := mainwindow.NewViewPort(40, 0, 60, 47, level, &baseLayer) + State.Do(func() { + vp.Render() + }) + d, _ := time.ParseDuration("166ms") // ~6 fps for terrain + d2, _ := time.ParseDuration("1ms") + terrainAnimationTicker := time.NewTicker(d) + //fpsTicker := time.NewTicker(d2) //main loop! for { - //fresh inputs to chan - State.Do(func() { - var key, keycode = ui.ReadKey(ctx) - if keycode == blt.TK_CLOSE { - ctx.Logger().Warn().Msg("exiting on window close...") - State.exit <- struct{}{} - ctx.Logger().Warn().Msg("...done") - return - } - switch key { - case "F10": - blt.Close() - blt.Open() - blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;") - case "Ctrl+q": - fallthrough - case "Escape": - ctx.Logger().Info().Msg("exiting on quit command...") - State.exit <- struct{}{} - ctx.Logger().Info().Msg("...done") - return - default: - //blt.Print(1, 3, "Key: "+key) - State.input <- key - //baseLayer.Print(1, 4, "█") - } - }) - //read inputs, write to screen - State.Do(func() { - key := <-State.input - if key != "" { - blt.ClearArea(0, 3, 80, 1) - menuLayer.Print(1, 3, "Key: "+key) - baseLayer.Print(1, 5, key) - } - }) + select { + case <-terrainAnimationTicker.C: + //ctx.Logger().Debug().Msg("hb!") + State.Do(func() { + vp.Render() + }) + case key := <-State.input: + switch key { + case "F10": + State.Do(func(){ + blt.Set("window: size=100x47; font: ./resources/fonts-ttf/UbuntuMono-R.ttf, size=11;") + return + }) + case "Ctrl+q": + fallthrough + case "Escape": + ctx.Logger().Info().Msg("exiting on quit command...") + State.exit <- struct{}{} + ctx.Logger().Info().Msg("...done") + return + default: + State.Do(func(){ + baseLayer.ClearArea(0, 3, 80, 1) + baseLayer.Print(1, 3, "Key: "+key) + baseLayer.Print(1, 4, "█") + return + }) + } - State.Do(func(){ - vp.Render() - }) - //update screen - State.Do(func() { - blt.Refresh() - }) + default: + State.Do(func() { + var key, keycode = ui.ReadKey(ctx) + if keycode == blt.TK_NONE { + return + } + if keycode == blt.TK_CLOSE { + ctx.Logger().Warn().Msg("exiting on window close...") + State.exit <- struct{}{} + ctx.Logger().Warn().Msg("...done") + return + } + State.input <- key + //time.Sleep(d2) + }) + + + //update screen + State.Do(func() { + blt.Refresh() + time.Sleep(d2) //Костыль для убирания 100% CPU + }) + } } ctx.Logger().Warn().Msg("and it continues") } diff --git a/engine/gamemap/mapgens/default.go b/engine/gamemap/mapgens/default.go index 8f271ef..785db4f 100644 --- a/engine/gamemap/mapgens/default.go +++ b/engine/gamemap/mapgens/default.go @@ -87,7 +87,7 @@ func digHTunnel(l *gamemap.Level, x1,x2,y int, fillage types.RectFill) { start = x2 finish = x1 } - for i := start; i <= finish; i++ { + for i := start; i <= finish - 1; i++ { l.Tiles[i][y] = fillage.Body.(func() *gamemap.Tile)() } } @@ -101,7 +101,7 @@ func digVTunnel(l *gamemap.Level, y1,y2,x int, fillage types.RectFill) { start = y2 finish = y1 } - for i := start; i <= finish; i++ { + for i := start; i <= finish - 1; i++ { l.Tiles[x][i] = fillage.Body.(func() *gamemap.Tile)() } } diff --git a/ui/mainwindow/layer.go b/ui/mainwindow/layer.go index 8c71dc2..190e4bb 100644 --- a/ui/mainwindow/layer.go +++ b/ui/mainwindow/layer.go @@ -91,6 +91,12 @@ func (layer *Layer) Clear(r *types.Rect) { blt.ClearArea(r.X, r.Y, r.W, r.H) } +func (layer *Layer) ClearArea(x,y,w,h int) { + layer.before() + blt.ClearArea(x,y,w,h) + layer.after() +} + func (layer *Layer) Render() {