static bool getDrawableBounds(GLint *width, GLint *height) { #if defined(__linux__) if (dlsym(RTLD_DEFAULT, "eglGetCurrentContext")) { EGLContext currentContext = eglGetCurrentContext(); if (currentContext == EGL_NO_CONTEXT) { return false; } EGLSurface currentSurface = eglGetCurrentSurface(EGL_DRAW); if (currentSurface == EGL_NO_SURFACE) { return false; } EGLDisplay currentDisplay = eglGetCurrentDisplay(); if (currentDisplay == EGL_NO_DISPLAY) { return false; } if (!eglQuerySurface(currentDisplay, currentSurface, EGL_WIDTH, width) || !eglQuerySurface(currentDisplay, currentSurface, EGL_HEIGHT, height)) { return false; } return true; } #endif #if defined(_WIN32) HDC hDC = wglGetCurrentDC(); if (!hDC) { return false; } HWND hWnd = WindowFromDC(hDC); RECT rect; if (!GetClientRect(hWnd, &rect)) { return false; } *width = rect.right - rect.left; *height = rect.bottom - rect.top; return true; #elif defined(__APPLE__) CGLContextObj ctx = CGLGetCurrentContext(); if (ctx == NULL) { return false; } CGSConnectionID cid; CGSWindowID wid; CGSSurfaceID sid; if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) { return false; } CGRect rect; if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) { return false; } *width = rect.size.width; *height = rect.size.height; return true; #elif defined(HAVE_X11) Display *display; Drawable drawable; Window root; int x, y; unsigned int w, h, bw, depth; display = glXGetCurrentDisplay(); if (!display) { return false; } drawable = glXGetCurrentDrawable(); if (drawable == None) { return false; } if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) { return false; } *width = w; *height = h; return true; #else return false; #endif }
static bool getDrawableBounds(GLint *width, GLint *height) { #if defined(_WIN32) HDC hDC = wglGetCurrentDC(); if (!hDC) { return false; } HWND hWnd = WindowFromDC(hDC); RECT rect; if (!GetClientRect(hWnd, &rect)) { return false; } *width = rect.right - rect.left; *height = rect.bottom - rect.top; #elif defined(__APPLE__) CGLContextObj ctx = CGLGetCurrentContext(); if (ctx == NULL) { return false; } CGSConnectionID cid; CGSWindowID wid; CGSSurfaceID sid; if (CGLGetSurface(ctx, &cid, &wid, &sid) != kCGLNoError) { return false; } CGRect rect; if (CGSGetSurfaceBounds(cid, wid, sid, &rect) != 0) { return false; } *width = rect.size.width; *height = rect.size.height; #else Display *display; Drawable drawable; Window root; int x, y; unsigned int w, h, bw, depth; display = glXGetCurrentDisplay(); if (!display) { return false; } drawable = glXGetCurrentDrawable(); if (drawable == None) { return false; } if (!XGetGeometry(display, drawable, &root, &x, &y, &w, &h, &bw, &depth)) { return false; } *width = w; *height = h; #endif return true; }