twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

commit 13385d7b8b91f4997be7e62f1060a54a126c217b
parent 2eca3ecd6dfb333f196e1cb0f8eb26cde0df1145
Author: bsandro <brian.drosan@gmail.com>
Date:   Sun,  9 May 2021 16:37:16 +0300

Listening to http requests to spawn an animated sprite.

Diffstat:
Aassets/chikaiKnife.png | 0
Aassets/chikaiNoPls.png | 0
Aassets/space.jpg | 0
Mebitest.go | 127++++++++++++++++++++++++++++++-------------------------------------------------
Minit.go | 8+++++---
5 files changed, 53 insertions(+), 82 deletions(-)

diff --git a/assets/chikaiKnife.png b/assets/chikaiKnife.png Binary files differ. diff --git a/assets/chikaiNoPls.png b/assets/chikaiNoPls.png Binary files differ. diff --git a/assets/space.jpg b/assets/space.jpg Binary files differ. diff --git a/ebitest.go b/ebitest.go @@ -1,6 +1,7 @@ package main import ( + "io" _ "bytes" "errors" "fmt" @@ -9,21 +10,23 @@ import ( _ "image" _ "image/jpeg" _ "image/png" + "image/color" "log" "math" "math/rand" "os" "strconv" "time" - //"net/http" - //_ "net/http/pprof" + "net/http" ) const S_FRAMERATE = 60 const S_SPEED = 200.0 +const S_TTL = 20 -var NumSprites int = 1 +var has_req bool = false +var NumSprites int = 1 var ScreenSize struct { width, height int } @@ -37,6 +40,7 @@ type Sprite struct { x, y float64 angle float64 rotation float64 + expires int64 } type Game struct { @@ -44,6 +48,8 @@ type Game struct { sprites []*Sprite } +var game *Game + func (g *Game) Update() error { if !g.inited { fmt.Println("(Game::update()) calling Game::init()") @@ -55,6 +61,19 @@ func (g *Game) Update() error { return errors.New("exit") } + if ebiten.IsKeyPressed(ebiten.KeySpace) || has_req { + g.sprites = append(g.sprites, makeRandomSprite()) + has_req = false + } + + sprites_new := []*Sprite{} + for _, sprite := range g.sprites { + if sprite.expires > time.Now().Unix() { + sprites_new = append(sprites_new, sprite) + } + } + + g.sprites = sprites_new for _, sprite := range g.sprites { err := sprite.Update() if err != nil { @@ -117,10 +136,10 @@ func (sprite *Sprite) Update() error { } func (g *Game) Draw(screen *ebiten.Image) { - defer g.ShowDebug(screen) + //defer g.ShowDebug(screen) // background - tileImageAlt(screen, Images["grass.jpg"]) + screen.Fill(color.RGBA{0xFF, 0, 0xFF, 0xFF}) for _, sprite := range g.sprites { //w, h := sprite.img.Size() @@ -128,6 +147,7 @@ func (g *Game) Draw(screen *ebiten.Image) { //sprite.opt.GeoM.Translate(-float64(w)/2.0, -float64(h)/2.0) //sprite.opt.GeoM.Rotate(sprite.rotation) sprite.opt.GeoM.Translate(sprite.x, sprite.y) + sprite.opt.ColorM.Translate(0, 0, 0, -0.0003) screen.DrawImage(sprite.img, &sprite.opt) } } @@ -146,99 +166,48 @@ func (g *Game) init() { rand.Seed(time.Now().UnixNano()) for i := 0; i < NumSprites; i++ { - x := float64(ScreenSize.width) / 2.0 - y := float64(ScreenSize.height) / 2.0 - angle := rand.Float64() * math.Pi * 2.0 - fmt.Printf("initial x, y, angle: %0.2f %0.2f %0.2f\n", x, y, angle) - g.sprites = append(g.sprites, makeSprite("BakaHa2.webp", x, y, angle)) + g.sprites = append(g.sprites, makeRandomSprite()) } } -func main() { - fmt.Println("ebiten subgame") +func makeRandomSprite() *Sprite { + x := float64(ScreenSize.width) / 2.0 + y := float64(ScreenSize.height) / 2.0 + angle := rand.Float64() * math.Pi * 2.0 + fmt.Printf("initial x, y, angle: %0.2f %0.2f %0.2f\n", x, y, angle) + return makeSprite("chikaiKnife.png", x, y, angle) +} +func main() { if len(os.Args) > 1 { NumSprites, _ = strconv.Atoi(os.Args[1]) } - game := &Game{} + game = &Game{} ebiten.SetWindowSize(ScreenSize.width, ScreenSize.height) - ebiten.SetWindowTitle("ebiten subgame") + ebiten.SetWindowTitle("Twitchapon Receiver Module") ebiten.SetMaxTPS(S_FRAMERATE) - //go func() { http.ListenAndServe("localhost:8080", nil) }() + http.HandleFunc("/", handleHttpRequest) + go func() { + http.ListenAndServe("localhost:2345", nil) + }() + ebiten.SetRunnableOnUnfocused(true) if err := ebiten.RunGame(game); err != nil { log.Fatal(err) } } -func tileImage(screen *ebiten.Image, img *ebiten.Image) { - width, height := float32(ScreenSize.width), float32(ScreenSize.height) - sx, sy := -width/2.0, -height/2.0 - var x, y float32 - vs := []ebiten.Vertex{ - { - DstX: x, - DstY: y, - SrcX: sx, - SrcY: sy, - ColorR: 1, - ColorG: 1, - ColorB: 1, - ColorA: 1, - }, - { - DstX: x + width, - DstY: y, - SrcX: sx + width, - SrcY: sy, - ColorR: 1, - ColorG: 1, - ColorB: 1, - ColorA: 1, - }, - { - DstX: x, - DstY: y + height, - SrcX: sx, - SrcY: sy + height, - ColorR: 1, - ColorG: 1, - ColorB: 1, - ColorA: 1, - }, - { - DstX: x + width, - DstY: y + height, - SrcX: sx + width, - SrcY: sy + height, - ColorR: 1, - ColorG: 1, - ColorB: 1, - ColorA: 1, - }, - } - op := &ebiten.DrawTrianglesOptions{} - op.Address = ebiten.AddressRepeat - screen.DrawTriangles(vs, []uint16{0, 1, 2, 1, 2, 3}, img, op) -} - -func tileImageAlt(screen *ebiten.Image, img *ebiten.Image) { - scrW, scrH := ScreenSize.width, ScreenSize.height - imgW, imgH := img.Size() - for x := 0; x < scrW; x += imgW { - for y := 0; y < scrH; y += imgH { - op := &ebiten.DrawImageOptions{} - op.GeoM.Translate(float64(x), float64(y)) - screen.DrawImage(img, op) - } - } -} - func makeSprite(name string, x float64, y float64, angle float64) *Sprite { - return &Sprite{Images[name], ebiten.DrawImageOptions{}, x, y, angle, 0.0 /* rotation */} + return &Sprite{Images[name], ebiten.DrawImageOptions{}, x, y, angle, 0.0 /* rotation */, time.Now().Unix() + S_TTL /* expires */} } func (g *Game) ShowDebug(screen *ebiten.Image) { msg := fmt.Sprintf("TPS: %0.2f\nFPS: %0.2f", ebiten.CurrentTPS(), ebiten.CurrentFPS()) ebitenutil.DebugPrint(screen, msg) } + +func handleHttpRequest(w http.ResponseWriter, req *http.Request) { + io.WriteString(w, "OK\n") + fmt.Println("http request ok") + has_req = true +} diff --git a/init.go b/init.go @@ -7,6 +7,7 @@ import ( _ "golang.org/x/image/webp" "image" _ "image/jpeg" + _ "image/png" "log" ) @@ -24,9 +25,10 @@ func init() { fmt.Printf("Loaded '%s' - %d bytes\n", fname, len(raw)) } - imageNames := []string{"BakaHa2.webp", "grass.jpg"} + //imageNames := []string{"BakaHa2.webp", "space.jpg"} - for _, name := range imageNames { + //for _, name := range imageNames { + for name, _ := range Assets { img, err := getImage(name) if err != nil { log.Fatal(err) @@ -36,5 +38,5 @@ func init() { } ScreenSize.width = 1280 - ScreenSize.height = 800 + ScreenSize.height = 720 }