added bubblesort + mergesort (hacked in)
This commit is contained in:
parent
60bca68145
commit
2ba223598e
|
@ -2,11 +2,20 @@
|
|||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "visualizers/histogram.h"
|
||||
|
||||
void bubblesort(Application* app);
|
||||
void mergesort(Application* app, size_t left, size_t right);
|
||||
void merge(Application* app, size_t left, size_t middle, size_t right);
|
||||
|
||||
void shuffle(int* array, size_t n);
|
||||
|
||||
void framebuffer_changed(GLFWwindow* window, int width, int height);
|
||||
|
||||
int init_application(Application* app, int width, int height, const char* title)
|
||||
{
|
||||
const char* err;
|
||||
|
@ -23,7 +32,10 @@ int init_application(Application* app, int width, int height, const char* title)
|
|||
return -1;
|
||||
}
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_changed);
|
||||
|
||||
glfwMakeContextCurrent(window);
|
||||
glfwSwapInterval(0);
|
||||
app->window = window;
|
||||
|
||||
if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
|
@ -37,7 +49,7 @@ int init_application(Application* app, int width, int height, const char* title)
|
|||
|
||||
app->active_visualizer = &histogram_visualizer;
|
||||
|
||||
visualizer_data.array_size = 100;
|
||||
visualizer_data.array_size = 2500;
|
||||
visualizer_data.array = (int*)malloc(visualizer_data.array_size * sizeof(int));
|
||||
// TODO: Check if NULL
|
||||
|
||||
|
@ -45,6 +57,7 @@ int init_application(Application* app, int width, int height, const char* title)
|
|||
{
|
||||
visualizer_data.array[i] = i;
|
||||
}
|
||||
shuffle(visualizer_data.array, visualizer_data.array_size);
|
||||
|
||||
if(!app->active_visualizer->init())
|
||||
{
|
||||
|
@ -68,15 +81,138 @@ void run_application(Application* app)
|
|||
{
|
||||
GLFWwindow* window = app->window;
|
||||
|
||||
// bubblesort(app);
|
||||
mergesort(app, 0, visualizer_data.array_size - 1);
|
||||
|
||||
while(!glfwWindowShouldClose(window))
|
||||
{
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
}
|
||||
|
||||
void framebuffer_changed(GLFWwindow* window, int width, int height)
|
||||
{
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void shuffle(int *array, size_t n)
|
||||
{
|
||||
if (n > 1)
|
||||
{
|
||||
size_t i;
|
||||
for (i = 0; i < n - 1; i++)
|
||||
{
|
||||
size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
|
||||
int t = array[j];
|
||||
array[j] = array[i];
|
||||
array[i] = t;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void bubblesort(Application* app)
|
||||
{
|
||||
for(size_t n = visualizer_data.array_size; n > 1; n--)
|
||||
{
|
||||
for(int i = 0; i < n - 1; i++)
|
||||
{
|
||||
if(visualizer_data.array[i] > visualizer_data.array[i + 1])
|
||||
{
|
||||
int tmp = visualizer_data.array[i];
|
||||
visualizer_data.array[i] = visualizer_data.array[i + 1];
|
||||
visualizer_data.array[i + 1] = tmp;
|
||||
}
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void mergesort(Application* app, size_t left, size_t right)
|
||||
{
|
||||
if(right > left)
|
||||
{
|
||||
size_t middle = (left + right) / 2;
|
||||
mergesort(app, left, middle);
|
||||
mergesort(app, middle + 1, right);
|
||||
|
||||
merge(app, left, middle, right);
|
||||
}
|
||||
}
|
||||
|
||||
void merge(Application* app, size_t left, size_t middle, size_t right)
|
||||
{
|
||||
int* tmp = (int*)malloc(visualizer_data.array_size * sizeof(int));
|
||||
for(size_t k = left; k <= middle; k++)
|
||||
{
|
||||
tmp[k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
for(size_t k = middle + 1; k <= right; k++)
|
||||
{
|
||||
tmp[right + middle + 1 - k] = visualizer_data.array[k];
|
||||
}
|
||||
|
||||
size_t i = left;
|
||||
size_t j = right;
|
||||
size_t k = left;
|
||||
|
||||
while(i < j)
|
||||
{
|
||||
if(tmp[i] <= tmp[j])
|
||||
{
|
||||
visualizer_data.array[k] = tmp[i];
|
||||
i++;
|
||||
}
|
||||
else
|
||||
{
|
||||
visualizer_data.array[k] = tmp[j];
|
||||
j--;
|
||||
}
|
||||
|
||||
k++;
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
||||
|
||||
visualizer_data.array[right] = tmp[i];
|
||||
|
||||
if(glfwWindowShouldClose(app->window))
|
||||
return;
|
||||
|
||||
glfwPollEvents();
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
|
||||
app->active_visualizer->render();
|
||||
|
||||
glfwSwapBuffers(app->window);
|
||||
}
|
|
@ -116,7 +116,7 @@ void histogram_render(void)
|
|||
{
|
||||
glm_mat4_identity(model);
|
||||
glm_translate(model, (vec3) {(float)i / visualizer_data.array_size, 0.0f, 0.0f});
|
||||
glm_scale(model, (vec3) {1.0f / (visualizer_data.array_size + 1), 1.0f / visualizer_data.array_size + (float)i / (visualizer_data.array_size + 1), 1.0f});
|
||||
glm_scale(model, (vec3) {1.0f / (visualizer_data.array_size + 1), 1.0f / visualizer_data.array_size + (float)visualizer_data.array[i] / (visualizer_data.array_size + 1), 1.0f});
|
||||
|
||||
glUniformMatrix4fv(model_location, 1, GL_FALSE, model[0]);
|
||||
|
||||
|
|
Loading…
Reference in a new issue