JoelOtter / termloop

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

crash on SetPostion #38

Closed splace closed 6 years ago

splace commented 7 years ago

i have a program that changes some text's position, from a go routine, and occasionally i get this crash;

panic: runtime error: index out of range

goroutine 1 [running]: github.com/JoelOtter/termloop.(Text).Draw(0xc420014500, 0xc420092000) /home/simon/gocode/src/github.com/JoelOtter/termloop/text.go:39 +0xdd github.com/JoelOtter/termloop.(Screen).Draw(0xc420092000) /home/simon/gocode/src/github.com/JoelOtter/termloop/screen.go:54 +0xb9 github.com/JoelOtter/termloop.(*Game).Start(0xc420012510) /home/simon/gocode/src/github.com/JoelOtter/termloop/game.go:115 +0x3f9 main.main() /home/simon/Dropbox/github/working/moving/text.go:31 +0xfa

code.....

package main

import "github.com/JoelOtter/termloop"
import "github.com/splace/joysticks"
import "fmt"

func main() {
    g := termloop.NewGame()
    m:=termloop.NewText(60, 20, "<0,0>", termloop.ColorWhite, termloop.ColorBlue)
    g.Screen().AddEntity(m)

    go func(){
        evtChans := joysticks.Capture(
            joysticks.Channel{1, joysticks.HID.OnMove},
            joysticks.Channel{1, joysticks.HID.OnClose},
        )
        pos:=joysticks.PositionFromVelocity(evtChans[0])
        for{
            select {
            case evt:= <-pos:
                posEvt:=evt.(joysticks.CoordsEvent)
                m.SetText(fmt.Sprintf("<%1.2f,%1.2f>",posEvt.X,posEvt.Y))
                m.SetPosition(int(posEvt.X*80+60), int(posEvt.Y*80+20))
            case <-evtChans[1]:
                return
            }
        }   
    }()
    g.SetEndKey(termloop.KeyEnd)
    g.Start()
}
JoelOtter commented 7 years ago

Suggest you avoid using goroutines - Termloop makes no guarantees about being thread-safe.

On 29 Apr 2017, 01:43 +0100, simon place notifications@github.com, wrote:

i have a program that changes to some text's position, from a go routine, and occasionally i get this crash;

panic: runtime error: index out of range

goroutine 1 [running]: github.com/JoelOtter/termloop.(Text).Draw(0xc420014500, 0xc420092000) /home/simon/gocode/src/github.com/JoelOtter/termloop/text.go:39 +0xdd github.com/JoelOtter/termloop.(Screen).Draw(0xc420092000) /home/simon/gocode/src/github.com/JoelOtter/termloop/screen.go:54 +0xb9 github.com/JoelOtter/termloop.(*Game).Start(0xc420012510) /home/simon/gocode/src/github.com/JoelOtter/termloop/game.go:115 +0x3f9 main.main() /home/simon/Dropbox/github/working/moving/text.go:31 +0xfa

— 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/38), or mute the thread (https://github.com/notifications/unsubscribe-auth/ACoyLGf__4sXoQ4rx-x2i1_CJtlZRTRHks5r0ofBgaJpZM4NMGMC).