This commit is contained in:
blueloveTH 2023-05-12 14:30:39 +08:00
parent a7110a3f63
commit 9e72433cc7
4 changed files with 292 additions and 1 deletions

View File

@ -9,7 +9,7 @@ pipeline = [
["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], ["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
["obj.h", "codeobject.h", "frame.h"], ["obj.h", "codeobject.h", "frame.h"],
["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"], ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
["_generated.h", "iter.h", "base64.h", "cffi.h", "linalg.h", "requests.h", "io.h", "pocketpy.h"] ["_generated.h", "iter.h", "base64.h", "cffi.h", "linalg.h", "easing.h", "requests.h", "io.h", "pocketpy.h"]
] ]
copied = set() copied = set()
@ -68,6 +68,7 @@ print("amalgamated/pocketpy.h")
# my custom things... # my custom things...
if os.path.exists("/mnt/e/PainterEngine/project/pocketpy.h"): if os.path.exists("/mnt/e/PainterEngine/project/pocketpy.h"):
shutil.copy("amalgamated/pocketpy.h", "/mnt/e/PainterEngine/project/pocketpy.h") shutil.copy("amalgamated/pocketpy.h", "/mnt/e/PainterEngine/project/pocketpy.h")
shutil.copy("src/easing.pyi", "/mnt/e/PainterEngine/game/easing.pyi")
shutil.copy("src/linalg.pyi", "/mnt/e/PainterEngine/game/linalg.pyi") shutil.copy("src/linalg.pyi", "/mnt/e/PainterEngine/game/linalg.pyi")

257
src/easing.h Normal file
View File

@ -0,0 +1,257 @@
#pragma once
#include <cmath>
#include "cffi.h"
namespace pkpy{
// https://easings.net/
static const double PI = 3.1415926545;
inline static double easeLinear( double x ) {
return x;
}
inline static double easeInSine( double x ) {
return 1.0 - std::cos( x * PI / 2 );
}
inline static double easeOutSine( double x ) {
return std::sin( x * PI / 2 );
}
inline static double easeInOutSine( double x ) {
return -( std::cos( PI * x ) - 1 ) / 2;
}
inline static double easeInQuad( double x ) {
return x * x;
}
inline static double easeOutQuad( double x ) {
return 1 - std::pow( 1 - x, 2 );
}
inline static double easeInOutQuad( double x ) {
if( x < 0.5 ) {
return 2 * x * x;
} else {
return 1 - std::pow( -2 * x + 2, 2 ) / 2;
}
}
inline static double easeInCubic( double x ) {
return x * x * x;
}
inline static double easeOutCubic( double x ) {
return 1 - std::pow( 1 - x, 3 );
}
inline static double easeInOutCubic( double x ) {
if( x < 0.5 ) {
return 4 * x * x * x;
} else {
return 1 - std::pow( -2 * x + 2, 3 ) / 2;
}
}
inline static double easeInQuart( double x ) {
return std::pow( x, 4 );
}
inline static double easeOutQuart( double x ) {
return 1 - std::pow( 1 - x, 4 );
}
inline static double easeInOutQuart( double x ) {
if( x < 0.5 ) {
return 8 * std::pow( x, 4 );
} else {
return 1 - std::pow( -2 * x + 2, 4 ) / 2;
}
}
inline static double easeInQuint( double x ) {
return std::pow( x, 5 );
}
inline static double easeOutQuint( double x ) {
return 1 - std::pow( 1 - x, 5 );
}
inline static double easeInOutQuint( double x ) {
if( x < 0.5 ) {
return 16 * std::pow( x, 5 );
} else {
return 1 - std::pow( -2 * x + 2, 5 ) / 2;
}
}
inline static double easeInExpo( double x ) {
return x == 0 ? 0 : std::pow( 2, 10 * x - 10 );
}
inline static double easeOutExpo( double x ) {
return x == 1 ? 1 : 1 - std::pow( 2, -10 * x );
}
inline double easeInOutExpo( double x ) {
if( x == 0 ) {
return 0;
} else if( x == 1 ) {
return 1;
} else if( x < 0.5 ) {
return std::pow( 2, 20 * x - 10 ) / 2;
} else {
return (2 - std::pow( 2, -20 * x + 10 )) / 2;
}
}
inline static double easeInCirc( double x ) {
return 1 - std::sqrt( 1 - std::pow( x, 2 ) );
}
inline static double easeOutCirc( double x ) {
return std::sqrt( 1 - std::pow( x - 1, 2 ) );
}
inline static double easeInOutCirc( double x ) {
if( x < 0.5 ) {
return (1 - std::sqrt( 1 - std::pow( 2 * x, 2 ) )) / 2;
} else {
return (std::sqrt( 1 - std::pow( -2 * x + 2, 2 ) ) + 1) / 2;
}
}
inline static double easeInBack( double x ) {
const double c1 = 1.70158;
const double c3 = c1 + 1;
return c3 * x * x * x - c1 * x * x;
}
inline static double easeOutBack( double x ) {
const double c1 = 1.70158;
const double c3 = c1 + 1;
return 1 + c3 * std::pow( x - 1, 3 ) + c1 * std::pow( x - 1, 2 );
}
inline static double easeInOutBack( double x ) {
const double c1 = 1.70158;
const double c2 = c1 * 1.525;
if( x < 0.5 ) {
return (std::pow( 2 * x, 2 ) * ((c2 + 1) * 2 * x - c2)) / 2;
} else {
return (std::pow( 2 * x - 2, 2 ) * ((c2 + 1) * (x * 2 - 2) + c2) + 2) / 2;
}
}
inline static double easeInElastic( double x ) {
const double c4 = (2 * PI) / 3;
if( x == 0 ) {
return 0;
} else if( x == 1 ) {
return 1;
} else {
return -std::pow( 2, 10 * x - 10 ) * std::sin( (x * 10 - 10.75) * c4 );
}
}
inline static double easeOutElastic( double x ) {
const double c4 = (2 * PI) / 3;
if( x == 0 ) {
return 0;
} else if( x == 1 ) {
return 1;
} else {
return std::pow( 2, -10 * x ) * std::sin( (x * 10 - 0.75) * c4 ) + 1;
}
}
inline double easeInOutElastic( double x ) {
const double c5 = (2 * PI) / 4.5;
if( x == 0 ) {
return 0;
} else if( x == 1 ) {
return 1;
} else if( x < 0.5 ) {
return -(std::pow( 2, 20 * x - 10 ) * std::sin( (20 * x - 11.125) * c5 )) / 2;
} else {
return (std::pow( 2, -20 * x + 10 ) * std::sin( (20 * x - 11.125) * c5 )) / 2 + 1;
}
}
inline static double easeOutBounce( double x ) {
const double n1 = 7.5625;
const double d1 = 2.75;
if( x < 1 / d1 ) {
return n1 * x * x;
} else if( x < 2 / d1 ) {
x -= 1.5 / d1;
return n1 * x * x + 0.75;
} else if( x < 2.5 / d1 ) {
x -= 2.25 / d1;
return n1 * x * x + 0.9375;
} else {
x -= 2.625 / d1;
return n1 * x * x + 0.984375;
}
}
inline double easeInBounce( double x ) {
return 1 - easeOutBounce(1 - x);
}
inline static double easeInOutBounce( double x ) {
return x < 0.5
? (1 - easeOutBounce(1 - 2 * x)) / 2
: (1 + easeOutBounce(2 * x - 1)) / 2;
}
inline void add_module_easing(VM* vm){
PyObject* mod = vm->new_module("easing");
#define EASE(name) \
vm->bind_func<1>(mod, "Ease"#name, [](VM* vm, ArgsView args){ \
f64 t = CAST(f64, args[0]); \
return VAR(ease##name(t)); \
});
EASE(Linear)
EASE(InSine)
EASE(OutSine)
EASE(InOutSine)
EASE(InQuad)
EASE(OutQuad)
EASE(InOutQuad)
EASE(InCubic)
EASE(OutCubic)
EASE(InOutCubic)
EASE(InQuart)
EASE(OutQuart)
EASE(InOutQuart)
EASE(InQuint)
EASE(OutQuint)
EASE(InOutQuint)
EASE(InExpo)
EASE(OutExpo)
EASE(InOutExpo)
EASE(InCirc)
EASE(OutCirc)
EASE(InOutCirc)
EASE(InBack)
EASE(OutBack)
EASE(InOutBack)
EASE(InElastic)
EASE(OutElastic)
EASE(InOutElastic)
EASE(InBounce)
EASE(OutBounce)
EASE(InOutBounce)
#undef EASE
}
} // namespace pkpy

31
src/easing.pyi Normal file
View File

@ -0,0 +1,31 @@
def EaseLinear(x: float) -> float: ...
def EaseInSine(x: float) -> float: ...
def EaseOutSine(x: float) -> float: ...
def EaseInOutSine(x: float) -> float: ...
def EaseInQuad(x: float) -> float: ...
def EaseOutQuad(x: float) -> float: ...
def EaseInOutQuad(x: float) -> float: ...
def EaseInCubic(x: float) -> float: ...
def EaseOutCubic(x: float) -> float: ...
def EaseInOutCubic(x: float) -> float: ...
def EaseInQuart(x: float) -> float: ...
def EaseOutQuart(x: float) -> float: ...
def EaseInOutQuart(x: float) -> float: ...
def EaseInQuint(x: float) -> float: ...
def EaseOutQuint(x: float) -> float: ...
def EaseInOutQuint(x: float) -> float: ...
def EaseInExpo(x: float) -> float: ...
def EaseOutExpo(x: float) -> float: ...
def EaseInOutExpo(x: float) -> float: ...
def EaseInCirc(x: float) -> float: ...
def EaseOutCirc(x: float) -> float: ...
def EaseInOutCirc(x: float) -> float: ...
def EaseInBack(x: float) -> float: ...
def EaseOutBack(x: float) -> float: ...
def EaseInOutBack(x: float) -> float: ...
def EaseInElastic(x: float) -> float: ...
def EaseOutElastic(x: float) -> float: ...
def EaseInOutElastic(x: float) -> float: ...
def EaseInBounce(x: float) -> float: ...
def EaseOutBounce(x: float) -> float: ...
def EaseInOutBounce(x: float) -> float: ...

View File

@ -8,6 +8,7 @@
#include "base64.h" #include "base64.h"
#include "cffi.h" #include "cffi.h"
#include "linalg.h" #include "linalg.h"
#include "easing.h"
#include "requests.h" #include "requests.h"
#include "io.h" #include "io.h"
#include "_generated.h" #include "_generated.h"
@ -1054,6 +1055,7 @@ inline void VM::post_init(){
} }
add_module_linalg(this); add_module_linalg(this);
add_module_easing(this);
#endif #endif
} }