main.mm 9.4 KB

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