From ea37c58ee80c06c968f2e8836b051ab763203174 Mon Sep 17 00:00:00 2001 From: obvionaoe Date: Thu, 9 May 2024 16:01:48 +0100 Subject: [PATCH 1/2] Add WindowOption and RunWithOptions function to allow for extra configuration --- crt.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/crt.go b/crt.go index ffa29d3..6e08553 100644 --- a/crt.go +++ b/crt.go @@ -66,6 +66,43 @@ type Window struct { invalidateBuffer bool } +type WindowOption func(window *Window) + +// WithWindowTitle sets the window title. +func WithWindowTitle(title string) WindowOption { + return func(window *Window) { + ebiten.SetWindowTitle(title) + } +} + +// WithScreenFilter enables the screen filter. +func WithScreenFilter() WindowOption { + return func(window *Window) { + ebiten.SetScreenFilterEnabled(true) + } +} + +// WithoutScreenFilter disables the screen filter. +func WithoutScreenFilter() WindowOption { + return func(window *Window) { + ebiten.SetScreenFilterEnabled(true) + } +} + +// WithWindowDecoration enables window decorations. +func WithWindowDecoration() WindowOption { + return func(window *Window) { + ebiten.SetWindowDecorated(true) + } +} + +// WithoutWindowDecoration enables window decorations. +func WithoutWindowDecoration() WindowOption { + return func(window *Window) { + ebiten.SetWindowDecorated(false) + } +} + // NewGame creates a new terminal game with the given dimensions and font faces. func NewGame(width int, height int, fonts Fonts, tty io.Reader, adapter InputAdapter, defaultBg color.Color) (*Window, error) { if defaultBg == nil { @@ -631,6 +668,20 @@ func (g *Window) Run(title string) error { return nil } +func (g *Window) RunWithOptions(options ...WindowOption) error { + ebiten.SetWindowSize(int(float64(g.cellsWidth*g.cellWidth)/DeviceScale()), int(float64(g.cellsHeight*g.cellHeight)/DeviceScale())) + + for _, opt := range options { + opt(g) + } + + if err := ebiten.RunGame(g); err != nil { + return err + } + + return nil +} + func (g *Window) Kill() { SysKill() } From d976f616733fbd51a77aa2c66d236fdea80100c9 Mon Sep 17 00:00:00 2001 From: obvionaoe Date: Thu, 9 May 2024 16:15:19 +0100 Subject: [PATCH 2/2] Remove test functions for WindowOption --- crt.go | 35 ----------------------------------- 1 file changed, 35 deletions(-) diff --git a/crt.go b/crt.go index 6e08553..ef72ec2 100644 --- a/crt.go +++ b/crt.go @@ -68,41 +68,6 @@ type Window struct { type WindowOption func(window *Window) -// WithWindowTitle sets the window title. -func WithWindowTitle(title string) WindowOption { - return func(window *Window) { - ebiten.SetWindowTitle(title) - } -} - -// WithScreenFilter enables the screen filter. -func WithScreenFilter() WindowOption { - return func(window *Window) { - ebiten.SetScreenFilterEnabled(true) - } -} - -// WithoutScreenFilter disables the screen filter. -func WithoutScreenFilter() WindowOption { - return func(window *Window) { - ebiten.SetScreenFilterEnabled(true) - } -} - -// WithWindowDecoration enables window decorations. -func WithWindowDecoration() WindowOption { - return func(window *Window) { - ebiten.SetWindowDecorated(true) - } -} - -// WithoutWindowDecoration enables window decorations. -func WithoutWindowDecoration() WindowOption { - return func(window *Window) { - ebiten.SetWindowDecorated(false) - } -} - // NewGame creates a new terminal game with the given dimensions and font faces. func NewGame(width int, height int, fonts Fonts, tty io.Reader, adapter InputAdapter, defaultBg color.Color) (*Window, error) { if defaultBg == nil {