generated from AfonsoCMSousa/CPP-OpenGLTemplate
fix: massive memory leak (not all fixed) and added more test cases
This commit is contained in:
parent
a1f868c27b
commit
7cdecceef2
@ -6,6 +6,7 @@
|
|||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
#define G_CONSTANT 1.98f
|
#define G_CONSTANT 1.98f
|
||||||
|
|
||||||
@ -148,7 +149,7 @@ void collision_update(Particle *particles, unsigned long long count, float resti
|
|||||||
|
|
||||||
void *t_update(void *args) {
|
void *t_update(void *args) {
|
||||||
Physics_params *params = (Physics_params *)args;
|
Physics_params *params = (Physics_params *)args;
|
||||||
while (params->exit != 1) {
|
while (!params->exit) {
|
||||||
pthread_mutex_lock(params->mutex);
|
pthread_mutex_lock(params->mutex);
|
||||||
while (params->run == 0) {
|
while (params->run == 0) {
|
||||||
pthread_cond_wait(params->cond, params->mutex); // sleeps here
|
pthread_cond_wait(params->cond, params->mutex); // sleeps here
|
||||||
@ -202,6 +203,7 @@ void *t_update(void *args) {
|
|||||||
pthread_barrier_wait(params->barrier);
|
pthread_barrier_wait(params->barrier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
printf("INFO: Thread %llu exiting\n", params->thread_id);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -20,13 +20,13 @@ const char FRAGMENT_SHADER_SOURCE[] = "./source/shader/fragment.glsl";
|
|||||||
const char VERTEX_SHADER_SOURCE[] = "./source/shader/vertex.glsl";
|
const char VERTEX_SHADER_SOURCE[] = "./source/shader/vertex.glsl";
|
||||||
|
|
||||||
const char PROGRAM_NAME[] = "Particle Sim";
|
const char PROGRAM_NAME[] = "Particle Sim";
|
||||||
const unsigned int THREAD_COUNT = 1;
|
const unsigned int THREAD_COUNT = 16;
|
||||||
|
|
||||||
const unsigned int SCR_WIDTH = 2560;
|
const unsigned int SCR_WIDTH = 2560;
|
||||||
const unsigned int SCR_HEIGHT = 1440;
|
const unsigned int SCR_HEIGHT = 1440;
|
||||||
const char *glsl_version;
|
const char *glsl_version;
|
||||||
|
|
||||||
const unsigned int PARTICLE_COUNT = 3;
|
const unsigned int PARTICLE_COUNT = 500;
|
||||||
|
|
||||||
GLFWwindow *window;
|
GLFWwindow *window;
|
||||||
unsigned int VBO, VAO;
|
unsigned int VBO, VAO;
|
||||||
@ -220,9 +220,29 @@ int main() {
|
|||||||
if (particles == NULL) {
|
if (particles == NULL) {
|
||||||
snprintf(errLog, 128, "There was a issue allocating memory for the particle list\n");
|
snprintf(errLog, 128, "There was a issue allocating memory for the particle list\n");
|
||||||
} else {
|
} else {
|
||||||
particles[0] = particle_init({1.000001f, 1.0f, 0.994f}, {1.0f, 0.0f, 0.0f, 255.0f});
|
for (unsigned long long i = 0; i < PARTICLE_COUNT; i++) {
|
||||||
particles[1] = particle_init({1.0f, 1.2f, 1.0f}, {0.0f, 1.0f, 0.0f, 255.0f});
|
// TEST: 3 Particles
|
||||||
particles[2] = particle_init({0.99995f, 1.4f, 1.004f}, {0.0f, 0.0f, 1.0f, 255.0f});
|
// INFO: Best run with 3 particles and 1 thread
|
||||||
|
/*
|
||||||
|
particles[0] = particle_init({1.000001f, 1.0f, 0.994f}, {1.0f, 0.0f, 0.0f, 255.0f});
|
||||||
|
particles[1] = particle_init({1.0f, 1.2f, 1.0f}, {0.0f, 1.0f, 0.0f, 255.0f});
|
||||||
|
particles[2] = particle_init({0.99995f, 1.4f, 1.004f}, {0.0f, 0.0f, 1.0f, 255.0f});
|
||||||
|
*/
|
||||||
|
|
||||||
|
// TEST: Cloud of particles in a 2x2x2 cube
|
||||||
|
// INFO: Best run with 500 particles and 16 threads
|
||||||
|
float x = (float)(rand() % 200) / 100.0f; // Random x between 0 and 2
|
||||||
|
float y = (float)(rand() % 200) / 100.0f; // Random y between 0 and 2
|
||||||
|
float z = (float)(rand() % 200) / 100.0f; // Random z between 0 and 2
|
||||||
|
|
||||||
|
particles[i] = particle_init({x, y, z}, {x, y, z, 255.0f});
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
particles[0] = particle_init({1.000001f, 1.0f, 0.994f}, {1.0f, 0.0f, 0.0f, 255.0f});
|
||||||
|
particles[1] = particle_init({1.0f, 1.2f, 1.0f}, {0.0f, 1.0f, 0.0f, 255.0f});
|
||||||
|
particles[2] = particle_init({0.99995f, 1.4f, 1.004f}, {0.0f, 0.0f, 1.0f, 255.0f});
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
float current_time;
|
float current_time;
|
||||||
@ -259,7 +279,6 @@ int main() {
|
|||||||
// Poll events
|
// Poll events
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
// ESC releases cursor; if cursor already free, close window
|
// ESC releases cursor; if cursor already free, close window
|
||||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||||
if (camera.cursor_captured) {
|
if (camera.cursor_captured) {
|
||||||
@ -270,7 +289,7 @@ int main() {
|
|||||||
glfwSetWindowShouldClose(window, true);
|
glfwSetWindowShouldClose(window, true);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
camera_keyboard(camera, window, delta_time);
|
camera_keyboard(camera, window, delta_time);
|
||||||
|
|
||||||
// Build MVP
|
// Build MVP
|
||||||
@ -293,7 +312,7 @@ int main() {
|
|||||||
for (int i = 0; i < THREAD_COUNT; i++)
|
for (int i = 0; i < THREAD_COUNT; i++)
|
||||||
alive_count += thread_params[i].alive_count;
|
alive_count += thread_params[i].alive_count;
|
||||||
|
|
||||||
collision_update(particles, alive_count, 0.7f);
|
collision_update(particles, alive_count, 0.3f);
|
||||||
|
|
||||||
// Render
|
// Render
|
||||||
glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
|
glClearColor(0.03f, 0.03f, 0.03f, 1.0f);
|
||||||
@ -339,6 +358,19 @@ int main() {
|
|||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
|
||||||
|
for (int i = 0; i < THREAD_COUNT; i++) {
|
||||||
|
thread_params[i].exit = 1;
|
||||||
|
pthread_cond_broadcast(&cond); // Wake up threads so they can exit
|
||||||
|
}
|
||||||
|
|
||||||
|
free(threads);
|
||||||
|
free(thread_params);
|
||||||
free(particles);
|
free(particles);
|
||||||
|
|
||||||
|
ImGui_ImplGlfw_Shutdown();
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui::DestroyContext();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user