10.03.2019 - Round 1 results of our "A Love Letter For FreeBASIC" game dev competition have been published. Please be sure to check the results thread: http://games.freebasic.net/forum/index.php?topic=629.0. Don't forget that the competition is continuing with a round 2, lasting till 29th of April, 300 USD first prize. Stay tuned!

Author Topic: FreeBASIC Easing Library  (Read 4272 times)

Pritchard

  • Global Moderator
  • Forum Howler
  • *****
  • Posts: 160
    • View Profile
    • Email
FreeBASIC Easing Library
« on: January 26, 2013, 03:20:11 AM »
https://github.com/Pritchard/FB-Easing

From README.md:

Quote
This library for the FreeBASIC Compiler implements the easing methods found here: http://easings.net/

It was created by Alexander Pritchard as part of his efforts to improve the functionality of the FreeBASIC Compiler.

Main source:  https://github.com/Pritchard/FB-Easing/blob/master/Easing.bas
Interactive Demo:  https://github.com/Pritchard/FB-Easing/blob/master/EntityDemo.bas
Demo:  https://github.com/Pritchard/FB-Easing/blob/master/Demo.bas

Be sure to check out the entire repository.  There are additional demos and utility functions.

Easings provide a shorthand method for delegating the task of smoothly transitioning objects or values.  They are especially useful in visual effects and animations, where they are used to simulate common accelerations or other visual effects.

Note (01/26/2013):  This library was made between the hours of 12 AM and 4 AM tonight.  Your feedback is thus greatly appreciated.
« Last Edit: January 26, 2013, 02:25:05 PM by Pritchard »

Pritchard

  • Global Moderator
  • Forum Howler
  • *****
  • Posts: 160
    • View Profile
    • Email
Re: FreeBASIC Easing Library
« Reply #1 on: January 26, 2013, 05:13:00 AM »
Update (01/26/2013):  Length and Callback demos
Be sure to check out Github for the latest demos!

Code: [Select]
#include "Easing.bas"
using pritchard.animation

screenres 640, 480, 32

var shortEase = type<Easing>( @Easing.in_bounce, 1.5 )
var mediumEase =  type<Easing>( @Easing.in_bounce, 3 )
var longEase =  type<Easing>( @Easing.in_bounce, 5 )

while( longEase.IsComplete = 0 )

screenlock()
cls()

circle( shortEase.Y * 640, 100 ), 20
circle( mediumEase.Y * 640, 200 ), 20
circle( longEase.Y * 640, 300 ), 20
screenunlock()

sleep 16, 1

var y = shortEase.time_step( 1 / 60 )
y = mediumEase.time_step( 1 / 60 )
y = longEase.time_step( 1 / 60 )

wend

Callbacks:

Code: [Select]
#include "Easing.bas"
using pritchard.animation

screenres 640, 480, 32

type Entity
dim as integer x, y
dim as integer ease_x, ease_y
dim as integer x_direction, y_direction
dim as integer ease_index

dim as EasingFunction walk_steps(0 to 3) = _
{ _
@Easing.linear, _
@Easing.in_elastic, _
@Easing.out_bounce, _
@Easing.in_out_exponential _
}

dim as Easing walk
end type

sub handle_step( byref ease as Easing, byval user_data as any ptr )

dim as Entity ptr player = cast( Entity ptr, user_data )

player->ease_x = ease.Y * player->x_direction * 300
player->ease_y = ease.Y * player->y_direction * 300

end sub

sub next_step( byref ease as Easing, byval user_data as any ptr )

dim as Entity ptr player = cast( Entity ptr, user_data )

player->x += player->ease_x
player->y += player->ease_y

player->ease_x = 0
player->ease_y = 0

player->ease_index += 1
if( player->ease_index > ubound(player->walk_steps) ) then player->ease_index = 0

player->walk.easing_function = player->walk_steps( player->ease_index )

select case player->ease_index
case 0:
player->x_direction = 1
player->y_direction = 0
case 1:
player->x_direction = 0
player->y_direction = 1
case 2:
player->x_direction = -1
player->y_direction = 0
case 3:
player->x_direction = 0
player->y_direction = -1
end select

player->walk.t = 0

end sub

dim as Entity player
player.x = 145
player.y = 65
player.walk.user_data = @player
player.walk.easing_function = player.walk_steps(0)
player.x_direction = 1
player.walk.step_callback = @handle_step
player.walk.completed_callback = @next_step

while( multikey(&h01) = 0 )

screenlock()
cls()
line ( player.x + player.ease_x, player.y + player.ease_y ) - step (40, 50), rgb(255, 255, 255), bf
screenunlock()

sleep 16, 1
var y = player.walk.time_step( 1 / 60 )

wend
« Last Edit: January 26, 2013, 01:50:54 PM by Pritchard »

Lachie Dazdarian

  • Double dipper
  • Administrator
  • Forum Sage
  • *****
  • Posts: 1308
    • Yahoo Instant Messenger - lachie13
    • View Profile
    • The Maker Of Stuff
    • Email
Re: FreeBASIC Easing Library
« Reply #2 on: January 26, 2013, 06:13:06 AM »
Cool stuff. Could you print the name of the current graph function as it is being displayed on the screen? I think that info would be useful.

Also, maybe some examples where and which a specific function can be applied the best...in the readme maybe.
"Things like Basic and Free Basic provide much-needed therapy and a return to sanity and a correct appreciation of people. The arrogant folk really hate a word like 'Basic' - fine, and good riddance." ~ pragmatist

Pritchard

  • Global Moderator
  • Forum Howler
  • *****
  • Posts: 160
    • View Profile
    • Email
Re: FreeBASIC Easing Library
« Reply #3 on: January 26, 2013, 11:35:15 AM »
Great ideas, Lachie.

The main demo has been updated on Github to display the full name of the easings.

I added the callback demo to try and show off specific easing functions.  However, it's clear that an interactive demo which allows the user to change the easing function at runtime should be created.  I'll post again when I've added it.

ecxjoe

  • Novice
  • ***
  • Posts: 78
    • View Profile
    • Delta Code
Re: FreeBASIC Easing Library
« Reply #4 on: January 26, 2013, 01:16:38 PM »
Awesome! I'm gonna use this.

Pritchard

  • Global Moderator
  • Forum Howler
  • *****
  • Posts: 160
    • View Profile
    • Email
Re: FreeBASIC Easing Library
« Reply #5 on: January 26, 2013, 01:56:27 PM »
Update (1/26/2013), Version 1.00.03 Released!  https://github.com/Pritchard/FB-Easing

New utilities file!  https://github.com/Pritchard/FB-Easing/blob/master/EasingUtilities.bas
New demo!  https://github.com/Pritchard/FB-Easing/blob/master/EntityDemo.bas
New functionality!  Delays!

Delays cause an initial delay to occur before the X value of the easing is reported as changed.  This delays the graph by however long the delay is.

Next feature will probably be allowing the user to set the direction as either forward (0 to 1) or reverse (1 to 0).


@ecxjoe:  Feel free to try it out!  I hope you'll enjoy the latest update and demo :)