Start reimplementing tween.c functions

This commit is contained in:
twinaphex 2014-10-10 00:01:45 +02:00
parent 3376c8dd4e
commit 821e37f480
2 changed files with 23 additions and 24 deletions

View File

@ -1,33 +1,30 @@
#include "tween.h" #include "tween.h"
#include <math.h> #include <math.h>
tween_t* tweens = NULL; static tween_t* tweens = NULL;
int numtweens = 0; static int numtweens = 0;
static void tween_free(tween_t *tw)
{
if (tw)
free(tw);
tw = NULL;
}
void add_tween(float duration, float target_value, float* subject, void add_tween(float duration, float target_value, float* subject,
easingFunc easing, tweenCallback callback) easingFunc easing, tweenCallback callback)
{ {
tween_t *tween; tween_t *tween = NULL;
tween_t *tweens_tmp; tweens = (tween_t*)
realloc(tweens, (numtweens + 1) * sizeof(tween_t));
numtweens++; if (!tweens) /* Realloc failed. */
tweens_tmp = (tween_t*)realloc(tweens, numtweens * sizeof(tween_t));
if (tweens_tmp != NULL)
{ {
tweens = tweens_tmp; tween_free(tweens);
}
else // realloc failed
{
if (tweens != NULL)
{
free(tweens);
tweens = NULL;
}
return; return;
} }
numtweens++;
tween = (tween_t*)&tweens[numtweens-1]; tween = (tween_t*)&tweens[numtweens-1];
if (!tween) if (!tween)
@ -43,10 +40,8 @@ void add_tween(float duration, float target_value, float* subject,
tween->callback = callback; tween->callback = callback;
} }
void update_tween(void *data, float dt) void update_tween(tween_t *tween, float dt)
{ {
tween_t *tween = (tween_t*)data;
if (!tween) if (!tween)
return; return;

View File

@ -21,6 +21,7 @@
#include <stdlib.h> #include <stdlib.h>
typedef float (*easingFunc)(float, float, float, float); typedef float (*easingFunc)(float, float, float, float);
typedef void (*tweenCallback) (void); typedef void (*tweenCallback) (void);
typedef struct typedef struct
@ -35,8 +36,11 @@ typedef struct
tweenCallback callback; tweenCallback callback;
} tween_t; } tween_t;
void add_tween(float duration, float target_value, float* subject, easingFunc easing, tweenCallback callback); void add_tween(float duration, float target_value,
void update_tween(void *data, float dt); float* subject, easingFunc easing, tweenCallback callback);
void update_tween(tween_t *data, float dt);
void update_tweens(float dt); void update_tweens(float dt);
// from https://github.com/kikito/tween.lua/blob/master/tween.lua // from https://github.com/kikito/tween.lua/blob/master/tween.lua