main.mm 11 KB

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