Skip to content
This repository has been archived by the owner on May 31, 2024. It is now read-only.

Commit

Permalink
updating to newest easygif version.
Browse files Browse the repository at this point in the history
Adding a useful worker pool util.
  • Loading branch information
GaryBrownEEngr committed Dec 8, 2023
1 parent 42f707d commit ce0c752
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 10 deletions.
2 changes: 1 addition & 1 deletion examples/randomwalk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func main() {
func simStartFunc(sim models.Scratch) {
sim.AddCostume(sprite.DecodeCodedSprite(sprite.TurtleImage), "t")

// go scratch.CreateGif(sim, time.Millisecond*100, time.Millisecond*100, "./examples/randomwalk/randomwalk.gif", 100)
// go scratch.CreateGifDithered(sim, time.Millisecond*100, time.Millisecond*100, "./examples/randomwalk/randomwalk.gif", 100)

for {
go turtleRandomWalk(sim)
Expand Down
Binary file modified examples/randomwalk/randomwalk.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 38 additions & 6 deletions gifCreator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@ func TakeScreenshot(sim models.Scratch, outputPNGPath string) error {
return easygif.SaveImageToPNG(screenshot, outputPNGPath)
}

// Start this as a go routine to create a GIF of your creation.
func CreateGif(
func TakeScreenshotVideo(
sim models.Scratch,
delayBetweenScreenshots time.Duration,
delayBetweenGifFrames time.Duration,
outputGifFilePath string,
frameCount int,
) {
) []image.Image {
// Collect the images
fmt.Printf("GIF: %s: Collecting images\n", outputGifFilePath)
frames := make([]image.Image, 0, frameCount)
nextTime := time.Now()
for frameIndex := 0; frameIndex < frameCount; frameIndex++ {
Expand All @@ -35,6 +31,21 @@ func CreateGif(
time.Sleep(time.Until(nextTime))
}

return frames
}

// Start this as a go routine to create a GIF of your creation.
func CreateGif(
sim models.Scratch,
delayBetweenScreenshots time.Duration,
delayBetweenGifFrames time.Duration,
outputGifFilePath string,
frameCount int,
) {
// Collect the images
fmt.Printf("GIF: %s: Collecting images\n", outputGifFilePath)
frames := TakeScreenshotVideo(sim, delayBetweenScreenshots, frameCount)

fmt.Printf("GIF: %s: Processing images\n", outputGifFilePath)
err := easygif.EasyGifWrite(frames, delayBetweenGifFrames, outputGifFilePath)
if err != nil {
Expand All @@ -43,3 +54,24 @@ func CreateGif(

fmt.Printf("GIF: %s: Done\n", outputGifFilePath)
}

// Start this as a go routine to create a GIF of your creation.
func CreateGifDithered(
sim models.Scratch,
delayBetweenScreenshots time.Duration,
delayBetweenGifFrames time.Duration,
outputGifFilePath string,
frameCount int,
) {
// Collect the images
fmt.Printf("GIF: %s: Collecting images\n", outputGifFilePath)
frames := TakeScreenshotVideo(sim, delayBetweenScreenshots, frameCount)

fmt.Printf("GIF: %s: Processing images\n", outputGifFilePath)
err := easygif.EasyDitheredGifWrite(frames, delayBetweenGifFrames, outputGifFilePath)
if err != nil {
log.Printf("Error while running easygif.EasyGifWrite(): %v\n", err)
}

fmt.Printf("GIF: %s: Done\n", outputGifFilePath)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/GaryBrownEEngr/scratch
go 1.21

require (
github.com/GaryBrownEEngr/easygif v0.0.1
github.com/GaryBrownEEngr/easygif v0.0.2
github.com/fogleman/gg v1.3.0
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0
github.com/hajimehoshi/ebiten/v2 v2.6.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc h1:7D+Bh06CRPCJO3gr2F7h1sriovOZ8BMhca2Rg85c2nk=
github.com/BurntSushi/xgb v0.0.0-20210121224620-deaf085860bc/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/GaryBrownEEngr/easygif v0.0.1 h1:j/aAU57vnhX/G0DGzg80XJuuBMKuLANEUkOwCyJi3lk=
github.com/GaryBrownEEngr/easygif v0.0.1/go.mod h1:xze5zu1I1zGObpOTdMlD4cexqbn30FjLVSi0mh1gyqw=
github.com/GaryBrownEEngr/easygif v0.0.2 h1:YNXnjhO0oX7hkzHjJkk6a1Pz9UIfVL5EY3/uDhjBuXQ=
github.com/GaryBrownEEngr/easygif v0.0.2/go.mod h1:nrEhZ881KuIKgHjsytSitSiblTz3q7utZRJbHqWeKnE=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
49 changes: 49 additions & 0 deletions tools/workerpool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package tools

import "sync"

// Based on https://medium.com/code-chasm/go-concurrency-pattern-worker-pool-a437117025b1
// WorkerPool is a contract for Worker Pool implementation
type WorkerPool interface {
AddTask(task func())
WaitForCompletion()
}

type workerPool struct {
maxWorker int
queuedTaskC chan func()
wg *sync.WaitGroup
}

var _ WorkerPool = &workerPool{} // make the compiler check this struct implements the interface.

func NewWorkerPool(workerCount int) *workerPool {
ret := &workerPool{
maxWorker: workerCount,
queuedTaskC: make(chan func(), 100),
wg: &sync.WaitGroup{},
}

ret.wg.Add(ret.maxWorker)

for i := 0; i < ret.maxWorker; i++ {
go func() {
for task := range ret.queuedTaskC {
task()
}

ret.wg.Done()
}()
}

return ret
}

func (s *workerPool) AddTask(task func()) {
s.queuedTaskC <- task
}

func (s *workerPool) WaitForCompletion() {
close(s.queuedTaskC)
s.wg.Wait()
}

0 comments on commit ce0c752

Please sign in to comment.