1
+ #include " imgui.h"
2
+ #include " imgui_impl_sdl.h"
3
+ #include " imgui_impl_opengl3.h"
4
+ #include < stdio.h>
5
+ #include < SDL.h>
6
+ #include < GL/gl3w.h>
7
+
8
+ class SimpleNodeEditor
9
+ {
10
+ public:
11
+ void show ()
12
+ {
13
+ ImGui::Begin (" hello, world" );
14
+ ImGui::End ();
15
+ }
16
+
17
+ private:
18
+ };
19
+
20
+ int main (int , char **)
21
+ {
22
+ if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0 )
23
+ {
24
+ printf (" Error: %s\n " , SDL_GetError ());
25
+ return -1 ;
26
+ }
27
+
28
+ #if __APPLE__
29
+ // GL 3.2 Core + GLSL 150
30
+ const char * glsl_version = " #version 150" ;
31
+ SDL_GL_SetAttribute (
32
+ SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); // Always required on Mac
33
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
34
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
35
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 2 );
36
+ #else
37
+ // GL 3.0 + GLSL 130
38
+ const char * glsl_version = " #version 130" ;
39
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, 0 );
40
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
41
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
42
+ SDL_GL_SetAttribute (SDL_GL_CONTEXT_MINOR_VERSION, 0 );
43
+ #endif
44
+
45
+ SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1 );
46
+ SDL_GL_SetAttribute (SDL_GL_DEPTH_SIZE, 24 );
47
+ SDL_GL_SetAttribute (SDL_GL_STENCIL_SIZE, 8 );
48
+ SDL_DisplayMode current;
49
+ SDL_GetCurrentDisplayMode (0 , ¤t);
50
+ SDL_Window* window = SDL_CreateWindow (
51
+ " imgui-node-editor example" ,
52
+ SDL_WINDOWPOS_CENTERED,
53
+ SDL_WINDOWPOS_CENTERED,
54
+ 1280 ,
55
+ 720 ,
56
+ SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
57
+ SDL_GLContext gl_context = SDL_GL_CreateContext (window);
58
+ SDL_GL_SetSwapInterval (1 ); // Enable vsync
59
+
60
+ IMGUI_CHECKVERSION ();
61
+ ImGui::CreateContext ();
62
+ ImGuiIO& io = ImGui::GetIO ();
63
+ (void )io;
64
+
65
+ ImGui_ImplSDL2_InitForOpenGL (window, gl_context);
66
+ ImGui_ImplOpenGL3_Init (glsl_version);
67
+
68
+ // Setup style
69
+ ImGui::StyleColorsDark ();
70
+
71
+ // Simple node editor
72
+ SimpleNodeEditor node_example;
73
+
74
+ bool done = false ;
75
+ while (!done)
76
+ {
77
+ SDL_Event event;
78
+ while (SDL_PollEvent (&event))
79
+ {
80
+ ImGui_ImplSDL2_ProcessEvent (&event);
81
+ if (event.type == SDL_QUIT)
82
+ done = true ;
83
+ if (event.type == SDL_WINDOWEVENT && event.window .event == SDL_WINDOWEVENT_CLOSE &&
84
+ event.window .windowID == SDL_GetWindowID (window))
85
+ done = true ;
86
+ }
87
+
88
+ // Start the Dear ImGui frame
89
+ ImGui_ImplOpenGL3_NewFrame ();
90
+ ImGui_ImplSDL2_NewFrame (window);
91
+ ImGui::NewFrame ();
92
+
93
+ node_example.show ();
94
+
95
+ // Rendering
96
+ ImGui::Render ();
97
+ SDL_GL_MakeCurrent (window, gl_context);
98
+ glViewport (0 , 0 , (int )io.DisplaySize .x , (int )io.DisplaySize .y );
99
+ glClearColor (clear_color.x , clear_color.y , clear_color.z , clear_color.w );
100
+ glClear (GL_COLOR_BUFFER_BIT);
101
+ ImGui_ImplOpenGL3_RenderDrawData (ImGui::GetDrawData ());
102
+ SDL_GL_SwapWindow (window);
103
+ }
104
+
105
+ ImGui_ImplOpenGL3_Shutdown ();
106
+ ImGui_ImplSDL2_Shutdown ();
107
+ ImGui::DestroyContext ();
108
+
109
+ SDL_GL_DeleteContext (gl_context);
110
+ SDL_DestroyWindow (window);
111
+ SDL_Quit ();
112
+ }
0 commit comments