disposable singletons to an array in loader; single float for both notification and tooltip

This commit is contained in:
minjaesong
2019-05-24 20:38:35 +09:00
parent c5e0de2393
commit 0b7a3a5636
13 changed files with 116 additions and 69 deletions

8
.idea/markdown-exported-files.xml generated Normal file
View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="MarkdownExportedFiles">
<htmlFiles />
<imageFiles />
<otherFiles />
</component>
</project>

2
.idea/misc.xml generated
View File

@@ -38,7 +38,7 @@
<property name="caretWidth" class="java.lang.Integer" />
</properties>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_10" default="false" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="false" project-jdk-name="13-incubate" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@@ -13,20 +13,19 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.ScreenUtils;
import com.github.strikerx3.jxinput.XInputDevice;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import net.torvald.getcpuname.GetCpuName;
import net.torvald.terrarum.blockstats.MinimapComposer;
import net.torvald.terrarum.controller.GdxControllerAdapter;
import net.torvald.terrarum.controller.TerrarumController;
import net.torvald.terrarum.controller.XinputControllerAdapter;
import net.torvald.terrarum.gamecontroller.KeyToggler;
import net.torvald.terrarum.imagefont.TinyAlphNum;
import net.torvald.terrarum.modulebasegame.Ingame;
import net.torvald.terrarum.modulebasegame.IngameRenderer;
import net.torvald.terrarum.utils.JsonFetcher;
import net.torvald.terrarum.utils.JsonWriter;
import net.torvald.terrarum.worlddrawer.BlocksDrawer;
@@ -286,6 +285,7 @@ public class AppLoader implements ApplicationListener {
public static CommonResourcePool resourcePool;
public static HashSet<File> tempFilePool = new HashSet();
public static HashSet<Disposable> disposableSingletonsPool = new HashSet();
@Override
public void create() {
@@ -517,11 +517,12 @@ public class AppLoader implements ApplicationListener {
screen.dispose();
}
IngameRenderer.INSTANCE.dispose();
PostProcessor.INSTANCE.dispose();
MinimapComposer.INSTANCE.dispose();
//IngameRenderer.INSTANCE.dispose();
//PostProcessor.INSTANCE.dispose();
//MinimapComposer.INSTANCE.dispose();
//FloatDrawer.INSTANCE.dispose();
Terrarum.INSTANCE.dispose();
//Terrarum.INSTANCE.dispose();
shaderBayerSkyboxFill.dispose();
shaderHicolour.dispose();
@@ -539,6 +540,8 @@ public class AppLoader implements ApplicationListener {
textureWhiteCircle.dispose();
logo.getTexture().dispose();
disposableSingletonsPool.forEach(Disposable::dispose);
ModMgr.INSTANCE.disposeMods();
deleteTempfiles();

View File

@@ -11,6 +11,7 @@ import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.badlogic.gdx.math.Matrix4
import com.badlogic.gdx.utils.Disposable
import net.torvald.terrarum.gamecontroller.KeyToggler
import net.torvald.terrarum.ui.BasicDebugInfoWindow
import net.torvald.terrarum.worlddrawer.BlocksDrawer
@@ -18,7 +19,7 @@ import net.torvald.terrarum.worlddrawer.BlocksDrawer
/**
* Must be called by the App Loader
*/
object PostProcessor {
object PostProcessor : Disposable {
private lateinit var batch: SpriteBatch // not nulling to save some lines of code
private lateinit var shapeRenderer: ShapeRenderer
@@ -40,7 +41,11 @@ object PostProcessor {
private var functionRowHelper = Texture(Gdx.files.internal("assets/graphics/function_row_help.png"))
fun dispose() {
init {
AppLoader.disposableSingletonsPool.add(this)
}
override fun dispose() {
batch.dispose()
shapeRenderer.dispose()
try {

View File

@@ -10,6 +10,7 @@ import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.badlogic.gdx.graphics.glutils.ShaderProgram
import com.badlogic.gdx.graphics.glutils.ShapeRenderer
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.GdxRuntimeException
import com.jme3.math.FastMath
import net.torvald.random.HQRNG
@@ -37,7 +38,7 @@ typealias RGBA8888 = Int
*
* LibGDX Version Created by minjaesong on 2017-06-15.
*/
object Terrarum : Screen {
object Terrarum : Screen, Disposable {
/**
* All singleplayer "Player" must have this exact reference ID.
@@ -212,6 +213,9 @@ object Terrarum : Screen {
setGamepadButtonLabels()
AppLoader.disposableSingletonsPool.add(this)
}
private fun setGamepadButtonLabels() {

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.blockstats
import com.badlogic.gdx.graphics.Pixmap
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.GdxRuntimeException
import com.badlogic.gdx.utils.Queue
import net.torvald.terrarum.AppLoader
@@ -12,7 +13,7 @@ import net.torvald.terrarum.gameworld.GameWorld
import net.torvald.terrarum.worlddrawer.BlocksDrawer
import net.torvald.terrarum.worlddrawer.CreateTileAtlas
object MinimapComposer {
object MinimapComposer : Disposable {
// strategy: mosaic the textures, maximum texture size is 4 096.
@@ -66,6 +67,8 @@ object MinimapComposer {
init {
totalWidth = minimap.width
totalHeight = minimap.height
AppLoader.disposableSingletonsPool.add(this)
}
fun update() {
@@ -147,7 +150,7 @@ object MinimapComposer {
}
}
fun dispose() {
override fun dispose() {
liveTiles.forEach { it.dispose() }
minimap.dispose()
try {

View File

@@ -602,10 +602,16 @@ open class Ingame(batch: SpriteBatch) : IngameInstance(batch) {
}
/** Send message to notifier UI and toggle the UI as opened. */
fun sendNotification(msg1: String, msg2: String? = null) {
(notifier as Notification).sendNotification(if (msg2 != null) arrayOf(msg1, msg2) else arrayOf(msg1))
fun sendNotification(messages: Array<String>) {
(notifier as Notification).sendNotification(messages.toList())
}
fun sendNotification(messages: List<String>) {
(notifier as Notification).sendNotification(messages)
}
fun sendNotification(singleMessage: String) = sendNotification(listOf(singleMessage))
fun wakeDormantActors() {
var actorContainerSize = actorContainerInactive.size
var i = 0

View File

@@ -5,6 +5,7 @@ import com.badlogic.gdx.Input
import com.badlogic.gdx.graphics.*
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.graphics.glutils.FrameBuffer
import com.badlogic.gdx.utils.Disposable
import com.badlogic.gdx.utils.ScreenUtils
import net.torvald.terrarum.*
import net.torvald.terrarum.gameactors.ActorWithBody
@@ -23,7 +24,7 @@ import javax.swing.JFileChooser
*
* For the entire render path, see AppLoader.
*/
object IngameRenderer {
object IngameRenderer : Disposable {
/** for non-private use, use with care! */
lateinit var batch: SpriteBatch
private lateinit var camera: OrthographicCamera
@@ -61,6 +62,10 @@ object IngameRenderer {
private var debugMode = 0
init {
AppLoader.disposableSingletonsPool.add(this)
}
operator fun invoke(
gamePaused: Boolean,
world: GameWorldExtension,
@@ -604,7 +609,7 @@ object IngameRenderer {
private val TILE_SIZEF = CreateTileAtlas.TILE_SIZE.toFloat()
fun dispose() {
override fun dispose() {
fboRGB.dispose()
fboA.dispose()
fboRGB_lightMixed.dispose()

View File

@@ -2,7 +2,6 @@ package net.torvald.terrarum.modulebasegame.console
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.console.ConsoleCommand
import net.torvald.terrarum.langpack.Lang
import net.torvald.terrarum.modulebasegame.Ingame
/**
@@ -10,19 +9,14 @@ import net.torvald.terrarum.modulebasegame.Ingame
*/
internal object SetBulletin : ConsoleCommand {
override fun execute(args: Array<String>) {
send(Lang["ERROR_SAVE_CORRUPTED"], Lang["MENU_LABEL_CONTINUE_QUESTION"])
//send(Lang["ERROR_SAVE_CORRUPTED"], Lang["MENU_LABEL_CONTINUE_QUESTION"])
(Terrarum.ingame!! as Ingame).sendNotification(args.sliceArray(1..args.lastIndex))
println("sent notifinator")
}
override fun printUsage() {
}
/**
* Actually send notifinator
* @param message real message
*/
fun send(msg1: String, msg2: String? = null) {
(Terrarum.ingame!! as Ingame).sendNotification(msg1, msg2)
println("sent notifinator")
}
}

View File

@@ -0,0 +1,43 @@
package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import com.badlogic.gdx.utils.Disposable
import net.torvald.terrarum.AppLoader
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2019-05-24.
*/
object FloatDrawer : Disposable {
val tile = TextureRegionPack("assets/graphics/gui/message_black_tileable.tga", 8, 8)
init {
AppLoader.disposableSingletonsPool.add(this)
}
/**
* Draws the Float at given position in given size. The size is that of the centre area, excluding the edges. Size of the edges are 8x8 pixels.
*/
operator fun invoke(batch: SpriteBatch, x: Float, y: Float, w: Float, h: Float) {
// centre area
batch.draw(tile.get(1, 1), x, y, w, h)
// edges
batch.draw(tile.get(1, 0), x, y - tile.tileH, w, tile.tileH.toFloat())
batch.draw(tile.get(1, 2), x, y + h, w, tile.tileH.toFloat())
batch.draw(tile.get(0, 1), x - tile.tileW, y, tile.tileW.toFloat(), h)
batch.draw(tile.get(2, 1), x + w, y, tile.tileW.toFloat(), h)
// corners
batch.draw(tile.get(0, 0), x - tile.tileW, y - tile.tileH)
batch.draw(tile.get(2, 0), x + w, y - tile.tileH)
batch.draw(tile.get(2, 2), x + w, y + h)
batch.draw(tile.get(0, 2), x - tile.tileW, y + h)
}
override fun dispose() {
tile.dispose()
}
}

View File

@@ -8,15 +8,12 @@ import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.blendNormal
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2016-01-23.
*/
class Notification : UICanvas() {
private val segment = SEGMENT_BLACK
private var fontCol: Color = Color.WHITE // assuming alpha of 1.0
override var openCloseTime: Second = OPEN_CLOSE_TIME
@@ -28,14 +25,14 @@ class Notification : UICanvas() {
override var width: Int = 500
override var height: Int = segment.tileH
override var height: Int = 0
private val visibleTime = Math.min(
AppLoader.getConfigInt("notificationshowuptime"),
SHOWUP_MAX
) / 1000f
private var displayTimer = 0f
internal var message: Array<String> = Array(MESSAGES_DISPLAY) { "" }
internal var message: List<String> = listOf("")
init {
@@ -51,9 +48,6 @@ class Notification : UICanvas() {
}
}
private val textAreaHeight = 48f
private val imageToTextAreaDelta = (segment.tileH - textAreaHeight) / 2
private val drawColor = Color(1f,1f,1f,1f)
override fun renderUI(batch: SpriteBatch, camera: Camera) {
@@ -64,26 +58,22 @@ class Notification : UICanvas() {
val realTextWidth = 12 + if (message.size == 1)
Terrarum.fontGame.getWidth(message[0])
else
maxOf(Terrarum.fontGame.getWidth(message[0]), Terrarum.fontGame.getWidth(message[1]))
message.map { Terrarum.fontGame.getWidth(it) }.sorted().last()
val displayedTextWidth = maxOf(240, realTextWidth)
// force the UI to the centre of the screen
this.posX = (Terrarum.WIDTH - displayedTextWidth) / 2
val textHeight = message.size * Terrarum.fontGame.lineHeight
batch.color = drawColor
batch.draw(segment.get(0, 0), -segment.tileW.toFloat(), 0f)
batch.draw(segment.get(1, 0), 0f, 0f, displayedTextWidth.toFloat(), segment.tileH.toFloat())
batch.draw(segment.get(2, 0), displayedTextWidth.toFloat(), 0f)
FloatDrawer(batch, 0f, -textHeight, displayedTextWidth.toFloat(), textHeight)
batch.color = fontCol
message.forEachIndexed { index, s ->
val xoff = 6 + (displayedTextWidth - realTextWidth) / 2
val y = if (message.size == 1)
-2 + imageToTextAreaDelta + 0.5f * (textAreaHeight / 2) + (textAreaHeight / 2 - Terrarum.fontGame.lineHeight) / 2
else
-1 + imageToTextAreaDelta + index * (textAreaHeight / 2) + (textAreaHeight / 2 - Terrarum.fontGame.lineHeight) / 2
val y = -textHeight + Terrarum.fontGame.lineHeight * index
Terrarum.fontGame.draw(batch, s, LRmargin + xoff, y)
}
@@ -109,7 +99,7 @@ class Notification : UICanvas() {
endClosingFade(this)
}
fun sendNotification(message: Array<String>) {
fun sendNotification(message: List<String>) {
this.message = message
handler.openCloseCounter = 0f
handler.opacity = 0f
@@ -121,12 +111,6 @@ class Notification : UICanvas() {
companion object {
// private int messagesShowingIndex = 0;
val MESSAGES_DISPLAY = 2
val OPEN_CLOSE_TIME = 0.16f
// will be disposed by Terrarum (application main instance)
val SEGMENT_BLACK = TextureRegionPack("assets/graphics/gui/message_black.tga", 8, 56)
val SEGMENT_WHITE = TextureRegionPack("assets/graphics/gui/message_white.tga", 8, 56)
}
}

View File

@@ -2,12 +2,10 @@ package net.torvald.terrarum.modulebasegame.ui
import com.badlogic.gdx.graphics.Camera
import com.badlogic.gdx.graphics.Color
import com.badlogic.gdx.graphics.Texture
import com.badlogic.gdx.graphics.g2d.SpriteBatch
import net.torvald.terrarum.Second
import net.torvald.terrarum.Terrarum
import net.torvald.terrarum.ui.UICanvas
import net.torvald.terrarumsansbitmap.gdx.TextureRegionPack
/**
* Created by minjaesong on 2017-11-25.
@@ -22,38 +20,33 @@ class UITooltip : UICanvas() {
msgWidth = font.getWidth(value)
}
private val textures = TextureRegionPack("assets/graphics/gui/tooltip_black.tga", 8, 36)
private val font = Terrarum.fontGame
private var msgWidth = 0
val textMarginX = 4
override var width: Int
get() = msgWidth + (textMarginX + textures.tileW) * 2
get() = msgWidth + (textMarginX + FloatDrawer.tile.tileW) * 2
set(value) { throw Error("You are not supposed to set the width of the tooltip manually.") }
override var height: Int
get() = textures.tileH
get() = FloatDrawer.tile.tileH * 2 + font.lineHeight.toInt()
set(value) { throw Error("You are not supposed to set the height of the tooltip manually.") }
init {
textures.texture.setFilter(Texture.TextureFilter.Nearest, Texture.TextureFilter.Nearest)
}
override fun renderUI(batch: SpriteBatch, camera: Camera) {
val mouseX = 4f
val mouseY = 6f
val tooltipY = mouseY - textures.tileH
val tooltipY = mouseY - height
val txtW = msgWidth + 2f * textMarginX
batch.color = Color.WHITE
batch.draw(textures.get(0, 0), mouseX, tooltipY)
batch.draw(textures.get(1, 0), mouseX + textures.tileW, tooltipY, txtW, height.toFloat())
batch.draw(textures.get(2, 0), mouseX + textures.tileW + txtW, tooltipY)
font.draw(batch, message, mouseX + textures.tileW + textMarginX, mouseY - textures.tileH + (textures.tileH - font.lineHeight) / 2)
FloatDrawer(batch, mouseX - textMarginX, tooltipY, txtW, font.lineHeight)
font.draw(batch, message,
mouseX,
mouseY - height
)
}
override fun updateUI(delta: Float) {
@@ -73,7 +66,6 @@ class UITooltip : UICanvas() {
}
override fun dispose() {
textures.dispose()
}
}

View File

@@ -2,10 +2,10 @@
<module type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JVM 1.8">
<configuration version="3" platform="JVM 9">
<compilerSettings />
<compilerArguments>
<option name="jvmTarget" value="1.8" />
<option name="jvmTarget" value="11" />
</compilerArguments>
</configuration>
</facet>
@@ -16,7 +16,7 @@
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/net/torvald/terrarum/tests" isTestSource="true" />
</content>
<orderEntry type="jdk" jdkName="11" jdkType="JavaSDK" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="KotlinJavaRuntime" level="project" />
<orderEntry type="library" name="lib" level="project" />