Skip to content

Commit 317a583

Browse files
committed
fixed alloc, added warning
1 parent cc1a6fa commit 317a583

File tree

3 files changed

+76
-44
lines changed

3 files changed

+76
-44
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
/imgui_impl_opengl3.o
88
/imgui_tables.o
99
/imgui_widgets.o
10-
/main.o
10+
/main.o
11+
cantor

README

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
Cantor function rendered in real time :DD
2-
3-
(for now using dumb getter + dumb recursive function with lots of allocations)
1+
Cantor function rendered in real time :DD

main.cpp

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,43 +6,44 @@
66
#include <GLFW/glfw3.h>
77
#include <math.h>
88
#include <vector>
9+
#include <iostream>
910
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
1011
#pragma comment(lib, "legacy_stdio_definitions")
1112
#endif
1213

13-
std::vector<bool> generate_plus_null(int horisontal_steps) //Recursive function to generate monotonous sections
14+
#define MAX_STEP 20
15+
#define WARNING_STEP 15
16+
17+
void generate_plus_null(int horisontal_steps, int current_position, bool* output_function) //Recursive function to generate monotonous sections
1418
{
15-
std::vector<bool> output;
1619
if (horisontal_steps == 1)
1720
{
18-
output.push_back(false);
19-
return output;
21+
output_function[current_position] = false;
22+
return;
23+
}
24+
generate_plus_null(horisontal_steps / 3, current_position, output_function);
25+
for (int i = current_position + horisontal_steps / 3; i < current_position + 2 * horisontal_steps / 3; i++)
26+
{
27+
output_function[i] = true;
2028
}
21-
std::vector<bool> left_side = generate_plus_null(horisontal_steps / 3);
22-
for (int i = 0; i < horisontal_steps / 3; i++) output.push_back(left_side.at(i));
23-
for (int i = 0; i < horisontal_steps / 3; i++) output.push_back(true);
24-
for (int i = 0; i < horisontal_steps / 3; i++) output.push_back(left_side.at(left_side.size() - i - 1));
25-
return output;
29+
generate_plus_null(horisontal_steps / 3, current_position + 2 * horisontal_steps / 3, output_function);
2630
}
2731

2832
static void glfw_error_callback(int error, const char* description)
2933
{
3034
fprintf(stderr, "GLFW Error %d: %s\n", error, description);
3135
}
3236

33-
// Main code
3437
int main(int, char**)
3538
{
3639
// Generic setup stuff
3740
glfwSetErrorCallback(glfw_error_callback);
38-
if (!glfwInit())
39-
return 1;
41+
if (!glfwInit()) return 1;
4042
const char* glsl_version = "#version 130";
4143
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
4244
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
4345
GLFWwindow* window = glfwCreateWindow(1280, 720, "Cantor", nullptr, nullptr);
44-
if (window == nullptr)
45-
return 1;
46+
if (window == nullptr) return 1;
4647
glfwMakeContextCurrent(window);
4748
glfwSwapInterval(1);
4849
IMGUI_CHECKVERSION();
@@ -54,6 +55,15 @@ int main(int, char**)
5455
ImGui_ImplGlfw_InitForOpenGL(window, true);
5556
ImGui_ImplOpenGL3_Init(glsl_version);
5657
ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
58+
// Setting up values
59+
float* function = (float*)IM_ALLOC(sizeof(float) * 4);
60+
function[0] = 0.0f; function[1] = 0.5f; function[2] = 0.5f; function[3] = 1.0f;
61+
int size = 3;
62+
int display_count = 1;
63+
bool warning_shown = false;
64+
bool warning_active = false;
65+
int pre_warning_value = 1;
66+
int post_warning_value = 1;
5767
// Main loop
5868
while (!glfwWindowShouldClose(window))
5969
{
@@ -63,41 +73,64 @@ int main(int, char**)
6373
ImGui::NewFrame();
6474
// Actual code
6575
{
66-
static int display_count = 1;
67-
static std::vector<bool> plus_null = {false, true, false};
68-
static std::vector<float> current_integral_function = {0.5f, 0.5f, 1.0f};
69-
static ImGuiWindowFlags flags = ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings;
76+
if (warning_active)
77+
{
78+
ImGui::OpenPopup("Warning!");
79+
ImVec2 center = ImGui::GetMainViewport()->GetCenter();
80+
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
81+
if (ImGui::BeginPopupModal("Warning!", NULL, ImGuiWindowFlags_AlwaysAutoResize))
82+
{
83+
ImGui::Text("Your computer may hang.");
84+
if (ImGui::Button("OK."))
85+
{
86+
display_count = post_warning_value;
87+
warning_active = false;
88+
warning_shown = true;
89+
}
90+
ImGui::SameLine();
91+
if (ImGui::Button("Cancel."))
92+
{
93+
display_count = pre_warning_value;
94+
warning_active = false;
95+
}
96+
if (!warning_active) ImGui::CloseCurrentPopup();
97+
ImGui::EndPopup();
98+
}
99+
}
70100
const ImGuiViewport* viewport = ImGui::GetMainViewport();
71101
ImGui::SetNextWindowPos(viewport->WorkPos);
72102
ImGui::SetNextWindowSize(viewport->WorkSize);
73-
ImGui::Begin("Cantor", nullptr, flags);
74-
struct Funcs
103+
ImGui::Begin("Cantor", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoSavedSettings);
104+
int pre_step_value = display_count;
105+
if (ImGui::SliderInt("Step count", &display_count, 1, MAX_STEP, "%d", ImGuiSliderFlags_NoInput))
75106
{
76-
static float KantorLines(void*, int i)
107+
if (display_count > WARNING_STEP && !warning_shown)
77108
{
78-
if (i==0) return 0.0f;
79-
i-=1;
80-
return current_integral_function[i];
109+
pre_warning_value = pre_step_value;
110+
post_warning_value = display_count;
111+
warning_active = true;
81112
}
82-
};
83-
if (ImGui::SliderInt("Step count", &display_count, 1, 15))
84-
{
85-
plus_null = generate_plus_null(pow(3, display_count));
86-
float current = 0;
87-
current_integral_function = std::vector<float>();
88-
for (int i = 0; i < plus_null.size(); i++)
113+
else
89114
{
90-
plus_null[i] ? current : current += 1.0f/pow(2,display_count);
91-
current_integral_function.push_back(current);
115+
free(function);
116+
size = pow(3, display_count);
117+
function = (float*)IM_ALLOC(sizeof(float) * pow(3, display_count) + 1);
118+
bool* plus_null = (bool*)IM_ALLOC(sizeof(bool) * pow(3, display_count));
119+
generate_plus_null(pow(3, display_count), 0, plus_null);
120+
float current_value = 0;
121+
function[0] = 0.0f; // We always store one extra null value so our plot doesn't start from nowhere
122+
for (int i = 0; i < size; i++)
123+
{
124+
plus_null[i] ? current_value : current_value += (1.0f / pow(2, display_count));
125+
function[i + 1] = current_value;
126+
}
127+
free(plus_null);
92128
}
129+
93130
}
94131
ImGui::SameLine();
95-
ImGui::Text(": %.1f FPS.", io.Framerate);
96-
float (*func)(void*, int) = Funcs::KantorLines;
97-
if (current_integral_function.size() == pow(3, display_count))
98-
{
99-
ImGui::PlotLines("##Lines", func, NULL, pow(3, display_count) + 1, 0, NULL, 0.0f, 1.0f, ImVec2(-FLT_MIN, -FLT_MIN));
100-
}
132+
ImGui::Text("| %.1f FPS.", io.Framerate);
133+
ImGui::PlotLines("##Lines", function, size + 1, 0, NULL, 0.0f, 1.0f, ImVec2(-FLT_MIN, -FLT_MIN));
101134
ImGui::End();
102135
}
103136
// Rendering
@@ -117,4 +150,4 @@ int main(int, char**)
117150
glfwDestroyWindow(window);
118151
glfwTerminate();
119152
return 0;
120-
}
153+
}

0 commit comments

Comments
 (0)