-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathscreeneffect.cpp
More file actions
362 lines (282 loc) · 9.82 KB
/
screeneffect.cpp
File metadata and controls
362 lines (282 loc) · 9.82 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include "prism/screeneffect.h"
#include "prism/file.h"
#include "prism/timer.h"
#include "prism/memoryhandler.h"
#include "prism/physicshandler.h"
#include "prism/system.h"
#include "prism/texture.h"
#include "prism/system.h"
#include "prism/log.h"
#include "prism/math.h"
#include "prism/stlutil.h"
#ifdef _WIN32
#include <imgui/imgui.h>
#include "prism/windows/debugimgui_win.h"
#endif
using namespace std;
namespace prism {
struct FadeInStruct;
typedef int(*IsScreenEffectOverFunction)(struct FadeInStruct*);
typedef struct FadeInStruct {
AnimationHandlerElement** mAnimationElements;
int mAnimationAmount;
PhysicsHandlerElement* mPhysicsElement;
Vector3D* mSize;
PhysicsHandlerElement* mAlphaPhysicsElement;
double* mAlpha;
Duration mDuration;
ScreenEffectFinishedCB mCB;
void* mCaller;
IsScreenEffectOverFunction mIsOverFunction;
} FadeIn;
typedef struct {
double mR;
double mG;
double mB;
} FadeColor;
using namespace std;
static struct {
TextureData mWhiteTexture;
int mIsActive;
double mZ;
int mFullLineSize;
AnimationHandlerElement* mScreenFillElement;
FadeColor mFadeColor;
map<int, FadeIn> mFadeIns;
} gScreenEffect;
#ifdef _WIN32
static void imguiFadeIns() {
if (ImGui::TreeNode("Fade Ins"))
{
for (auto& e : gScreenEffect.mFadeIns)
{
ImGui::Text("Duration: %d", e.second.mDuration);
ImGui::Text("Size: %f, %f", e.second.mSize->x, e.second.mSize->y);
ImGui::Text("Alpha: %f", *e.second.mAlpha);
ImGui::Text("Amount: %d", e.second.mAnimationAmount);
ImGui::Text("Is Over: %d", e.second.mIsOverFunction(&e.second));
}
ImGui::TreePop();
}
}
static void imguiScreenEffectsData()
{
ImGui::Text("Active: %d", gScreenEffect.mIsActive);
ImGui::Text("Z: %f", gScreenEffect.mZ);
ImGui::Text("Full Line Size: %d", gScreenEffect.mFullLineSize);
ImGui::Text("Fade Color: %f, %f, %f", gScreenEffect.mFadeColor.mR, gScreenEffect.mFadeColor.mG, gScreenEffect.mFadeColor.mB);
imguiFadeIns();
}
void imguiScreenEffects() {
static bool isWindowShown = false;
imguiPrismAddTab("Prism", "ScreenEffects", &isWindowShown);
if (isWindowShown)
{
ImGui::Begin("ScreenEffects", &isWindowShown);
imguiScreenEffectsData();
ImGui::End();
}
}
#endif
void initScreenEffects() {
gScreenEffect.mWhiteTexture = createWhiteTexture();
gScreenEffect.mFullLineSize = 10;
gScreenEffect.mZ = 80;
gScreenEffect.mScreenFillElement = NULL;
gScreenEffect.mFadeColor.mR = gScreenEffect.mFadeColor.mG = gScreenEffect.mFadeColor.mB = 0;
gScreenEffect.mIsActive = 1;
}
void shutdownScreenEffects() {
if (!gScreenEffect.mIsActive) return;
unloadTexture(gScreenEffect.mWhiteTexture);
gScreenEffect.mIsActive = 0;
}
static void loadScreenEffectHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
gScreenEffect.mFadeIns.clear();
}
static void unloadScreenEffectHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
gScreenEffect.mFadeIns.clear();
}
static void unloadedBehaviour(Duration tDuration, ScreenEffectFinishedCB tOptionalCB, void* tCaller) {
if (tOptionalCB != NULL) {
addTimerCB(tDuration, tOptionalCB, tCaller);
}
}
static int isVerticalLineFadeInOver(FadeIn* tFadeIn) {
return tFadeIn->mSize->y <= 0;
}
static void removeFadeIn(FadeIn* e) {
removeFromPhysicsHandler(e->mPhysicsElement);
removeFromPhysicsHandler(e->mAlphaPhysicsElement);
int i;
for (i = 0; i < e->mAnimationAmount; i++) {
removeHandledAnimation(e->mAnimationElements[i]);
}
freeMemory(e->mAnimationElements);
}
static void updateSingleFadeInAnimation(FadeIn* e, int i) {
setAnimationSize(e->mAnimationElements[i], *e->mSize, Vector3D(0, 0, 0));
setAnimationTransparency(e->mAnimationElements[i], *e->mAlpha);
}
static int updateFadeIn(FadeIn& e) {
if (e.mIsOverFunction(&e)) {
if (e.mCB) e.mCB(e.mCaller);
removeFadeIn(&e);
return 1;
}
int i;
for (i = 0; i < e.mAnimationAmount; i++) {
updateSingleFadeInAnimation(&e, i);
}
return 0;
}
static void updateScreenEffectHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
stl_int_map_remove_predicate(gScreenEffect.mFadeIns, updateFadeIn);
}
static void addFadeIn_internal(Duration tDuration, ScreenEffectFinishedCB tOptionalCB, void* tCaller, const Vector3D& tStartPatchSize, const Vector3D& tFullPatchSize, const Vector3D& tSizeDelta, double tStartAlpha, double tAlphaDelta, IsScreenEffectOverFunction tIsOverFunc) {
if (!gScreenEffect.mIsActive) {
unloadedBehaviour(tDuration, tOptionalCB, tCaller);
return;
}
ScreenSize screen = getScreenSize();
FadeIn e;
e.mDuration = tDuration;
e.mCB = tOptionalCB;
e.mCaller = tCaller;
e.mDuration = tDuration;
e.mPhysicsElement = addToPhysicsHandler(tStartPatchSize);
addAccelerationToHandledPhysics(e.mPhysicsElement, tSizeDelta);
e.mSize = &getPhysicsFromHandler(e.mPhysicsElement)->mPosition;
e.mAlphaPhysicsElement = addToPhysicsHandler(Vector3D(tStartAlpha, 0, 0));
addAccelerationToHandledPhysics(e.mAlphaPhysicsElement, Vector3D(tAlphaDelta, 0, 0));
e.mAlpha = &getPhysicsFromHandler(e.mAlphaPhysicsElement)->mPosition.x;
e.mIsOverFunction = tIsOverFunc;
int amountX = (int)((screen.x + (tFullPatchSize.x - 1)) / tFullPatchSize.x);
int amountY = (int)((screen.y + (tFullPatchSize.y - 1)) / tFullPatchSize.y);
e.mAnimationAmount = amountX * amountY;
e.mAnimationElements = (AnimationHandlerElement**)allocMemory(e.mAnimationAmount * sizeof(AnimationHandlerElement*));
Position p = Vector3D(0, 0, gScreenEffect.mZ);
int i;
for (i = 0; i < e.mAnimationAmount; i++) {
e.mAnimationElements[i] = playAnimationLoop(p, &gScreenEffect.mWhiteTexture, createOneFrameAnimation(), makeRectangleFromTexture(gScreenEffect.mWhiteTexture));
updateSingleFadeInAnimation(&e, i);
setAnimationColor(e.mAnimationElements[i], gScreenEffect.mFadeColor.mR, gScreenEffect.mFadeColor.mG, gScreenEffect.mFadeColor.mB);
p = vecAdd(p, Vector3D(tFullPatchSize.x, 0, 0));
if (p.x >= screen.x) {
p = vecAdd(p, tFullPatchSize);
p.x = 0;
}
}
stl_int_map_push_back(gScreenEffect.mFadeIns, e);
}
static int isFadeInOver(FadeIn* e) {
return (*e->mAlpha) <= 0;
}
void addFadeIn(Duration tDuration, ScreenEffectFinishedCB tOptionalCB, void* tCaller) {
double da = -1 / (double)tDuration;
Vector3D patchSize = Vector3D(getScreenSize().x, getScreenSize().y, 1);
addFadeIn_internal(tDuration, tOptionalCB, tCaller, patchSize, patchSize, Vector3D(0, 0, 0), 1, da, isFadeInOver);
}
void addVerticalLineFadeIn(Duration tDuration, ScreenEffectFinishedCB tOptionalCB, void* tCaller) {
double dy = -gScreenEffect.mFullLineSize / (double)tDuration;
addFadeIn_internal(tDuration, tOptionalCB, tCaller, Vector3D(getScreenSize().x, gScreenEffect.mFullLineSize + 1, 1), Vector3D(getScreenSize().x, gScreenEffect.mFullLineSize, 1), Vector3D(0, dy, 0), 1, 0, isVerticalLineFadeInOver);
}
static int skipSingleFadeInCB(FadeIn& e) {
if (e.mCB) e.mCB(e.mCaller);
removeFadeIn(&e);
return 1;
}
void skipFadeIn()
{
if (!gScreenEffect.mIsActive) return;
stl_int_map_remove_predicate(gScreenEffect.mFadeIns, skipSingleFadeInCB);
}
typedef struct {
void* mCaller;
ScreenEffectFinishedCB mCB;
} FadeOutData;
static void fadeOutOverCB(void* tCaller) {
FadeOutData* e = (FadeOutData*)tCaller;
setScreenBlack();
if (e->mCB) {
e->mCB(e->mCaller);
}
freeMemory(e);
}
static int isFadeOutOver(FadeIn* e) {
return (*e->mAlpha) >= 1;
}
void addFadeOut(Duration tDuration, ScreenEffectFinishedCB tOptionalCB, void* tCaller) {
double da = 1 / (double)tDuration;
Vector3D patchSize = Vector3D(getScreenSize().x, getScreenSize().y, 1);
FadeOutData* e = (FadeOutData*)allocMemory(sizeof(FadeOutData));
e->mCB = tOptionalCB;
e->mCaller = tCaller;
addFadeIn_internal(tDuration, fadeOutOverCB, e, patchSize, patchSize, Vector3D(0, 0, 0), 0, da, isFadeOutOver);
}
void setFadeColor(Color tColor) {
getRGBFromColor(tColor, &gScreenEffect.mFadeColor.mR, &gScreenEffect.mFadeColor.mG, &gScreenEffect.mFadeColor.mB);
}
void setFadeColorRGB(double r, double g, double b) {
gScreenEffect.mFadeColor.mR = r;
gScreenEffect.mFadeColor.mG = g;
gScreenEffect.mFadeColor.mB = b;
}
void setScreenEffectZ(double tZ)
{
gScreenEffect.mZ = tZ;
}
void drawColoredRectangle(const GeoRectangle& tRect, Color tColor) {
if (!gScreenEffect.mIsActive) return;
double dx = (tRect.mBottomRight.x - tRect.mTopLeft.x);
double dy = (tRect.mBottomRight.y - tRect.mTopLeft.y);
dx /= gScreenEffect.mWhiteTexture.mTextureSize.x;
dy /= gScreenEffect.mWhiteTexture.mTextureSize.y;
scaleDrawing3D(Vector3D(dx, dy, 1), tRect.mTopLeft);
setDrawingBaseColor(tColor);
drawSprite(gScreenEffect.mWhiteTexture, tRect.mTopLeft, makeRectangleFromTexture(gScreenEffect.mWhiteTexture));
setDrawingParametersToIdentity();
}
void drawColoredHorizontalLine(const Position& tA, const Position& tB, Color tColor)
{
if (tA.y != tB.y) return;
double x = min(tA.x, tB.x);
double w = (double)abs((double)(tB.x - tA.x));
drawColoredRectangle(GeoRectangle(x, tA.y, tA.z, w, 1), tColor);
}
void drawColoredPoint(const Position& tPoint, Color tColor) {
drawColoredRectangle(GeoRectangle(tPoint.x, tPoint.y, tPoint.z, 1, 1), tColor);
}
void setScreenBlack() {
if (!gScreenEffect.mIsActive) return;
setScreenColor(COLOR_BLACK);
}
void unsetScreenBlack() {
if (!gScreenEffect.mIsActive) return;
unsetScreenColor();
}
void setScreenWhite() {
setScreenColor(COLOR_WHITE);
}
void unsetScreenWhite() {
unsetScreenColor();
}
TextureData getEmptyWhiteTexture()
{
return gScreenEffect.mWhiteTexture;
}
TextureData* getEmptyWhiteTextureReference()
{
return &gScreenEffect.mWhiteTexture;
}
ActorBlueprint getScreenEffectHandler()
{
return makeActorBlueprint(loadScreenEffectHandler, unloadScreenEffectHandler, updateScreenEffectHandler);
}
}