Skip to content

Commit dac009e

Browse files
committed
added template config and sketch
1 parent 26fb2a9 commit dac009e

File tree

4 files changed

+103
-8
lines changed

4 files changed

+103
-8
lines changed

examples/noise/main.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
46
"log"
57

68
"github.com/aldernero/sketchy"
@@ -9,6 +11,8 @@ import (
911
"github.com/lucasb-eyer/go-colorful"
1012
)
1113

14+
var hasShownMinMax bool = false
15+
1216
func update(s *sketchy.Sketch) {
1317
s.Rand.SetSeed(int64(s.Var("seed")))
1418
s.Rand.SetNoiseOctaves(int(s.Var("octaves")))
@@ -22,28 +26,48 @@ func update(s *sketchy.Sketch) {
2226

2327
func draw(s *sketchy.Sketch, c *gg.Context) {
2428
cellSize := s.Var("cellSize")
29+
minNoise := 100.0
30+
maxNoise := -100.0
2531
for x := 0.0; x < s.SketchWidth; x += cellSize {
2632
for y := 0.0; y < s.SketchHeight; y += cellSize {
2733
noise := s.Rand.Noise2D(x, y)
28-
hue := sketchy.Map(0, 1, 0, 360, noise)
34+
if noise > maxNoise {
35+
maxNoise = noise
36+
}
37+
if noise < minNoise {
38+
minNoise = noise
39+
}
40+
hue := sketchy.Map(-1, 1, 0, 360, noise)
2941
cellColor := colorful.Hsl(hue, 0.5, 0.5)
3042
c.SetColor(cellColor)
3143
c.DrawRectangle(x, y, cellSize, cellSize)
3244
c.Fill()
3345
}
3446
}
47+
if !hasShownMinMax {
48+
fmt.Println(minNoise, maxNoise)
49+
hasShownMinMax = true
50+
}
3551
}
3652

3753
func main() {
38-
s, err := sketchy.NewSketchFromFile("config.json")
39-
s.Updater = update
40-
s.Drawer = draw
54+
var configFile string
55+
var prefix string
56+
var randomSeed int64
57+
flag.StringVar(&configFile, "c", "sketch.json", "Sketch config file")
58+
flag.StringVar(&configFile, "p", "sketch", "Output file prefix")
59+
flag.Int64Var(&randomSeed, "s", 0, "Random number generator seed")
60+
s, err := sketchy.NewSketchFromFile(configFile)
4161
if err != nil {
4262
log.Fatal(err)
4363
}
64+
s.Prefix = prefix
65+
s.RandomSeed = randomSeed
66+
s.Updater = update
67+
s.Drawer = draw
4468
s.Init()
4569
ebiten.SetWindowSize(int(s.ControlWidth+s.SketchWidth), int(s.SketchHeight))
46-
ebiten.SetWindowTitle(s.Title)
70+
ebiten.SetWindowTitle("Sketchy - " + s.Title)
4771
ebiten.SetWindowResizable(false)
4872
if err := ebiten.RunGame(s); err != nil {
4973
log.Fatal(err)

sketch.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"image/color"
7+
"io/ioutil"
78
"log"
89
"os"
910

@@ -13,7 +14,7 @@ import (
1314
)
1415

1516
const (
16-
DefaultTitle = "Sketchy Sketch"
17+
DefaultTitle = "Sketch"
1718
DefaultPrefix = "sketch"
1819
DefaultBackgroundColor = "#1e1e1e"
1920
DefaultOutlineColor = "#ffdb00"
@@ -32,6 +33,7 @@ type Sketch struct {
3233
ControlOutlineColor string `json:"ControlOutlineColor"`
3334
SketchBackgroundColor string `json:"SketchBackgroundColor"`
3435
SketchOutlineColor string `json:"SketchOutlineColor"`
36+
RandomSeed int64 `json:"RandomSeed"`
3537
Controls []Slider `json:"Controls"`
3638
Updater SketchUpdater `json:"-"`
3739
Drawer SketchDrawer `json:"-"`
@@ -64,7 +66,7 @@ func (s *Sketch) Init() {
6466
}
6567
s.buildMaps()
6668
s.parseColors()
67-
s.Rand = NewRng(0)
69+
s.Rand = NewRng(s.RandomSeed)
6870
ctx := gg.NewContext(int(s.ControlWidth), int(s.SketchHeight))
6971
s.PlaceControls(s.ControlWidth, s.SketchHeight, ctx)
7072
}
@@ -173,5 +175,11 @@ func (s *Sketch) parseColors() {
173175
}
174176

175177
func (s *Sketch) saveConfig() {
176-
178+
configJson, _ := json.MarshalIndent(s, "", " ")
179+
fname := s.Prefix + "_config_" + GetTimestampString() + ".json"
180+
err := ioutil.WriteFile(fname, configJson, 0644)
181+
if err != nil {
182+
fmt.Println(err)
183+
}
184+
fmt.Println("Saved config ", fname)
177185
}

template/sketch.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"log"
6+
7+
"github.com/aldernero/sketchy"
8+
"github.com/fogleman/gg"
9+
"github.com/hajimehoshi/ebiten/v2"
10+
)
11+
12+
func update(s *sketchy.Sketch) {
13+
// Update logic goes here
14+
}
15+
16+
func draw(s *sketchy.Sketch, c *gg.Context) {
17+
// Drawing code goes here
18+
}
19+
20+
func main() {
21+
var configFile string
22+
var prefix string
23+
var randomSeed int64
24+
flag.StringVar(&configFile, "c", "sketch.json", "Sketch config file")
25+
flag.StringVar(&configFile, "p", "sketch", "Output file prefix")
26+
flag.Int64Var(&randomSeed, "s", 0, "Random number generator seed")
27+
s, err := sketchy.NewSketchFromFile(configFile)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
s.Prefix = prefix
32+
s.RandomSeed = randomSeed
33+
s.Updater = update
34+
s.Drawer = draw
35+
s.Init()
36+
ebiten.SetWindowSize(int(s.ControlWidth+s.SketchWidth), int(s.SketchHeight))
37+
ebiten.SetWindowTitle("Sketchy - " + s.Title)
38+
ebiten.SetWindowResizable(false)
39+
if err := ebiten.RunGame(s); err != nil {
40+
log.Fatal(err)
41+
}
42+
}

template/sketch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"SketchWidth": 1080,
3+
"SketchHeight": 1080,
4+
"ControlWidth": 240,
5+
"Controls": [
6+
{
7+
"Name": "control1",
8+
"MinVal": 1,
9+
"MaxVal": 100,
10+
"Val": 10,
11+
"Incr": 1
12+
},
13+
{
14+
"Name": "control2",
15+
"MinVal": 0,
16+
"MaxVal": 2,
17+
"Val": 0.9,
18+
"Incr": 0.01
19+
}
20+
]
21+
}

0 commit comments

Comments
 (0)