diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_main.iml b/.idea/modules/kudens_main.iml index ef317f0..85efb71 100644 --- a/.idea/modules/kudens_main.iml +++ b/.idea/modules/kudens_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_main.iml b/.idea/modules/kudens_main.iml index ef317f0..85efb71 100644 --- a/.idea/modules/kudens_main.iml +++ b/.idea/modules/kudens_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_test.iml b/.idea/modules/kudens_test.iml index 8517dde..7bf052f 100644 --- a/.idea/modules/kudens_test.iml +++ b/.idea/modules/kudens_test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_main.iml b/.idea/modules/kudens_main.iml index ef317f0..85efb71 100644 --- a/.idea/modules/kudens_main.iml +++ b/.idea/modules/kudens_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_test.iml b/.idea/modules/kudens_test.iml index 8517dde..7bf052f 100644 --- a/.idea/modules/kudens_test.iml +++ b/.idea/modules/kudens_test.iml @@ -1,5 +1,5 @@ - + diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_main.iml b/.idea/modules/kudens_main.iml index ef317f0..85efb71 100644 --- a/.idea/modules/kudens_main.iml +++ b/.idea/modules/kudens_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_test.iml b/.idea/modules/kudens_test.iml index 8517dde..7bf052f 100644 --- a/.idea/modules/kudens_test.iml +++ b/.idea/modules/kudens_test.iml @@ -1,5 +1,5 @@ - + diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/src/main/kotlin/games/perses/sound/Sounds.kt b/src/main/kotlin/games/perses/sound/Sounds.kt index 4efa4dd..b2d0c2c 100644 --- a/src/main/kotlin/games/perses/sound/Sounds.kt +++ b/src/main/kotlin/games/perses/sound/Sounds.kt @@ -9,7 +9,7 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { +class Sound(val name:String, val url: String, val defaultVolume: Double = 0.75, val numberOfChannels: Int) { var channels: Array var nextChannel: Int = 0 @@ -22,13 +22,14 @@ audio.src = url audio.pause() audio.load() - audio.volume = volume + audio.volume = defaultVolume } } } - fun play() { + fun play(volume: Double = defaultVolume) { //println("PLAYING: $name - $nextChannel") + channels[nextChannel]?.volume = volume channels[nextChannel]?.currentTime = 0.0 channels[nextChannel]?.play() @@ -49,10 +50,14 @@ sounds.put(name, Sound(name, url, volume, channels)) } - fun play(name: String, volume: Float = 0.75f) { + fun play(name: String, volume: Double? = null) { val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") - sound.play() + if (volume != null) { + sound.play(volume) + } else { + sound.play() + } } fun pause(name: String) { diff --git a/.idea/modules/kudens.iml b/.idea/modules/kudens.iml index 4aae43b..2163fa9 100644 --- a/.idea/modules/kudens.iml +++ b/.idea/modules/kudens.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_main.iml b/.idea/modules/kudens_main.iml index ef317f0..85efb71 100644 --- a/.idea/modules/kudens_main.iml +++ b/.idea/modules/kudens_main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/kudens_test.iml b/.idea/modules/kudens_test.iml index 8517dde..7bf052f 100644 --- a/.idea/modules/kudens_test.iml +++ b/.idea/modules/kudens_test.iml @@ -1,5 +1,5 @@ - + diff --git a/src/main/kotlin/games/perses/game/Game.kt b/src/main/kotlin/games/perses/game/Game.kt index a8d0578..544b6e9 100644 --- a/src/main/kotlin/games/perses/game/Game.kt +++ b/src/main/kotlin/games/perses/game/Game.kt @@ -74,6 +74,7 @@ var currentTime = start var currentDelta = 0f var pause: Boolean = false + var clearScreenEveryFrame = true var clearRed = 0f var clearGreen = 0f @@ -152,38 +153,44 @@ } fun gameLoop() { - if (!Textures.ready()) { - gl().clearColor(1f, 1f, 1f, 1f) - gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) - } else { - resize() - - if (!pause) { - html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - - gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + try { + if (!Textures.ready()) { + gl().clearColor(1f, 1f, 1f, 1f) gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } else { + resize() - gl().enable(WebGLRenderingContext.BLEND) - gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); + if (!pause) { + html.canvas2d.clearRect(0.0, 0.0, view.width.toDouble(), view.height.toDouble()); - val time = Date().getTime() - currentDelta = ((time - currentTime) / 1000f).toFloat() - currentTime = time + if (clearScreenEveryFrame) { + gl().clearColor(clearRed, clearGreen, clearBlue, clearAlpha) + gl().clear(WebGLRenderingContext.COLOR_BUFFER_BIT) + } - val timeInSeconds = (currentTime - start) / 1000f + gl().enable(WebGLRenderingContext.BLEND) + gl().blendFunc(WebGLRenderingContext.SRC_ALPHA, WebGLRenderingContext.ONE_MINUS_SRC_ALPHA) //ONE_MINUS_DST_ALPHA); - fpsCountTime += currentDelta - fpsCount++ - while (fpsCountTime > 1f) { - fps = fpsCount - fpsCountTime -= 1f - fpsCount = 0 + val time = Date().getTime() + currentDelta = ((time - currentTime) / 1000f).toFloat() + currentTime = time + + val timeInSeconds = (currentTime - start) / 1000f + + fpsCountTime += currentDelta + fpsCount++ + while (fpsCountTime > 1f) { + fps = fpsCount + fpsCountTime -= 1f + fpsCount = 0 + } + + currentScreen.update(timeInSeconds.toFloat(), currentDelta) + currentScreen.render() } - - currentScreen.update(timeInSeconds.toFloat(), currentDelta) - currentScreen.render() } + } catch(e: Exception) { + console.log(e.message, e) } window.requestAnimationFrame { diff --git a/src/main/kotlin/games/perses/sound/Sounds.kt b/src/main/kotlin/games/perses/sound/Sounds.kt index 4efa4dd..b2d0c2c 100644 --- a/src/main/kotlin/games/perses/sound/Sounds.kt +++ b/src/main/kotlin/games/perses/sound/Sounds.kt @@ -9,7 +9,7 @@ * Time: 12:34 */ -class Sound(val name:String, val url: String, val volume: Double = 0.75, val numberOfChannels: Int) { +class Sound(val name:String, val url: String, val defaultVolume: Double = 0.75, val numberOfChannels: Int) { var channels: Array var nextChannel: Int = 0 @@ -22,13 +22,14 @@ audio.src = url audio.pause() audio.load() - audio.volume = volume + audio.volume = defaultVolume } } } - fun play() { + fun play(volume: Double = defaultVolume) { //println("PLAYING: $name - $nextChannel") + channels[nextChannel]?.volume = volume channels[nextChannel]?.currentTime = 0.0 channels[nextChannel]?.play() @@ -49,10 +50,14 @@ sounds.put(name, Sound(name, url, volume, channels)) } - fun play(name: String, volume: Float = 0.75f) { + fun play(name: String, volume: Double? = null) { val sound: Sound = sounds[name] ?: throw IllegalArgumentException("Sound '$name' not found, load it first!") - sound.play() + if (volume != null) { + sound.play(volume) + } else { + sound.play() + } } fun pause(name: String) { diff --git a/src/main/kotlin/games/perses/text/Texts.kt b/src/main/kotlin/games/perses/text/Texts.kt index 4caa6f7..2fdc347 100644 --- a/src/main/kotlin/games/perses/text/Texts.kt +++ b/src/main/kotlin/games/perses/text/Texts.kt @@ -10,9 +10,15 @@ fun drawText(x: Float, y: Float, message: String, font: String = "bold 24pt Arial", fillStyle: String = "white") { var yy = y + var xx = x if (yy < 0) { yy += Game.view.height } + if (xx < 0) { + xx += Game.view.width + } + yy = Game.view.height - yy + Game.html.canvas2d.fillStyle = fillStyle Game.html.canvas2d.font = font Game.html.canvas2d.fillText(message, x.toDouble(), yy.toDouble())