fond memleak, colordance broken

This commit is contained in:
anton.gurov 2019-10-31 15:08:09 +03:00
parent c372670953
commit 96d6818754
4 changed files with 166 additions and 78 deletions

View File

@ -77,7 +77,7 @@ func main() {
vp := mainwindow.NewViewPort(40, 0, 60, 47, level, mw.GetLayer("base")) vp := mainwindow.NewViewPort(40, 0, 60, 47, level, mw.GetLayer("base"))
vp.Render() vp.Render()
go handleInput(mainCtx, mw.GetLayer("base")) go decodeInput(mainCtx, mw.GetLayer("base"))
//but every call to bearlibterminal must be wrapped to closure and passed to mainfunc //but every call to bearlibterminal must be wrapped to closure and passed to mainfunc
var exit = false var exit = false
@ -114,7 +114,7 @@ func setupLayers(mainwindow *mainwindow.MainWindow) {
mainwindow.AddLayer("menu", 2, "white") mainwindow.AddLayer("menu", 2, "white")
} }
func handleInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) { func decodeInput(ctx util.ClientCtx, baseLayer *mainwindow.Layer) {
var exit = false var exit = false
for !exit{ for !exit{
select { select {

View File

@ -6,49 +6,95 @@ import (
) )
import blt "lab.zaar.be/thefish/bearlibterminal" import blt "lab.zaar.be/thefish/bearlibterminal"
type ColorHolder struct { type ColorHolder interface {
GetColor() uint32
}
type DanceColorHolder struct {
A uint8
R *ring.Ring
G *ring.Ring
B *ring.Ring
}
func (chd *DanceColorHolder) GetColor() uint32 {
return blt.ColorFromARGB(
chd.A,
chd.R.Next().Value.(uint8),
chd.G.Next().Value.(uint8),
chd.B.Next().Value.(uint8),
)
}
type PlainColorHolder struct {
A uint8
R uint8 R uint8
G uint8 G uint8
B uint8 B uint8
} }
func (chb *PlainColorHolder) GetColor() uint32 {
return blt.ColorFromARGB(
chb.A,
chb.R,
chb.G,
chb.B,
)
}
type TileColorSet struct { type TileColorSet struct {
Fg func() uint32 Fg ColorHolder
Bg func() uint32 Bg ColorHolder
DarkFg func() uint32 DarkFg ColorHolder
DarkBg func() uint32 DarkBg ColorHolder
current *ColorHolder
} }
type Appearance struct { type Appearance struct {
Char string Char string `json:"char"`
ColorSet *TileColorSet ColorSet *TileColorSet `json:"colorSet"`
} }
var crng = util.NewRNG() var crng = util.NewRNG()
func NewColorComponentRing(colorValue uint8, minGlow, maxGlow, step int) *ring.Ring { type Tile struct {
*Appearance `json:"app"`
Name string `json:"name"`
Description string `json:"desc"`
BlocksPass bool `json:"blocksPass"`
BlocksSight bool `json:"blocksSight"`
Colordance bool `json:"colordance"`
Explored bool
MustDraw bool
Visible bool
}
func singleColorRing(colorValue uint8) *ring.Ring {
r := ring.New(1)
r.Next().Value = colorValue
return r
}
func fillColorRing(colorValue uint8, minGlow, maxGlow, step int) *ring.Ring {
q := make([]uint8, 0) q := make([]uint8, 0)
color := int(colorValue) color := int(colorValue)
for color < maxGlow { for color < maxGlow {
q = append(q, uint8(color)) q = append(q, uint8(color))
color = crng.Range(0, step) + color color = crng.Range(1, step) + color
}
color = crng.Range(0, step + minGlow)
for uint8(color) < colorValue {
q = append(q, uint8(color))
color = crng.Range(0, step + minGlow)
} }
color = crng.Range(0, step+minGlow)
q = append(q, uint8(color))
//for uint8(color) < uint8(colorValue) {
// q = append(q, uint8(color))
// color = crng.Range(1, step+minGlow)
//}
r := ring.New(len(q)) r := ring.New(len(q))
for _, v := range q { for _, v := range q {
r.Next().Value = v r.Next().Value = v
} }
return r return r
} }
func colordance(colorValue uint8, minGlow, maxGlow, step int) uint8 { func colordance(colorValue uint8, minGlow, maxGlow, step int) uint8 {
color := crng.Range(0, step) + int(colorValue) color := crng.Range(0, step) + int(colorValue)
if color > maxGlow { if color > maxGlow {
@ -57,18 +103,6 @@ func colordance(colorValue uint8, minGlow, maxGlow, step int) uint8 {
return uint8(color) return uint8(color)
} }
type Tile struct {
*Appearance
Name string
Description string
BlocksPass bool
BlocksSight bool
Explored bool
MustDraw bool
Visible bool
Colordance bool
}
func NewWall() *Tile { func NewWall() *Tile {
return &Tile{ return &Tile{
Name: "Wall", Name: "Wall",
@ -80,10 +114,10 @@ func NewWall() *Tile {
Appearance: &Appearance{ Appearance: &Appearance{
Char: "#", Char: "#",
ColorSet: &TileColorSet{ ColorSet: &TileColorSet{
Fg: func() uint32 { return blt.ColorFromARGB(255, 130, 110, 150) }, Fg: &PlainColorHolder{255, 130, 110, 150},
Bg: func() uint32 { return blt.ColorFromARGB(255, 172, 170, 173) }, Bg: &PlainColorHolder{255, 172, 170, 173},
DarkFg: func() uint32 { return blt.ColorFromARGB(255, 20, 20, 68) }, DarkFg: &PlainColorHolder{255, 20, 20, 68},
DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, DarkBg: &PlainColorHolder{255, 7, 7, 30},
}, },
}, },
} }
@ -100,17 +134,17 @@ func NewFloor() *Tile {
Appearance: &Appearance{ Appearance: &Appearance{
Char: ".", Char: ".",
ColorSet: &TileColorSet{ ColorSet: &TileColorSet{
Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, Fg: &PlainColorHolder{255, 220, 220, 250},
Bg: func() uint32 { return blt.ColorFromARGB(255, 19, 19, 70) }, Bg: &PlainColorHolder{255, 19, 19, 70},
DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, DarkFg: &PlainColorHolder{255, 30, 20, 50},
DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, DarkBg: &PlainColorHolder{255, 7, 7, 30},
}, },
}, },
} }
} }
func NewWaterTile() *Tile { func NewWaterTile() *Tile {
ch := &ColorHolder{19, 19, 70} //ch := &ColorHolder{19, 19, 70}
return &Tile{ return &Tile{
Name: "Water", Name: "Water",
Description: "Murky water", Description: "Murky water",
@ -123,26 +157,30 @@ func NewWaterTile() *Tile {
Appearance: &Appearance{ Appearance: &Appearance{
Char: " ", Char: " ",
ColorSet: &TileColorSet{ ColorSet: &TileColorSet{
current: ch, Fg: &PlainColorHolder{255, 220, 220, 250},
Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, Bg: &DanceColorHolder{
Bg: func() uint32 { 255,
return blt.ColorFromARGB( singleColorRing(19),
255, fillColorRing(19, 0, 15, 2),
ch.R, fillColorRing(70, 120, 220, 12),
colordance(ch.G, 0, 15, 2),
colordance(ch.B, 120, 220, 12),
)
}, },
DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, //Bg: func() uint32 {
DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, // return blt.ColorFromARGB(
// 255,
// ch.R,
// colordance(ch.G, 0, 15, 2),
// colordance(ch.B, 120, 220, 12),
// )
//},
DarkFg: &PlainColorHolder{255, 30, 20, 50 },
DarkBg: &PlainColorHolder{255, 7, 7, 30},
}, },
}, },
} }
} }
func NewDeepWaterTile() *Tile { func NewDeepWaterTile() *Tile {
ch := &ColorHolder{5, 2, 122} //ch := &ColorHolder{5, 2, 154}
return &Tile{ return &Tile{
Name: "Deep Water", Name: "Deep Water",
Description: "Deep water", Description: "Deep water",
@ -154,19 +192,44 @@ func NewDeepWaterTile() *Tile {
Appearance: &Appearance{ Appearance: &Appearance{
Char: " ", Char: " ",
ColorSet: &TileColorSet{ ColorSet: &TileColorSet{
current: ch, Fg: &PlainColorHolder{255, 220, 220, 250},
Fg: func() uint32 { return blt.ColorFromARGB(255, 220, 220, 250) }, Bg: &DanceColorHolder{
Bg: func() uint32 { 255,
return blt.ColorFromARGB( singleColorRing(5),
255, fillColorRing(2,2,42,4),
ch.R, fillColorRing(154, 150, 229, 12),
colordance(ch.G, 2, 42, 4),
colordance(ch.B, 180, 229, 12),
)
}, },
DarkFg: func() uint32 { return blt.ColorFromARGB(255, 30, 20, 50) }, //Bg: func() uint32 {
DarkBg: func() uint32 { return blt.ColorFromARGB(255, 7, 7, 30) }, // return blt.ColorFromARGB(
// 255,
// ch.R,
// colordance(ch.G, 2, 42, 4),
// colordance(ch.B, 180, 229, 12),
// )
//},
DarkFg: &PlainColorHolder{255, 30, 20, 50},
DarkBg: &PlainColorHolder{255, 7, 7, 30},
}, },
}, },
} }
} }
func (t *Tile) GetChar() string {
return t.Char
}
func (t *Tile) GetRawColor() uint32 {
if !t.Visible {
return t.Appearance.ColorSet.Fg.GetColor()
} else {
return t.Appearance.ColorSet.DarkFg.GetColor()
}
}
func (t *Tile) GetRawBgColor() uint32 {
if !t.Visible {
return t.Appearance.ColorSet.Bg.GetColor()
} else {
return t.Appearance.ColorSet.DarkBg.GetColor()
}
}

View File

@ -54,6 +54,22 @@ func (layer *Layer) PutWithBackground(x,y int, symbol interface{}, bgColorName s
blt.Composition(prevCompMode) blt.Composition(prevCompMode)
} }
func (Layer *Layer) PutToBase(x,y int, symbol interface{}, fg uint32, bg uint32) {
if symbol == nil {
return
}
rnes := []rune(symbol.(string))
prevColor := uint32(blt.State(blt.TK_COLOR))
prevBgColor := uint32(blt.State(blt.TK_BKCOLOR))
blt.BkColor(bg)
blt.Color(fg)
if (len(rnes)) > 0 {
blt.Put(x, y, int(rnes[0]))
}
blt.Color(prevColor)
blt.BkColor(prevBgColor)
}
func (layer *Layer) after() *Layer { func (layer *Layer) after() *Layer {
blt.Color(layer.defaultColor) blt.Color(layer.defaultColor)
blt.Layer(0) blt.Layer(0)

View File

@ -155,21 +155,30 @@ func (vp *ViewPort) Render() {
for y := 0; y < vp.H; y++ { for y := 0; y < vp.H; y++ {
for x := 0; x < vp.W; x++ { for x := 0; x < vp.W; x++ {
mapCoords := types.Coords{vp.X + x, vp.Y + y} mapCoords := types.Coords{vp.X + x, vp.Y + y}
//vp.level.GetTile(mapCoords).Render()
tile := vp.level.GetTile(mapCoords) tile := vp.level.GetTile(mapCoords)
if tile.Visible {
if tile.MustDraw { //vp.layer.
//darkened version of landscape // WithRawColor(tile.GetRawColor()).
vp.layer.WithColor("green"). // PutWithBackground(mapCoords.X, mapCoords.Y, tile.GetChar(), "grey")
Put(mapCoords.X, mapCoords.Y, tile.Char)
} vp.layer.PutToBase(mapCoords.X,mapCoords.Y,tile.GetChar(), tile.GetRawColor(), tile.GetRawBgColor())
} else {
if redraw == true || tile.Colordance { //if tile.Visible {
vp.layer.WithColor("white"). // if tile.MustDraw {
Put(mapCoords.X, mapCoords.Y, tile.Char) // //darkened version of landscape
tile.Explored = true // vp.layer.WithColor("green").
tile.MustDraw = true // Put(mapCoords.X, mapCoords.Y, tile.Char)
} // }
} //} else {
// if redraw == true || tile.Colordance {
// vp.layer.WithColor("grey").
// Put(mapCoords.X, mapCoords.Y, tile.Char)
// tile.Explored = true
// tile.MustDraw = true
// }
//}
} }
} }