TWP: Split easing functions from camera

This commit is contained in:
scemino 2024-04-19 20:31:13 +02:00
parent c4e6a8f7a0
commit 261621e54c
5 changed files with 133 additions and 72 deletions

View File

@ -114,16 +114,4 @@ void Camera::update(Common::SharedPtr<Room> room, Common::SharedPtr<Object> foll
}
}
InterpolationMethod intToInterpolationMethod(int value) {
bool loop = (value & 0x10);
bool swing = (value & 0x20);
bool stopLooping = (value & 0x40);
InterpolationKind kind = (InterpolationKind)(value & 0x0F);
InterpolationMethod im;
im.kind = kind;
im.loop = loop && !stopLooping;
im.swing = swing;
return im;
}
} // namespace Twp

View File

@ -24,6 +24,7 @@
#include "common/func.h"
#include "math/vector2d.h"
#include "twp/easing.h"
#include "twp/rectf.h"
namespace Twp {
@ -31,66 +32,6 @@ namespace Twp {
class Object;
class Room;
typedef float EasingFunc(float t);
typedef struct EasingFunc_t {
EasingFunc *func;
} EasingFunc_t;
enum InterpolationKind {
IK_LINEAR = 0,
IK_EASEIN = 1,
IK_EASEINOUT = 2,
IK_EASEOUT = 3,
IK_SLOWEASEIN = 4,
IK_SLOWEASEOUT = 5
};
struct InterpolationMethod {
InterpolationKind kind = IK_LINEAR;
bool loop = false;
bool swing = false;
};
InterpolationMethod intToInterpolationMethod(int value);
static float linear(float t) { return t; }
static float easeIn(float t) {
return t * t * t * t;
}
static float easeOut(float t) {
float f = (t - 1.0f);
return f * f * f * (1.0f - t) + 1.0f;
}
static float easeInOut(float t) {
if (t < 0.5f)
return 8.0f * t * t * t * t;
float f = (t - 1.0f);
return -8.f * f * f * f * f + 1.f;
}
inline EasingFunc_t easing(InterpolationKind kind) {
switch (kind) {
case IK_LINEAR:
return {&linear};
case IK_EASEIN:
return {&easeIn};
case IK_EASEINOUT:
return {&easeInOut};
case IK_EASEOUT:
return {&easeOut};
case IK_SLOWEASEIN:
return {&easeIn};
case IK_SLOWEASEOUT:
return {&easeOut};
}
error("Invalid interpolation kind: %d", kind);
return {&linear};
}
class Camera {
public:
void setAt(const Math::Vector2d &at);

38
engines/twp/easing.cpp Normal file
View File

@ -0,0 +1,38 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "twp/easing.h"
namespace Twp {
InterpolationMethod intToInterpolationMethod(int value) {
bool loop = (value & 0x10);
bool swing = (value & 0x20);
bool stopLooping = (value & 0x40);
InterpolationKind kind = (InterpolationKind)(value & 0x0F);
InterpolationMethod im;
im.kind = kind;
im.loop = loop && !stopLooping;
im.swing = swing;
return im;
}
} // namespace Twp

93
engines/twp/easing.h Normal file
View File

@ -0,0 +1,93 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef TWP_EASING_H
#define TWP_EASING_H
#include "common/func.h"
#include "math/vector2d.h"
#include "twp/rectf.h"
namespace Twp {
typedef float EasingFunc(float t);
typedef struct EasingFunc_t {
EasingFunc *func;
} EasingFunc_t;
enum InterpolationKind {
IK_LINEAR = 0,
IK_EASEIN = 1,
IK_EASEINOUT = 2,
IK_EASEOUT = 3,
IK_SLOWEASEIN = 4,
IK_SLOWEASEOUT = 5
};
struct InterpolationMethod {
InterpolationKind kind = IK_LINEAR;
bool loop = false;
bool swing = false;
};
InterpolationMethod intToInterpolationMethod(int value);
static float linear(float t) { return t; }
static float easeIn(float t) {
return t * t * t * t;
}
static float easeOut(float t) {
float f = (t - 1.0f);
return f * f * f * (1.0f - t) + 1.0f;
}
static float easeInOut(float t) {
if (t < 0.5f)
return 8.0f * t * t * t * t;
float f = (t - 1.0f);
return -8.f * f * f * f * f + 1.f;
}
inline EasingFunc_t easing(InterpolationKind kind) {
switch (kind) {
case IK_LINEAR:
return {&linear};
case IK_EASEIN:
return {&easeIn};
case IK_EASEINOUT:
return {&easeInOut};
case IK_EASEOUT:
return {&easeOut};
case IK_SLOWEASEIN:
return {&easeIn};
case IK_SLOWEASEOUT:
return {&easeOut};
}
error("Invalid interpolation kind: %d", kind);
return {&linear};
}
} // namespace Twp
#endif

View File

@ -9,6 +9,7 @@ MODULE_OBJS = \
console.o \
dialog.o \
dialogs.o \
easing.o \
enginedialogtarget.o \
font.o \
genlib.o \