mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-14 12:34:05 +09:00
distinguishable item placeholder image
This commit is contained in:
@@ -1,22 +1,38 @@
|
||||
package net.torvald.terrarum
|
||||
|
||||
import com.badlogic.gdx.graphics.Texture
|
||||
import com.badlogic.gdx.graphics.g2d.TextureRegion
|
||||
import com.badlogic.gdx.utils.Disposable
|
||||
import com.badlogic.gdx.utils.Queue
|
||||
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
|
||||
|
||||
/**
|
||||
* Created by minjaesong on 2019-03-10.
|
||||
*/
|
||||
object CommonResourcePool {
|
||||
|
||||
private val loadingList = Queue<Pair<Pair<String, Class<*>>, () -> Disposable>>()
|
||||
private val pool = HashMap<String, Disposable>()
|
||||
private val loadingList = Queue<Pair<String, () -> Any>>()
|
||||
private val pool = HashMap<String, Any>()
|
||||
//private val typesMap = HashMap<String, Class<*>>()
|
||||
private var loadCounter = -1 // using counters so that the loading can be done on separate thread (gg if the asset requires GL context to be loaded)
|
||||
val loaded: Boolean
|
||||
get() = loadCounter == 0
|
||||
|
||||
fun <T> addToLoadingList(identifier: String, type: Class<T>, loadFunction: () -> Disposable) {
|
||||
loadingList.addFirst(identifier to type to loadFunction)
|
||||
init {
|
||||
addToLoadingList("itemplaceholder_24") {
|
||||
val t = TextureRegion(Texture("assets/item_kari_24.tga"))
|
||||
t.flip(false, true)
|
||||
/*return*/t
|
||||
}
|
||||
addToLoadingList("itemplaceholder_48") {
|
||||
val t = TextureRegion(Texture("assets/item_kari_48.tga"))
|
||||
t.flip(false, true)
|
||||
/*return*/t
|
||||
}
|
||||
}
|
||||
|
||||
fun addToLoadingList(identifier: String, loadFunction: () -> Any) {
|
||||
loadingList.addFirst(identifier to loadFunction)
|
||||
|
||||
if (loadCounter == -1)
|
||||
loadCounter = 1
|
||||
@@ -31,8 +47,7 @@ object CommonResourcePool {
|
||||
if (loaded) throw IllegalStateException("Assets are already loaded and shipped out :p")
|
||||
|
||||
while (!loadingList.isEmpty) {
|
||||
val (k, loadfun) = loadingList.removeFirst()
|
||||
val (name, type) = k
|
||||
val (name, loadfun) = loadingList.removeFirst()
|
||||
|
||||
if (pool.containsKey(name)) {
|
||||
throw IllegalArgumentException("Assets with identifier '$name' already exists.")
|
||||
@@ -45,16 +60,24 @@ object CommonResourcePool {
|
||||
}
|
||||
}
|
||||
|
||||
operator fun get(identifier: String): Disposable {
|
||||
val obj = pool[identifier]!!
|
||||
|
||||
return obj
|
||||
operator fun get(identifier: String): Any {
|
||||
return pool[identifier]!!
|
||||
}
|
||||
|
||||
fun getAsTextureRegionPack(identifier: String) = get(identifier) as TextureRegionPack
|
||||
fun getAsTextureRegion(identifier: String) = get(identifier) as TextureRegion
|
||||
fun getAsTexture(identifier: String) = get(identifier) as Texture
|
||||
|
||||
fun dispose() {
|
||||
pool.forEach { _, u ->
|
||||
try {
|
||||
u.dispose()
|
||||
if (u is Disposable)
|
||||
u.dispose()
|
||||
if (u is Texture)
|
||||
u.dispose()
|
||||
if (u is TextureRegion)
|
||||
u.texture.dispose()
|
||||
// TODO
|
||||
}
|
||||
catch (e: Throwable) {
|
||||
e.printStackTrace()
|
||||
|
||||
Reference in New Issue
Block a user