Skip to content

Commit

Permalink
dispose the assets
Browse files Browse the repository at this point in the history
  • Loading branch information
merlinorg committed May 17, 2022
1 parent f2fd15e commit 62324b5
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 27 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ buildscript {
allprojects {
apply plugin: "eclipse"

version = '1.2'
version = '1.3'
ext {
appName = "run"
gdxVersion = '1.11.0'
Expand Down
31 changes: 20 additions & 11 deletions core/src/org/merlin/tertis/Tertis.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,21 @@ import com.badlogic.gdx.utils.ScreenUtils
import com.badlogic.gdx.{ApplicationAdapter, Gdx, Input}
import org.merlin.tertis.common.Starfield
import org.merlin.tertis.home.Home
import org.merlin.tertis.util.TextureWrapper
import org.merlin.tertis.util.{GarbageCan, TextureWrapper}

class Tertis extends ApplicationAdapter {
import Tertis.garbage

private var batch: PolygonSpriteBatch = _
private var scene: Scene = _

override def create(): Unit = {

Gdx.input.setCatchKey(Input.Keys.BACK, true)

Prefs.loadPreferences()

batch = new PolygonSpriteBatch()
batch = garbage.add(new PolygonSpriteBatch())

Tertis.logo = TextureWrapper.load("logo.png")
Tertis.play = TextureWrapper.load("play.png")
Expand All @@ -47,12 +49,12 @@ class Tertis extends ApplicationAdapter {
Tertis.metaKey =
TextureWrapper.load("meta-key.png") // linear filter doesn't help

Tertis.click = Gdx.audio.newSound(Gdx.files.internal("click.mp3"))
Tertis.drop = Gdx.audio.newSound(Gdx.files.internal("drop.mp3"))
Tertis.crash = Gdx.audio.newSound(Gdx.files.internal("crash.mp3"))
Tertis.end = Gdx.audio.newSound(Gdx.files.internal("triangle.mp3"))
Tertis.click = Tertis.loadSound("click.mp3")
Tertis.drop = Tertis.loadSound("drop.mp3")
Tertis.crash = Tertis.loadSound("crash.mp3")
Tertis.end = Tertis.loadSound("triangle.mp3")

// TODO: dispose of everything
Tertis.pixture = Tertis.solidTexture(1f, 1f, 1f, 1f)

Text.loadFonts()

Expand All @@ -71,7 +73,7 @@ class Tertis extends ApplicationAdapter {
}

override def dispose(): Unit = {
batch.dispose()
garbage.dispose()
}

private def setScene(newScene: Scene): Unit = {
Expand All @@ -82,6 +84,8 @@ class Tertis extends ApplicationAdapter {
}

object Tertis {
implicit val garbage: GarbageCan = new GarbageCan

var logo: TextureWrapper = _
var play: TextureWrapper = _

Expand All @@ -105,6 +109,8 @@ object Tertis {
var arrowKey: TextureWrapper = _
var metaKey: TextureWrapper = _

var pixture: Texture = _

var click: Sound = _
var drop: Sound = _
var crash: Sound = _
Expand All @@ -115,12 +121,15 @@ object Tertis {
private def isMobile(tpe: ApplicationType) =
tpe == ApplicationType.Android || tpe == ApplicationType.iOS

val pixture = solidTexture(1f, 1f, 1f, 1f)
private def loadSound(path: String)(implicit garbage: GarbageCan): Sound =
garbage.add(Gdx.audio.newSound(Gdx.files.internal(path)))

def solidTexture(r: Float, g: Float, b: Float, a: Float): Texture = {
private def solidTexture(r: Float, g: Float, b: Float, a: Float)(implicit
garbage: GarbageCan
): Texture = {
val pixel = new Pixmap(1, 1, Format.RGBA8888)
pixel.setColor(r, g, b, a)
pixel.fill()
new Texture(pixel)
garbage.add(new Texture(pixel))
}
}
18 changes: 10 additions & 8 deletions core/src/org/merlin/tertis/Text.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ package org.merlin.tertis
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator
import com.badlogic.gdx.graphics.g2d.{BitmapFont, GlyphLayout, PolygonSpriteBatch}
import com.badlogic.gdx.graphics.g2d.{
BitmapFont,
GlyphLayout,
PolygonSpriteBatch
}
import org.merlin.tertis.Geometry.Dimension
import org.merlin.tertis.home.Home
import org.merlin.tertis.util.GarbageCan

object Text {
def loadFonts(): Unit = {
def loadFonts()(implicit garbage: GarbageCan): Unit = {
val generator = new FreeTypeFontGenerator(
Gdx.files.internal("OpenSans-Regular.ttf")
)
val parameter = new FreeTypeFontGenerator.FreeTypeFontParameter
parameter.characters = FreeTypeFontGenerator.DEFAULT_CHARS + CharExtras
parameter.size = Dimension.toInt
bigFont = generator.generateFont(parameter)
parameter.size = (Dimension * 3 / 4).toInt
mediumFont = generator.generateFont(parameter)
mediumFont = garbage.add(generator.generateFont(parameter))
parameter.size = (Dimension * 9 / 16).toInt
smallFont = generator.generateFont(parameter)
smallFont = garbage.add(generator.generateFont(parameter))
parameter.size = (Dimension * 3 / 8).toInt
tinyFont = generator.generateFont(parameter)
tinyFont = garbage.add(generator.generateFont(parameter))
generator.dispose()
}

private val CharExtras = Home.Title

var bigFont: BitmapFont = _
var mediumFont: BitmapFont = _
var smallFont: BitmapFont = _
var tinyFont: BitmapFont = _
Expand Down
1 change: 1 addition & 0 deletions core/src/org/merlin/tertis/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ package object tertis {
}

implicit class ColorOps(val self: Color) extends AnyVal {

/** Returns a new colour with alpha set to [alpha]. */
def withAlpha(alpha: Float): Color =
new Color(self.r, self.g, self.b, alpha)
Expand Down
19 changes: 19 additions & 0 deletions core/src/org/merlin/tertis/util/GarbageCan.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package org.merlin.tertis.util

import com.badlogic.gdx.utils.Disposable

import scala.collection.mutable

class GarbageCan extends Disposable {
private val trash = mutable.ListBuffer.empty[Disposable]
def add[A <: Disposable](a: A): A = {
trash.append(a)
a
}

override def dispose(): Unit = {
trash.foreach(_.dispose())
trash.clear()
}

}
16 changes: 9 additions & 7 deletions core/src/org/merlin/tertis/util/TextureWrapper.scala
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package org.merlin.tertis.util
package org.merlin.tertis
package util

import com.badlogic.gdx.Gdx
import com.badlogic.gdx.graphics.{Pixmap, Texture}
import com.badlogic.gdx.utils.Disposable

class TextureWrapper(val pixmap: Pixmap) {
class TextureWrapper(val pixmap: Pixmap) extends Disposable {

val width = pixmap.getWidth
val height = pixmap.getHeight
val width: Int = pixmap.getWidth
val height: Int = pixmap.getHeight
val texture = new Texture(pixmap)

def dispose(): Unit = {
override def dispose(): Unit = {
texture.dispose()
pixmap.dispose()
}

}

object TextureWrapper {
def load(path: String): TextureWrapper = {
def load(path: String)(implicit garbageCan: GarbageCan): TextureWrapper = {
val fileHandle = Gdx.files.internal(path)
val pixmap = new Pixmap(fileHandle)
new TextureWrapper(pixmap)
garbageCan.add(new TextureWrapper(pixmap))
}

implicit def toTexture(wrapper: TextureWrapper): Texture = wrapper.texture
Expand Down

0 comments on commit 62324b5

Please sign in to comment.