Optimize TitleScreen by pre-allocating HighScoreManager and using KTX canvas extensions

This commit is contained in:
cmclark00 2025-03-27 18:26:10 -04:00
parent 2da2444758
commit 0f1404d969

View file

@ -13,6 +13,9 @@ import android.util.Log
import com.mintris.model.HighScoreManager import com.mintris.model.HighScoreManager
import com.mintris.model.HighScore import com.mintris.model.HighScore
import kotlin.math.abs import kotlin.math.abs
import androidx.core.graphics.withTranslation
import androidx.core.graphics.withScale
import androidx.core.graphics.withRotation
class TitleScreen @JvmOverloads constructor( class TitleScreen @JvmOverloads constructor(
context: Context, context: Context,
@ -30,6 +33,7 @@ class TitleScreen @JvmOverloads constructor(
private var width = 0 private var width = 0
private var height = 0 private var height = 0
private val tetrominosToAdd = mutableListOf<Tetromino>() private val tetrominosToAdd = mutableListOf<Tetromino>()
private val highScoreManager = HighScoreManager(context) // Pre-allocate HighScoreManager
// Touch handling variables // Touch handling variables
private var startX = 0f private var startX = 0f
@ -195,58 +199,31 @@ class TitleScreen @JvmOverloads constructor(
tetrominosToAdd.add(createRandomTetromino()) tetrominosToAdd.add(createRandomTetromino())
} else { } else {
try { try {
// Save canvas state before rotation
canvas.save()
// Translate to the tetromino's position
canvas.translate(tetromino.x, tetromino.y)
// Scale according to the tetromino's scale factor
canvas.scale(tetromino.scale, tetromino.scale)
// Rotate around the center of the tetromino
val centerX = tetromino.shape.size * cellSize / 2
val centerY = tetromino.shape.size * cellSize / 2
canvas.rotate(tetromino.rotation.toFloat(), centerX, centerY)
// Draw the tetromino // Draw the tetromino
for (row in tetromino.shape.indices) { for (y in 0 until tetromino.shape.size) {
for (col in 0 until tetromino.shape[row].size) { for (x in 0 until tetromino.shape.size) {
if (tetromino.shape[row][col] == 1) { if (tetromino.shape[y][x] == 1) {
// Draw larger glow effect val left = x * cellSize
glowPaint.alpha = 30 val top = y * cellSize
canvas.drawRect( val right = left + cellSize
col * cellSize - 8, val bottom = top + cellSize
row * cellSize - 8,
(col + 1) * cellSize + 8,
(row + 1) * cellSize + 8,
glowPaint
)
// Draw medium glow // Draw block with glow effect
glowPaint.alpha = 60 canvas.withTranslation(tetromino.x, tetromino.y) {
canvas.drawRect( withScale(tetromino.scale, tetromino.scale) {
col * cellSize - 4, withRotation(tetromino.rotation.toFloat(),
row * cellSize - 4, tetromino.shape.size * cellSize / 2,
(col + 1) * cellSize + 4, tetromino.shape.size * cellSize / 2) {
(row + 1) * cellSize + 4, // Draw glow
glowPaint canvas.drawRect(left - 8f, top - 8f, right + 8f, bottom + 8f, glowPaint)
) // Draw block
canvas.drawRect(left, top, right, bottom, paint)
// Draw main block }
canvas.drawRect( }
col * cellSize, }
row * cellSize,
(col + 1) * cellSize,
(row + 1) * cellSize,
paint
)
} }
} }
} }
// Restore canvas state
canvas.restore()
} catch (e: Exception) { } catch (e: Exception) {
Log.e("TitleScreen", "Error drawing tetromino", e) Log.e("TitleScreen", "Error drawing tetromino", e)
} }
@ -260,8 +237,7 @@ class TitleScreen @JvmOverloads constructor(
val titleY = height * 0.4f val titleY = height * 0.4f
canvas.drawText("mintris", width / 2f, titleY, titlePaint) canvas.drawText("mintris", width / 2f, titleY, titlePaint)
// Draw high scores // Draw high scores using pre-allocated manager
val highScoreManager = HighScoreManager(context)
val highScores: List<HighScore> = highScoreManager.getHighScores() val highScores: List<HighScore> = highScoreManager.getHighScores()
val highScoreY = height * 0.5f val highScoreY = height * 0.5f
if (highScores.isNotEmpty()) { if (highScores.isNotEmpty()) {