Preloading sounds, reduces lag on clientside when first starting a computer some.

This commit is contained in:
Florian Nücke 2015-01-24 15:27:20 +01:00
parent 603f23a0fb
commit 40bbac10c6
2 changed files with 47 additions and 1 deletions

View File

@ -0,0 +1,16 @@
assets/opencomputers/sounds/computer_running.ogg
assets/opencomputers/sounds/floppy_access1.ogg
assets/opencomputers/sounds/floppy_access2.ogg
assets/opencomputers/sounds/floppy_access3.ogg
assets/opencomputers/sounds/floppy_access4.ogg
assets/opencomputers/sounds/floppy_access5.ogg
assets/opencomputers/sounds/floppy_access6.ogg
assets/opencomputers/sounds/floppy_eject.ogg
assets/opencomputers/sounds/floppy_insert.ogg
assets/opencomputers/sounds/hdd_access1.ogg
assets/opencomputers/sounds/hdd_access2.ogg
assets/opencomputers/sounds/hdd_access3.ogg
assets/opencomputers/sounds/hdd_access4.ogg
assets/opencomputers/sounds/hdd_access5.ogg
assets/opencomputers/sounds/hdd_access6.ogg
assets/opencomputers/sounds/hdd_access7.ogg

View File

@ -8,8 +8,10 @@ import java.util.Timer
import java.util.TimerTask
import java.util.UUID
import com.google.common.base.Charsets
import cpw.mods.fml.client.FMLClientHandler
import cpw.mods.fml.common.eventhandler.SubscribeEvent
import cpw.mods.fml.common.gameevent.TickEvent.ClientTickEvent
import cpw.mods.fml.relauncher.ReflectionHelper
import li.cil.oc.OpenComputers
import li.cil.oc.Settings
@ -25,6 +27,7 @@ import paulscode.sound.SoundSystem
import paulscode.sound.SoundSystemConfig
import scala.collection.mutable
import scala.io.Source
object Sound {
private val sources = mutable.Map.empty[TileEntity, PseudoLoopingStream]
@ -101,6 +104,33 @@ object Sound {
manager = event.manager
}
private var hasPreloaded = Settings.get.soundVolume <= 0
@SubscribeEvent
def onTick(e: ClientTickEvent) {
if (!hasPreloaded && soundSystem != null) {
hasPreloaded = true
new Thread(new Runnable() {
override def run(): Unit = {
val preloadConfigLocation = new ResourceLocation(Settings.resourceDomain, "sounds/preload.cfg")
val preloadConfigResource = Minecraft.getMinecraft.getResourceManager.getResource(preloadConfigLocation)
for (location <- Source.fromInputStream(preloadConfigResource.getInputStream)(Charsets.UTF_8).getLines()) {
val url = getClass.getClassLoader.getResource(location)
if (url != null) try {
val sourceName = "preload_" + location
soundSystem.newSource(false, sourceName, url, location, true, 0, 0, 0, SoundSystemConfig.ATTENUATION_NONE, 16)
soundSystem.activate(sourceName)
soundSystem.removeSource(sourceName)
} catch {
case _: Throwable => // Meh.
}
else OpenComputers.log.warn(s"Couldn't preload sound $location!")
}
}
})
}
}
@SubscribeEvent
def onWorldUnload(event: WorldEvent.Unload) {
commandQueue.synchronized(commandQueue.clear())
@ -110,7 +140,7 @@ object Sound {
}
private abstract class Command(val when: Long, val tileEntity: TileEntity) extends Ordered[Command] {
def apply()
def apply(): Unit
override def compare(that: Command) = (that.when - when).toInt
}