Moved a tonne of code around

This commit is contained in:
2021-11-22 09:20:01 -08:00
parent fb454d98a4
commit 6d8fe79a76
227 changed files with 61 additions and 810 deletions

View File

@ -0,0 +1,20 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "animation.h"
float animForwardAndBackward(float t) {
return (t < 0.5 ? t : 1 - t);
}
float animForwardAndBackwardScaled(float t) {
return animForwardAndBackward(t) * 2.0f;
}
float animTimeScaleFromFrameTime(int32_t frames, float time) {
return 1.0f / (float)frames / time;
}

View File

@ -0,0 +1,38 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../../libs.h"
/**
* Animation tool for converting 0-1 space into a 0-0.5 back to zero space. This
* is intended to make a "Forward then backwards" effect for animation. This
* method will not scale t.
* @param t Time in space to back and fourth on between 0 and 1.
* @returns Forward and backwards time. 0 to 0.5 are as such, 0.5 to 1 are from
* 0.5 to 0.
*/
float animForwardAndBackward(float t);
/**
* Animation tool for converting 0-1 space into a 0-1-0 space. Scaled version of
* animForwardAndBackward().
* @param t Time in space to back and fourth on between 0 and 1.
* @returns Forward and backwards time.
*/
float animForwardAndBackwardScaled(float t);
/**
* Returns the time scale (speed to multiply time range by) for a given frame
* time and frame count. E.g. 3 frames at 0.5 time each would have a time scale
* of 1.5.
*
* @param frames Frames to get the scale of.
* @param time Time to get the scale of.
* @return The time scale.
*/
float animTimeScaleFromFrameTime(int32_t frames, float time);

View File

@ -0,0 +1,60 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "easing.h"
float easeLinear(float t) {
return t;
}
float easeInQuad(float t) {
return t * t;
}
float easeOutQuad(float t) {
return t * (2 - t);
}
float easeInOutQuad(float t) {
return t < .5 ? 2 * t * t : -1 + (4 - 2 * t) * t;
}
float easeInCubic(float t) {
return t * t * t;
}
float easeOutCubic(float t) {
return (t - 1) * (t - 1) * (t - 1) + 1;
}
float easeInOutCubic(float t) {
return t < .5 ? 4 * t * t * t : (t - 1) * (2 * t - 2) * (2 * t - 2) + 1;
}
float easeInQuart(float t) {
return t * t * t * t;
}
float easeOutQuart(float t) {
return 1 - (t-1)*(t-1)*(t-1)*(t-1);
}
float easeInOutQuart(float t) {
return t < .5 ? 8*t*t*t*t : 1-8*(t-1)*(t-1)*(t-1)*(t-1);
}
float easeInQuint(float t) {
return t*t*t*t*t;
}
float easeOutQuint(float t) {
return 1 + (t-1)*(t-1)*(t-1)*(t-1)*(t-1);
}
float easeInOutQuint(float t) {
return t<.5 ? 16*t*t*t*t*t : 1+16*(t-1)*(t-1)*(t-1)*(t-1)*(t-1);
}

View File

@ -0,0 +1,32 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
typedef float easefunction_t(float t);
/**
* Returns the ease time for a given real time duration span.
* @param start At what point in time the animation started
* @param current The current point in time the animation is at.
* @param duration The total duration on the animation.
* @returns The easing time (0-1 time) that the animation is at.
*/
#define easeTimeToEase(start, current, duration) ((current-start)/duration)
float easeLinear(float t);
float easeInQuad(float t);
float easeOutQuad(float t);
float easeInOutQuad(float t);
float easeInCubic(float t);
float easeOutCubic(float t);
float easeInOutCubic(float t);
float easeInQuart(float t);
float easeOutQuart(float t);
float easeInOutQuart(float t);
float easeInQuint(float t);
float easeOutQuint(float t);
float easeInOutQuint(float t);

View File

@ -0,0 +1,97 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "queue.h"
void queueInit(queue_t *queue) {
queue->timeline = 0;
queue->count = 0;
queue->current = ANIMATION_QUEUE_START;
}
queueaction_t * queueNext(queue_t *queue) {
queueaction_t *action;
// Is there a currently running action? If so, end it.
if(queue->current != ANIMATION_QUEUE_START) {
action = queue->items + queue->current;
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
}
// Prepare to go to next action if there is a next action.
queue->current++;
queue->actionStarted = queue->timeline;
if(queue->current >= queue->count) return NULL;
// Go to next action, start it.
action = queue->items + queue->current;
if(action->onStart != NULL) action->onStart(queue, action, queue->current);
return action;
}
queueaction_t * queueAdd(queue_t *queue) {
queueaction_t *action;
action = queue->items + queue->count;
action->index = queue->count;
action->data = NULL;
action->onStart = NULL;
action->onUpdate = NULL;
action->onEnd = NULL;
queue->count++;
return action;
}
void queueUpdate(queue_t *queue, float delta) {
queueaction_t *action;
queue->timeline += delta;
if(queue->current >= queue->count) return;
action = queue->items + queue->current;
if(action->onUpdate != NULL) {
action->onUpdate(queue, action, queue->current);
}
}
void queueDispose(queue_t *queue) {
queueaction_t *action;
if(queue->current >= queue->count) return;
action = queue->items + queue->current;
if(action->onEnd != NULL) action->onEnd(queue, action, queue->current);
}
void queueRestack(queue_t *queue) {
uint8_t i;
// Rewind the array.
arrayRewind(sizeof(queueaction_t), queue->items, ANIMATION_QUEUE_START,
queue->current, queue->count
);
// Now rewind the stack
queue->count -= queue->current;
queue->current = 0;
// Now fix indexes
for(i = 0; i < queue->count; i++) {
queue->items[i].index = i;
}
}
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i) {
float n = queue->timeline - queue->actionStarted;
if(n < queue->delays[i]) return;
queueNext(queue);
}
queueaction_t * queueDelay(queue_t *queue, float delay) {
queueaction_t *action = queueAdd(queue);
queue->delays[action->index] = delay;
action->onUpdate = &_queueDelayUpdate;
return action;
}

View File

@ -0,0 +1,113 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#pragma once
#include "../../libs.h"
#include "../../util/array.h"
#define ANIMATION_QUEUE_ITEM_MAX 128
#define ANIMATION_QUEUE_START 0xFF
typedef struct _queueaction_t queueaction_t;
typedef struct _queue_t queue_t;
/**
* Callback for queue events.
* @param conversation Conversation this text is attached to.
* @param text Text item that is being used in the callback
* @param i Index of the item in the queue.
*/
typedef void queuecallback_t(queue_t *queue, queueaction_t *action, uint8_t i);
typedef struct _queueaction_t {
/** Index that the action is within the queue */
uint8_t index;
/** Pointer to any custom user data */
void *data;
/** Callback to fire the moment the action is active in the queue */
queuecallback_t *onStart;
/** Callback to fire when this action has ended */
queuecallback_t *onEnd;
/** Callback to fire every update of this queue while action is active. */
queuecallback_t *onUpdate;
} queueaction_t;
typedef struct _queue_t {
/** Array of items within the queue. */
queueaction_t items[ANIMATION_QUEUE_ITEM_MAX];
uint8_t count;
/** Current index within the array of actions that is currently processing */
uint8_t current;
/** Internal timeline tracking */
float timeline;
/** Time that the current aciton started */
float actionStarted;
/** Delay Queue Item Storage */
float delays[ANIMATION_QUEUE_ITEM_MAX];
} queue_t;
/**
* Initialize the queue set.
* @param queue Queue to initialize.
*/
void queueInit(queue_t *queue);
/**
* Goes to the next action, or start the queue if this is the first action.
* @param queue Queue to skip to the next action.
* @returns Action that was just started.
*/
queueaction_t * queueNext(queue_t *queue);
/**
* Add a queue action to the queue.
* @param convo Queue to add to.
* @return Pointer to the queue action that was added.
*/
queueaction_t * queueAdd(queue_t *queue);
/**
* Updates the queue logic.
* @param convo Queue to update.
* @param delta Time delta to tick the queue by.
*/
void queueUpdate(queue_t *queue, float delta);
/**
* Dispose the queue when finished.
* @param queue Queue to dispose.
*/
void queueDispose(queue_t *queue);
/**
* Restacks the queue. The restack process essentially rewinds the queue so that
* the current items move back to position 0, and allows you to add more items
* to the queue again. Because the memory does shift, and the indexes do change
* a lot of your pointers will break, make sure you re-reference all your
* pointers.
*
* @param queue Queue to restack.
*/
void queueRestack(queue_t *queue);
/** Callbacks for Queue Delay Action */
void _queueDelayUpdate(queue_t *queue, queueaction_t *action, uint8_t i);
/**
* Adds a delay action to a queue.
* @param queue Queue to add to.
* @param delay Delay time (in seconds) to have.
* @return Pointer to the action added to the queue.
*/
queueaction_t * queueDelay(queue_t *queue, float delay);

View File

@ -0,0 +1,141 @@
/**
* Copyright (c) 2021 Dominic Masters
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
#include "timeline.h"
void _timelineActionDeltaUpdateStart(
timeline_t *timeline, timelineaction_t *action, uint8_t i
) {
if(action->data == NULL) return;
*((float *)action->data) = timeline->initials[i];
}
void _timelineActionDeltaUpdateDuration(
timeline_t *timeline, timelineaction_t *action, uint8_t i
) {
float n;
if(action->data == NULL) return;
n = timeline->easings[i](
timelineActionGetTimeRaw(timeline, action)
);
*((float *)action->data) = timeline->initials[i] + (timeline->deltas[i] * n);
}
void _timelineActionDeltaUpdateEnd(
timeline_t *timeline, timelineaction_t *action, uint8_t i
) {
if(action->data == NULL) return;
*((float *)action->data) = timeline->initials[i] + timeline->deltas[i];
}
void timelineInit(timeline_t *timeline) {
timeline->current = 0;
timeline->diff = 0;
timeline->previous = 0;
timeline->user = 0;
timeline->actionCount = 0;
}
void timelineUpdate(timeline_t *timeline, float delta) {
uint8_t i;
timelineaction_t *action;
float full;
timeline->diff = delta;
timeline->previous = timeline->current;
timeline->current = timeline->current + delta;
// Find all actions that would have started or ended in this timespan.
for(i = 0; i < timeline->actionCount; i++) {
action = timeline->actions +i;
// Has the action started yet?
if(action->start > timeline->current) continue;
// Did we start this frame?
if(action->start > timeline->previous && action->onStart != NULL) {
action->onStart(timeline, action, i);
}
// Durations of 0 only fire starts, never ends or durations.
if(action->duration == 0) continue;
// Is the end still in the future? Durations in negatives go forever
full = action->start+action->duration;
if(action->duration < 0 || full > timeline->current) {
if(action->onDuration != NULL) action->onDuration(timeline, action, i);
} else if(full > timeline->previous) {// Did we end this frame?
if(action->onEnd != NULL) action->onEnd(timeline, action, i);
if(action->loop) action->start = timeline->current;
}
}
}
bool timelineIsFinished(timeline_t *timeline) {
uint8_t i;
timelineaction_t *action;
for(i = 0; i < timeline->actionCount; i++) {
action = timeline->actions +i;
if(action->start > timeline->current) return false;
if(action->duration < 0) return false;
if(action->duration == 0) continue;
if(action->start+action->duration > timeline->current) return false;
}
return true;
}
timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
float duration
) {
timelineaction_t *action = timeline->actions + (timeline->actionCount++);
action->loop = false;
action->start = start, action->duration = duration;
action->onStart = action->onEnd = action->onDuration = NULL;
return action;
}
timelineaction_t * timelineAddDeltaAction(timeline_t *timeline,
float start, float duration, float initial, float delta,
easefunction_t *easing, float *destination
) {
timelineaction_t *action;
timeline->initials[timeline->actionCount] = initial;
timeline->deltas[timeline->actionCount] = delta;
timeline->easings[timeline->actionCount] = (
easing == NULL ? &easeLinear : easing
);
action = timelineAddAction(timeline, start, duration);
action->data = destination;
action->onStart = &_timelineActionDeltaUpdateStart;
action->onDuration = &_timelineActionDeltaUpdateDuration;
action->onEnd = &_timelineActionDeltaUpdateEnd;
return action;
}
timelineaction_t * timelineAddDeltaActionTo(timeline_t *timeline,
float start, float duration, float initial, float end,
easefunction_t *easing, float *destination
) {
return timelineAddDeltaAction(
timeline, start, duration, initial, end-initial, easing, destination
);
}
float timelineActionGetTimeRaw(timeline_t *timeline, timelineaction_t *action) {
return (timeline->current - action->start) / action->duration;
}
float timelineActionGetTime(timeline_t *tl, timelineaction_t *at) {
return mathClamp(timelineActionGetTimeRaw(tl, at), 0, 1);
}
void timelineClear(timeline_t *timeline) {
timeline->actionCount = 0;
}

View File

@ -0,0 +1,187 @@
// Copyright (c) 2021 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "../../libs.h"
#include "../../util/math.h"
#include "easing.h"
/** Maximum number of actions a timeline can support, smaller than 0xFF */
#define TIMELINE_ACTION_COUNT_MAX 128
/** Type forwarders */
typedef struct _timeline_t timeline_t;
typedef struct _timelineaction_t timelineaction_t;
/**
* Callback for when a timeline event occurs
* @param timeline The timeline that fired this callback.
* @param action The action that this callback is attached to.
* @param i The index that this action is within the timeline.
*/
typedef void timelinecallback_t(timeline_t *timeline, timelineaction_t *action,
uint8_t i
);
typedef struct _timelineaction_t {
/** Pointer to any custom user data the timeline action wants to use. */
void *data;
/**
* The time that this action should occur within the timeline
* set to 0 or less to start immediately.
*/
float start;
/**
* The duration of the action, this will decide for how long onDuration will
* be called for, and when onEnd should be called.
* Set to a negative number to have this continue forever.
* Set to 0 to only fire the onStart.
* onStart can fire with either onDuration and onEnd on the same frame, but
* onEnd and onDuration cannot fire the same frame.
*/
float duration;
/**
* Enables animation looping. This works by forcing start to be equal to the
* current time at the point in time that onEnd is called. This will also stop
* onStart being called so ensure that your onStart and onEnd logic works.
*/
bool loop;
timelinecallback_t *onStart;
timelinecallback_t *onDuration;
timelinecallback_t *onEnd;
} timelineaction_t;
typedef struct _timeline_t {
/** The current time as far as the timeline is concerned */
float current;
/** The time of the last "frame" as far as the timeline is concerned */
float previous;
/** The frame time diff, essentially current = previous + diff */
float diff;
/** User pointer, allows you to point to some other data */
void *user;
/** Actions within the timeline */
timelineaction_t actions[TIMELINE_ACTION_COUNT_MAX];
/** For delta actions, storage of the initial values */
float initials[TIMELINE_ACTION_COUNT_MAX];
/** For delta actions, storage of their deltas. */
float deltas[TIMELINE_ACTION_COUNT_MAX];
/** Easing functions to use for delta functions */
easefunction_t *easings[TIMELINE_ACTION_COUNT_MAX];
uint8_t actionCount;
} timeline_t;
/**
* Initializes a timeline back to its default state.
*
* @param timeline Timeline to initialize.
*/
void timelineInit(timeline_t *timeline);
/**
* Ticks the timeline. This can be done using any delta.
*
* @param timeline Timeline to tick
* @param delta Delta to tick.
*/
void timelineUpdate(timeline_t *timeline, float delta);
/**
* Returns true if every action in the timeline has finished.
*
* @param timeline Timeline to check
* @return True if finished, otherwise false.
*/
bool timelineIsFinished(timeline_t *timeline);
/**
* Adds an action to the timeline. This will initialize the action callbacks to
* NULL, you will need to initialize your own callbacks.
*
* @param timeline Timeline to add to.
* @param start Start time.
* @param duration Duration time
* @return Pointer to the timeline action or NULL if the list is full.
*/
timelineaction_t * timelineAddAction(timeline_t *timeline, float start,
float duration
);
/**
* Add a special action kind that can treat delta style animations. These are
* animations that have a start, and an end value, that will be tracked for you
* and keep you up to date.
*
* @param timeline Timeline to add to.
* @param start Starting time.
* @param duration Animation duration.
* @param initial Initial value for the animation.
* @param delta Delta value for the animation.
* @param easing Pointer to an easing function to use, set to NULL for linear.
* @param destination A constant floating point to update.
* @return The queued timeline action.
*/
timelineaction_t * timelineAddDeltaAction(timeline_t *timeline,
float start, float duration, float initial, float delta,
easefunction_t *easing, float *destination
);
/**
* Shorthand for timelineAddDeltaAction that will calculate the delta based on
* and end position.
*
* @param timeline Timeline to add to.
* @param start Starting time.
* @param duration Animation duration.
* @param initial Initial value for the animation.
* @param end End value for the animation.
* @param easing Pointer to an easing function to use, set to NULL for linear.
* @param destination A constant floating point to update.
* @return The queued timeline action.
*/
timelineaction_t * timelineAddDeltaActionTo(timeline_t *timeline,
float start, float duration, float initial, float end,
easefunction_t *easing, float *destination
);
/**
* Gets the timeline action's animation time. This is a representation of
* 0 - 1 where 0 means the animation is at the start, and 1 meaning the
* animation is at full completion. This is not clamped and may exceed 1.
*
* @param timeline Timeline the action belongs to.
* @param action Action itself.
* @return 0 - 1+, where 0 = start, 1 = duration, >1 is time since duration.
*/
float timelineActionGetTimeRaw(timeline_t *timeline, timelineaction_t *action);
/**
* Gets the timeline action's animation time between 0 - 1, clamped.
*
* @param tl Timeline to get delta from.
* @param at Timeline action.
* @return 0 - 1, where 0 = start, 1 = duration.
*/
float timelineActionGetTime(timeline_t *tl, timelineaction_t *at);
/**
* Removes all actions from the timeline.
*
* @param timeline Timeline to clear.
*/
void timelineClear(timeline_t *timeline);