2020-01-31 23:18:11 -05:00
|
|
|
package d2render
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"log"
|
2020-06-29 00:41:58 -04:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-07-01 08:59:24 -04:00
|
|
|
// ErrWasInit holding an error instance for initialized rendering system
|
|
|
|
ErrWasInit = errors.New("rendering system is already initialized")
|
|
|
|
// ErrNotInit holding an error instance for non-initialized rendering system
|
|
|
|
ErrNotInit = errors.New("rendering system has not been initialized")
|
|
|
|
// ErrInvalidRenderer holding an error instance for invalid rendering system specification
|
2020-02-01 21:51:49 -05:00
|
|
|
ErrInvalidRenderer = errors.New("invalid rendering system specified")
|
2020-01-31 23:18:11 -05:00
|
|
|
)
|
|
|
|
|
2020-06-29 00:41:58 -04:00
|
|
|
var singleton d2interface.Renderer
|
2020-01-31 23:18:11 -05:00
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// Initialize the renderer
|
2020-06-29 00:41:58 -04:00
|
|
|
func Initialize(rend d2interface.Renderer) error {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyNotInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
singleton = rend
|
|
|
|
log.Printf("Initialized the %s renderer...", singleton.GetRendererName())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// SetWindowIcon sets the window icon by a given file name as string
|
2020-02-09 14:12:04 -05:00
|
|
|
func SetWindowIcon(fileName string) {
|
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
singleton.SetWindowIcon(fileName)
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// Run will run the renderer
|
2020-06-29 00:41:58 -04:00
|
|
|
func Run(f func(d2interface.Surface) error, width, height int, title string) error {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
singleton.Run(f, width, height, title)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// IsDrawingSkipped checks whether the drawing is skipped
|
2020-02-09 14:12:04 -05:00
|
|
|
func IsDrawingSkipped() bool {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton.IsDrawingSkipped()
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// CreateSurface creates a new surface, which returns the newly created surface or error
|
2020-06-29 00:41:58 -04:00
|
|
|
func CreateSurface(surface d2interface.Surface) (d2interface.Surface, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
return singleton.CreateSurface(surface)
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// NewSurface adds a new surface, and returns the new surface or error
|
2020-06-29 00:41:58 -04:00
|
|
|
func NewSurface(width, height int, filter d2interface.Filter) (d2interface.Surface, error) {
|
2020-02-09 14:12:04 -05:00
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
return singleton.NewSurface(width, height, filter)
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// IsFullScreen checks whether the window is on full screen
|
2020-02-09 14:12:04 -05:00
|
|
|
func IsFullScreen() bool {
|
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
return singleton.IsFullScreen()
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// SetFullScreen sets the window in fullscreen or windowed mode depending on the fullScreen flag
|
2020-02-09 14:12:04 -05:00
|
|
|
func SetFullScreen(fullScreen bool) {
|
|
|
|
verifyWasInit()
|
|
|
|
singleton.SetFullScreen(fullScreen)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// SetVSyncEnabled sets or unsets the VSync depending on the given vsync parameter flag
|
2020-02-09 14:12:04 -05:00
|
|
|
func SetVSyncEnabled(vsync bool) {
|
|
|
|
verifyWasInit()
|
|
|
|
singleton.SetVSyncEnabled(vsync)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// GetVSyncEnabled checks whether the VSync is enabled or not
|
2020-02-09 14:12:04 -05:00
|
|
|
func GetVSyncEnabled() bool {
|
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
return singleton.GetVSyncEnabled()
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// GetCursorPos returns the exact current position of the cursor
|
2020-02-09 14:12:04 -05:00
|
|
|
func GetCursorPos() (int, int) {
|
|
|
|
verifyWasInit()
|
2020-01-31 23:18:11 -05:00
|
|
|
return singleton.GetCursorPos()
|
|
|
|
}
|
|
|
|
|
2020-07-01 08:59:24 -04:00
|
|
|
// CurrentFPS returns the current frames per second
|
2020-02-09 14:12:04 -05:00
|
|
|
func CurrentFPS() float64 {
|
|
|
|
verifyWasInit()
|
|
|
|
return singleton.CurrentFPS()
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyWasInit() {
|
2020-01-31 23:18:11 -05:00
|
|
|
if singleton == nil {
|
2020-02-09 14:12:04 -05:00
|
|
|
panic(ErrNotInit)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func verifyNotInit() {
|
|
|
|
if singleton != nil {
|
|
|
|
panic(ErrWasInit)
|
2020-01-31 23:18:11 -05:00
|
|
|
}
|
|
|
|
}
|