-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtil.h
124 lines (105 loc) · 3.04 KB
/
Util.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#pragma once
#include "GameCode/Animation.h"
#include "GameCode/MetalSlug.h"
#include<iostream>
#include<sstream>
#include<string>
namespace Util {
const class MessageFormater {
private:
template<typename T>
static void appendToStream(std::ostringstream& oss, const T& arg) {
oss << arg;
}
template<typename T, typename... Args>
static void appendToStream(std::ostringstream& oss, const T& arg, const Args&... args) {
oss << arg;
appendToStream(oss, args...);
}
public:
template<typename... Args>
static std::string print(const Args&... args) {
std::ostringstream oss;
appendToStream(oss, args...);
return oss.str();
}
};
const class Generator {
public:
static MetalSlug::Rect generatePixelRectFromCenter(float centerX, float centerY, float width, float height) {
int halfWidth = width / 2;
int halfHeight = height / 2;
MetalSlug::Rect rect = {centerX - halfWidth, centerY - halfHeight, width, height};
return rect;
}
};
const class AnimationUtil {
public:
static void initAnimationMetaData(MetalSlug::AnimationMetaData &metaData, MetalSlug::PlatformSpecificImage *spriteSheet, float animDelay, int rows, int columns, const MetalSlug::Vec2f &relativeCorner, const MetalSlug::Vec2f &framePixelSize) {
metaData.animDelay = animDelay;
metaData.spriteSheet = spriteSheet;
metaData.rows = rows;
metaData.columns = columns;
metaData.relativeCorner = relativeCorner;
metaData.framePixelSize = framePixelSize;
float h = (metaData.framePixelSize.y/224.0f)*2;
float w = h*(metaData.framePixelSize.x/metaData.framePixelSize.y);
MetalSlug::Rect animRect = {0, 0, fabs(w), fabs(h)};
metaData.rect = animRect;
}
};
const class LevelUtil {
public:
static MetalSlug::Rect convertLevelColliderBlockPixelRectToGameRect(const MetalSlug::Rect& pixelRect, int backgroundPixelWidth, int backgroundPixelHeight) {
// NOTE: i have no idea what's going in this function, stay awayy
float tx = backgroundPixelWidth/304.0f;
float ty = backgroundPixelHeight/224.0f;
float magicX = 112.0117f;
float w = pixelRect.width / magicX;
float h = pixelRect.height / 112.0f;
float x = (pixelRect.x / magicX) - 1.357f*tx + w / 2.0f;
float y = ty - (2 * ty * pixelRect.y / backgroundPixelHeight) - h / 2.0f;
return { x, y, w, h };
}
};
const class Math {
public:
// Clamp function for float values
static float clampf(float value, float min, float max) {
if (value < min) {
return min;
}
else if (value > max) {
return max;
}
else {
return value;
}
}
static float normalizef(float value, float range) {
// TODO: might need more checking before actually doing anything
return value / range;
}
// Clamp function for int values
static int clampi(int value, int min, int max) {
if (value < min) {
return min;
}
else if (value > max) {
return max;
}
else {
return value;
}
}
static float lerp(float start, float end, float t) {
return start + t * (end - start);
}
static float upCurve(float t) {
return -powf((t - 1), 2) + 1;
}
static float downCurve(float t) {
return -powf(t, 2) + 1;
}
};
}