From 8076d5235c52333c655f42adbefa9e054e111bf7 Mon Sep 17 00:00:00 2001 From: Erwin Coumans Date: Sat, 4 Jun 2016 21:51:21 -0700 Subject: [PATCH] fix window resize/width bookkeeping bug in MacOpenGLWindow.mm --- examples/OpenGLWindow/MacOpenGLWindow.mm | 28 ++++++++---------------- examples/SimpleOpenGL3/main.cpp | 6 ++--- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/examples/OpenGLWindow/MacOpenGLWindow.mm b/examples/OpenGLWindow/MacOpenGLWindow.mm index 301608cca..d3b3ba31e 100644 --- a/examples/OpenGLWindow/MacOpenGLWindow.mm +++ b/examples/OpenGLWindow/MacOpenGLWindow.mm @@ -102,11 +102,11 @@ float loop; } -(void)drawRect:(NSRect)rect { + if (([self frame].size.width != m_lastWidth) || ([self frame].size.height != m_lastHeight)) { m_lastWidth = [self frame].size.width; m_lastHeight = [self frame].size.height; - // Only needed on resize: [m_context clearDrawable]; @@ -114,7 +114,6 @@ float loop; float width = [self frame].size.width; float height = [self frame].size.height; - // Get view dimensions in pixels // glViewport(0,0,10,10); @@ -209,16 +208,12 @@ struct MacOpenGLWindowInternalData m_myview = 0; m_pool = 0; m_window = 0; - m_width = -1; - m_height = -1; m_exitRequested = false; } NSApplication* m_myApp; TestView* m_myview; NSAutoreleasePool* m_pool; NSWindow* m_window; - int m_width; - int m_height; bool m_exitRequested; }; @@ -294,8 +289,6 @@ void MacOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) if (m_internalData) closeWindow(); - int width = ci.m_width; - int height = ci.m_height; const char* windowTitle = ci.m_title; @@ -303,9 +296,7 @@ void MacOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; m_internalData = new MacOpenGLWindowInternalData; - m_internalData->m_width = width; - m_internalData->m_height = height; - + m_internalData->m_pool = [NSAutoreleasePool new]; m_internalData->m_myApp = [NSApplication sharedApplication]; //myApp = [MyApp sharedApplication]; @@ -373,7 +364,7 @@ void MacOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) [newItem release]; */ - NSRect frame = NSMakeRect(0., 0., width, height); + NSRect frame = NSMakeRect(0., 0., ci.m_width, ci.m_height); m_internalData->m_window = [NSWindow alloc]; [m_internalData->m_window initWithContentRect:frame @@ -423,9 +414,7 @@ void MacOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci) [m_internalData->m_window makeKeyAndOrderFront: nil]; [m_internalData->m_myview MakeCurrent]; - m_internalData->m_width = m_internalData->m_myview.GetWindowWidth; - m_internalData->m_height = m_internalData->m_myview.GetWindowHeight; - + [NSApp activateIgnoringOtherApps:YES]; @@ -1035,13 +1024,13 @@ void MacOpenGLWindow::startRendering() float aspect; //b3Vector3 extents; - if (m_internalData->m_width > m_internalData->m_height) + if (getWidth() > getHeight()) { - aspect = (float)m_internalData->m_width / (float)m_internalData->m_height; + aspect = (float)getWidth() / (float)getHeight(); //extents.setValue(aspect * 1.0f, 1.0f,0); } else { - aspect = (float)m_internalData->m_height / (float)m_internalData->m_width; + aspect = (float)getHeight() / (float)getWidth(); //extents.setValue(1.0f, aspect*1.f,0); } @@ -1136,6 +1125,7 @@ int MacOpenGLWindow::getWidth() const { if (m_internalData && m_internalData->m_myview && m_internalData->m_myview.GetWindowWidth) return m_internalData->m_myview.GetWindowWidth; + return 0; } @@ -1152,7 +1142,7 @@ void MacOpenGLWindow::setResizeCallback(b3ResizeCallback resizeCallback) [m_internalData->m_myview setResizeCallback:resizeCallback]; if (resizeCallback) { - (resizeCallback)(m_internalData->m_width,m_internalData->m_height); + (resizeCallback)(getWidth(), getHeight()); } } diff --git a/examples/SimpleOpenGL3/main.cpp b/examples/SimpleOpenGL3/main.cpp index 015db129e..726abf693 100644 --- a/examples/SimpleOpenGL3/main.cpp +++ b/examples/SimpleOpenGL3/main.cpp @@ -14,8 +14,8 @@ static b3MouseButtonCallback sOldMouseButtonCB = 0; static b3KeyboardCallback sOldKeyboardCB = 0; //static b3RenderCallback sOldRenderCB = 0; -float gWidth = 0 ; -float gHeight = 0; +float gWidth = 1024; +float gHeight = 768; void MyWheelCallback(float deltax, float deltay) { @@ -61,7 +61,7 @@ int main(int argc, char* argv[]) b3CommandLineArgs myArgs(argc,argv); - SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",1024,768,true); + SimpleOpenGL3App* app = new SimpleOpenGL3App("SimpleOpenGL3App",gWidth,gHeight,true); app->m_instancingRenderer->getActiveCamera()->setCameraDistance(13); app->m_instancingRenderer->getActiveCamera()->setCameraPitch(0);