1
1
mirror of https://github.com/OpenDiablo2/OpenDiablo2 synced 2024-11-07 18:57:18 -05:00
OpenDiablo2/d2term/d2term.go

115 lines
2.0 KiB
Go
Raw Normal View History

package d2term
import (
"errors"
"log"
"github.com/OpenDiablo2/OpenDiablo2/d2input"
"github.com/OpenDiablo2/OpenDiablo2/d2render/d2surface"
)
var (
ErrHasInit error = errors.New("terminal system is already initialized")
ErrNotInit error = errors.New("terminal system is not initialized")
)
var singleton *terminal
func Initialize() error {
if singleton != nil {
return ErrHasInit
}
terminal, err := createTerminal()
if err != nil {
return err
}
if err := d2input.BindHandlerWithPriority(terminal, d2input.PriorityHigh); err != nil {
log.Println(err)
return err
}
singleton = terminal
return nil
}
func Shutdown() {
if singleton != nil {
d2input.UnbindHandler(singleton)
singleton = nil
}
}
func Advance(elapsed float64) error {
if singleton == nil {
return ErrNotInit
}
if singleton != nil {
return singleton.advance(elapsed)
}
return ErrNotInit
}
func Output(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.output(format, params...)
}
func OutputInfo(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputInfo(format, params...)
}
func OutputWarning(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputWarning(format, params...)
}
func OutputError(format string, params ...interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.outputError(format, params...)
}
func BindAction(name, description string, action interface{}) error {
if singleton == nil {
return ErrNotInit
}
return singleton.bindAction(name, description, action)
}
func UnbindAction(name string) error {
if singleton == nil {
return ErrNotInit
}
return singleton.unbindAction(name)
}
func Render(surface *d2surface.Surface) error {
if singleton == nil {
return ErrNotInit
}
return singleton.render(surface)
}
func BindLogger() {
log.SetOutput(&terminalLogger{writer: log.Writer()})
}