From 0468d0a85beb51765a38b5c81815885ce2d6d608 Mon Sep 17 00:00:00 2001 From: acivev Date: Tue, 17 Dec 2024 13:28:08 +0100 Subject: [PATCH] add Util and Effect Snowfall --- src/com/acivev/ui/Effect.kt | 60 +++++++++++++++++++ src/com/acivev/utils/DevicePerfomanceUtils.kt | 53 ++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 src/com/acivev/ui/Effect.kt create mode 100644 src/com/acivev/utils/DevicePerfomanceUtils.kt diff --git a/src/com/acivev/ui/Effect.kt b/src/com/acivev/ui/Effect.kt new file mode 100644 index 000000000..cdda8edc7 --- /dev/null +++ b/src/com/acivev/ui/Effect.kt @@ -0,0 +1,60 @@ +package com.acivev.ui + +import android.content.Context +import com.acivev.utils.DevicePerformanceUtil +import org.anddev.andengine.entity.particle.ParticleSystem +import org.anddev.andengine.entity.particle.emitter.PointParticleEmitter +import org.anddev.andengine.entity.particle.initializer.AccelerationInitializer +import org.anddev.andengine.entity.particle.initializer.RotationInitializer +import org.anddev.andengine.entity.particle.initializer.VelocityInitializer +import org.anddev.andengine.entity.particle.modifier.AlphaModifier +import org.anddev.andengine.entity.particle.modifier.ExpireModifier +import org.anddev.andengine.entity.particle.modifier.ScaleModifier +import org.anddev.andengine.entity.scene.Scene +import ru.nsu.ccfit.zuev.osu.Config +import ru.nsu.ccfit.zuev.osu.ResourceManager +import javax.microedition.khronos.opengles.GL10 + +fun addSnowfall(scene: Scene, context: Context) { + val snowflakeTextures = ResourceManager.getInstance().loadHighQualityAsset("snow", "snow.png") + + val isLowEndDevice = DevicePerformanceUtil.isLowEndDevice(context) + val maxParticles = if (isLowEndDevice) 15 else 75 + + val snowParticleSystem = ParticleSystem( + PointParticleEmitter(Config.getRES_WIDTH() / 2f, 0f), // Emitter at the top center + 10f, 15f, maxParticles, // Min rate, max rate, max particles + snowflakeTextures + ).also { particleSystem -> + + // Random horizontal position across the screen + particleSystem.addParticleInitializer { particle -> + particle.setPosition( + Math.random().toFloat() * Config.getRES_WIDTH(), 0f + ) + + } + + // Random horizontal and vertical speed + particleSystem.addParticleInitializer(VelocityInitializer(-50f, 50f, 100f, 300f)) + + // Gravity effect + particleSystem.addParticleInitializer(AccelerationInitializer(0f, 10f)) + + // Random rotation + particleSystem.addParticleInitializer(RotationInitializer(0.0f, 360.0f)) + + // Random size from .2 to .8 + particleSystem.addParticleModifier(ScaleModifier(0.2f, .8f, 0.0f, 1.0f)) + + // Fade out + particleSystem.addParticleModifier(AlphaModifier(1.0f, 0.0f, 0.0f, 4.0f)) + + // Random lifetime between 1 to 7 seconds + particleSystem.addParticleModifier(ExpireModifier(1.0f, 7.0f)) + + particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA) + particleSystem.isParticlesSpawnEnabled = true + } + scene.attachChild(snowParticleSystem) +} \ No newline at end of file diff --git a/src/com/acivev/utils/DevicePerfomanceUtils.kt b/src/com/acivev/utils/DevicePerfomanceUtils.kt new file mode 100644 index 000000000..e6afe1f6d --- /dev/null +++ b/src/com/acivev/utils/DevicePerfomanceUtils.kt @@ -0,0 +1,53 @@ +package com.acivev.utils + +import android.app.ActivityManager +import android.content.Context +import android.content.pm.ConfigurationInfo +import android.util.Log +import androidx.core.content.getSystemService +import com.acivev.utils.OpenGLVersionUtil.supportsGLES3 + +/** + * Utility class to check if the device is low-end or high-end. + * The checks are based on OpenGL version and RAM. + * Additional checks can be added as needed. + */ +object DevicePerformanceUtil { + fun isLowEndDevice(context: Context): Boolean { + // Check for GLES3 support + if (!supportsGLES3(context)) { + Log.w("DevicePerformance", "Low-end device detected") + return true + } else { + Log.i("DevicePerformance", "High-end device detected") + } + + // Additional checks like RAM or CPU can go here + val totalRam = getTotalRAM(context) + return totalRam < 2000 // High-end device < 2GB RAM + } + + private fun getTotalRAM(context: Context): Long { + val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager + val memoryInfo = ActivityManager.MemoryInfo() + activityManager.getMemoryInfo(memoryInfo) + return memoryInfo.totalMem / (1024 * 1024) // Convert bytes to MB + } +} + +object OpenGLVersionUtil { + /** + * Checks if the device supports OpenGL ES 3.0 or higher. + * + * @param context The application context. + * @return true if the device supports GLES 3.0 or higher, false otherwise. + */ + fun supportsGLES3(context: Context): Boolean { + val activityManager = context.getSystemService() + if (activityManager != null) { + val configInfo: ConfigurationInfo = activityManager.deviceConfigurationInfo + return configInfo.reqGlEsVersion >= 0x30000 // Check if GLES 3.0 or higher + } + return false // Default to false if unable to retrieve info + } +} \ No newline at end of file