mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
graphics: Add comments
This commit is contained in:
parent
0831fd2a0d
commit
b47e564762
@ -77,7 +77,7 @@ func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float
|
|||||||
q.appendVertices(vertices)
|
q.appendVertices(vertices)
|
||||||
if 0 < len(q.commands) {
|
if 0 < len(q.commands) {
|
||||||
if c, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok {
|
if c, ok := q.commands[len(q.commands)-1].(*drawImageCommand); ok {
|
||||||
if c.isMergeable(dst, src, clr, mode) {
|
if c.canMerge(dst, src, clr, mode) {
|
||||||
c.verticesNum += len(vertices)
|
c.verticesNum += len(vertices)
|
||||||
q.m.Unlock()
|
q.m.Unlock()
|
||||||
return
|
return
|
||||||
@ -95,6 +95,9 @@ func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float
|
|||||||
q.m.Unlock()
|
q.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enqueue enqueues a drawing command other than a draw-image command.
|
||||||
|
//
|
||||||
|
// For a draw-image commmand, use EnqueueDrawImageCommand.
|
||||||
func (q *commandQueue) Enqueue(command command) {
|
func (q *commandQueue) Enqueue(command command) {
|
||||||
q.m.Lock()
|
q.m.Lock()
|
||||||
q.commands = append(q.commands, command)
|
q.commands = append(q.commands, command)
|
||||||
@ -132,6 +135,7 @@ func (q *commandQueue) commandGroups() [][]command {
|
|||||||
return gs
|
return gs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Flush flushes the command queue.
|
||||||
func (q *commandQueue) Flush() error {
|
func (q *commandQueue) Flush() error {
|
||||||
q.m.Lock()
|
q.m.Lock()
|
||||||
defer q.m.Unlock()
|
defer q.m.Unlock()
|
||||||
@ -176,15 +180,18 @@ func (q *commandQueue) Flush() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FlushCommands flushes the command queue.
|
||||||
func FlushCommands() error {
|
func FlushCommands() error {
|
||||||
return theCommandQueue.Flush()
|
return theCommandQueue.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fillCommand represents a drawing command to fill an image with a solid color.
|
||||||
type fillCommand struct {
|
type fillCommand struct {
|
||||||
dst *Image
|
dst *Image
|
||||||
color color.RGBA
|
color color.RGBA
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes the fillCommand.
|
||||||
func (c *fillCommand) Exec(indexOffsetInBytes int) error {
|
func (c *fillCommand) Exec(indexOffsetInBytes int) error {
|
||||||
f, err := c.dst.createFramebufferIfNeeded()
|
f, err := c.dst.createFramebufferIfNeeded()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -208,6 +215,7 @@ func (c *fillCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// drawImageCommand represents a drawing command to draw an image on another image.
|
||||||
type drawImageCommand struct {
|
type drawImageCommand struct {
|
||||||
dst *Image
|
dst *Image
|
||||||
src *Image
|
src *Image
|
||||||
@ -216,10 +224,12 @@ type drawImageCommand struct {
|
|||||||
mode opengl.CompositeMode
|
mode opengl.CompositeMode
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// QuadVertexSizeInBytes returns the size in bytes of vertices for a quadrangle.
|
||||||
func QuadVertexSizeInBytes() int {
|
func QuadVertexSizeInBytes() int {
|
||||||
return 4 * theArrayBufferLayout.totalBytes()
|
return 4 * theArrayBufferLayout.totalBytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes the drawImageCommand.
|
||||||
func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
f, err := c.dst.createFramebufferIfNeeded()
|
f, err := c.dst.createFramebufferIfNeeded()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -252,6 +262,10 @@ func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// split splits the drawImageCommand c into two drawImageCommands.
|
||||||
|
//
|
||||||
|
// split is called when the number of vertices reaches of the maximum and
|
||||||
|
// a command is needed to be executed as another draw call.
|
||||||
func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
|
func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
|
||||||
c1 := *c
|
c1 := *c
|
||||||
c2 := *c
|
c2 := *c
|
||||||
@ -262,7 +276,9 @@ func (c *drawImageCommand) split(quadsNum int) [2]*drawImageCommand {
|
|||||||
return [2]*drawImageCommand{&c1, &c2}
|
return [2]*drawImageCommand{&c1, &c2}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *drawImageCommand) isMergeable(dst, src *Image, clr *affine.ColorM, mode opengl.CompositeMode) bool {
|
// canMerge returns a boolean value indicating whether the other drawImageCommand can be merged
|
||||||
|
// with the drawImageCommand c.
|
||||||
|
func (c *drawImageCommand) canMerge(dst, src *Image, clr *affine.ColorM, mode opengl.CompositeMode) bool {
|
||||||
if c.dst != dst {
|
if c.dst != dst {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
@ -278,15 +294,18 @@ func (c *drawImageCommand) isMergeable(dst, src *Image, clr *affine.ColorM, mode
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// quadsNum returns the number of quadrangles.
|
||||||
func (c *drawImageCommand) quadsNum() int {
|
func (c *drawImageCommand) quadsNum() int {
|
||||||
return c.verticesNum * opengl.Float.SizeInBytes() / QuadVertexSizeInBytes()
|
return c.verticesNum * opengl.Float.SizeInBytes() / QuadVertexSizeInBytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// replacePixelsCommand represents a command to replace pixels of an image.
|
||||||
type replacePixelsCommand struct {
|
type replacePixelsCommand struct {
|
||||||
dst *Image
|
dst *Image
|
||||||
pixels []uint8
|
pixels []uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes the replacePixelsCommand.
|
||||||
func (c *replacePixelsCommand) Exec(indexOffsetInBytes int) error {
|
func (c *replacePixelsCommand) Exec(indexOffsetInBytes int) error {
|
||||||
f, err := c.dst.createFramebufferIfNeeded()
|
f, err := c.dst.createFramebufferIfNeeded()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -313,10 +332,12 @@ func (c *replacePixelsCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// disposeCommand represents a command to dispose an image.
|
||||||
type disposeCommand struct {
|
type disposeCommand struct {
|
||||||
target *Image
|
target *Image
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes the disposeCommand.
|
||||||
func (c *disposeCommand) Exec(indexOffsetInBytes int) error {
|
func (c *disposeCommand) Exec(indexOffsetInBytes int) error {
|
||||||
if c.target.framebuffer != nil {
|
if c.target.framebuffer != nil {
|
||||||
opengl.GetContext().DeleteFramebuffer(c.target.framebuffer.native)
|
opengl.GetContext().DeleteFramebuffer(c.target.framebuffer.native)
|
||||||
@ -327,12 +348,14 @@ func (c *disposeCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newImageFromImageCommand represents a command to create an image from an image.RGBA.
|
||||||
type newImageFromImageCommand struct {
|
type newImageFromImageCommand struct {
|
||||||
result *Image
|
result *Image
|
||||||
img *image.RGBA
|
img *image.RGBA
|
||||||
filter opengl.Filter
|
filter opengl.Filter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes the newImageFromImageCommand.
|
||||||
func (c *newImageFromImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *newImageFromImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
origSize := c.img.Bounds().Size()
|
origSize := c.img.Bounds().Size()
|
||||||
if origSize.X < 1 {
|
if origSize.X < 1 {
|
||||||
@ -355,6 +378,7 @@ func (c *newImageFromImageCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newImageCommand represents a command to create an empty image with given width and height.
|
||||||
type newImageCommand struct {
|
type newImageCommand struct {
|
||||||
result *Image
|
result *Image
|
||||||
width int
|
width int
|
||||||
@ -362,6 +386,7 @@ type newImageCommand struct {
|
|||||||
filter opengl.Filter
|
filter opengl.Filter
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes a newImageCommand.
|
||||||
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
w := emath.NextPowerOf2Int(c.width)
|
w := emath.NextPowerOf2Int(c.width)
|
||||||
h := emath.NextPowerOf2Int(c.height)
|
h := emath.NextPowerOf2Int(c.height)
|
||||||
@ -381,6 +406,7 @@ func (c *newImageCommand) Exec(indexOffsetInBytes int) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// newScreenFramebufferImageCommand is a command to create a special image for the screen.
|
||||||
type newScreenFramebufferImageCommand struct {
|
type newScreenFramebufferImageCommand struct {
|
||||||
result *Image
|
result *Image
|
||||||
width int
|
width int
|
||||||
@ -389,6 +415,7 @@ type newScreenFramebufferImageCommand struct {
|
|||||||
offsetY float64
|
offsetY float64
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Exec executes a newScreenFramebufferImageCommand.
|
||||||
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
|
func (c *newScreenFramebufferImageCommand) Exec(indexOffsetInBytes int) error {
|
||||||
if c.width < 1 {
|
if c.width < 1 {
|
||||||
return errors.New("graphics: width must be equal or more than 1.")
|
return errors.New("graphics: width must be equal or more than 1.")
|
||||||
|
@ -63,8 +63,11 @@ type Image struct {
|
|||||||
filter opengl.Filter
|
filter opengl.Filter
|
||||||
|
|
||||||
// baseImage and baseColor are exclusive.
|
// baseImage and baseColor are exclusive.
|
||||||
basePixels []uint8
|
basePixels []uint8
|
||||||
baseColor color.RGBA
|
baseColor color.RGBA
|
||||||
|
|
||||||
|
// drawImageHistory is a set of draw-image commands.
|
||||||
|
// TODO: This should be merged with the similar command queue in package grpahics (#433).
|
||||||
drawImageHistory []*drawImageHistoryItem
|
drawImageHistory []*drawImageHistoryItem
|
||||||
|
|
||||||
// stale indicates whether the image needs to be synced with GPU as soon as possible.
|
// stale indicates whether the image needs to be synced with GPU as soon as possible.
|
||||||
|
Loading…
Reference in New Issue
Block a user