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. ImGui::NewFrame();
  38. // Global data for the demo
  39. static bool show_demo_window = true;
  40. static bool show_another_window = false;
  41. static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
  42. // 1. Show a simple window.
  43. // Tip: if we don't call ImGui::Begin()/ImGui::End() the widgets automatically appears in a window called "Debug".
  44. {
  45. static float f = 0.0f;
  46. static int counter = 0;
  47. ImGui::Text("Hello, world!"); // Display some text (you can use a format string too)
  48. ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
  49. ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
  50. ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our windows open/close state
  51. ImGui::Checkbox("Another Window", &show_another_window);
  52. if (ImGui::Button("Button")) // Buttons return true when clicked (NB: most widgets return true when edited/activated)
  53. counter++;
  54. ImGui::SameLine();
  55. ImGui::Text("counter = %d", counter);
  56. ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
  57. }
  58. // 2. Show another simple window. In most cases you will use an explicit Begin/End pair to name your windows.
  59. if (show_another_window)
  60. {
  61. ImGui::Begin("Another Window", &show_another_window);
  62. ImGui::Text("Hello from another window!");
  63. if (ImGui::Button("Close Me"))
  64. show_another_window = false;
  65. ImGui::End();
  66. }
  67. // 3. Show the ImGui demo window. Most of the sample code is in ImGui::ShowDemoWindow(). Read its code to learn more about Dear ImGui!
  68. if (show_demo_window)
  69. {
  70. 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!
  71. ImGui::ShowDemoWindow(&show_demo_window);
  72. }
  73. // Rendering
  74. ImGui::Render();
  75. [[self openGLContext] makeCurrentContext];
  76. ImGuiIO& io = ImGui::GetIO();
  77. GLsizei width = (GLsizei)(io.DisplaySize.x * io.DisplayFramebufferScale.x);
  78. GLsizei height = (GLsizei)(io.DisplaySize.y * io.DisplayFramebufferScale.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(ImGui::GetDrawData());
  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. // Flip coordinate system upside down on Y
  110. -(BOOL)isFlipped
  111. {
  112. return (YES);
  113. }
  114. -(void)dealloc
  115. {
  116. animationTimer = nil;
  117. }
  118. // Forward Mouse/Keyboard events to dear imgui OSX back-end. It returns true when imgui is expecting to use the event.
  119. -(void)keyUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  120. -(void)keyDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  121. -(void)flagsChanged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  122. -(void)mouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  123. -(void)mouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  124. -(void)scrollWheel:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event); }
  125. @end
  126. //-----------------------------------------------------------------------------------
  127. // ImGuiExampleAppDelegate
  128. //-----------------------------------------------------------------------------------
  129. @interface ImGuiExampleAppDelegate : NSObject <NSApplicationDelegate>
  130. @property (nonatomic, readonly) NSWindow* window;
  131. @end
  132. @implementation ImGuiExampleAppDelegate
  133. @synthesize window = _window;
  134. -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
  135. {
  136. return YES;
  137. }
  138. -(NSWindow*)window
  139. {
  140. if (_window != nil)
  141. return (_window);
  142. NSRect viewRect = NSMakeRect(100.0, 100.0, 100.0 + 1280.0, 100 + 720.0);
  143. _window = [[NSWindow alloc] initWithContentRect:viewRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:YES];
  144. [_window setTitle:@"ImGui OSX+OpenGL2 Example"];
  145. [_window setOpaque:YES];
  146. [_window makeKeyAndOrderFront:NSApp];
  147. return (_window);
  148. }
  149. -(void)setupMenu
  150. {
  151. NSMenu* mainMenuBar = [[NSMenu alloc] init];
  152. NSMenu* appMenu;
  153. NSMenuItem* menuItem;
  154. appMenu = [[NSMenu alloc] initWithTitle:@"ImGui OSX+OpenGL2 Example"];
  155. menuItem = [appMenu addItemWithTitle:@"Quit ImGui OSX+OpenGL2 Example" action:@selector(terminate:) keyEquivalent:@"q"];
  156. [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand];
  157. menuItem = [[NSMenuItem alloc] init];
  158. [menuItem setSubmenu:appMenu];
  159. [mainMenuBar addItem:menuItem];
  160. appMenu = nil;
  161. [NSApp setMainMenu:mainMenuBar];
  162. }
  163. -(void)dealloc
  164. {
  165. _window = nil;
  166. }
  167. -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
  168. {
  169. // Make the application a foreground application (else it won't receive keyboard events)
  170. ProcessSerialNumber psn = {0, kCurrentProcess};
  171. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  172. // Menu
  173. [self setupMenu];
  174. NSOpenGLPixelFormatAttribute attrs[] =
  175. {
  176. NSOpenGLPFADoubleBuffer,
  177. NSOpenGLPFADepthSize, 32,
  178. 0
  179. };
  180. NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  181. ImGuiExampleView* view = [[ImGuiExampleView alloc] initWithFrame:self.window.frame pixelFormat:format];
  182. format = nil;
  183. #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  184. if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6)
  185. [view setWantsBestResolutionOpenGLSurface:YES];
  186. #endif // MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  187. [self.window setContentView:view];
  188. if ([view openGLContext] == nil)
  189. NSLog(@"No OpenGL Context!");
  190. // Setup Dear ImGui binding
  191. IMGUI_CHECKVERSION();
  192. ImGui::CreateContext();
  193. ImGuiIO& io = ImGui::GetIO(); (void)io;
  194. //io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
  195. ImGui_ImplOSX_Init();
  196. ImGui_ImplOpenGL2_Init();
  197. // Setup style
  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. }