Added some documentation

This commit is contained in:
Sasha Koshka 2022-11-09 01:01:13 -05:00
parent 509c6f0bc6
commit 7a6be48cb8
3 changed files with 56 additions and 4 deletions

View File

@ -3,6 +3,7 @@ package stone
import "time" import "time"
import "image/color" import "image/color"
// Application represents an application.
type Application struct { type Application struct {
DamageBuffer DamageBuffer
@ -11,15 +12,22 @@ type Application struct {
config Config config Config
} }
// SetSize sets the application's buffer size. This may or may not change the
// size of the window, depending on the backend used.
func (application *Application) SetSize (width, height int) { func (application *Application) SetSize (width, height int) {
application.DamageBuffer.SetSize(width, height) application.DamageBuffer.SetSize(width, height)
} }
// SetTitle sets the application's title. If in a window, it will appear as the
// window's name.
func (application *Application) SetTitle (title string) { func (application *Application) SetTitle (title string) {
application.title = title application.title = title
application.backend.SetTitle(title) application.backend.SetTitle(title)
} }
// Run initializes the application, and then calls callback. Operations inside
// of callback are allowed to interact with the application. Depending on the
// backend used, this function may bind to the main thread.
func (application *Application) Run ( func (application *Application) Run (
callback func (application *Application), callback func (application *Application),
) ( ) (
@ -48,31 +56,41 @@ func (application *Application) Run (
return return
} }
// Await blocks until an event is recieved, or until the specified timeout has
// elapsed. If the timeout is zero, it will wait forever. This function returns
// true if the backend is still active, and false if it has closed.
func (application *Application) Await (timeout time.Duration) (keepRunning bool) { func (application *Application) Await (timeout time.Duration) (keepRunning bool) {
keepRunning = application.backend.Await(timeout) keepRunning = application.backend.Await(timeout)
return return
} }
// Poll updates the window and checks for new events. This function returns true
// if the backend is still active, and false if it has closed.
func (application *Application) Poll () (keepRunning bool) { func (application *Application) Poll () (keepRunning bool) {
keepRunning = application.backend.Poll() keepRunning = application.backend.Poll()
return return
} }
// Title returns the application's title.
func (application *Application) Title () (title string) { func (application *Application) Title () (title string) {
title = application.title title = application.title
return return
} }
// Config returns a pointer to the application's configuration.
func (application *Application) Config () (config *Config) { func (application *Application) Config () (config *Config) {
config = &application.config config = &application.config
return return
} }
// JustPressed returns true if the specified button is pressed, but was not
// pressed the last time events were checked.
func (application *Application) JustPressed (button Button) (pressed bool) { func (application *Application) JustPressed (button Button) (pressed bool) {
pressed = application.backend.JustPressed(button) pressed = application.backend.JustPressed(button)
return return
} }
// JustReleased returns true if the specified button
func (application *Application) JustReleased (button Button) (released bool) { func (application *Application) JustReleased (button Button) (released bool) {
released = application.backend.JustReleased(button) released = application.backend.JustReleased(button)
return return

View File

@ -9,6 +9,7 @@ import "github.com/faiface/pixel/pixelgl"
import "golang.org/x/image/font/basicfont" import "golang.org/x/image/font/basicfont"
import "git.tebibyte.media/sashakoshka/stone" import "git.tebibyte.media/sashakoshka/stone"
// Backend represents an instance of the pixel backend
type Backend struct { type Backend struct {
window *pixelgl.Window window *pixelgl.Window
boundsDirty bool boundsDirty bool
@ -32,9 +33,11 @@ type Backend struct {
} }
} }
// Run satisfies the Run method of the Backend interface. Due to the nature of
// pixel, this will forcibly bind to the main thread.
func (backend *Backend) Run (callback func (application *stone.Application)) { func (backend *Backend) Run (callback func (application *stone.Application)) {
// backend.showBounds = true // backend.showBounds = true
backend.showCellBounds = true // backend.showCellBounds = true
if backend.fontFace == nil { if backend.fontFace == nil {
backend.fontFace = basicfont.Face7x13 backend.fontFace = basicfont.Face7x13
@ -66,12 +69,14 @@ func (backend *Backend) Run (callback func (application *stone.Application)) {
Bounds: backend.calculateWindowSize(), Bounds: backend.calculateWindowSize(),
}) })
backend.Poll() backend.Poll()
// TODO: this should return the error and not panic
if err != nil { panic(err.Error()) } if err != nil { panic(err.Error()) }
callback(backend.application) callback(backend.application)
}) })
} }
// Await fulfills the Await method of the Backend interface.
func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) { func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) {
if backend.window == nil { if backend.window == nil {
panic("call to Backend.Await before window exists") panic("call to Backend.Await before window exists")
@ -84,6 +89,7 @@ func (backend *Backend) Await (timeout time.Duration) (keepRunning bool) {
return return
} }
// Poll fulfills the Poll method of the Backend interface.
func (backend *Backend) Poll () (keepRunning bool) { func (backend *Backend) Poll () (keepRunning bool) {
if backend.window == nil { if backend.window == nil {
panic("call to Backend.Poll before window exists") panic("call to Backend.Poll before window exists")
@ -96,42 +102,50 @@ func (backend *Backend) Poll () (keepRunning bool) {
return return
} }
// SetTitle fulfills the SetTitle method of the Backend interface.
func (backend *Backend) SetTitle (title string) { func (backend *Backend) SetTitle (title string) {
if backend.window != nil { if backend.window != nil {
backend.window.SetTitle(title) backend.window.SetTitle(title)
} }
} }
// JustPressed fulfills the JustPressed method of the Backend interface.
func (backend *Backend) JustPressed (button stone.Button) (pressed bool) { func (backend *Backend) JustPressed (button stone.Button) (pressed bool) {
pressed = backend.window.JustPressed(pixelgl.Button(button)) pressed = backend.window.JustPressed(pixelgl.Button(button))
return return
} }
// JustReleased fulfills the JustReleased method of the Backend interface.
func (backend *Backend) JustReleased (button stone.Button) (released bool) { func (backend *Backend) JustReleased (button stone.Button) (released bool) {
released = backend.window.JustReleased(pixelgl.Button(button)) released = backend.window.JustReleased(pixelgl.Button(button))
return return
} }
// Pressed fulfills the Pressed method of the Backend interface.
func (backend *Backend) Pressed (button stone.Button) (pressed bool) { func (backend *Backend) Pressed (button stone.Button) (pressed bool) {
pressed = backend.window.Pressed(pixelgl.Button(button)) pressed = backend.window.Pressed(pixelgl.Button(button))
return return
} }
// Repeated fulfills the Repeated method of the Backend interface.
func (backend *Backend) Repeated (button stone.Button) (repeated bool) { func (backend *Backend) Repeated (button stone.Button) (repeated bool) {
repeated = backend.window.Repeated(pixelgl.Button(button)) repeated = backend.window.Repeated(pixelgl.Button(button))
return return
} }
// Typed fulfills the Typed method of the Backend interface.
func (backend *Backend) Typed () (text string) { func (backend *Backend) Typed () (text string) {
text = backend.window.Typed() text = backend.window.Typed()
return return
} }
// Resized fulfills the Resized method of the Backend interface.
func (backend *Backend) Resized () (resized bool) { func (backend *Backend) Resized () (resized bool) {
resized = backend.boundsDirty resized = backend.boundsDirty
return return
} }
// MousePosition fulfills the MousePosition method of the Backend interface.
func (backend *Backend) MousePosition () (x, y int) { func (backend *Backend) MousePosition () (x, y int) {
vector := backend.window.MousePosition() vector := backend.window.MousePosition()
x = int ( x = int (
@ -145,6 +159,7 @@ func (backend *Backend) MousePosition () (x, y int) {
return return
} }
// draw renders updates to the screen.
func (backend *Backend) draw () { func (backend *Backend) draw () {
// didDrawing := false // didDrawing := false
width, height := backend.application.Size() width, height := backend.application.Size()
@ -231,6 +246,8 @@ func (backend *Backend) draw () {
backend.window.SwapBuffers() backend.window.SwapBuffers()
} }
// processEvents reacts to events recieved from pixel, resizing and
// recalculating things as need be.
func (backend *Backend) processEvents () { func (backend *Backend) processEvents () {
newBounds := backend.window.Bounds().Max newBounds := backend.window.Bounds().Max
backend.boundsDirty = backend.windowBounds != newBounds backend.boundsDirty = backend.windowBounds != newBounds
@ -260,6 +277,8 @@ func (backend *Backend) processEvents () {
} }
} }
// vectorAtPosition generates a pixel vector at the top left of the specified
// cell.
func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) { func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) {
vector = pixel.V ( vector = pixel.V (
float64 ( float64 (
@ -271,15 +290,22 @@ func (backend *Backend) vectorAtPosition (x, y int) (vector pixel.Vec) {
return return
} }
// calculateWindowSize calculates window bounds based on the internal buffer
// size.
func (backend *Backend) calculateWindowSize () (bounds pixel.Rect) { func (backend *Backend) calculateWindowSize () (bounds pixel.Rect) {
width, height := backend.application.Size() width, height := backend.application.Size()
bounds = pixel.R ( bounds = pixel.R (
0, 0, 0, 0,
float64(width * backend.metrics.cellWidth), float64 (
float64(height * backend.metrics.cellHeight)) width * backend.metrics.cellWidth +
backend.metrics.padding * 2),
float64 (
height * backend.metrics.cellHeight +
backend.metrics.padding * 2))
return return
} }
// factory instantiates a pixel backend.
func factory (application *stone.Application) (output stone.Backend, err error) { func factory (application *stone.Application) (output stone.Backend, err error) {
backend := &Backend { backend := &Backend {
application: application, application: application,
@ -289,6 +315,7 @@ func factory (application *stone.Application) (output stone.Backend, err error)
return return
} }
// init registers this backend when the program starts.
func init () { func init () {
stone.RegisterBackend(factory) stone.RegisterBackend(factory)
} }

View File

@ -2,6 +2,8 @@ package stone
import "image/color" import "image/color"
// Config stores configuration parameters. Backends only should honor parameters
// that they can support.
type Config struct { type Config struct {
colors [4]color.Color colors [4]color.Color
padding int padding int
@ -9,21 +11,26 @@ type Config struct {
fontName string fontName string
} }
// Color returns the color value at the specified index.
func (config *Config) Color (index Color) (value color.Color) { func (config *Config) Color (index Color) (value color.Color) {
value = config.colors[index] value = config.colors[index]
return return
} }
// Padding specifies how many cell's worth of padding should be on all sides of
// the buffer.
func (config *Config) Padding () (padding int) { func (config *Config) Padding () (padding int) {
padding = config.padding padding = config.padding
return return
} }
// FontSize specifies how big the font should be.
func (config *Config) FontSize () (fontSize int) { func (config *Config) FontSize () (fontSize int) {
fontSize = config.fontSize fontSize = config.fontSize
return return
} }
// FontName specifies the name of the font to use.
func (config *Config) FontName () (fontName string) { func (config *Config) FontName () (fontName string) {
fontName = config.fontName fontName = config.fontName
return return