Created
February 20, 2020 03:18
-
-
Save Noofbiz/f6170de02a7d90de5ada04b4e338ef7d to your computer and use it in GitHub Desktop.
Static streaming from an image in engo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"image" | |
"image/color" | |
"image/color/palette" | |
"math/rand" | |
"time" | |
"github.com/EngoEngine/ecs" | |
"github.com/EngoEngine/engo" | |
"github.com/EngoEngine/engo/common" | |
) | |
type DefaultScene struct{} | |
func (*DefaultScene) Preload() {} | |
func (scene *DefaultScene) Setup(u engo.Updater) { | |
w, _ := u.(*ecs.World) | |
common.SetBackground(color.RGBA{R: 0, G: 255, B: 0, A: 255}) | |
w.AddSystem(&common.RenderSystem{}) | |
w.AddSystem(&VideoSystem{}) | |
} | |
func (*DefaultScene) Type() string { return "GameWorld" } | |
type VideoSystem struct { | |
bg *background | |
elapsed float32 | |
} | |
type background struct { | |
ecs.BasicEntity | |
common.RenderComponent | |
common.SpaceComponent | |
} | |
func (s *VideoSystem) New(w *ecs.World) { | |
rand.Seed(time.Now().UnixNano()) | |
rect := image.Rect(0, 0, 1024, 640) | |
s.bg = &background{BasicEntity: ecs.NewBasic()} | |
s.bg.SpaceComponent = common.SpaceComponent{ | |
Position: engo.Point{X: 0, Y: 0}, | |
Width: 1024, | |
Height: 640, | |
} | |
img := image.NewNRGBA(rect) | |
for i := rect.Min.X; i < rect.Max.X; i++ { | |
for j := rect.Min.Y; j < rect.Max.Y; j++ { | |
img.Set(i, j, palette.Plan9[rand.Intn(len(palette.Plan9))]) | |
} | |
} | |
obj := common.NewImageObject(img) | |
s.bg.RenderComponent = common.RenderComponent{Drawable: common.NewTextureSingle(obj)} | |
for _, system := range w.Systems() { | |
switch sys := system.(type) { | |
case *common.RenderSystem: | |
sys.Add(&s.bg.BasicEntity, &s.bg.RenderComponent, &s.bg.SpaceComponent) | |
} | |
} | |
} | |
func (s *VideoSystem) Add() {} | |
func (s *VideoSystem) Remove(basic ecs.BasicEntity) {} | |
func (s *VideoSystem) Update(dt float32) { | |
rect := image.Rect(0, 0, 1024, 640) | |
img := image.NewNRGBA(rect) | |
for i := rect.Min.X; i < rect.Max.X; i++ { | |
for j := rect.Min.Y; j < rect.Max.Y; j++ { | |
img.Set(i, j, palette.Plan9[rand.Intn(len(palette.Plan9))]) | |
} | |
} | |
obj := common.NewImageObject(img) | |
tex := common.NewTextureSingle(obj) | |
s.bg.Drawable.Close() | |
s.bg.Drawable = tex | |
} | |
func main() { | |
opts := engo.RunOptions{ | |
Title: "Animation Demo", | |
Width: 1024, | |
Height: 640, | |
} | |
engo.Run(opts, &DefaultScene{}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment