Moved default NewRendererOptions to generic part instead of in specific renderers.

This commit is contained in:
Sander Schobers 2020-05-17 08:29:02 +02:00
parent e2472cffef
commit f20397c684
4 changed files with 16 additions and 15 deletions

View File

@ -11,11 +11,7 @@ func init() {
type rendererFactory struct{}
func (f rendererFactory) New(title string, width, height int) (ui.Renderer, error) {
return f.NewOptions(title, width, height, ui.NewRendererOptions{Resizable: true})
}
func (f rendererFactory) NewOptions(title string, width, height int, opts ui.NewRendererOptions) (ui.Renderer, error) {
func (f rendererFactory) New(title string, width, height int, opts ui.NewRendererOptions) (ui.Renderer, error) {
renderer, err := NewRenderer(width, height, allg5.NewDisplayOptions{
Resizable: opts.Resizable,
Vsync: opts.VSync,

View File

@ -11,11 +11,7 @@ func init() {
type rendererFactory struct{}
func (f rendererFactory) New(title string, width, height int) (ui.Renderer, error) {
return f.NewOptions(title, width, height, ui.NewRendererOptions{Resizable: true})
}
func (f rendererFactory) NewOptions(title string, width, height int, opts ui.NewRendererOptions) (ui.Renderer, error) {
func (f rendererFactory) New(title string, width, height int, opts ui.NewRendererOptions) (ui.Renderer, error) {
return NewRenderer(title, int32(width), int32(height), NewRendererOptions{
Location: sdl.Point{X: sdl.WINDOWPOS_UNDEFINED, Y: sdl.WINDOWPOS_UNDEFINED},
Resizable: opts.Resizable,

View File

@ -116,7 +116,7 @@ func (b *basic) Init(ctx ui.Context) error {
}
func run() error {
var render, err = ui.NewRenderer("Basic Example", 800, 600)
var render, err = ui.NewRendererDefault("Basic Example", 800, 600)
if err != nil {
return err
}

View File

@ -6,18 +6,26 @@ import (
"opslag.de/schobers/geom"
)
// RendererFactory can be used to inject a new factory for creating renderers.
type RendererFactory interface {
New(title string, width, height int) (Renderer, error)
NewOptions(title string, width, height int, opts NewRendererOptions) (Renderer, error)
New(title string, width, height int, opts NewRendererOptions) (Renderer, error)
}
var rendererFactory RendererFactory
func NewRenderer(title string, width, height int) (Renderer, error) {
// NewRenderer creates a new renderer based on the registered renderer factory.
func NewRenderer(title string, width, height int, opts NewRendererOptions) (Renderer, error) {
if rendererFactory == nil {
return nil, errors.New("no renderer factory registered")
}
return rendererFactory.New(title, width, height)
return rendererFactory.New(title, width, height, opts)
}
// NewRendererDefault creates a new renderer with default options set based on the registered renderer factory.
func NewRendererDefault(title string, width, height int) (Renderer, error) {
return rendererFactory.New(title, width, height, NewRendererOptions{
Resizable: true,
})
}
// SetRendererFactory sets the new factory that is used to create a new renderer.
@ -25,6 +33,7 @@ func SetRendererFactory(factory RendererFactory) {
rendererFactory = factory
}
// NewRendererOptions provides options when creating a new renderer.
type NewRendererOptions struct {
Location *geom.PointF32
Resizable bool