main.mm 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. // Dear ImGui: standalone example application for OSX + OpenGL2, using legacy fixed pipeline
  2. // Learn about Dear ImGui:
  3. // - FAQ https://dearimgui.com/faq
  4. // - Getting Started https://dearimgui.com/getting-started
  5. // - Documentation https://dearimgui.com/docs (same as your local docs/ folder).
  6. // - Introduction, links and more at the top of imgui.cpp
  7. #import <Cocoa/Cocoa.h>
  8. #import <OpenGL/gl.h>
  9. #import <OpenGL/glu.h>
  10. #include "imgui.h"
  11. #include "imgui_impl_opengl2.h"
  12. #include "imgui_impl_osx.h"
  13. //-----------------------------------------------------------------------------------
  14. // AppView
  15. //-----------------------------------------------------------------------------------
  16. @interface AppView : NSOpenGLView
  17. {
  18. NSTimer* animationTimer;
  19. }
  20. @end
  21. @implementation AppView
  22. -(void)prepareOpenGL
  23. {
  24. [super prepareOpenGL];
  25. #ifndef DEBUG
  26. GLint swapInterval = 1;
  27. [[self openGLContext] setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval];
  28. if (swapInterval == 0)
  29. NSLog(@"Error: Cannot set swap interval.");
  30. #endif
  31. }
  32. -(void)initialize
  33. {
  34. // Setup Dear ImGui context
  35. // FIXME: This example doesn't have proper cleanup...
  36. IMGUI_CHECKVERSION();
  37. ImGui::CreateContext();
  38. ImGuiIO& io = ImGui::GetIO(); (void)io;
  39. io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  40. io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls
  41. // Setup Dear ImGui style
  42. ImGui::StyleColorsDark();
  43. //ImGui::StyleColorsLight();
  44. // Setup Platform/Renderer backends
  45. ImGui_ImplOSX_Init(self);
  46. ImGui_ImplOpenGL2_Init();
  47. // Load Fonts
  48. // - If no fonts are loaded, dear imgui will use the default font. You can also load multiple fonts and use ImGui::PushFont()/PopFont() to select them.
  49. // - AddFontFromFileTTF() will return the ImFont* so you can store it if you need to select the font among multiple.
  50. // - If the file cannot be loaded, the function will return a nullptr. Please handle those errors in your application (e.g. use an assertion, or display an error and quit).
  51. // - Use '#define IMGUI_ENABLE_FREETYPE' in your imconfig file to use Freetype for higher quality font rendering.
  52. // - Read 'docs/FONTS.md' for more instructions and details.
  53. // - Remember that in C/C++ if you want to include a backslash \ in a string literal you need to write a double backslash \\ !
  54. //io.Fonts->AddFontDefault();
  55. //io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\segoeui.ttf", 18.0f);
  56. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/DroidSans.ttf", 16.0f);
  57. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Roboto-Medium.ttf", 16.0f);
  58. //io.Fonts->AddFontFromFileTTF("../../misc/fonts/Cousine-Regular.ttf", 15.0f);
  59. //ImFont* font = io.Fonts->AddFontFromFileTTF("c:\\Windows\\Fonts\\ArialUni.ttf", 18.0f);
  60. //IM_ASSERT(font != nullptr);
  61. }
  62. -(void)updateAndDrawDemoView
  63. {
  64. // Start the Dear ImGui frame
  65. ImGuiIO& io = ImGui::GetIO();
  66. ImGui_ImplOpenGL2_NewFrame();
  67. ImGui_ImplOSX_NewFrame(self);
  68. ImGui::NewFrame();
  69. // Our state (make them static = more or less global) as a convenience to keep the example terse.
  70. static bool show_demo_window = true;
  71. static bool show_another_window = false;
  72. static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  73. // 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
  74. if (show_demo_window)
  75. ImGui::ShowDemoWindow(&show_demo_window);
  76. // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
  77. {
  78. static float f = 0.0f;
  79. static int counter = 0;
  80. ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
  81. ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
  82. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
  83. ImGui::Checkbox("Another Window", &show_another_window);
  84. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  85. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  86. if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
  87. counter++;
  88. ImGui::SameLine();
  89. ImGui::Text("counter = %d", counter);
  90. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / io.Framerate, io.Framerate);
  91. ImGui::End();
  92. }
  93. // 3. Show another simple window.
  94. if (show_another_window)
  95. {
  96. ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
  97. ImGui::Text("Hello from another window!");
  98. if (ImGui::Button("Close Me"))
  99. show_another_window = false;
  100. ImGui::End();
  101. }
  102. // Rendering
  103. ImGui::Render();
  104. ImDrawData* draw_data = ImGui::GetDrawData();
  105. [[self openGLContext] makeCurrentContext];
  106. GLsizei width = (GLsizei)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x);
  107. GLsizei height = (GLsizei)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y);
  108. glViewport(0, 0, width, height);
  109. glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w);
  110. glClear(GL_COLOR_BUFFER_BIT);
  111. ImGui_ImplOpenGL2_RenderDrawData(draw_data);
  112. // Present
  113. [[self openGLContext] flushBuffer];
  114. if (!animationTimer)
  115. animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(animationTimerFired:) userInfo:nil repeats:YES];
  116. }
  117. -(void)reshape { [super reshape]; [[self openGLContext] update]; [self updateAndDrawDemoView]; }
  118. -(void)drawRect:(NSRect)bounds { [self updateAndDrawDemoView]; }
  119. -(void)animationTimerFired:(NSTimer*)timer { [self setNeedsDisplay:YES]; }
  120. -(void)dealloc { animationTimer = nil; }
  121. @end
  122. //-----------------------------------------------------------------------------------
  123. // AppDelegate
  124. //-----------------------------------------------------------------------------------
  125. @interface AppDelegate : NSObject <NSApplicationDelegate>
  126. @property (nonatomic, readonly) NSWindow* window;
  127. @end
  128. @implementation AppDelegate
  129. @synthesize window = _window;
  130. -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
  131. {
  132. return YES;
  133. }
  134. -(NSWindow*)window
  135. {
  136. if (_window != nil)
  137. return (_window);
  138. NSRect viewRect = NSMakeRect(100.0, 100.0, 100.0 + 1280.0, 100 + 720.0);
  139. _window = [[NSWindow alloc] initWithContentRect:viewRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:YES];
  140. [_window setTitle:@"Dear ImGui OSX+OpenGL2 Example"];
  141. [_window setAcceptsMouseMovedEvents:YES];
  142. [_window setOpaque:YES];
  143. [_window makeKeyAndOrderFront:NSApp];
  144. return (_window);
  145. }
  146. -(void)setupMenu
  147. {
  148. NSMenu* mainMenuBar = [[NSMenu alloc] init];
  149. NSMenu* appMenu;
  150. NSMenuItem* menuItem;
  151. appMenu = [[NSMenu alloc] initWithTitle:@"Dear ImGui OSX+OpenGL2 Example"];
  152. menuItem = [appMenu addItemWithTitle:@"Quit Dear ImGui OSX+OpenGL2 Example" action:@selector(terminate:) keyEquivalent:@"q"];
  153. [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
  154. menuItem = [[NSMenuItem alloc] init];
  155. [menuItem setSubmenu:appMenu];
  156. [mainMenuBar addItem:menuItem];
  157. appMenu = nil;
  158. [NSApp setMainMenu:mainMenuBar];
  159. }
  160. -(void)dealloc
  161. {
  162. _window = nil;
  163. }
  164. -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
  165. {
  166. // Make the application a foreground application (else it won't receive keyboard events)
  167. ProcessSerialNumber psn = {0, kCurrentProcess};
  168. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  169. // Menu
  170. [self setupMenu];
  171. NSOpenGLPixelFormatAttribute attrs[] =
  172. {
  173. NSOpenGLPFADoubleBuffer,
  174. NSOpenGLPFADepthSize, 32,
  175. 0
  176. };
  177. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  178. AppView* view = [[AppView alloc] initWithFrame:self.window.frame pixelFormat:format];
  179. format = nil;
  180. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  181. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  182. [view setWantsBestResolutionOpenGLSurface:YES];
  183. #endif // MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  184. [self.window setContentView:view];
  185. if ([view openGLContext] == nil)
  186. NSLog(@"No OpenGL Context!");
  187. [view initialize];
  188. }
  189. @end
  190. //-----------------------------------------------------------------------------------
  191. // Application main() function
  192. //-----------------------------------------------------------------------------------
  193. int main(int argc, const char* argv[])
  194. {
  195. @autoreleasepool
  196. {
  197. NSApp = [NSApplication sharedApplication];
  198. AppDelegate* delegate = [[AppDelegate alloc] init];
  199. [[NSApplication sharedApplication] setDelegate:delegate];
  200. [NSApp run];
  201. }
  202. return NSApplicationMain(argc, argv);
  203. }