Skip to content

Commit

Permalink
add Util and Effect Snowfall
Browse files Browse the repository at this point in the history
  • Loading branch information
Acivev committed Dec 17, 2024
1 parent 51211b2 commit 0468d0a
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/com/acivev/ui/Effect.kt
Original file line number Diff line number Diff line change
@@ -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)
}
53 changes: 53 additions & 0 deletions src/com/acivev/utils/DevicePerfomanceUtils.kt
Original file line number Diff line number Diff line change
@@ -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<ActivityManager>()
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
}
}

0 comments on commit 0468d0a

Please sign in to comment.