Added some documentation

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

View File

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