Loading resources from disk/embed if possible.

This commit is contained in:
Sander Schobers 2021-08-09 07:13:49 +02:00
parent 13b6a50a45
commit 0532534142
7 changed files with 49 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package main
import (
"image"
"image/color"
"math/rand"
"strconv"
@ -35,17 +36,27 @@ func newLevelControl(ctx ui.Context, level *tins2021.Level) *levelController {
"blocked": newCubeTexture(textures, tins2021.Purple),
"colored": newCubeTexture(textures, tins2021.Blue),
}
newAnimatedTexture(textures, "star", tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames))
newAnimatedTexture(textures, "heart", tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames))
newAnimatedTexture(ctx, "star", "resources/images/star.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateStar(5), tins2021.Yellow, tins2021.NewRotateAnimation(defaultAnimationFrames))
})
newAnimatedTexture(ctx, "heart", "resources/images/heart.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHeart(), tins2021.Red, tins2021.NewRotateAnimation(defaultAnimationFrames))
})
control.MonsterTextureNames = map[tins2021.MonsterType]string{
tins2021.MonsterTypeStraight: "straight-walking-monster",
tins2021.MonsterTypeRandom: "random-walking-monster",
tins2021.MonsterTypeChaser: "chasing-monster",
}
newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeStraight], tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeRandom], tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
newAnimatedTexture(textures, control.MonsterTextureNames[tins2021.MonsterTypeChaser], tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeStraight], "resources/images/monster-straight.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Green, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
})
newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeRandom], "resources/images/monster-random.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Blue, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
})
newAnimatedTexture(ctx, control.MonsterTextureNames[tins2021.MonsterTypeChaser], "resources/images/monster-chaser.png", func() image.Image {
return tins2021.AnimatePolygon(tins2021.CreateHexagon(), tins2021.Purple, tins2021.NewWobbleAnimation(defaultAnimationFrames, 30))
})
small, err := tins2021.NewBitmapFont(ctx.Renderer(), ctx.Fonts().Font("small"), tins2021.AllCharacters...)
if err != nil {

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@ -2,8 +2,9 @@ package main
import (
"image"
"image/png"
"os"
"opslag.de/schobers/geom"
"opslag.de/schobers/tins2021"
"opslag.de/schobers/zntg/ui"
)
@ -44,10 +45,39 @@ func (t cubeTexture) Scaled(textures *ui.Textures, scale float32) cubeTexture {
}
}
func newAnimatedTexture(textures *ui.Textures, name string, polygon geom.PolygonF, color string, animation tins2021.AnimationRenderer) tins2021.AnimatedTexture {
texture, err := textures.CreateTextureGo(name, tins2021.AnimatePolygon(polygon, color, animation), true)
func loadTextureImage(ctx ui.Context, resource string) image.Image {
in, err := ctx.Resources().OpenResource(resource)
if err != nil {
return nil
}
defer in.Close()
im, err := png.Decode(in)
if err != nil {
return nil
}
return im
}
func saveTextureImage(resource string, im image.Image) {
out, err := os.Create(resource)
if err != nil {
return
}
defer out.Close()
png.Encode(out, im)
}
func newAnimatedTexture(ctx ui.Context, name, resource string, render animatedTextureRenderFn) tins2021.AnimatedTexture {
raw := loadTextureImage(ctx, resource)
if raw == nil {
raw = render()
saveTextureImage(resource, raw)
}
texture, err := ctx.Textures().CreateTextureGo(name, raw, true)
if err != nil {
panic(err)
}
return tins2021.NewSquareAnimatedTexture(texture)
}
type animatedTextureRenderFn func() image.Image