XVisuals/main.go

134 lines
2.7 KiB
Go
Raw Permalink Normal View History

2024-10-22 18:34:39 +00:00
package main
import (
"image/color"
"image/png"
"log"
"math"
"math/rand"
"os"
"strconv"
"time"
"github.com/hajimehoshi/ebiten/v2"
)
type Game struct {
points []*Position
preScreens []*ColorArray
screen *ebiten.Image
pressCountEnter int
pressCountSpace int
}
func (g *Game) NewImage() {
image := NewColorArray()
for i := 0; i < 3; i++ {
x, y := rand.Intn(screenHeight), rand.Intn(screenWidth)
g.points[i] = &Position{x, y}
}
for x := 0; x < screenWidth; x++ {
for y := 0; y < screenHeight; y++ {
c := make([]uint16, 3)
for i := 0; i < 3; i++ {
p := g.points[i]
if p.X != x || p.Y != y {
v := math.Abs(p.Dist(&Position{x, y}))
c[i] = function.Func(v)
} else {
c[0] = 65535
c[1] = 65535
c[2] = 65535
break
}
}
image.Set(x, y, color.RGBA64{c[0], c[1], c[2], 255})
}
}
g.preScreens = append(g.preScreens, image)
}
func (gm *Game) Init() {
for i := 0; i < layerCount; i++ {
gm.NewImage()
}
for x := 0; x < screenWidth; x++ {
for y := 0; y < screenHeight; y++ {
var r, g, b int64 = 0, 0, 0
for i := 0; i < layerCount; i++ {
R, G, B, _ := gm.preScreens[i].At(x, y).RGBA()
r += int64(R)
g += int64(G)
b += int64(B)
}
r /= int64(layerCount)
g /= int64(layerCount)
b /= int64(layerCount)
gm.screen.Set(x, y, color.RGBA64{uint16(r), uint16(g), uint16(b), 65535})
}
}
}
func (g *Game) ReInit() {
g.points = make([]*Position, 3)
g.preScreens = make([]*ColorArray, 0)
g.screen = ebiten.NewImage(screenWidth, screenHeight)
g.Init()
}
func NewGame() *Game {
gm := &Game{
make([]*Position, 3),
make([]*ColorArray, 0),
ebiten.NewImage(screenWidth, screenHeight),
0,
0,
}
gm.Init()
return gm
}
func (g *Game) Update() error {
if ebiten.IsKeyPressed(ebiten.KeyEnter) && g.pressCountEnter == 0 {
file, err := os.Create(strconv.FormatInt(time.Now().Unix(), 10) + ".png")
if err != nil {
return err
}
defer file.Close()
err = png.Encode(file, g.screen)
if err != nil {
return err
}
g.pressCountEnter++
} else if !ebiten.IsKeyPressed(ebiten.KeyEnter) && g.pressCountEnter > 0 {
g.pressCountEnter = 0
}
if ebiten.IsKeyPressed(ebiten.KeySpace) && g.pressCountSpace == 0 {
g.ReInit()
g.pressCountSpace++
} else if !ebiten.IsKeyPressed(ebiten.KeySpace) && g.pressCountSpace > 0 {
g.pressCountSpace = 0
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
screen.DrawImage(g.screen, nil)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
g := NewGame()
ebiten.SetWindowSize(windowWidth, windowHeight)
ebiten.SetWindowTitle("XVisuals")
if err := ebiten.RunGame(g); err != nil {
log.Fatal(err)
}
}