Switching to Pixel for Grpahics (much faster!)
This commit is contained in:
parent
3ba02dfb02
commit
faa2a7cc96
|
@ -4,8 +4,13 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
|
"github.com/wcharczuk/go-chart"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"runtime/pprof"
|
||||||
"strconv"
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -177,7 +182,7 @@ func (b *Ball) Reset(env *core.Environment, t *core.Turtle) {
|
||||||
env.Occupy(t, 50, 50)
|
env.Occupy(t, 50, 50)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
ball := new(Ball)
|
ball := new(Ball)
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
|
@ -198,18 +203,15 @@ func main() {
|
||||||
experiment.InitPheromone("2", color.RGBA{0x00, 0x00, 0xFF, 0x00})
|
experiment.InitPheromone("2", color.RGBA{0x00, 0x00, 0xFF, 0x00})
|
||||||
experiment.InitPheromone("ball", color.RGBA{0xff, 0xff, 0xff, 0xff})
|
experiment.InitPheromone("ball", color.RGBA{0xff, 0xff, 0xff, 0xff})
|
||||||
|
|
||||||
// x := []float64{-2}
|
x := []float64{-2}
|
||||||
// team1 := []float64{0}
|
team1 := []float64{0}
|
||||||
// team2 := []float64{1}
|
team2 := []float64{1}
|
||||||
|
|
||||||
// var graph chart.Chart
|
var graph chart.Chart
|
||||||
/**experiment.AddPlot("Goals Scored", func(environment *core.Environment, turtles []*core.Turtle) *chart.Chart {
|
experiment.AddPlot("Goals Scored", func(environment *core.Environment, turtles []*core.Turtle) *chart.Chart {
|
||||||
x = append(x, float64(environment.Step))
|
x = append(x, float64(environment.Step))
|
||||||
team1 = append(team1, float64(ball.Team1Score))
|
team1 = append(team1, float64(ball.Team1Score))
|
||||||
team2 = append(team2, float64(ball.Team2Score))
|
team2 = append(team2, float64(ball.Team2Score))
|
||||||
|
|
||||||
if environment.Step % 10 == 0 {
|
|
||||||
|
|
||||||
graph = chart.Chart{
|
graph = chart.Chart{
|
||||||
Background: chart.Style{
|
Background: chart.Style{
|
||||||
Padding: chart.Box{
|
Padding: chart.Box{
|
||||||
|
@ -218,10 +220,10 @@ func main() {
|
||||||
},
|
},
|
||||||
XAxis: chart.XAxis{Name: "Time Step", NameStyle: chart.Style{Show: true}, Style: chart.Style{Show: true, TextRotationDegrees: 90}, ValueFormatter: func(v interface{}) string {
|
XAxis: chart.XAxis{Name: "Time Step", NameStyle: chart.Style{Show: true}, Style: chart.Style{Show: true, TextRotationDegrees: 90}, ValueFormatter: func(v interface{}) string {
|
||||||
return fmt.Sprintf("%d", int(v.(float64)))
|
return fmt.Sprintf("%d", int(v.(float64)))
|
||||||
},},
|
}},
|
||||||
YAxis: chart.YAxis{Name: "Goals Scored", NameStyle: chart.Style{Show: true}, Style: chart.Style{Show: true}, ValueFormatter: func(v interface{}) string {
|
YAxis: chart.YAxis{Name: "Goals Scored", NameStyle: chart.Style{Show: true}, Style: chart.Style{Show: true}, ValueFormatter: func(v interface{}) string {
|
||||||
return fmt.Sprintf("%d", int(v.(float64)))
|
return fmt.Sprintf("%d", int(v.(float64)))
|
||||||
},},
|
}},
|
||||||
Series: []chart.Series{
|
Series: []chart.Series{
|
||||||
chart.ContinuousSeries{
|
chart.ContinuousSeries{
|
||||||
XValues: x,
|
XValues: x,
|
||||||
|
@ -233,9 +235,8 @@ func main() {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return &graph
|
return &graph
|
||||||
})*/
|
})
|
||||||
|
|
||||||
gamelengths := []int{}
|
gamelengths := []int{}
|
||||||
gamewins := []int{}
|
gamewins := []int{}
|
||||||
|
@ -346,9 +347,23 @@ func main() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, os.Interrupt)
|
||||||
|
go func() {
|
||||||
|
for sig := range c {
|
||||||
|
fmt.Printf("Got Signal %v", sig)
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
||||||
func GetTeams(turtles []*core.Turtle) (redTeam, blueTeam []*core.Turtle) {
|
func GetTeams(turtles []*core.Turtle) (redTeam, blueTeam []*core.Turtle) {
|
||||||
for _, turtle := range turtles {
|
for _, turtle := range turtles {
|
||||||
if turtle.GetAttribute("team") == "1" {
|
if turtle.GetAttribute("team") == "1" {
|
||||||
|
|
|
@ -94,6 +94,7 @@ func (e *Experiment) InitTurtles(f func() core.Actor) {
|
||||||
func (e *Experiment) AddPlot(title string, plotfunc func(environment *core.Environment, turtles []*core.Turtle) *chart.Chart) {
|
func (e *Experiment) AddPlot(title string, plotfunc func(environment *core.Environment, turtles []*core.Turtle) *chart.Chart) {
|
||||||
plot := graphics.NewPlot(title, int32(*width), int32(*height), int32(*pxsize))
|
plot := graphics.NewPlot(title, int32(*width), int32(*height), int32(*pxsize))
|
||||||
plot.GeneratePlot = plotfunc
|
plot.GeneratePlot = plotfunc
|
||||||
|
go plot.RenderAsync()
|
||||||
e.plots = append(e.plots, plot)
|
e.plots = append(e.plots, plot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -45,7 +46,7 @@ func (sm *Bird) Run(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitTurtles(func() core.Actor {
|
experiment.InitTurtles(func() core.Actor {
|
||||||
|
@ -59,3 +60,7 @@ func main() {
|
||||||
}
|
}
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
@ -46,7 +47,7 @@ func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
n := 0
|
n := 0
|
||||||
|
@ -62,3 +63,7 @@ func main() {
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -70,7 +71,7 @@ func (sm *Isolate) Run(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
honestTurtles := experiment.GetNumTurtles() - (*byzantineTurtles)
|
honestTurtles := experiment.GetNumTurtles() - (*byzantineTurtles)
|
||||||
|
@ -89,3 +90,7 @@ func main() {
|
||||||
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
|
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"github.com/foolusion/quadtree"
|
"github.com/foolusion/quadtree"
|
||||||
"github.com/wcharczuk/go-chart"
|
"github.com/wcharczuk/go-chart"
|
||||||
"image/color"
|
"image/color"
|
||||||
|
@ -67,7 +68,7 @@ func (sm *LightningBug) Run(env *core.Environment, t *core.Turtle) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
num := 0
|
num := 0
|
||||||
|
@ -135,3 +136,7 @@ func main() {
|
||||||
experiment.InitPheromone("light", color.RGBA{0xff, 0xff, 0x00, 0x00})
|
experiment.InitPheromone("light", color.RGBA{0xff, 0xff, 0x00, 0x00})
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +26,7 @@ func (sm *MazeGeneration) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Drop(env, 1, "trail")
|
t.Drop(env, 1, "trail")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitTurtles(func() core.Actor {
|
experiment.InitTurtles(func() core.Actor {
|
||||||
|
@ -36,3 +37,7 @@ func main() {
|
||||||
experiment.InitPheromone("trail", color.RGBA{0x81, 0x00, 0x81, 0x00})
|
experiment.InitPheromone("trail", color.RGBA{0x81, 0x00, 0x81, 0x00})
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"github.com/wcharczuk/go-chart"
|
"github.com/wcharczuk/go-chart"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
|
@ -92,7 +93,7 @@ func (sm *Prey) Run(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitEnvironment(func(env *core.Environment) {
|
experiment.InitEnvironment(func(env *core.Environment) {
|
||||||
|
@ -201,3 +202,7 @@ func main() {
|
||||||
}
|
}
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -34,7 +35,7 @@ func (sm *SlimeMold) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Drop(env, 1, "trail")
|
t.Drop(env, 1, "trail")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitTurtles(func() core.Actor {
|
experiment.InitTurtles(func() core.Actor {
|
||||||
|
@ -46,3 +47,7 @@ func main() {
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,10 +4,11 @@ import (
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
"git.openprivacy.ca/sarah/microworlds/models"
|
"git.openprivacy.ca/sarah/microworlds/models"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
|
|
||||||
|
@ -51,3 +52,7 @@ func main() {
|
||||||
}
|
}
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -48,7 +49,7 @@ func (sm *Slush) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Step(env)
|
t.Step(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitTurtles(NewSlushTurtle)
|
experiment.InitTurtles(NewSlushTurtle)
|
||||||
|
@ -56,3 +57,7 @@ func main() {
|
||||||
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
|
experiment.InitPheromone("2", color.RGBA{0xFF, 0x00, 0xFF, 0x00})
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
@ -130,7 +131,7 @@ func (sm *Snowball) Run(env *core.Environment, t *core.Turtle) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitNTurtles(func() core.Actor {
|
experiment.InitNTurtles(func() core.Actor {
|
||||||
|
@ -191,3 +192,7 @@ func main() {
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
"git.openprivacy.ca/sarah/microworlds/models"
|
"git.openprivacy.ca/sarah/microworlds/models"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
)
|
)
|
||||||
|
@ -284,9 +285,9 @@ var utrap = [][]int{
|
||||||
{W, W, W, W, W, W, W, W, W, W, W, W, W, W, W},
|
{W, W, W, W, W, W, W, W, W, W, W, W, W, W, W},
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
|
|
||||||
maze = maze0
|
//maze = maze0
|
||||||
|
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
|
@ -361,3 +362,7 @@ func main() {
|
||||||
}
|
}
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -60,7 +61,7 @@ func (sm *Turtle) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Drop(env, 1, "turtle-scent")
|
t.Drop(env, 1, "turtle-scent")
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
num := -1
|
num := -1
|
||||||
|
@ -83,3 +84,7 @@ func main() {
|
||||||
|
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"git.openprivacy.ca/sarah/microworlds/experiments"
|
"git.openprivacy.ca/sarah/microworlds/experiments"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"image/color"
|
"image/color"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
)
|
)
|
||||||
|
@ -17,6 +18,7 @@ type WoodChips struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *WoodChips) Setup(env *core.Environment, t *core.Turtle) {
|
func (a *WoodChips) Setup(env *core.Environment, t *core.Turtle) {
|
||||||
|
t.SetColor(color.RGBA{0x00, 0xFF, 0xFF, 0x88})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *WoodChips) Run(env *core.Environment, t *core.Turtle) {
|
func (a *WoodChips) Run(env *core.Environment, t *core.Turtle) {
|
||||||
|
@ -43,7 +45,7 @@ func (a *WoodChips) Run(env *core.Environment, t *core.Turtle) {
|
||||||
t.Step(env)
|
t.Step(env)
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func mainrun() {
|
||||||
experiment := new(experiments.Experiment)
|
experiment := new(experiments.Experiment)
|
||||||
experiment.InitializeExperiment()
|
experiment.InitializeExperiment()
|
||||||
experiment.InitEnvironment(func(environment *core.Environment) {
|
experiment.InitEnvironment(func(environment *core.Environment) {
|
||||||
|
@ -65,3 +67,7 @@ func main() {
|
||||||
experiment.InitPheromone("trail", color.RGBA{0x80, 0xFF, 0x00, 0x00})
|
experiment.InitPheromone("trail", color.RGBA{0x80, 0xFF, 0x00, 0x00})
|
||||||
experiment.Run()
|
experiment.Run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
pixelgl.Run(mainrun)
|
||||||
|
}
|
||||||
|
|
7
go.mod
7
go.mod
|
@ -5,9 +5,16 @@ go 1.13
|
||||||
require (
|
require (
|
||||||
github.com/BurntSushi/toml v0.3.1 // indirect
|
github.com/BurntSushi/toml v0.3.1 // indirect
|
||||||
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4
|
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4
|
||||||
|
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 // indirect
|
||||||
|
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 // indirect
|
||||||
|
github.com/faiface/pixel v0.8.0
|
||||||
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be
|
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be
|
||||||
|
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 // indirect
|
||||||
|
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 // indirect
|
||||||
|
github.com/go-gl/mathgl v0.0.0-20190713194549-592312d8590a // indirect
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
|
||||||
github.com/nickng/bibtex v1.0.1 // indirect
|
github.com/nickng/bibtex v1.0.1 // indirect
|
||||||
|
github.com/pkg/errors v0.8.1 // indirect
|
||||||
github.com/veandco/go-sdl2 v0.3.3
|
github.com/veandco/go-sdl2 v0.3.3
|
||||||
github.com/wcharczuk/go-chart v2.0.1+incompatible
|
github.com/wcharczuk/go-chart v2.0.1+incompatible
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
|
||||||
|
|
15
go.sum
15
go.sum
|
@ -2,12 +2,26 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
|
||||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||||
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4 h1:ilm6YOZRb8ZA/ulKbNcL+AC/fbpkM3dRZ9kWXhBwx8U=
|
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4 h1:ilm6YOZRb8ZA/ulKbNcL+AC/fbpkM3dRZ9kWXhBwx8U=
|
||||||
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4/go.mod h1:K2M5YTG4dpIXvuERd53snM5THchZczdy+k9gH02Shww=
|
github.com/asim/quadtree v0.0.0-20190907063054-ae2e556e6bb4/go.mod h1:K2M5YTG4dpIXvuERd53snM5THchZczdy+k9gH02Shww=
|
||||||
|
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380 h1:FvZ0mIGh6b3kOITxUnxS3tLZMh7yEoHo75v3/AgUqg0=
|
||||||
|
github.com/faiface/glhf v0.0.0-20181018222622-82a6317ac380/go.mod h1:zqnPFFIuYFFxl7uH2gYByJwIVKG7fRqlqQCbzAnHs9g=
|
||||||
|
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3 h1:baVdMKlASEHrj19iqjARrPbaRisD7EuZEVJj6ZMLl1Q=
|
||||||
|
github.com/faiface/mainthread v0.0.0-20171120011319-8b78f0a41ae3/go.mod h1:VEPNJUlxl5KdWjDvz6Q1l+rJlxF2i6xqDeGuGAxa87M=
|
||||||
|
github.com/faiface/pixel v0.8.0 h1:phOHW6ixfMAKRamjnvhI6FFI2VRyPEq7+LmmkDGXB/4=
|
||||||
|
github.com/faiface/pixel v0.8.0/go.mod h1:CEUU/s9E82Kqp01Boj1O67KnBskqiLghANqvUJGgDAM=
|
||||||
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be h1:fLLJtjJ2bdBHXLPtRsDVdnVEr9zBmw9be/WF751O0Gg=
|
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be h1:fLLJtjJ2bdBHXLPtRsDVdnVEr9zBmw9be/WF751O0Gg=
|
||||||
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be/go.mod h1:Gk+3NkBZRly4/GnVRU95Wwv+JmmyoVGmdcEDwA5Wm1M=
|
github.com/foolusion/quadtree v0.0.0-20140826014210-88d124c993be/go.mod h1:Gk+3NkBZRly4/GnVRU95Wwv+JmmyoVGmdcEDwA5Wm1M=
|
||||||
|
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7 h1:SCYMcCJ89LjRGwEa0tRluNRiMjZHalQZrVrvTbPh+qw=
|
||||||
|
github.com/go-gl/gl v0.0.0-20190320180904-bf2b1f2f34d7/go.mod h1:482civXOzJJCPzJ4ZOX/pwvXBWSnzD4OKMdH4ClKGbk=
|
||||||
|
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1 h1:QbL/5oDUmRBzO9/Z7Seo6zf912W/a6Sr4Eu0G/3Jho0=
|
||||||
|
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
|
||||||
|
github.com/go-gl/mathgl v0.0.0-20190713194549-592312d8590a h1:yoAEv7yeWqfL/l9A/J5QOndXIJCldv+uuQB1DSNQbS0=
|
||||||
|
github.com/go-gl/mathgl v0.0.0-20190713194549-592312d8590a/go.mod h1:yhpkQzEiH9yPyxDUGzkmgScbaBVlhC06qodikEM0ZwQ=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||||
github.com/nickng/bibtex v1.0.1 h1:Uop3DVOdQdrTamXfxr65f9KyHrd4RhttXwHi1BY6Wk0=
|
github.com/nickng/bibtex v1.0.1 h1:Uop3DVOdQdrTamXfxr65f9KyHrd4RhttXwHi1BY6Wk0=
|
||||||
github.com/nickng/bibtex v1.0.1/go.mod h1:0qHZj8RRrLaGXyPoF9odM3M1EX1HnWiwACyR3wgGf8U=
|
github.com/nickng/bibtex v1.0.1/go.mod h1:0qHZj8RRrLaGXyPoF9odM3M1EX1HnWiwACyR3wgGf8U=
|
||||||
|
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
|
||||||
|
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
github.com/veandco/go-sdl2 v0.3.0 h1:IWYkHMp8V3v37NsKjszln8FFnX2+ab0538J371t+rss=
|
github.com/veandco/go-sdl2 v0.3.0 h1:IWYkHMp8V3v37NsKjszln8FFnX2+ab0538J371t+rss=
|
||||||
github.com/veandco/go-sdl2 v0.3.0/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
|
github.com/veandco/go-sdl2 v0.3.0/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
|
||||||
github.com/veandco/go-sdl2 v0.3.1 h1:WMQ72+BeCj2o/zOO/wsB4MMpqHr1Y67m6QNFxp053ug=
|
github.com/veandco/go-sdl2 v0.3.1 h1:WMQ72+BeCj2o/zOO/wsB4MMpqHr1Y67m6QNFxp053ug=
|
||||||
|
@ -16,6 +30,7 @@ github.com/veandco/go-sdl2 v0.3.3 h1:4/TirgB2MQ7oww3pM3Yfgf1YbChMlAQAmiCPe5koK0I
|
||||||
github.com/veandco/go-sdl2 v0.3.3/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
|
github.com/veandco/go-sdl2 v0.3.3/go.mod h1:FB+kTpX9YTE+urhYiClnRzpOXbiWgaU3+5F2AB78DPg=
|
||||||
github.com/wcharczuk/go-chart v2.0.1+incompatible h1:0pz39ZAycJFF7ju/1mepnk26RLVLBCWz1STcD3doU0A=
|
github.com/wcharczuk/go-chart v2.0.1+incompatible h1:0pz39ZAycJFF7ju/1mepnk26RLVLBCWz1STcD3doU0A=
|
||||||
github.com/wcharczuk/go-chart v2.0.1+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE=
|
github.com/wcharczuk/go-chart v2.0.1+incompatible/go.mod h1:PF5tmL4EIx/7Wf+hEkpCqYi5He4u90sw+0+6FhrryuE=
|
||||||
|
golang.org/x/image v0.0.0-20190321063152-3fc05d484e9f/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
|
||||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
package graphics
|
package graphics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/faiface/pixel"
|
||||||
|
"github.com/faiface/pixel/imdraw"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
|
"image"
|
||||||
|
"image/color"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
|
||||||
// "strconv"
|
// "strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Graphics struct {
|
type Graphics struct {
|
||||||
window *sdl.Window
|
window *pixelgl.Window
|
||||||
renderer *sdl.Renderer
|
|
||||||
width, height, pxsize int32
|
width, height, pxsize int32
|
||||||
|
imd *imdraw.IMDraw
|
||||||
t int
|
t int
|
||||||
colorMap map[string][4]uint8
|
colorMap map[string][4]uint8
|
||||||
}
|
}
|
||||||
|
@ -22,27 +24,31 @@ func NewGraphics(width, height, pxsize int32) *Graphics {
|
||||||
graphics.width = width
|
graphics.width = width
|
||||||
graphics.height = height
|
graphics.height = height
|
||||||
graphics.pxsize = pxsize
|
graphics.pxsize = pxsize
|
||||||
window, err := sdl.CreateWindow("Microworlds", sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
|
cfg := pixelgl.WindowConfig{
|
||||||
width*pxsize, height*pxsize, sdl.WINDOW_SHOWN)
|
Title: "Microworlds",
|
||||||
|
Bounds: pixel.R(0, 0, float64(width*pxsize), float64(height*pxsize)),
|
||||||
|
VSync: true,
|
||||||
|
Resizable: false,
|
||||||
|
}
|
||||||
|
|
||||||
|
win, err := pixelgl.NewWindow(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
graphics.window = window
|
graphics.imd = imdraw.New(nil)
|
||||||
|
graphics.window = win
|
||||||
surface, _ := window.GetSurface()
|
|
||||||
renderer, err := sdl.CreateSoftwareRenderer(surface)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
graphics.colorMap = make(map[string][4]uint8)
|
graphics.colorMap = make(map[string][4]uint8)
|
||||||
graphics.renderer = renderer
|
|
||||||
return graphics
|
return graphics
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graphics) DrawTileColor(x, y int32) {
|
func (g *Graphics) DrawTileColor(imd *imdraw.IMDraw, x, y int32, rgba color.RGBA) {
|
||||||
g.renderer.FillRect(&sdl.Rect{X: x * g.pxsize, Y: y * g.pxsize, W: g.pxsize, H: g.pxsize})
|
|
||||||
|
imd.Color = rgba
|
||||||
|
imd.Push(pixel.V(float64(x*g.pxsize), float64(y*g.pxsize)))
|
||||||
|
imd.Push(pixel.V(float64((x*g.pxsize)+g.pxsize), float64(y*g.pxsize)))
|
||||||
|
imd.Push(pixel.V(float64(x*g.pxsize), float64((y*g.pxsize)+g.pxsize)))
|
||||||
|
imd.Push(pixel.V(float64((x*g.pxsize)+g.pxsize), float64((y*g.pxsize)+g.pxsize)))
|
||||||
|
imd.Rectangle(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graphics) ColorPheromone(name string, color [4]uint8) {
|
func (g *Graphics) ColorPheromone(name string, color [4]uint8) {
|
||||||
|
@ -50,11 +56,14 @@ func (g *Graphics) ColorPheromone(name string, color [4]uint8) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
g.renderer.SetDrawColor(0x00, 0x00, 0x0, 0xff)
|
g.imd.Clear()
|
||||||
g.renderer.FillRect(&sdl.Rect{X: 0, Y: 0, W: g.width * g.pxsize, H: g.width * g.pxsize})
|
//g.window.Clear(color.RGBA{0,0,0,0})
|
||||||
|
pheromoneImage := image.NewRGBA(image.Rect(0, 0, int(g.width), int(g.height)))
|
||||||
for x := 0; x < int(g.width); x++ {
|
for x := 0; x < int(g.width); x++ {
|
||||||
for y := 0; y < int(g.height); y++ {
|
for y := 0; y < int(g.height); y++ {
|
||||||
|
if env.HasValue(x, y) {
|
||||||
|
g.DrawTileColor(g.imd, int32(x), int32(y), color.RGBA{255, 255, 255, uint8(255)})
|
||||||
|
} else {
|
||||||
scaledamountRedTotal := 0
|
scaledamountRedTotal := 0
|
||||||
scaledamountGreenTotal := 0
|
scaledamountGreenTotal := 0
|
||||||
scaledamountBlueTotal := 0
|
scaledamountBlueTotal := 0
|
||||||
|
@ -74,34 +83,28 @@ func (g *Graphics) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
scaledamountBlueTotal += int(scaledamountBlue)
|
scaledamountBlueTotal += int(scaledamountBlue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
pheromoneImage.SetRGBA(x, y, color.RGBA{uint8(scaledamountRedTotal / len(g.colorMap)), uint8(scaledamountGreenTotal / len(g.colorMap)), uint8(scaledamountBlueTotal / len(g.colorMap)), uint8(0xF0)})
|
||||||
g.renderer.SetDrawColor(uint8(scaledamountRedTotal/len(g.colorMap)), uint8(scaledamountGreenTotal/len(g.colorMap)), uint8(scaledamountBlueTotal/len(g.colorMap)), uint8(0xF0))
|
}
|
||||||
g.DrawTileColor(int32(x), int32(y))
|
}
|
||||||
|
|
||||||
if env.HasValue(x, y) {
|
|
||||||
g.renderer.SetDrawColor(255, 255, 255, uint8(255))
|
|
||||||
g.DrawTileColor(int32(x), int32(y))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g.renderer.SetDrawColor(0x4b, 0x35, 0x57, uint8(255))
|
pd := pixel.PictureDataFromImage(pheromoneImage)
|
||||||
if env.Get(x, y) != nil {
|
sprite := pixel.NewSprite(pd, pd.Bounds())
|
||||||
g.DrawTileColor(int32(x), int32(y))
|
mat := pixel.IM
|
||||||
}
|
|
||||||
}
|
mat = mat.Moved(g.window.Bounds().Center())
|
||||||
}
|
mat = mat.ScaledXY(pixel.V(float64(g.width*g.pxsize)/2, float64(g.height*g.pxsize)/2), pixel.V(float64(g.pxsize), float64(-g.pxsize)))
|
||||||
|
sprite.Draw(g.window, mat)
|
||||||
|
|
||||||
for _, t := range turtles {
|
for _, t := range turtles {
|
||||||
if t.GetAttribute("status") == "dead" {
|
if t.GetAttribute("status") == "dead" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
x, y := t.Pos()
|
x, y := t.Pos()
|
||||||
col := t.GetColor()
|
g.DrawTileColor(g.imd, int32(x), int32(y), t.GetColor())
|
||||||
g.renderer.SetDrawColor(col.R, col.G, col.B, col.A)
|
|
||||||
g.DrawTileColor(int32(x), int32(y))
|
|
||||||
}
|
}
|
||||||
|
g.imd.Draw(g.window)
|
||||||
g.renderer.Present()
|
g.window.Update()
|
||||||
g.window.UpdateSurface()
|
|
||||||
// surface, _ := g.window.GetSurface()
|
// surface, _ := g.window.GetSurface()
|
||||||
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
// surface.SaveBMP("./images/" + strconv.Itoa(g.t) + ".bmp")
|
||||||
g.t++
|
g.t++
|
||||||
|
|
|
@ -2,70 +2,83 @@ package graphics
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"git.openprivacy.ca/sarah/microworlds/core"
|
"git.openprivacy.ca/sarah/microworlds/core"
|
||||||
"github.com/veandco/go-sdl2/sdl"
|
"github.com/faiface/pixel"
|
||||||
|
"github.com/faiface/pixel/pixelgl"
|
||||||
"github.com/wcharczuk/go-chart"
|
"github.com/wcharczuk/go-chart"
|
||||||
"image"
|
"image"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Plot struct {
|
type Plot struct {
|
||||||
Graphics
|
|
||||||
GeneratePlot func(*core.Environment, []*core.Turtle) *chart.Chart
|
GeneratePlot func(*core.Environment, []*core.Turtle) *chart.Chart
|
||||||
|
async chan chart.Chart
|
||||||
|
width, height int32
|
||||||
|
window *pixelgl.Window
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlot(title string, width, height, pxsize int32) *Plot {
|
func NewPlot(title string, width, height, pxsize int32) *Plot {
|
||||||
graphics := new(Plot)
|
graphics := new(Plot)
|
||||||
graphics.width = width
|
graphics.width = width
|
||||||
graphics.height = height
|
graphics.height = height
|
||||||
window, err := sdl.CreateWindow(title, sdl.WINDOWPOS_UNDEFINED, sdl.WINDOWPOS_UNDEFINED,
|
cfg := pixelgl.WindowConfig{
|
||||||
width, height, sdl.WINDOW_SHOWN|sdl.WINDOW_RESIZABLE)
|
Title: title,
|
||||||
|
Bounds: pixel.R(0, 0, float64(width), float64(height)),
|
||||||
|
VSync: true,
|
||||||
|
Resizable: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
win, err := pixelgl.NewWindow(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
graphics.window = window
|
graphics.window = win
|
||||||
|
graphics.async = make(chan chart.Chart)
|
||||||
surface, _ := window.GetSurface()
|
|
||||||
renderer, err := sdl.CreateSoftwareRenderer(surface)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "Failed to create renderer: %s\n", err)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
graphics.colorMap = make(map[string][4]uint8)
|
|
||||||
graphics.renderer = renderer
|
|
||||||
return graphics
|
return graphics
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Plot) Render(env *core.Environment, turtles []*core.Turtle) {
|
func (p *Plot) RenderAsync() {
|
||||||
graph := p.GeneratePlot(env, turtles)
|
w := p.window.Bounds().W()
|
||||||
|
h := p.window.Bounds().H()
|
||||||
// graph.Elements = []chart.Renderable{chart.Legend(graph)}
|
for {
|
||||||
w, h := p.window.GetSize()
|
graph := <-p.async
|
||||||
surface, _ := p.window.GetSurface()
|
nw := p.window.Bounds().W()
|
||||||
renderer, _ := sdl.CreateSoftwareRenderer(surface)
|
nh := p.window.Bounds().H()
|
||||||
p.renderer = renderer
|
if nw != w || nh != h {
|
||||||
|
w = nw
|
||||||
|
h = nh
|
||||||
|
}
|
||||||
graph.Width = int(w)
|
graph.Width = int(w)
|
||||||
graph.Height = int(h)
|
graph.Height = int(h)
|
||||||
if env.Step > 2 {
|
|
||||||
buffer := bytes.NewBuffer([]byte{})
|
buffer := bytes.NewBuffer([]byte{})
|
||||||
err := graph.Render(chart.PNG, buffer)
|
err := graph.Render(chart.PNG, buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
graph, _, _ := image.Decode(buffer)
|
graphimg, _, _ := image.Decode(buffer)
|
||||||
|
pd := pixel.PictureDataFromImage(graphimg)
|
||||||
|
sprite := pixel.NewSprite(pd, pd.Bounds())
|
||||||
|
sprite.Draw(p.window, pixel.IM.Moved(p.window.Bounds().Center()))
|
||||||
|
|
||||||
for x := 0; x < int(w); x++ {
|
/**for x := 0; x < int(w); x++ {
|
||||||
for y := 0; y < int(h); y++ {
|
for y := 0; y < int(h); y++ {
|
||||||
col := graph.At(x, y)
|
col := graphimg.At(x, y)
|
||||||
r, g, b, a := col.RGBA()
|
r, g, b, a := col.RGBA()
|
||||||
|
if r != 0xff && g != 0xff && b != 0xff {
|
||||||
p.renderer.SetDrawColor(uint8(r), uint8(g), uint8(b), uint8(a))
|
p.renderer.SetDrawColor(uint8(r), uint8(g), uint8(b), uint8(a))
|
||||||
p.renderer.DrawPoint(int32(x), int32(y))
|
p.renderer.DrawPoint(int32(x), int32(y))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}*/
|
||||||
|
p.window.Update()
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Plot) Render(env *core.Environment, turtles []*core.Turtle) {
|
||||||
|
graph := p.GeneratePlot(env, turtles)
|
||||||
|
if env.Step > 2 {
|
||||||
|
p.async <- *graph
|
||||||
}
|
}
|
||||||
p.window.UpdateSurface()
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue