changeset 382:e76822e943af

Merge branch 'main' of github.com:saemideluxe/semicongine
author Sam <sam@basx.dev>
date Sat, 02 Dec 2023 22:26:26 +0700
parents bba0e9c881d1 (current diff) ade6e9324ae0 (diff)
children 8e926856a9ce
files generators/vulkan_api/config.nims generators/vulkan_api/vulkan_api_generator.nim semicongine.nim semicongine/animation.nim semicongine/audio.nim semicongine/collision.nim semicongine/core.nim semicongine/core/audiotypes.nim semicongine/core/buildconfig.nim semicongine/core/color.nim semicongine/core/dynamic_arrays.nim semicongine/core/fonttypes.nim semicongine/core/gpu_types.nim semicongine/core/imagetypes.nim semicongine/core/matrix.nim semicongine/core/utils.nim semicongine/core/vector.nim semicongine/core/vulkanapi.nim semicongine/engine.nim semicongine/events.nim semicongine/material.nim semicongine/mesh.nim semicongine/noise.nim semicongine/platform/audio.nim semicongine/platform/linux/audio.nim semicongine/platform/linux/surface.nim semicongine/platform/linux/symkey_map.nim semicongine/platform/linux/vulkanExtensions.nim semicongine/platform/linux/window.nim semicongine/platform/surface.nim semicongine/platform/vulkanExtensions.nim semicongine/platform/window.nim semicongine/platform/windows/audio.nim semicongine/platform/windows/surface.nim semicongine/platform/windows/virtualkey_map.nim semicongine/platform/windows/vulkanExtensions.nim semicongine/platform/windows/window.nim semicongine/renderer.nim semicongine/resources.nim semicongine/resources/audio.nim semicongine/resources/font.nim semicongine/resources/image.nim semicongine/resources/lodepng.c semicongine/resources/lodepng.h semicongine/resources/mesh.nim semicongine/resources/stb_truetype.h semicongine/resources/stb_vorbis.c semicongine/scene.nim semicongine/settings.nim semicongine/telemetry.nim semicongine/text.nim semicongine/vulkan.nim semicongine/vulkan/buffer.nim semicongine/vulkan/commandbuffer.nim semicongine/vulkan/descriptor.nim semicongine/vulkan/device.nim semicongine/vulkan/drawable.nim semicongine/vulkan/framebuffer.nim semicongine/vulkan/image.nim semicongine/vulkan/instance.nim semicongine/vulkan/memory.nim semicongine/vulkan/physicaldevice.nim semicongine/vulkan/pipeline.nim semicongine/vulkan/platform/android.nim semicongine/vulkan/platform/directfb.nim semicongine/vulkan/platform/fuchsia.nim semicongine/vulkan/platform/ggp.nim semicongine/vulkan/platform/ios.nim semicongine/vulkan/platform/macos.nim semicongine/vulkan/platform/metal.nim semicongine/vulkan/platform/provisional.nim semicongine/vulkan/platform/sci.nim semicongine/vulkan/platform/screen.nim semicongine/vulkan/platform/vi.nim semicongine/vulkan/platform/wayland.nim semicongine/vulkan/platform/win32.nim semicongine/vulkan/platform/xcb.nim semicongine/vulkan/platform/xlib.nim semicongine/vulkan/platform/xlib_xrandr.nim semicongine/vulkan/renderpass.nim semicongine/vulkan/shader.nim semicongine/vulkan/swapchain.nim semicongine/vulkan/syncing.nim src/semicongine/animation.nim
diffstat 1 files changed, 12 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/semicongine/animation.nim	Sat Dec 02 22:23:29 2023 +0700
+++ b/semicongine/animation.nim	Sat Dec 02 22:26:26 2023 +0700
@@ -34,7 +34,7 @@
     iterations: int
   AnimationPlayer*[T] = object
     animation*: Animation[T]
-    currentTime: float32
+    currentTime*: float32
     playing*: bool
     currentDirection: int
     currentIteration: int
@@ -125,17 +125,18 @@
     iterations: iterations
   )
 
-func resetPlayer*(player: var AnimationPlayer) =
+proc reset*(player: var AnimationPlayer) =
+  player.currentValue = player.animation.animationFunction(0)
   player.currentTime = 0
   player.currentDirection = if player.animation.direction == Backward: -1 else : 1
   player.currentIteration = player.animation.iterations
 
 
-func newAnimationPlayer*[T](animation: Animation[T]): AnimationPlayer[T] =
+proc newAnimationPlayer*[T](animation: Animation[T]): AnimationPlayer[T] =
   result = AnimationPlayer[T](animation: animation, playing: false)
-  result.resetPlayer()
+  result.reset()
 
-func newAnimationPlayer*[T](value: T = default(T)): AnimationPlayer[T] =
+proc newAnimationPlayer*[T](value: T = default(T)): AnimationPlayer[T] =
   newAnimationPlayer[T](newAnimation[T]((t: AnimationTime) => value, 0))
 
 func start*(player: var AnimationPlayer) =
@@ -148,11 +149,12 @@
   # TODO: check this function, not 100% correct I think
   if player.playing:
     player.currentTime += float32(player.currentDirection) * dt
-    if abs(player.currentTime) > player.animation.duration:
+    if not (0 <= player.currentTime and player.currentTime < player.animation.duration):
       dec player.currentIteration
+      # last iteration reached
       if player.currentIteration <= 0 and player.animation.iterations != 0:
         player.stop()
-        player.resetPlayer()
+      # more iterations
       else:
         case player.animation.direction:
           of Forward:
@@ -163,5 +165,7 @@
             player.currentDirection = -player.currentDirection
             player.currentTime += float32(player.currentDirection) * dt * 2'f32
 
-  player.currentValue = player.animation.animationFunction((abs(player.currentTime) / player.animation.duration) mod high(AnimationTime))
+    player.currentValue = player.animation.animationFunction(
+      max(low(AnimationTime), min(player.currentTime / player.animation.duration, high(AnimationTime)))
+    )
   return player.currentValue