examples: Refactoring

This commit is contained in:
Hajime Hoshi 2018-01-29 23:18:10 +09:00
parent 19f7f44f1c
commit f773a33f2e
2 changed files with 14 additions and 5 deletions

View File

@ -91,6 +91,7 @@ func update(screen *ebiten.Image) error {
// The image is just too big. Adjust the scale.
op.GeoM.Scale(0.25, 0.25)
// Scale the image by the device ratio so that the rendering result can be same
// on various (diffrent-DPI) environments.
op.GeoM.Scale(scale, scale)

View File

@ -36,13 +36,26 @@ var (
gophersRenderTarget *ebiten.Image
)
func init() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
}
func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() {
return nil
}
// Shrink the image once.
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
gophersRenderTarget.DrawImage(gophersImage, op)
// Enlarge the shrunk image.
// The filter is the nearest filter, so the result will be mosaic.
op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio)
screen.DrawImage(gophersRenderTarget, op)
@ -50,11 +63,6 @@ func update(screen *ebiten.Image) error {
}
func main() {
var err error
gophersImage, _, err = ebitenutil.NewImageFromFile(ebitenutil.JoinStringsIntoFilePath("_resources", "images", "gophers.jpg"), ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
w, h := gophersImage.Size()
gophersRenderTarget, _ = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {