twitchapon-anim

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

commit 1d15db2531feede823467faaf2b86486ec079345
parent f51477cc80f70cf15c72401ec41a4e054607c0b8
Author: bsandro <brian.drosan@gmail.com>
Date:   Mon, 10 May 2021 22:54:22 +0300

Moved update/draw calls into TGifObject methods

Diffstat:
Mebitest.go | 41+++--------------------------------------
Mgif_object.go | 56+++++++++++++++++++++++++++++++++++++++++++++++++++++---
2 files changed, 56 insertions(+), 41 deletions(-)

diff --git a/ebitest.go b/ebitest.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/hajimehoshi/ebiten/v2" "github.com/hajimehoshi/ebiten/v2/ebitenutil" - "image" "image/color" _ "image/jpeg" _ "image/png" @@ -82,6 +81,8 @@ func (g *Game) Update() error { } } + _ = g.animation.Update() + return nil } @@ -153,43 +154,7 @@ func (g *Game) Draw(screen *ebiten.Image) { screen.DrawImage(sprite.img, &sprite.opt) } - // screen size - scr_w, scr_h := ScreenSize.width, ScreenSize.height - // reference image (canvas) size - img_w, img_h := g.animation.Anim.Config.Width, g.animation.Anim.Config.Height - // frame rectangle - var bounds image.Rectangle = g.animation.Anim.Image[g.animation.Frame].Bounds() - // frame image - src_img := ebiten.NewImageFromImage(g.animation.Anim.Image[g.animation.Frame]) - //src_w, src_h := src_img.Size() - img_opts := &ebiten.DrawImageOptions{} - // shifting by rectangle properties - img_opts.GeoM.Translate(float64(bounds.Min.X), float64(bounds.Min.Y)) - //img_opts.GeoM.Translate(-float64(src_w / 2), -float64(src_h / 2)) - //img_opts.GeoM.Translate(-float64(img_w / 2), -float64(img_h / 2)) - //img_opts.GeoM.Translate(float64(src_w / 2), float64(src_h / 2)) - //img_opts.GeoM.Translate(float64(scr_w / 2), float64(scr_h / 2)) - - // image from reference size - img := ebiten.NewImage(img_w, img_h) - //img.Fill(color.RGBA{0xF0, 0x10, 0x0F, 0xFF}) - - img.DrawImage(src_img, img_opts) - opts := &ebiten.DrawImageOptions{} - // center image - opts.GeoM.Translate(-float64(img_w/2), -float64(img_h/2)) - opts.GeoM.Translate(float64(scr_w/2), float64(scr_h/2)) - screen.DrawImage(img, opts) - delay := int64(g.animation.Anim.Delay[g.animation.Frame]) - cur_time := time.Now() - elapsed := cur_time.Sub(g.animation.FrameTime) - if elapsed.Milliseconds() > delay*10 { - g.animation.FrameTime = cur_time - g.animation.Frame += 1 - if g.animation.Frame >= len(g.animation.Anim.Image) { - g.animation.Frame = 0 - } - } + g.animation.Draw(screen) } func (g *Game) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) { diff --git a/gif_object.go b/gif_object.go @@ -1,15 +1,65 @@ package main import ( + "github.com/hajimehoshi/ebiten/v2" + "image" "image/gif" "os" "time" ) type TGifObject struct { - Anim *gif.GIF - Frame int - FrameTime time.Time + Anim *gif.GIF + FrameNum int + FrameTime time.Time + FrameImage *ebiten.Image +} + +func (obj *TGifObject) Update() error { + delay := int64(obj.Anim.Delay[obj.FrameNum]) + cur_time := time.Now() + elapsed := cur_time.Sub(obj.FrameTime) + + if obj.FrameImage != nil && elapsed.Milliseconds() < delay*10 { + return nil + } + + // frame rectangle + var bounds image.Rectangle = obj.Anim.Image[obj.FrameNum].Bounds() + // base picture size + img_w, img_h := obj.Anim.Config.Width, obj.Anim.Config.Height + // frame image + src_img := ebiten.NewImageFromImage(obj.Anim.Image[obj.FrameNum]) + //src_w, src_h := src_img.Size() + img_opts := &ebiten.DrawImageOptions{} + // shifting by rectangle properties + img_opts.GeoM.Translate(float64(bounds.Min.X), float64(bounds.Min.Y)) + + // image from reference size + obj.FrameImage = ebiten.NewImage(img_w, img_h) + //img.Fill(color.RGBA{0xF0, 0x10, 0x0F, 0xFF}) + + obj.FrameImage.DrawImage(src_img, img_opts) + obj.FrameTime = cur_time + obj.FrameNum += 1 + if obj.FrameNum >= len(obj.Anim.Image) { + obj.FrameNum = 0 + } + + return nil +} + +func (obj *TGifObject) Draw(screen *ebiten.Image) { + if obj.FrameImage != nil { + img_w, img_h := obj.Anim.Config.Width, obj.Anim.Config.Height + scr_w, scr_h := ScreenSize.width, ScreenSize.height + opts := &ebiten.DrawImageOptions{} + // center image + opts.GeoM.Translate(-float64(img_w/2), -float64(img_h/2)) + opts.GeoM.Translate(float64(scr_w/2), float64(scr_h/2)) + + screen.DrawImage(obj.FrameImage, opts) + } } func MakeGifObject(file_path string) (obj *TGifObject, err error) {