Simple Example : tween.TEST()

In this section I will explain the tween.TEST() function used as a general test. If you want to test it just uncomment the last line

 ; tween.TEST()

and run it.

The first section defines the tween.TEST() function, all easing names and some more variables to handle all text objects, one for each easing function.

Function tween.TEST()
This program shows how to use tweens and will test all tween functions
It also uses recursive callback functions to build tween chains.

  ; All available easing functions
  Local easing =
  { "linear",
    "inquad", "outquad", "inoutquad", "outinquad",
    "incubic", "outcubic", "inoutcubic", "outincubic",
    "inquart", "outquart", "inoutquart", "outinquart",
    "inquint", "outquint", "inoutquint", "outinquint",
    "insine", "outsine", "inoutsine", "outinsine",
    "inexpo", "outexpo", "inoutexpo", "outinexpo",
    "incirc", "outcirc", "inoutcirc", "outincirc",
    "inelastic", "outelastic", "inoutelastic", "outinelastic",
    "inback", "outback", "inoutback", "outinback",
    "inbounce", "outbounce", "inoutbounce", "outinbounce" }
  Local Items   = {}                  ; Stores all items usend in our test
  Local StartE  = 0                   ; First easing
  Local EndE    = ListItems(easing)-1 ; Last easing
  Local Timer   = StartTimer(Nil)     ; Start a timer to track delta times
  Local rate    = 1000/60             ; Frame rate = 16.6ms to achieve 60FPS

We also needs to define a function to update the screen, it will be called regurarly using an Interval every rate milliseconds.

  Local UpdateScreen = Function()
    ; This routine will update the screen, tween functions needs the delta time
    ; to be updated correctly so the first thing to do is calculate this value
    ; each time the screen is updated.
    Local cTime = GetTimer(Timer)   ; Current time
    Local dTime = cTime - OldTime   ; Delta time

    ; Updates all active tweens

    ; Clear the screen
    Box(0, 0, 640, 480, #BLACK)

    ; Display the objects, one for each defined tween
    For Local i = StartE To EndE
      TextOut(Items[i].x, Items[i].y, easing[i], { Color = Items[i].Color })

    ; Message in the top-left screen corner
    TextOut(10, 10, "(ESC to quit) - DT:" .. dTime, { Color = #RED })

    OldTime = cTime

    ; Swap buffers without waiting for the vertical blank

To make it endless we define another function that will be called when each transition will reach the end point.

  ;*** CALLBACK FUNCTION used to restart the tween animations
  ;*** once they have ended.
  Local fRepeat = {} ; Needed to make recursive callbacks
  fRepeat = Function(id)
    ; Callback function used initialize and restart every item tween
    Local id =
    Items[id].x = 0
    Items[id].color = GetRandomColor()
    tween.start(10000, Items[id], { x = 560 }, easing[id], fRepeat, { id = id })

The next loop initializes all tweens, it assigns the callback function passing the object id as parameter, this way the callback function will know exactly which tween has ended and need to be restarted. Please note that the tweens are not yet started because the tween.start() function is enclosed in the field.

  ; Initialize the tweens, one for each animation available
  ; Every tween get the callback function 'fRepeat()' that will
  ; be executed when the tween ends.
  For Local i = StartE To EndE
    Local b = i
    Items[i] = { x = 0, y = 50 + i*8 }
    Items[i].fun = Function(a)
                     Local a = b
                     Items[a].x = 0
                     tween.start(10000, Items[a], { x = 560 }, easing[a], fRepeat, { id = a })

Now let's setup the renderer, which will also be responsible of the tweens update.

  ; Setup the renderer at the specified refresh rate
  Local Renderer = SetInterval(Nil, UpdateScreen, rate)

We initialize the OldTime variable for the delta time calculation.

  ; Initialize the previous time for the first delta time
  ; calculation
  Local OldTime  = GetTimer(Timer) 

For each text object assign a random color and start its tween executing the Items[i].fun(i) function.

  ; Create text colors and start all defined tweens
  For Local i = StartE To EndE
    Items[i].color = GetRandomColor()

Enable the double buffer for a smoother experience and initialize the fill style *Box() call used in the updateScreen()** function.


Finally you need an infinite loop to make it work

  ; Infinite loop
hollywoodlibs/easing/simpleex.txt · Ultima modifica: 2016/10/06 12:21 da admin
CC Attribution-Noncommercial 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0