JoelOtter / termloop

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

Change background color? #40

Closed jackbackes closed 5 years ago

jackbackes commented 7 years ago

I'm trying to implement a Day/Night cycle (right now by simply cycling green from 55 to 255), but it is unclear to me how to change the Background color.

This is my code currently (run as a goroutine), but unfortunately it does not work...

func DayNight(level tl.Level, l *tl.Cell, s *tl.Screen) { // "l" is the background for "level"
        ticker := time.NewTicker(100 * time.Millisecond)
        sunRise := true
        sunSet := false

        color := 55

        for {
                select {
                case <- ticker.C:
                        if color >= 255 {
                                sunRise = false
                                sunSet = true
                        }
                        if color <= 55 {
                                sunRise = true
                                sunSet = false
                        }
                        if sunRise == true {
                                color = color + 1
                        }
                        if sunSet == true {
                                color = color - 1
                        }
                        l.Bg = tl.RgbTo256Color(0, color, 0)
                        level.DrawBackground(s)
                }
        }
}
func main() {
        g := tl.NewGame()
        levelCell := tl.Cell {
                Bg: tl.RgbTo256Color(0, 51, 0),
                Fg: tl.ColorBlack,
                Ch: 'v',
        }
        level := tl.NewBaseLevel(levelCell)
        go DayNight(level, &levelCell, g.Screen())
        level.AddEntity(tl.NewRectangle(10, 10, 50, 20, tl.ColorBlue))
        g.Screen().SetLevel(level)
        g.Start()
}
JoelOtter commented 7 years ago

Termloop is single-threaded - doing graphics stuff inside goroutines is officially not supported.

What kind of Level are you using - is it one you implemented yourself, or BaseLevel? You'll probably have to implement your own Level as the BaseLevel's background cell isn't actually exported (though maybe it should be).

On 5 August 2017 at 03:58, thejohnbackes notifications@github.com wrote:

I'm trying to implement a Day/Night cycle (right now by simply cycling green from 55 to 255), but it is unclear to me how to change the Background color.

This is my code currently (run as a goroutine), but unfortunately it does not work...

func DayNight(level tl.Level, l tl.Cell, s tl.Screen) { ticker := time.NewTicker(100 * time.Millisecond) sunRise := true sunSet := false

    color := 55

    for {
            select {
            case <- ticker.C:
                    if color >= 255 {
                            sunRise = false
                            sunSet = true
                    }
                    if color <= 55 {
                            sunRise = true
                            sunSet = false
                    }
                    if sunRise == true {
                            fmt.Printf("^")
                            color = color + 1
                    }
                    if sunSet == true {
                            fmt.Printf("v")
                            color = color - 1
                    }
                    l.Bg = tl.RgbTo256Color(0, color, 0)
                    level.DrawBackground(s)
            }
    }

}

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JoelOtter/termloop/issues/40, or mute the thread https://github.com/notifications/unsubscribe-auth/ACoyLKHGQoo7ovY07z7PvxmTD9tF9gwnks5sU9pvgaJpZM4OuWEf .

jackbackes commented 7 years ago

ok, good to know. I used BaseLevel, and yes, the trouble I had was that bg was not exported. Or maybe have a GetBG method rather than exporting the field directly? On Sat, Aug 5, 2017 at 3:27 AM Joel Auterson notifications@github.com wrote:

Termloop is single-threaded - doing graphics stuff inside goroutines is officially not supported.

What kind of Level are you using - is it one you implemented yourself, or BaseLevel? You'll probably have to implement your own Level as the BaseLevel's background cell isn't actually exported (though maybe it should be).

On 5 August 2017 at 03:58, thejohnbackes notifications@github.com wrote:

I'm trying to implement a Day/Night cycle (right now by simply cycling green from 55 to 255), but it is unclear to me how to change the Background color.

This is my code currently (run as a goroutine), but unfortunately it does not work...

func DayNight(level tl.Level, l tl.Cell, s tl.Screen) { ticker := time.NewTicker(100 * time.Millisecond) sunRise := true sunSet := false

color := 55

for { select { case <- ticker.C: if color >= 255 { sunRise = false sunSet = true } if color <= 55 { sunRise = true sunSet = false } if sunRise == true { fmt.Printf("^") color = color + 1 } if sunSet == true { fmt.Printf("v") color = color - 1 } l.Bg = tl.RgbTo256Color(0, color, 0) level.DrawBackground(s) } } }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/JoelOtter/termloop/issues/40, or mute the thread < https://github.com/notifications/unsubscribe-auth/ACoyLKHGQoo7ovY07z7PvxmTD9tF9gwnks5sU9pvgaJpZM4OuWEf

.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/JoelOtter/termloop/issues/40#issuecomment-320435953, or mute the thread https://github.com/notifications/unsubscribe-auth/AM94FBxIHSMxGZEFuMCeVwx_n_6J6QLcks5sVEOLgaJpZM4OuWEf .