| |
| |
| |
|
|
| #import <Cocoa/Cocoa.h> |
| #import <OpenGL/gl.h> |
| #import <OpenGL/glu.h> |
|
|
| #include "imgui.h" |
| #include "imgui_impl_opengl2.h" |
| #include "imgui_impl_osx.h" |
|
|
| |
| |
| |
|
|
| @interface AppView : NSOpenGLView |
| { |
| NSTimer* animationTimer; |
| } |
| @end |
|
|
| @implementation AppView |
|
|
| -(void)prepareOpenGL |
| { |
| [super prepareOpenGL]; |
|
|
| #ifndef DEBUG |
| GLint swapInterval = 1; |
| [[self openGLContext] setValues:&swapInterval forParameter:NSOpenGLCPSwapInterval]; |
| if (swapInterval == 0) |
| NSLog(@"Error: Cannot set swap interval."); |
| #endif |
| } |
|
|
| -(void)initialize |
| { |
| |
| |
| IMGUI_CHECKVERSION(); |
| ImGui::CreateContext(); |
| ImGuiIO& io = ImGui::GetIO(); (void)io; |
| |
| |
|
|
| |
| ImGui::StyleColorsDark(); |
| |
|
|
| |
| ImGui_ImplOSX_Init(self); |
| ImGui_ImplOpenGL2_Init(); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| } |
|
|
| -(void)updateAndDrawDemoView |
| { |
| |
| ImGui_ImplOpenGL2_NewFrame(); |
| ImGui_ImplOSX_NewFrame(self); |
| ImGui::NewFrame(); |
|
|
| |
| static bool show_demo_window = true; |
| static bool show_another_window = false; |
| static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f); |
|
|
| |
| if (show_demo_window) |
| ImGui::ShowDemoWindow(&show_demo_window); |
|
|
| |
| { |
| static float f = 0.0f; |
| static int counter = 0; |
|
|
| ImGui::Begin("Hello, world!"); |
|
|
| ImGui::Text("This is some useful text."); |
| ImGui::Checkbox("Demo Window", &show_demo_window); |
| ImGui::Checkbox("Another Window", &show_another_window); |
|
|
| ImGui::SliderFloat("float", &f, 0.0f, 1.0f); |
| ImGui::ColorEdit3("clear color", (float*)&clear_color); |
|
|
| if (ImGui::Button("Button")) |
| counter++; |
| ImGui::SameLine(); |
| ImGui::Text("counter = %d", counter); |
|
|
| ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate); |
| ImGui::End(); |
| } |
|
|
| |
| if (show_another_window) |
| { |
| ImGui::Begin("Another Window", &show_another_window); |
| ImGui::Text("Hello from another window!"); |
| if (ImGui::Button("Close Me")) |
| show_another_window = false; |
| ImGui::End(); |
| } |
|
|
| |
| ImGui::Render(); |
| ImDrawData* draw_data = ImGui::GetDrawData(); |
|
|
| [[self openGLContext] makeCurrentContext]; |
| GLsizei width = (GLsizei)(draw_data->DisplaySize.x * draw_data->FramebufferScale.x); |
| GLsizei height = (GLsizei)(draw_data->DisplaySize.y * draw_data->FramebufferScale.y); |
| glViewport(0, 0, width, height); |
| glClearColor(clear_color.x * clear_color.w, clear_color.y * clear_color.w, clear_color.z * clear_color.w, clear_color.w); |
| glClear(GL_COLOR_BUFFER_BIT); |
|
|
| ImGui_ImplOpenGL2_RenderDrawData(draw_data); |
|
|
| |
| [[self openGLContext] flushBuffer]; |
|
|
| if (!animationTimer) |
| animationTimer = [NSTimer scheduledTimerWithTimeInterval:0.017 target:self selector:@selector(animationTimerFired:) userInfo:nil repeats:YES]; |
| } |
|
|
| -(void)reshape { [super reshape]; [[self openGLContext] update]; [self updateAndDrawDemoView]; } |
| -(void)drawRect:(NSRect)bounds { [self updateAndDrawDemoView]; } |
| -(void)animationTimerFired:(NSTimer*)timer { [self setNeedsDisplay:YES]; } |
| -(void)dealloc { animationTimer = nil; } |
|
|
| |
| |
| |
|
|
| |
| |
| -(void)mouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)rightMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)otherMouseDown:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)mouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)rightMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)otherMouseUp:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)mouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)mouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)rightMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)rightMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)otherMouseMoved:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)otherMouseDragged:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
| -(void)scrollWheel:(NSEvent *)event { ImGui_ImplOSX_HandleEvent(event, self); } |
|
|
| @end |
|
|
| |
| |
| |
|
|
| @interface AppDelegate : NSObject <NSApplicationDelegate> |
| @property (nonatomic, readonly) NSWindow* window; |
| @end |
|
|
| @implementation AppDelegate |
| @synthesize window = _window; |
|
|
| -(BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication |
| { |
| return YES; |
| } |
|
|
| -(NSWindow*)window |
| { |
| if (_window != nil) |
| return (_window); |
|
|
| NSRect viewRect = NSMakeRect(100.0, 100.0, 100.0 + 1280.0, 100 + 720.0); |
|
|
| _window = [[NSWindow alloc] initWithContentRect:viewRect styleMask:NSWindowStyleMaskTitled|NSWindowStyleMaskMiniaturizable|NSWindowStyleMaskResizable|NSWindowStyleMaskClosable backing:NSBackingStoreBuffered defer:YES]; |
| [_window setTitle:@"Dear ImGui OSX+OpenGL2 Example"]; |
| [_window setAcceptsMouseMovedEvents:YES]; |
| [_window setOpaque:YES]; |
| [_window makeKeyAndOrderFront:NSApp]; |
|
|
| return (_window); |
| } |
|
|
| -(void)setupMenu |
| { |
| NSMenu* mainMenuBar = [[NSMenu alloc] init]; |
| NSMenu* appMenu; |
| NSMenuItem* menuItem; |
|
|
| appMenu = [[NSMenu alloc] initWithTitle:@"Dear ImGui OSX+OpenGL2 Example"]; |
| menuItem = [appMenu addItemWithTitle:@"Quit Dear ImGui OSX+OpenGL2 Example" action:@selector(terminate:) keyEquivalent:@"q"]; |
| [menuItem setKeyEquivalentModifierMask:NSEventModifierFlagCommand]; |
|
|
| menuItem = [[NSMenuItem alloc] init]; |
| [menuItem setSubmenu:appMenu]; |
|
|
| [mainMenuBar addItem:menuItem]; |
|
|
| appMenu = nil; |
| [NSApp setMainMenu:mainMenuBar]; |
| } |
|
|
| -(void)dealloc |
| { |
| _window = nil; |
| } |
|
|
| -(void)applicationDidFinishLaunching:(NSNotification *)aNotification |
| { |
| |
| ProcessSerialNumber psn = {0, kCurrentProcess}; |
| TransformProcessType(&psn, kProcessTransformToForegroundApplication); |
|
|
| |
| [self setupMenu]; |
|
|
| NSOpenGLPixelFormatAttribute attrs[] = |
| { |
| NSOpenGLPFADoubleBuffer, |
| NSOpenGLPFADepthSize, 32, |
| 0 |
| }; |
|
|
| NSOpenGLPixelFormat* format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs]; |
| AppView* view = [[AppView alloc] initWithFrame:self.window.frame pixelFormat:format]; |
| format = nil; |
| #if MAC_OS_X_VERSION_MAX_ALLOWED >= 1070 |
| if (floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_6) |
| [view setWantsBestResolutionOpenGLSurface:YES]; |
| #endif |
| [self.window setContentView:view]; |
|
|
| if ([view openGLContext] == nil) |
| NSLog(@"No OpenGL Context!"); |
|
|
| [view initialize]; |
| } |
|
|
| @end |
|
|
| |
| |
| |
|
|
| int main(int argc, const char* argv[]) |
| { |
| @autoreleasepool |
| { |
| NSApp = [NSApplication sharedApplication]; |
| AppDelegate* delegate = [[AppDelegate alloc] init]; |
| [[NSApplication sharedApplication] setDelegate:delegate]; |
| [NSApp run]; |
| } |
| return NSApplicationMain(argc, argv); |
| } |
|
|