Cavestory Mod API
CSMAPI_math.h
Go to the documentation of this file.
1 /*
2  Cavestory Multiplayer API
3  Copyright (C) 2021 Johnny Ledger
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 
26 #pragma once
27 
28 #include <math.h>
29 
30 // Smooth moving
31 
32 #define GET_INT_NORMAL(val, max) (float(val) / float(max))
33 #define GET_CENTER_OFFSET(w1, w2) ((MAX((w1), (w2)) / 2) - (MIN((w1), (w2)) / 2))
34 #define GET_CENTER_OFFSET_FIXED(w1, w2) (((w1) / 2) - ((w2) / 2))
35 #define degToRad(angleInDegrees) ((angleInDegrees) * M_PI / 180.0)
36 #define radToDeg(angleInRadians) ((angleInRadians) * 180.0 / M_PI)
37 #define M_PI_F 3.141592653589793f
38 #define SCROLL_BTN_AMOUNT 0.1f
39 
40 static inline int __ease_linear(float t, float b, float c, float d)
41 {
42  return int(c * t / d + b);
43 }
44 
45 static inline int __ease_quad(float t, float b, float c, float d)
46 {
47  t /= d / 2.f;
48  if (t < 1.0) return int(c / 2.f * t * t + b);
49  t--;
50  return int(-c / 2.f * (t * (t - 2.f) - 1.f) + b);
51 }
52 
53 static inline int __ease_sine(float t, float b, float c, float d)
54 {
55  return int(-c / 2.f * (cosf(M_PI_F * t / d) - 1.f) + b);
56 }
57 
58 static inline int __ease_expo(float t, float b, float c, float d)
59 {
60  t /= d / 2.f;
61  if (t < 1.0) return int(c / 2.f * powf(2.f, 10.f * (t - 1.f)) + b);
62  t--;
63  return int(c / 2.f * (-powf(2.f, -10.f * t) + 2.f) + b);
64 }
65 
66 static inline int __ease_circ(float t, float b, float c, float d)
67 {
68  t /= d / 2.f;
69  if (t < 1.0) return int(-c / 2.f * (sqrtf(1.f - t * t) - 1.f) + b);
70  t -= 2.f;
71  return int(c / 2.f * (sqrtf(1.f - t * t) + 1.f) + b);
72 }
73 
74 static inline int __ease_cube(float t, float b, float c, float d)
75 {
76  t /= d / 2.f;
77  if (t < 1.0) return int(c / 2.f * t * t * t + b);
78  t -= 2.f;
79  return int(c / 2.f * (t * t * t + 2.f) + b);
80 }
81 
82 static inline int __ease_quart(float t, float b, float c, float d)
83 {
84  t /= d / 2.f;
85  if (t < 1.0) return int(c / 2.f * t * t * t * t + b);
86  t -= 2.f;
87  return int(-c / 2.f * (t * t * t * t - 2.f) + b);
88 }
89 
90 #define __MOVE_FUNC(func_name, time_start, start, end, delay) (start + __ease_##func_name(float(CLAMP((GetTicks() - time_start), 0, delay)), 0.f, float(-(start - end)), float(delay)))
91 #define __MOVE_FUNC2(func_name, time_start, start, end, delay) (start + __ease_##func_name(float((GetTicks() - time_start) % delay), 0.f, float(-(start - end)), float(delay)))
92 #define MOVE_LINEAR(time_start, start, end, delay) __MOVE_FUNC(linear, time_start, start, end, delay)
93 #define MOVE_QUAD(time_start, start, end, delay) __MOVE_FUNC(quad, time_start, start, end, delay)
94 #define MOVE_SINE(time_start, start, end, delay) __MOVE_FUNC(sine, time_start, start, end, delay)
95 #define MOVE_EXPO(time_start, start, end, delay) __MOVE_FUNC(expo, time_start, start, end, delay)
96 #define MOVE_CIRC(time_start, start, end, delay) __MOVE_FUNC(circ, time_start, start, end, delay)
97 #define MOVE_CUBE(time_start, start, end, delay) __MOVE_FUNC(cube, time_start, start, end, delay)
98 #define MOVE_CUBE2(time_start, start, end, delay) __MOVE_FUNC2(cube, time_start, start, end, delay)
99 #define MOVE_QUART(time_start, start, end, delay) __MOVE_FUNC(quart, time_start, start, end, delay)