Code
namespace end
{ double delta_time = 0.0; void update();
}namespace end
{
double dev_app_t::get_delta_time()const
{
return delta_time;
} dev_app_t::dev_app_t()
{
} void dev_app_t::update()
{
end::update();
}
}namespace end
{
struct particle
{
float3 pos;
float3 prev_pos;
float3 velocity;
float4 color;
//could add lifetime if you’d like
};
sorted_pool_t<particle, 1024> s_pool; struct emitter
{
float3 pos;
float4 color;
sorted_pool_t<int, 1024> is_pool;
};
pool_t<particle, 2048> fpool;
emitter emit_botleft;
emitter emit_botright;
emitter emit_topleft;
emitter emit_topright; float4 randColor = { 0.2, 0.1, 0.4, 1 };
bool goingUp = true;
}namespace end
{
double calc_delta_time()
{
static std::chrono::time_point<std::chrono::high_resolution_clock> last_time = std::chrono::high_resolution_clock::now(); std::chrono::time_point<std::chrono::high_resolution_clock> new_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> elapsed_seconds = new_time - last_time;
last_time = new_time; //return elapsed_seconds.count();
return std::min(1.0 / 15.0, elapsed_seconds.count());
}
float rand_float(float min, float max)
{
float ratio = rand() / static_cast<float>(RAND_MAX);
return (max - min) * ratio + min;
}
// --------------------------------------------------------- sorted pool ---------------------------------------
void InitSortedPool(int num_part)
{
for (size_t i = 0; i < num_part; i++)
{
int index = s_pool.alloc();
if (index != -1) {
s_pool[index].pos = float3(0, 0, 0);
s_pool[index].color = float4(1, 165.0f/255.0f, 0, 1);
s_pool[index].velocity = float3(rand_float(-1, 1), rand_float(7,11), rand_float(-1,1));
}
}
}
void UpdateSortedPool()
{
float3 up_direction = float3(0,1,0);
float gravity = -9.8f;
for (size_t i = 0; i < s_pool.size(); i++)
{
if (s_pool[i].pos.y >= 0) {
s_pool[i].prev_pos = s_pool[i].pos;
s_pool[i].pos += (s_pool[i].velocity * delta_time) + (up_direction * gravity * 0.5f * pow(delta_time, 2));
s_pool[i].velocity += up_direction * gravity * delta_time;
}
else {
s_pool.free(i);
i--;
}
}
}
void RenderSortedPool()
{
for (int i = 0; i < s_pool.size(); i++)
{
debug_renderer::add_line(s_pool[i].prev_pos, s_pool[i].pos, s_pool[i].color, s_pool[i].color);
}
}
// --------------------------------------------------------- free pool -----------------------------------------
void InitFreePool(int num_part, emitter& emite)
{
for (size_t i = 0; i < num_part; i++)
{
int16_t fpIndex = fpool.alloc();
if (fpIndex != -1) { int16_t spIndex = emite.is_pool.alloc();
if (spIndex != -1) {
fpool[fpIndex].pos = emite.pos;
fpool[fpIndex].color = emite.color;
fpool[fpIndex].velocity = float3(rand_float(-1, 1), rand_float(9, 13), rand_float(-1, 1));
emite.is_pool[spIndex] = fpIndex;
}
else {
fpool.free(fpIndex);
}
}
}
}
void UpdateFreePool(emitter& emite)
{
float3 up_direction = float3(0, 1, 0);
float gravity = -9.8f;
for (size_t i = 0; i < emite.is_pool.size(); i++)
{
if (fpool[emite.is_pool[i]].pos.y >= 0) {
fpool[emite.is_pool[i]].prev_pos = fpool[emite.is_pool[i]].pos;
fpool[emite.is_pool[i]].pos += (fpool[emite.is_pool[i]].velocity * delta_time) + (up_direction * gravity * 0.5f * pow(delta_time, 2));
fpool[emite.is_pool[i]].velocity += up_direction * gravity * delta_time;
}
else {
fpool.free(emite.is_pool[i]);
emite.is_pool.free(i);
i--;
}
}
}
void RenderFreePool(emitter& emite)
{
for (int i = 0; i < emite.is_pool.size(); i++)
{
debug_renderer::add_line(fpool[emite.is_pool[i]].prev_pos, fpool[emite.is_pool[i]].pos, fpool[emite.is_pool[i]].color, fpool[emite.is_pool[i]].color);
}
} void DoTheParticleSystemStuff()
{
InitSortedPool(400);
UpdateSortedPool();
RenderSortedPool(); emit_botleft.pos = float3(-5, 0, -5);
emit_botleft.color = float4(1, 1, 0, 1);
InitFreePool(400, emit_botleft);
UpdateFreePool(emit_botleft);
RenderFreePool(emit_botleft); emit_botright.pos = float3(5, 0, -5);
emit_botright.color = float4(1, 0.4, 0.4, 1);
InitFreePool(400, emit_botright);
UpdateFreePool(emit_botright);
RenderFreePool(emit_botright); emit_topleft.pos = float3(-5, 0, 5);
emit_topleft.color = float4(0, 1, 0, 1);
InitFreePool(400, emit_topleft);
UpdateFreePool(emit_topleft);
RenderFreePool(emit_topleft); emit_topright.pos = float3(5, 0, 5);
emit_topright.color = float4(0.6, 0.6, 1, 1);
InitFreePool(400, emit_topright);
UpdateFreePool(emit_topright);
RenderFreePool(emit_topright);
} void update()
{
srand(time(NULL));
delta_time = calc_delta_time();
int num_lines = 20;
int half = num_lines * 0.5f;
float delta = static_cast<float>(delta_time);
//float4 lastColor;
//TODO do you Updates here
//randColor = { rand_float(0.1f, 1), rand_float(0.1f, 1) , rand_float(0.1f, 1) , 1};
/*if (goingUp) {
randColor.x += sin(delta);
randColor.y += sin(delta);
randColor.z += sin(delta);
}
else
{
randColor.x -= sin(delta);
randColor.y -= sin(delta);
randColor.z -= sin(delta);
}
if ((randColor.x >= 1) && (randColor.y >= 1) && (randColor.z >= 1))
goingUp = false;
if ((randColor.x <= 0.1f) && (randColor.y <= 0.1f) && (randColor.z <= 0.1f))
goingUp = true;*/ //DoTheParticleSystemStuff(); // nice purple (0.2, 0.1, 0.4, 1)
for (int i = 0; i <= num_lines; i++)
{
debug_renderer::add_line(float3(-half, 0, i-half), float3(half, 0, i-half), randColor, randColor);
}
for (int i = 0; i <= num_lines; i++)
{
debug_renderer::add_line(float3(i-half, 0, -half), float3(i-half, 0, half), randColor, randColor);
}
//lastColor = randColor;
}
}