JoelOtter / termloop

Terminal-based game engine for Go, built on top of Termbox
Other
1.43k stars 83 forks source link

Update transparent cells for the entity canvas #33

Closed fokoenecke closed 7 years ago

fokoenecke commented 7 years ago

Hi! I am trying to learn some Go and love your terminal libraries for starting some fun projects. Thank You!

I am creating an entity that has different orientations depending on which direction it is facing. That entity has some transparent pixels on it. On loading the entity, I create a new canvas for every orientation and keep a pointer to them. If the orientation of my entity changes, I use ApplyCanvas() to apply the according canvas to the underlying entity's canvas. Unfortunately, when the orientation changes, the transparent pixel won't apply, if there is already a color defined (renderCell() in screen.go). So the previous color for that pixel shows up, where a transparent one should be.

I understand why it's necessary to do this when drawing on a background, but would it be possible to use another function, that applies all pixels (including transparent) when not rendering to the screen, but on an internal(~sprite) canvas? What I did as a workaround, is using a special "Sprite" render Method and use this in ApplyCanvas().

func renderSpriteCell(old, new_ *Cell) {
    old.Ch = new_.Ch
    old.Bg = new_.Bg
    old.Fg = new_.Fg
}

func (e *Entity) ApplyCanvas(c *Canvas) {
    for i := 0; i < min(len(e.canvas), len(*c)); i++ {
        for j := 0; j < min(len(e.canvas[0]), len((*c)[0])); j++ {
            renderSpriteCell(&e.canvas[i][j], &(*c)[i][j])
        }
    }
}

In a real solution you wold need to use that in all relevant functions, I guess. Unfortunately I don't understand the implications of such change and don't feel confident in supplying a pull request.

Greetings!

JoelOtter commented 7 years ago

Interesting. :) I think a good solution here might be to offer an entity.SetCanvas(*Canvas) method - would that fit your needs? You'd then just swap in the canvas you wanted when the orientation changed, instead over overwriting.

fokoenecke commented 7 years ago

Yeah, sounds great. Didn't think about that. Since that wouldn't be much of a change, I could do that on my own and create a pull request.