-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathblitzparticles.cpp
More file actions
153 lines (120 loc) · 4.97 KB
/
Copy pathblitzparticles.cpp
File metadata and controls
153 lines (120 loc) · 4.97 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
#include "prism/blitzparticles.h"
#include "prism/math.h"
#include "prism/datastructures.h"
#include "prism/blitzcamerahandler.h"
#include "prism/profiling.h"
#ifdef _WIN32
#include <imgui/imgui.h>
#include "prism/windows/debugimgui_win.h"
#endif
namespace prism {
typedef struct {
Position mPos;
Velocity mVel;
Velocity mGravity;
double mAngle;
Vector3D mColor;
Duration mNow;
Duration mLifeTime;
} ParticleEntry;
static struct {
TextureData mWhiteTexture;
IntMap mParticles;
} gBlitzParticlesData;
#ifdef _WIN32
static int gImguiParticleRowIndex;
static void imguiParticleEntry(void* tCaller, void* tData)
{
(void)tCaller;
if (gImguiParticleRowIndex >= 200) return;
ParticleEntry* e = (ParticleEntry*)tData;
ImGui::TableNextRow(); ImGui::TableNextColumn();
ImGui::Text("%d", gImguiParticleRowIndex++); ImGui::TableNextColumn();
ImGui::Text("%.0f %.0f", e->mPos.x, e->mPos.y); ImGui::TableNextColumn();
ImGui::Text("%.0f/%.0f", e->mNow, e->mLifeTime); ImGui::TableNextColumn();
ImGui::Text("%.2f %.2f %.2f", e->mColor.x, e->mColor.y, e->mColor.z);
}
void imguiBlitzParticleHandler()
{
static bool isWindowShown = false;
imguiPrismAddTab("Blitz", "Particle Handler", &isWindowShown);
if (!isWindowShown) return;
ImGui::Begin("Blitz Particle Handler", &isWindowShown);
ImGui::Text("Particles = %d", int_map_size(&gBlitzParticlesData.mParticles));
ImGui::TextDisabled("(table capped at 200 rows)");
ImGui::Separator();
static ImGuiTableFlags flags = ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_ScrollY;
if (ImGui::BeginTable("Particles", 4, flags, ImVec2(0, 280)))
{
ImGui::TableSetupColumn("#");
ImGui::TableSetupColumn("Position");
ImGui::TableSetupColumn("Now/Life");
ImGui::TableSetupColumn("Color");
ImGui::TableHeadersRow();
gImguiParticleRowIndex = 0;
int_map_map(&gBlitzParticlesData.mParticles, imguiParticleEntry, NULL);
ImGui::EndTable();
}
ImGui::End();
}
#endif
static void loadParticleHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
gBlitzParticlesData.mWhiteTexture = createWhiteTexture();
gBlitzParticlesData.mParticles = new_int_map();
}
static int updateSingleParticle(void* tCaller, void* tData) {
(void)tCaller;
ParticleEntry* e = (ParticleEntry*)tData;
e->mVel = vecAdd(e->mVel, e->mGravity);
e->mPos = vecAdd(e->mPos, e->mVel);
if (handleDurationAndCheckIfOver(&e->mNow, e->mLifeTime)) {
return 1;
}
return 0;
}
static void updateParticleHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
int_map_remove_predicate(&gBlitzParticlesData.mParticles, updateSingleParticle, NULL);
}
static void drawSingleParticle(void* tCaller, void* tData) {
(void)tCaller;
ParticleEntry* e = (ParticleEntry*)tData;
Position pos = vecSub(e->mPos, getBlitzCameraHandlerPosition());
setDrawingRotationZ(e->mAngle, pos + Vector2D(0.5, 0.5));
setDrawingBaseColorAdvanced(e->mColor.x, e->mColor.y, e->mColor.z);
drawSprite(gBlitzParticlesData.mWhiteTexture, pos, makeRectangle(0, 0, 1, 1));
setDrawingBaseColorAdvanced(1, 1, 1);
setDrawingRotationZ(-e->mAngle, pos + Vector2D(0.5, 0.5));
}
static void drawParticleHandler(void* tData) {
(void)tData;
setProfilingSectionMarkerCurrentFunction();
int_map_map(&gBlitzParticlesData.mParticles, drawSingleParticle, NULL);
}
ActorBlueprint getBlitzParticleHandler() {
return makeActorBlueprint(loadParticleHandler, NULL, updateParticleHandler, drawParticleHandler);
};
void addBlitzParticles(int tAmount, const Position& tPosition, const Position& tPositionRange, double tSpeed, double tSpeedRange, double tAngle, double tAngleRange, const Velocity& tGravity, const Vector3D& tColor, const Vector3D& tColorRange, Duration tLifetime, Duration tLifetimeRange) {
int i;
for (i = 0; i < tAmount; i++) {
addBlitzParticle(tPosition, tPositionRange, tSpeed, tSpeedRange, tAngle, tAngleRange, tGravity, tColor, tColorRange, tLifetime, tLifetimeRange);
}
}
void addBlitzParticle(const Position& tPosition, const Position& tPositionRange, double tSpeed, double tSpeedRange, double tAngle, double tAngleRange, const Velocity& tGravity, const Vector3D& tColor, const Vector3D& tColorRange, Duration tLifetime, Duration tLifetimeRange)
{
ParticleEntry* e = (ParticleEntry*)allocMemory(sizeof(ParticleEntry));
e->mPos = vecAdd(vecSub(tPosition, vecScale(tPositionRange, 0.5)), vecScale(tPositionRange, randfrom(0, 1)));
double speed = randfrom(tSpeed - tSpeedRange / 2, tSpeed + tSpeedRange / 2);
double angle = randfrom(tAngle - tAngleRange / 2, tAngle + tAngleRange / 2);
e->mVel = vecRotateZ(Vector3D(speed, 0, 0), angle);
e->mGravity = tGravity;
e->mAngle = randfrom(0, 2 * M_PI);
e->mColor = vecAdd(vecSub(tColor, vecScale(tColorRange, 0.5)), vecScale(tColorRange, randfrom(0, 1)));
e->mNow = 0;
e->mLifeTime = randfrom(tLifetime - tLifetimeRange / 2, tLifetime + tLifetimeRange / 2);
int_map_push_back_owned(&gBlitzParticlesData.mParticles, e);
}
}