Skip to content
This repository was archived by the owner on Aug 15, 2023. It is now read-only.

Commit c35112b

Browse files
authored
Add clear and glclear tools (#88)
1 parent 48c3b54 commit c35112b

File tree

13 files changed

+555
-120
lines changed

13 files changed

+555
-120
lines changed

examples/tools/clear/clear.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"github.com/jacekolszak/pixiq/colornames"
5+
"github.com/jacekolszak/pixiq/image"
6+
"github.com/jacekolszak/pixiq/keyboard"
7+
"github.com/jacekolszak/pixiq/loop"
8+
"github.com/jacekolszak/pixiq/opengl"
9+
"github.com/jacekolszak/pixiq/tools/clear"
10+
"github.com/jacekolszak/pixiq/tools/glclear"
11+
)
12+
13+
type clearTool interface {
14+
SetColor(image.Color)
15+
Clear(image.Selection)
16+
}
17+
18+
func main() {
19+
opengl.RunOrDie(func(gl *opengl.OpenGL) {
20+
window, err := gl.OpenWindow(10, 10, opengl.Zoom(30))
21+
if err != nil {
22+
panic(err)
23+
}
24+
context := gl.Context()
25+
tools := []clearTool{
26+
glclear.New(context.NewClearCommand()), // GPU one
27+
clear.New(), // CPU one
28+
}
29+
tools[0].SetColor(colornames.Cornflowerblue)
30+
tools[1].SetColor(colornames.Hotpink)
31+
currentTool := 0
32+
keys := keyboard.New(window)
33+
loop.Run(window, func(frame *loop.Frame) {
34+
screen := frame.Screen()
35+
var (
36+
leftEye = screen.Selection(2, 2).WithSize(2, 2)
37+
rightEye = screen.Selection(6, 2).WithSize(2, 2)
38+
nose = screen.Selection(4, 5).WithSize(2, 2)
39+
mouth = screen.Selection(2, 8).WithSize(6, 1)
40+
)
41+
tool := tools[currentTool]
42+
tool.Clear(leftEye)
43+
tool.Clear(rightEye)
44+
tool.Clear(nose)
45+
tool.Clear(mouth)
46+
keys.Update()
47+
if keys.JustReleased(keyboard.Space) {
48+
currentTool++
49+
currentTool = currentTool % len(tools)
50+
}
51+
})
52+
53+
})
54+
}

gl/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (c *AcceleratedCommand) Run(output image.AcceleratedImageSelection, selecti
199199
panic("output image created in a different OpenGL context than program")
200200
}
201201

202-
c.api.UseProgram(c.program.id)
202+
c.program.use()
203203
c.api.Enable(scissorTest)
204204
c.api.BindFramebuffer(framebuffer, img.frameBufferID)
205205
loc := output.Location

gl/context.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package gl
22

33
import (
44
"fmt"
5+
6+
"github.com/jacekolszak/pixiq/image"
57
)
68

79
// Context is an OpenGL context
@@ -275,3 +277,40 @@ func (p *Program) AcceleratedCommand(command Command) *AcceleratedCommand {
275277
func (p *Program) ID() uint32 {
276278
return p.id
277279
}
280+
281+
func (p *Program) use() {
282+
if p.program != nil {
283+
p.api.UseProgram(p.id)
284+
}
285+
}
286+
287+
// NewClearCommand returns a command clearing all pixels in image.Selection
288+
func (c *Context) NewClearCommand() *ClearCommand {
289+
nilProgram := &Program{
290+
program: nil,
291+
uniformLocations: map[string]int32{},
292+
attributes: map[int32]attribute{},
293+
api: c.api,
294+
allImages: c.allImages,
295+
}
296+
cmd := &ClearCommand{}
297+
cmd.AcceleratedCommand = nilProgram.AcceleratedCommand(cmd)
298+
return cmd
299+
}
300+
301+
// ClearCommand clears the image.Selection using given color. By default color
302+
// is transparent.
303+
type ClearCommand struct {
304+
*AcceleratedCommand
305+
color image.Color
306+
}
307+
308+
// SetColor sets color which will be used to clear all pixels in image.Selection
309+
func (c *ClearCommand) SetColor(color image.Color) {
310+
c.color = color
311+
}
312+
313+
// RunGL implements gl.Command
314+
func (c *ClearCommand) RunGL(renderer *Renderer, _ []image.AcceleratedImageSelection) {
315+
renderer.Clear(c.color)
316+
}

gl/image.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ func (c *Context) NewAcceleratedImage(width, height int) *AcceleratedImage {
5151
api: c.api,
5252
}
5353
c.allImages[img] = img
54+
clearWithTransparentColor := c.NewClearCommand()
55+
clearWithTransparentColor.Run(img.wholeSelection(), []image.AcceleratedImageSelection{})
5456
return img
5557
}
5658

@@ -63,6 +65,13 @@ type AcceleratedImage struct {
6365
api API
6466
}
6567

68+
func (i *AcceleratedImage) wholeSelection() image.AcceleratedImageSelection {
69+
return image.AcceleratedImageSelection{
70+
Location: image.AcceleratedImageLocation{Width: i.width, Height: i.height},
71+
Image: i,
72+
}
73+
}
74+
6675
// TextureID returns texture identifier (aka name)
6776
func (i *AcceleratedImage) TextureID() uint32 {
6877
return i.textureID

gl/integration/opengl/opengl_bench_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package opengl_test
33
import (
44
"testing"
55

6+
"github.com/stretchr/testify/require"
7+
68
"github.com/jacekolszak/pixiq/image"
79
"github.com/jacekolszak/pixiq/opengl"
810
)
911

1012
func BenchmarkAcceleratedImage_Upload(b *testing.B) {
1113
openGL, err := opengl.New(mainThreadLoop)
14+
require.NoError(b, err)
15+
defer openGL.Destroy()
1216
if err != nil {
1317
panic(err)
1418
}

0 commit comments

Comments
 (0)