Basic animations

This commit is contained in:
2022-12-04 15:36:40 -08:00
parent ba9881e39d
commit b5d7c927c5
15 changed files with 314 additions and 28 deletions

View File

@ -11,5 +11,6 @@ target_sources(${DAWN_TARGET_NAME}
)
# Subdirs
add_subdirectory(animation)
add_subdirectory(font)
add_subdirectory(mesh)

View File

@ -0,0 +1,173 @@
// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "event/Event.hpp"
#include "Easing.hpp"
#include "util/mathutils.hpp"
namespace Dawn {
template<typename T>
struct Keyframe {
float_t time;
T value;
};
template<typename T>
struct TimelineItem {
T *modifies;
std::vector<struct Keyframe<T>> keyframes;
};
template<typename T>
struct Animation {
public:
bool_t loop = false;
bool_t finished = false;
float_t time = 0;
float_t duration = 0;
easefunction_t *easing = &easeLinear;
std::vector<struct TimelineItem<T>> timelineItems;
Event<> eventAnimationEnd;
/**
* Get an existing timeline item based on the value that will be modified.
*
* @param modifies Value that is intended to be modified for the timeline.
* @return The existing timeline item OR NULL if not found.
*/
struct TimelineItem<T> * getTimelineItem(T *modifies) {
assertNotNull(modifies);
auto it = this->timelineItems.begin();
while(it != this->timelineItems.end()) {
if(it->modifies == modifies) return &(*it);
++it;
}
return nullptr;
}
/**
* Add a timeline item to an animation.
*
* @param modifies Value that will be modified for the timeline item.
* @return The timeline item for that modified value.
*/
struct TimelineItem<T> * addTimelineItem(T *modifies) {
assertNotNull(modifies);
struct TimelineItem<T> item;
item.modifies = modifies;
this->timelineItems.push_back(item);
return &this->timelineItems.back();
}
/**
* Add a keyframe to the animation.
*
* @param modifies Pointer to the value that will be modified.
* @param time Time that the animation will occur at (gametime seconds).
* @param value Value for this keyframe
* @return The keyframe that was added.
*/
struct Keyframe<T> * addKeyframe(T *modifies, float_t time, T value) {
auto item = this->getTimelineItem(modifies);
if(item == nullptr) item = this->addTimelineItem(modifies);
struct Keyframe<T> keyframe;
keyframe.time = time;
keyframe.value = value;
this->duration = mathMax<float_t>(this->duration, time);
this->finished = false;
item->keyframes.push_back(keyframe);
return &item->keyframes.back();
}
/**
* Tick/Update the animation.
*
* @param delta Delta (in seconds) to update the animation by.
*/
void tick(float_t delta) {
if(this->finished) return;
float_t newTime = this->time + delta;
auto it = this->timelineItems.begin();
while(it != this->timelineItems.end()) {
struct Keyframe<T> *keyframeNext = nullptr;
struct Keyframe<T> *keyframeCurrent = nullptr;
// For each keyframe
auto itKey = it->keyframes.begin();
while(itKey != it->keyframes.end()) {
if(itKey->time > newTime) {
keyframeNext = &(*itKey);
break;
}
keyframeCurrent = &(*itKey);
++itKey;
}
// Skip when no keyframe
float_t oldTime;
T oldValue;
if(keyframeCurrent == nullptr) {
if(keyframeNext == nullptr) continue;
oldTime = this->time;
oldValue = *it->modifies;
} else if(keyframeNext == nullptr) {
*it->modifies = keyframeCurrent->value;
++it;
continue;
} else {
oldValue = keyframeCurrent->value;
oldTime = keyframeCurrent->time;
}
// Slerp between keyframes
float_t keyframeDelta = this->easing(
(newTime - oldTime) / (keyframeNext->time - oldTime)
);
*it->modifies = oldValue + (
(keyframeNext->value - oldValue) * keyframeDelta
);
++it;
}
// Update time.
this->time = newTime;
// Has the animation finished?
if(newTime < this->duration) return;
// Do we need to loop?
if(this->loop) {
this->time = 0;
return;
}
// Animation end.
this->finished = true;
this->eventAnimationEnd.invoke();
}
void restart() {
this->time = 0;
this->finished = false;
}
/**
* Clears an animaton of all its animation items and keyframes.
*/
void clear() {
this->timelineItems.clear();
this->duration = 0;
}
};
}

View File

@ -0,0 +1,10 @@
# Copyright (c) 2022 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Sources
# target_sources(${DAWN_TARGET_NAME}
# PRIVATE
# Animation.cpp
# )

View File

@ -7,6 +7,7 @@
#pragma once
#include "dawnlibs.hpp"
typedef float_t easefunction_t(float_t t);
/**