6
6
#include < GLFW/glfw3.h>
7
7
#include < math.h>
8
8
#include < vector>
9
+ #include < iostream>
9
10
#if defined(_MSC_VER) && (_MSC_VER >= 1900) && !defined(IMGUI_DISABLE_WIN32_FUNCTIONS)
10
11
#pragma comment(lib, "legacy_stdio_definitions")
11
12
#endif
12
13
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
14
18
{
15
- std::vector<bool > output;
16
19
if (horisontal_steps == 1 )
17
20
{
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 ;
20
28
}
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);
26
30
}
27
31
28
32
static void glfw_error_callback (int error, const char * description)
29
33
{
30
34
fprintf (stderr, " GLFW Error %d: %s\n " , error, description);
31
35
}
32
36
33
- // Main code
34
37
int main (int , char **)
35
38
{
36
39
// Generic setup stuff
37
40
glfwSetErrorCallback (glfw_error_callback);
38
- if (!glfwInit ())
39
- return 1 ;
41
+ if (!glfwInit ()) return 1 ;
40
42
const char * glsl_version = " #version 130" ;
41
43
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3 );
42
44
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 0 );
43
45
GLFWwindow* window = glfwCreateWindow (1280 , 720 , " Cantor" , nullptr , nullptr );
44
- if (window == nullptr )
45
- return 1 ;
46
+ if (window == nullptr ) return 1 ;
46
47
glfwMakeContextCurrent (window);
47
48
glfwSwapInterval (1 );
48
49
IMGUI_CHECKVERSION ();
@@ -54,6 +55,15 @@ int main(int, char**)
54
55
ImGui_ImplGlfw_InitForOpenGL (window, true );
55
56
ImGui_ImplOpenGL3_Init (glsl_version);
56
57
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 ;
57
67
// Main loop
58
68
while (!glfwWindowShouldClose (window))
59
69
{
@@ -63,41 +73,64 @@ int main(int, char**)
63
73
ImGui::NewFrame ();
64
74
// Actual code
65
75
{
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
+ }
70
100
const ImGuiViewport* viewport = ImGui::GetMainViewport ();
71
101
ImGui::SetNextWindowPos (viewport->WorkPos );
72
102
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))
75
106
{
76
- static float KantorLines ( void *, int i )
107
+ if (display_count > WARNING_STEP && !warning_shown )
77
108
{
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 ;
81
112
}
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
89
114
{
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);
92
128
}
129
+
93
130
}
94
131
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));
101
134
ImGui::End ();
102
135
}
103
136
// Rendering
@@ -117,4 +150,4 @@ int main(int, char**)
117
150
glfwDestroyWindow (window);
118
151
glfwTerminate ();
119
152
return 0 ;
120
- }
153
+ }
0 commit comments