HUGE memory savings (120MB -> 75MB) By saving atlases between ruleset resets!

This commit is contained in:
Yair Morgenstern
2021-03-29 17:07:59 +03:00
parent 12147e0a67
commit 29c9f3b3e4
3 changed files with 18 additions and 15 deletions

View File

@ -84,7 +84,7 @@ class UncivGame(parameters: UncivGameParameters) : Game() {
* - Skin (hence CameraStageBaseScreen.setSkin())
* - Font (hence Fonts.resetFont() inside setSkin())
*/
ImageGetter.atlas = TextureAtlas("game.atlas")
ImageGetter.resetAtlases()
settings = GameSaver.getGeneralSettings() // needed for the screen
ImageGetter.setNewRuleset(ImageGetter.ruleset) // This needs to come after the settings, since we may have default visual mods
CameraStageBaseScreen.setSkin() // needs to come AFTER the Texture reset, since the buttons depend on it

View File

@ -32,11 +32,19 @@ object ImageGetter {
// So, we now use TexturePacker in the DesktopLauncher class to pack all the different images into single images,
// and the atlas is what tells us what was packed where.
lateinit var atlas: TextureAtlas
val atlases = HashMap<String, TextureAtlas>()
var ruleset = Ruleset()
// We then shove all the drawables into a hashmap, because the atlas specifically tells us
// that the search on it is inefficient
private val textureRegionDrawables = HashMap<String, TextureRegionDrawable>()
internal val textureRegionDrawables = HashMap<String, TextureRegionDrawable>()
fun resetAtlases(){
atlases.values.forEach { it.dispose() }
atlases.clear()
atlas = TextureAtlas("game.atlas")
atlases["game"] = atlas
}
/** Required every time the ruleset changes, in order to load mod-specific images */
fun setNewRuleset(ruleset: Ruleset) {
@ -49,27 +57,22 @@ object ImageGetter {
}
for (singleImagesFolder in sequenceOf("BuildingIcons", "FlagIcons", "UnitIcons")) {
val tempAtlas = TextureAtlas("$singleImagesFolder.atlas")
if (!atlases.containsKey(singleImagesFolder)) atlases[singleImagesFolder] = TextureAtlas("$singleImagesFolder.atlas")
val tempAtlas = atlases[singleImagesFolder]!!
for (region in tempAtlas.regions) {
val drawable = TextureRegionDrawable(region)
textureRegionDrawables["$singleImagesFolder/" + region.name] = drawable
}
}
for (folder in Gdx.files.internal("SingleImages").list())
for (image in folder.list()) {
val texture = Texture(image)
// Since these aren't part of the packed texture we need to set this manually for each one
// Unfortunately since it's not power-of-2
texture.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear)
textureRegionDrawables[folder.name() + "/" + image.nameWithoutExtension()] = TextureRegionDrawable(texture)
}
// These are from the mods
for (mod in UncivGame.Current.settings.visualMods + ruleset.mods) {
val modAtlasFile = Gdx.files.local("mods/$mod/game.atlas")
if (!modAtlasFile.exists()) continue
val modAtlas = TextureAtlas(modAtlasFile)
if (!atlases.containsKey(mod)) atlases[mod] = TextureAtlas(modAtlasFile)
val modAtlas = atlases[mod]!!
for (region in modAtlas.regions) {
val drawable = TextureRegionDrawable(region)
textureRegionDrawables[region.name] = drawable

View File

@ -297,8 +297,8 @@ class OptionsPopup(val previousScreen:CameraStageBaseScreen) : Popup(previousScr
val tileSetSelectBox = SelectBox<String>(skin)
val tileSetArray = Array<String>()
val tileSets = ImageGetter.atlas.regions.filter { it.name.startsWith("TileSets") }
.map { it.name.split("/")[1] }.distinct()
val tileSets = ImageGetter.textureRegionDrawables.keys.asSequence().filter { it.startsWith("TileSets") }
.map { it.split("/")[1] }.distinct()
for (tileset in tileSets) tileSetArray.add(tileset)
tileSetSelectBox.items = tileSetArray
tileSetSelectBox.selected = settings.tileSet