normalized most of lines to <=100 chars

This commit is contained in:
faiface 2016-12-30 17:43:26 +01:00
parent fefa184652
commit 9096277914
8 changed files with 106 additions and 82 deletions

View File

@ -9,7 +9,8 @@ type NRGBA struct {
R, G, B, A float64 R, G, B, A float64
} }
// Add adds color d to color c component-wise and returns the result (the components are not clamped). // Add adds color d to color c component-wise and returns the result (the components are not
// clamped).
func (c NRGBA) Add(d NRGBA) NRGBA { func (c NRGBA) Add(d NRGBA) NRGBA {
return NRGBA{ return NRGBA{
R: c.R + d.R, R: c.R + d.R,
@ -19,7 +20,8 @@ func (c NRGBA) Add(d NRGBA) NRGBA {
} }
} }
// Sub subtracts color d from color c component-wise and returns the result (the components are not clamped). // Sub subtracts color d from color c component-wise and returns the result (the components
// are not clamped).
func (c NRGBA) Sub(d NRGBA) NRGBA { func (c NRGBA) Sub(d NRGBA) NRGBA {
return NRGBA{ return NRGBA{
R: c.R - d.R, R: c.R - d.R,
@ -39,7 +41,8 @@ func (c NRGBA) Mul(d NRGBA) NRGBA {
} }
} }
// Scaled multiplies each component of color c by scale and returns the result (the components are not clamped). // Scaled multiplies each component of color c by scale and returns the result (the components
// are not clamped).
func (c NRGBA) Scaled(scale float64) NRGBA { func (c NRGBA) Scaled(scale float64) NRGBA {
return NRGBA{ return NRGBA{
R: c.R * scale, R: c.R * scale,

View File

@ -6,10 +6,10 @@ import (
"math/cmplx" "math/cmplx"
) )
// Vec is a 2d vector type. It is unusually implemented as complex128 for convenience. Since Go does // Vec is a 2d vector type. It is unusually implemented as complex128 for convenience. Since
// not allow operator overloading, implementing vector as a struct leads to a bunch of methods for // Go does not allow operator overloading, implementing vector as a struct leads to a bunch of
// addition, subtraction and multiplication of vectors. With complex128, much of this functionality // methods for addition, subtraction and multiplication of vectors. With complex128, much of
// is given through operators. // this functionality is given through operators.
// //
// Create vectors with the V constructor: // Create vectors with the V constructor:
// //
@ -97,7 +97,8 @@ func (u Vec) Cross(v Vec) float64 {
return u.X()*v.Y() - v.X()*u.Y() return u.X()*v.Y() - v.X()*u.Y()
} }
// Rect is a 2d rectangle aligned with the axis of the coordinate system. It has a position and a size. // Rect is a 2d rectangle aligned with the axis of the coordinate system. It has a position
// and a size.
// //
// You can manipulate the position and the size using the usual vector operations. // You can manipulate the position and the size using the usual vector operations.
type Rect struct { type Rect struct {

View File

@ -11,8 +11,9 @@ import (
// Drawer is anything that can be drawn. It's by no means a drawer inside your table. // Drawer is anything that can be drawn. It's by no means a drawer inside your table.
// //
// Drawer consists of a single methods: Draw. Draw methods takes any number of Transform arguments. It applies these // Drawer consists of a single methods: Draw. Draw methods takes any number of Transform
// transforms in the reverse order and finally draws something transformed by these transforms. // arguments. It applies these transforms in the reverse order and finally draws something
// transformed by these transforms.
// //
// Example: // Example:
// //
@ -24,21 +25,23 @@ type Drawer interface {
Draw(t ...Transform) Draw(t ...Transform)
} }
// Group is used to effeciently handle a collection of objects with a common parent. Usually many objects share a parent, // Group is used to effeciently handle a collection of objects with a common parent. Usually many
// using a group can significantly increase performance in these cases. // objects share a parent, using a group can significantly increase performance in these cases.
// //
// To use a group, first, create a group and as it's parent use the common parent of the collection of objects: // To use a group, first, create a group and as it's parent use the common parent of the
// collection of objects:
// //
// group := pixel.NewGroup(commonParent) // group := pixel.NewGroup(commonParent)
// //
// Then, when creating the objects, use the group as their parent, instead of the original common parent, but, don't forget // Then, when creating the objects, use the group as their parent, instead of the original
// to put everything into a With block, like this: // common parent, but, don't forget to put everything into a With block, like this:
// //
// group.With(func() { // group.With(func() {
// object := newArbitratyObject(group, ...) // group is the parent of the object // object := newArbitratyObject(group, ...) // group is the parent of the object
// }) // })
// //
// When dealing with objects associated with a group, it's always necessary to wrap that into a With block: // When dealing with objects associated with a group, it's always necessary to wrap that into
// a With block:
// //
// group.With(func() { // group.With(func() {
// for _, obj := range objectsWithCommonParent { // for _, obj := range objectsWithCommonParent {
@ -74,10 +77,11 @@ func (g *Group) Do(sub func(pixelgl.Context)) {
// Shape is a general drawable shape constructed from vertices. // Shape is a general drawable shape constructed from vertices.
// //
// Vertices are specified in the vertex array of a shape. A shape can have a picture, a color (mask) and a static // Vertices are specified in the vertex array of a shape. A shape can have a picture, a color
// transform. // (mask) and a static transform.
// //
// Usually you use this type only indirectly throught other specific shapes (sprites, polygons, ...) embedding it. // Usually you use this type only indirectly throught other specific shapes (sprites, polygons,
// ...) embedding it.
type Shape struct { type Shape struct {
parent pixelgl.Doer parent pixelgl.Doer
picture *Picture picture *Picture
@ -155,10 +159,11 @@ func (s *Shape) Draw(t ...Transform) {
}) })
} }
// MultiShape is a shape composed of several other shapes. These shapes cannot be modifies after combined into a multishape. // MultiShape is a shape composed of several other shapes. These shapes cannot be modifies
// after combined into a multishape.
// //
// Using a multishape can greatly increase drawing performance. However, it's only usable when the relative transformations // Using a multishape can greatly increase drawing performance. However, it's only usable when
// of the shapes don't change (e.g. static blocks in a level). // the relative transformations of the shapes don't change (e.g. static blocks in a level).
// //
// All shapes in a multishape must share the same texture (or use no texture). // All shapes in a multishape must share the same texture (or use no texture).
type MultiShape struct { type MultiShape struct {
@ -234,18 +239,19 @@ func NewMultiShape(parent pixelgl.Doer, shapes ...*Shape) *MultiShape {
return &MultiShape{NewShape(parent, picture, color.White, Position(0), va)} return &MultiShape{NewShape(parent, picture, color.White, Position(0), va)}
} }
// Sprite is a picture that can be drawn on the screen. Optionally it can be color masked or tranformed. // Sprite is a picture that can be drawn on the screen. Optionally it can be color masked
// or tranformed.
// //
// Usually, you only transform objects when you're drawing them (by passing transforms to the Draw method). // Usually, you only transform objects when you're drawing them (by passing transforms to the
// With sprites however, it can be useful to also transform them "statically". For example, sprites are // Draw method). With sprites however, it can be useful to also transform them "statically". For
// anchor by their bottom-left corner by default. Setting a transform can change this anchored to the center, // example, sprites are anchor by their bottom-left corner by default. Setting a transform can
// or wherever you want. // change this anchored to the center, or wherever you want.
type Sprite struct { type Sprite struct {
*Shape *Shape
} }
// NewSprite creates a new sprite with the supplied picture. The sprite's size is the size of the supplied picture. // NewSprite creates a new sprite with the supplied picture. The sprite's size is the size of
// If you want to change the sprite's size, change it's transform. // the supplied picture. If you want to change the sprite's size, change it's transform.
func NewSprite(parent pixelgl.Doer, picture *Picture) *Sprite { func NewSprite(parent pixelgl.Doer, picture *Picture) *Sprite {
var va *pixelgl.VertexArray var va *pixelgl.VertexArray
@ -290,8 +296,8 @@ type LineColor struct {
width float64 width float64
} }
// NewLineColor creates a new line shape between points A and B filled with a single color. Parent is an object // NewLineColor creates a new line shape between points A and B filled with a single color. Parent
// that this shape belongs to, such as a window, or a graphics effect. // is an object that this shape belongs to, such as a window, or a graphics effect.
func NewLineColor(parent pixelgl.Doer, c color.Color, a, b Vec, width float64) *LineColor { func NewLineColor(parent pixelgl.Doer, c color.Color, a, b Vec, width float64) *LineColor {
var va *pixelgl.VertexArray var va *pixelgl.VertexArray
@ -371,8 +377,8 @@ type PolygonColor struct {
points []Vec points []Vec
} }
// NewPolygonColor creates a new polygon shape filled with a single color. Parent is an object that this shape belongs to, // NewPolygonColor creates a new polygon shape filled with a single color. Parent is an object
// such as a window, or a graphics effect. // that this shape belongs to, such as a window, or a graphics effect.
func NewPolygonColor(parent pixelgl.Doer, c color.Color, points ...Vec) *PolygonColor { func NewPolygonColor(parent pixelgl.Doer, c color.Color, points ...Vec) *PolygonColor {
var va *pixelgl.VertexArray var va *pixelgl.VertexArray
@ -436,9 +442,10 @@ type EllipseColor struct {
fill float64 fill float64
} }
// NewEllipseColor creates a new ellipse shape filled with a single color. Parent is an object that this shape belongs to, // NewEllipseColor creates a new ellipse shape filled with a single color. Parent is an object
// such as a window, or a graphics effect. Fill should be a number between 0 and 1 which specifies how much of the ellipse will // that this shape belongs to, such as a window, or a graphics effect. Fill should be a number
// be filled (from the outside). The value of 1 means that the whole ellipse is filled. The value of 0 means that none of the // between 0 and 1 which specifies how much of the ellipse will be filled (from the outside). The
// value of 1 means that the whole ellipse is filled. The value of 0 means that none of the
// ellipse is filled (which makes the ellipse invisible). // ellipse is filled (which makes the ellipse invisible).
func NewEllipseColor(parent pixelgl.Doer, c color.Color, radius Vec, fill float64) *EllipseColor { func NewEllipseColor(parent pixelgl.Doer, c color.Color, radius Vec, fill float64) *EllipseColor {
var va *pixelgl.VertexArray var va *pixelgl.VertexArray

View File

@ -224,6 +224,7 @@ func (w *Window) updateInput() {
glfw.PollEvents() glfw.PollEvents()
}) })
// cache current state to temp (so that if there are callbacks outside this function, everything works) // cache current state to temp (so that if there are callbacks outside this function,
// everything works)
w.tempInp = w.currInp w.tempInp = w.currInp
} }

View File

@ -10,8 +10,9 @@ import (
// Picture is a raster picture. It is usually used with sprites. // Picture is a raster picture. It is usually used with sprites.
// //
// A picture is created from an image.Image, that can be either loaded from a file, or generated. After the creation // A picture is created from an image.Image, that can be either loaded from a file, or
// a picture can be sliced (slicing creates a "sub-picture" from a picture) into smaller pictures. // generated. After the creation a picture can be sliced (slicing creates a "sub-picture"
// from a picture) into smaller pictures.
type Picture struct { type Picture struct {
texture *pixelgl.Texture texture *pixelgl.Texture
bounds Rect bounds Rect
@ -47,11 +48,11 @@ func (p *Picture) Texture() *pixelgl.Texture {
return p.texture return p.texture
} }
// Slice returns a picture within the supplied rectangle of the original picture. The original and the sliced picture // Slice returns a picture within the supplied rectangle of the original picture. The original
// share the same texture. // and the sliced picture share the same texture.
// //
// For example, suppose we have a 100x200 pixels picture. If we slice it with rectangle (50, 100, 50, 100), we get // For example, suppose we have a 100x200 pixels picture. If we slice it with rectangle (50,
// the upper-right quadrant of the original picture. // 100, 50, 100), we get the upper-right quadrant of the original picture.
func (p *Picture) Slice(slice Rect) *Picture { func (p *Picture) Slice(slice Rect) *Picture {
return &Picture{ return &Picture{
texture: p.texture, texture: p.texture,
@ -61,7 +62,8 @@ func (p *Picture) Slice(slice Rect) *Picture {
// Bounds returns the bounding rectangle of this picture relative to the most original picture. // Bounds returns the bounding rectangle of this picture relative to the most original picture.
// //
// If the original picture gets sliced with the return value of this method, this picture will be obtained. // If the original picture gets sliced with the return value of this method, this picture will
// be obtained.
func (p *Picture) Bounds() Rect { func (p *Picture) Bounds() Rect {
return p.bounds return p.bounds
} }

13
run.go
View File

@ -5,10 +5,12 @@ import (
"github.com/go-gl/glfw/v3.2/glfw" "github.com/go-gl/glfw/v3.2/glfw"
) )
// Run is essentialy the "main" function of Pixel. It exists mainly due to the technical limitations of OpenGL and operating systems, // Run is essentialy the "main" function of Pixel. It exists mainly due to the technical
// in short, all graphics and window manipulating calls must be done from the main thread. Run makes this possible. // limitations of OpenGL and operating systems, in short, all graphics and window manipulating
// calls must be done from the main thread. Run makes this possible.
// //
// Call this function from the main function of your application. This is necessary, so that Run runs on the main thread. // Call this function from the main function of your application. This is necessary, so that
// Run runs on the main thread.
// //
// func run() { // func run() {
// window := pixel.NewWindow(...) // window := pixel.NewWindow(...)
@ -21,8 +23,9 @@ import (
// pixel.Run(run) // pixel.Run(run)
// } // }
// //
// You can spawn any number of goroutines from you run function and interact with Pixel concurrently. // You can spawn any number of goroutines from you run function and interact with Pixel
// The only condition is that the Run function must be called from your main function. // concurrently. The only condition is that the Run function must be called from your main
// function.
func Run(run func()) { func Run(run func()) {
defer glfw.Terminate() defer glfw.Terminate()
pixelgl.Run(run) pixelgl.Run(run)

View File

@ -2,16 +2,16 @@ package pixel
import "github.com/go-gl/mathgl/mgl32" import "github.com/go-gl/mathgl/mgl32"
// Transform holds space transformation information. Concretely, a transformation is specified by position, // Transform holds space transformation information. Concretely, a transformation is specified
// anchor, scale and rotation. // by position, anchor, scale and rotation.
// //
// All points are first rotated around the anchor. Then they are multiplied by the scale. If the // All points are first rotated around the anchor. Then they are multiplied by the scale. If
// scale factor is 2, the object becomes 2x bigger. Finally, all points are moved, so that the original // the scale factor is 2, the object becomes 2x bigger. Finally, all points are moved, so that
// anchor is located precisely at the position. // the original anchor is located precisely at the position.
// //
// Create a Transform object with the Position function. This sets the position variable, which is the // Create a Transform object with the Position function. This sets the position variable,
// most important. Then use methods, like Scale and Rotate to change scale, rotation and achor. The order // which is the most important. Then use methods, like Scale and Rotate to change scale,
// in which you apply these methods is irrelevant. // rotation and achor. The order in which you apply these methods is irrelevant.
// //
// pixel.Position(pixel.V(100, 100)).Rotate(math.Pi / 3).Scale(1.5) // pixel.Position(pixel.V(100, 100)).Rotate(math.Pi / 3).Scale(1.5)
type Transform struct { type Transform struct {
@ -19,7 +19,8 @@ type Transform struct {
rot float64 rot float64
} }
// Position creates a Transformation object with specified position. Anchor is (0, 0), rotation is 0 and scale is 1. // Position creates a Transformation object with specified position. Anchor is (0, 0), rotation
// is 0 and scale is 1.
func Position(position Vec) Transform { func Position(position Vec) Transform {
return Transform{ return Transform{
pos: position, pos: position,
@ -73,17 +74,18 @@ func (t Transform) MoveAnchor(delta Vec) Transform {
// Scale scales the transform by the supplied factor. // Scale scales the transform by the supplied factor.
// //
// Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two
// to scaling once by 4. // times by 2 is equivalent to scaling once by 4.
func (t Transform) Scale(scale float64) Transform { func (t Transform) Scale(scale float64) Transform {
t.sca = t.sca.Scaled(scale) t.sca = t.sca.Scaled(scale)
return t return t
} }
// ScaleXY scales the transform by the supplied X and Y factor. Note, that scale is applied before rotation. // ScaleXY scales the transform by the supplied X and Y factor. Note, that scale is applied
// before rotation.
// //
// Note, that subsequent calls to this method accumulate the final scale factor. Scaling two times by 2 is equivalent // Note, that subsequent calls to this method accumulate the final scale factor. Scaling two
// to scaling once by 4. // times by 2 is equivalent to scaling once by 4.
func (t Transform) ScaleXY(scale Vec) Transform { func (t Transform) ScaleXY(scale Vec) Transform {
t.sca = V(t.sca.X()*scale.X(), t.sca.Y()*scale.Y()) t.sca = V(t.sca.X()*scale.X(), t.sca.Y()*scale.Y())
return t return t
@ -91,8 +93,8 @@ func (t Transform) ScaleXY(scale Vec) Transform {
// Rotate rotates the transform by the supplied angle in radians. // Rotate rotates the transform by the supplied angle in radians.
// //
// Note, that subsequent calls to this method accumulate the final rotation. Rotating two times by Pi/2 is // Note, that subsequent calls to this method accumulate the final rotation. Rotating two times
// equivalent to rotating once by Pi. // by Pi/2 is equivalent to rotating once by Pi.
func (t Transform) Rotate(angle float64) Transform { func (t Transform) Rotate(angle float64) Transform {
t.rot += angle t.rot += angle
return t return t
@ -134,8 +136,8 @@ func (t Transform) InvMat() mgl32.Mat3 {
return mat return mat
} }
// Camera is a convenience function, that returns a Transform that acts like a camera. // Camera is a convenience function, that returns a Transform that acts like a camera. Center is
// Center is the position in the world coordinates, that will be projected onto the center of the screen. // the position in the world coordinates, that will be projected onto the center of the screen.
// One unit in world coordinates will be projected onto zoom pixels. // One unit in world coordinates will be projected onto zoom pixels.
// //
// It is possible to apply additional rotations, scales and moves to the returned transform. // It is possible to apply additional rotations, scales and moves to the returned transform.

View File

@ -14,8 +14,8 @@ import (
) )
// WindowConfig is convenience structure for specifying all possible properties of a window. // WindowConfig is convenience structure for specifying all possible properties of a window.
// Properties are chosen in such a way, that you usually only need to set a few of them - defaults // Properties are chosen in such a way, that you usually only need to set a few of them -
// (zeros) should usually be sensible. // defaults (zeros) should usually be sensible.
// //
// Note that you always need to set the width and the height of a window. // Note that you always need to set the width and the height of a window.
type WindowConfig struct { type WindowConfig struct {
@ -28,7 +28,8 @@ type WindowConfig struct {
// Height of a window in pixels. // Height of a window in pixels.
Height float64 Height float64
// If set to nil, a window will be windowed. Otherwise it will be fullscreen on the specified monitor. // If set to nil, a window will be windowed. Otherwise it will be fullscreen on the
// specified monitor.
Fullscreen *Monitor Fullscreen *Monitor
// Whether a window is resizable. // Whether a window is resizable.
@ -46,11 +47,12 @@ type WindowConfig struct {
// Whether a window is maximized. // Whether a window is maximized.
Maximized bool Maximized bool
// VSync (vertical synchronization) synchronizes window's framerate with the framerate of the monitor. // VSync (vertical synchronization) synchronizes window's framerate with the framerate
// of the monitor.
VSync bool VSync bool
// Number of samples for multi-sample anti-aliasing (edge-smoothing). // Number of samples for multi-sample anti-aliasing (edge-smoothing). Usual values
// Usual values are 0, 2, 4, 8 (powers of 2 and not much more than this). // are 0, 2, 4, 8 (powers of 2 and not much more than this).
MSAASamples int MSAASamples int
} }
@ -183,8 +185,8 @@ func (w *Window) DefaultShader() *pixelgl.Shader {
// SetClosed sets the closed flag of a window. // SetClosed sets the closed flag of a window.
// //
// This is usefull when overriding the user's attempt to close a window, or just to close a window // This is usefull when overriding the user's attempt to close a window, or just to close a
// from within a program. // window from within a program.
func (w *Window) SetClosed(closed bool) { func (w *Window) SetClosed(closed bool) {
pixelgl.Do(func() { pixelgl.Do(func() {
w.window.SetShouldClose(closed) w.window.SetShouldClose(closed)
@ -207,8 +209,8 @@ func (w *Window) SetTitle(title string) {
}) })
} }
// SetSize resizes a window to the specified size in pixels. // SetSize resizes a window to the specified size in pixels. In case of a fullscreen window,
// In case of a fullscreen window, it changes the resolution of that window. // it changes the resolution of that window.
func (w *Window) SetSize(width, height float64) { func (w *Window) SetSize(width, height float64) {
pixelgl.Do(func() { pixelgl.Do(func() {
w.window.SetSize(int(width), int(height)) w.window.SetSize(int(width), int(height))
@ -239,10 +241,12 @@ func (w *Window) Hide() {
}) })
} }
// SetFullscreen sets a window fullscreen on a given monitor. If the monitor is nil, the window will be resored to windowed instead. // SetFullscreen sets a window fullscreen on a given monitor. If the monitor is nil, the window
// will be resored to windowed instead.
// //
// Note, that there is nothing about the resolution of the fullscreen window. The window is automatically set to the monitor's // Note, that there is nothing about the resolution of the fullscreen window. The window is
// resolution. If you want a different resolution, you need to set it manually with SetSize method. // automatically set to the monitor's resolution. If you want a different resolution, you need
// to set it manually with SetSize method.
func (w *Window) SetFullscreen(monitor *Monitor) { func (w *Window) SetFullscreen(monitor *Monitor) {
if w.Monitor() != monitor { if w.Monitor() != monitor {
if monitor == nil { if monitor == nil {
@ -281,7 +285,8 @@ func (w *Window) IsFullscreen() bool {
return w.Monitor() != nil return w.Monitor() != nil
} }
// Monitor returns a monitor a fullscreen window is on. If the window is not fullscreen, this function returns nil. // Monitor returns a monitor a fullscreen window is on. If the window is not fullscreen, this
// function returns nil.
func (w *Window) Monitor() *Monitor { func (w *Window) Monitor() *Monitor {
monitor := pixelgl.DoVal(func() interface{} { monitor := pixelgl.DoVal(func() interface{} {
return w.window.GetMonitor() return w.window.GetMonitor()