void GLViewEventHandler::OnGLFWWindowSizeFunCallback(GLFWwindow *windows, int width, int height) { auto view = Director::getInstance()->getOpenGLView(); if(view && view->getResolutionPolicy() != ResolutionPolicy::UNKNOWN) { Size resSize=view->getDesignResolutionSize(); ResolutionPolicy resPolicy=view->getResolutionPolicy(); view->setFrameSize(width, height); view->setDesignResolutionSize(resSize.width, resSize.height, resPolicy); Director::getInstance()->setViewport(); } }
void DynamicLight::debugDraw(cocos2d::Renderer *renderer, const cocos2d::Mat4 &transform, bool transformUpdated) { auto glView = Director::getInstance()->getOpenGLView(); auto width = glView->getDesignResolutionSize().width; auto height = glView->getDesignResolutionSize().height; auto occlusionX = width - lightSize / 2 - getPositionX(); auto occlusionY = height - lightSize / 2 - getPositionY(); auto shadowX = width - lightSize / 2 - getPositionX(); auto shadowY = height - lightSize - 15 - getPositionY(); occlusionMap->getSprite()->setColor(Color3B::RED); occlusionMap->setAnchorPoint({0, 0}); occlusionMap->setPosition({occlusionX, occlusionY}); occlusionMap->visit(renderer, transform, transformUpdated); occlusionMap->getSprite()->setColor(Color3B::WHITE); shadowMap1D->setAnchorPoint({0, 0}); shadowMap1D->setPosition({shadowX, shadowY}); shadowMap1D->visit(renderer, transform, transformUpdated); }
void GLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy) { CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } _designResolutionSize.setSize(width, height); _scaleX = (float)_screenSize.width / _designResolutionSize.width; _scaleY = (float)_screenSize.height / _designResolutionSize.height; if (resolutionPolicy == ResolutionPolicy::NO_BORDER) { _scaleX = _scaleY = MAX(_scaleX, _scaleY); } else if (resolutionPolicy == ResolutionPolicy::SHOW_ALL) { _scaleX = _scaleY = MIN(_scaleX, _scaleY); } else if ( resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) { _scaleX = _scaleY; _designResolutionSize.width = ceilf(_screenSize.width/_scaleX); } else if ( resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) { _scaleY = _scaleX; _designResolutionSize.height = ceilf(_screenSize.height/_scaleY); } // calculate the rect of viewport float viewPortW = _designResolutionSize.width * _scaleX; float viewPortH = _designResolutionSize.height * _scaleY; _viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH); _resolutionPolicy = resolutionPolicy; // reset director's member variables to fit visible rect auto director = Director::getInstance(); director->_winSizeInPoints = getDesignResolutionSize(); director->createStatsLabel(); director->setGLDefaultValues(); }
void GLView::updateDesignResolutionSize() { if (_screenSize.width > 0 && _screenSize.height > 0 && _designResolutionSize.width > 0 && _designResolutionSize.height > 0) { _scaleX = (float)_screenSize.width / _designResolutionSize.width; _scaleY = (float)_screenSize.height / _designResolutionSize.height; if (_resolutionPolicy == ResolutionPolicy::NO_BORDER) { _scaleX = _scaleY = MAX(_scaleX, _scaleY); } else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL) { _scaleX = _scaleY = MIN(_scaleX, _scaleY); } else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) { _scaleX = _scaleY; _designResolutionSize.width = ceilf(_screenSize.width/_scaleX); } else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) { _scaleY = _scaleX; _designResolutionSize.height = ceilf(_screenSize.height/_scaleY); } // calculate the rect of viewport float viewPortW = _designResolutionSize.width * _scaleX; float viewPortH = _designResolutionSize.height * _scaleY; _viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH); // reset director's member variables to fit visible rect auto director = Director::getInstance(); director->_winSizeInPoints = getDesignResolutionSize(); director->_isStatusLabelUpdated = true; director->setProjection(director->getProjection()); // Github issue #16139 // A default viewport is needed in order to display the FPS, // since the FPS are rendered in the Director, and there is no viewport there. // Everything, including the FPS should renderer in the Scene. glViewport(0, 0, _screenSize.width, _screenSize.height); } }
void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy) { CCAssert(resolutionPolicy != kResolutionUnKnown, "should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } m_obDesignResolutionSize.setSize(width, height); m_fScaleX = (float)m_obScreenSize.width / m_obDesignResolutionSize.width; m_fScaleY = (float)m_obScreenSize.height / m_obDesignResolutionSize.height; if (resolutionPolicy == kResolutionNoBorder) { m_fScaleX = m_fScaleY = MAX(m_fScaleX, m_fScaleY); } if (resolutionPolicy == kResolutionShowAll) { m_fScaleX = m_fScaleY = MIN(m_fScaleX, m_fScaleY); } if ( resolutionPolicy == kResolutionFixedHeight) { m_fScaleX = m_fScaleY; m_obDesignResolutionSize.width = ceilf(m_obScreenSize.width/m_fScaleX); } if ( resolutionPolicy == kResolutionFixedWidth) { m_fScaleY = m_fScaleX; m_obDesignResolutionSize.height = ceilf(m_obScreenSize.height/m_fScaleY); } // calculate the rect of viewport float viewPortW = m_obDesignResolutionSize.width * m_fScaleX; float viewPortH = m_obDesignResolutionSize.height * m_fScaleY; m_obViewPortRect.setRect((m_obScreenSize.width - viewPortW) / 2, (m_obScreenSize.height - viewPortH) / 2, viewPortW, viewPortH); m_eResolutionPolicy = resolutionPolicy; // reset director's member variables to fit visible rect CAApplication::getApplication()->m_obWinSizeInPoints = getDesignResolutionSize(); CAApplication::getApplication()->createStatsLabel(); CAApplication::getApplication()->setGLDefaultValues(); }
void EGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy) { CCAssert(resolutionPolicy != kResolutionUnKnown, "should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } _designResolutionSize.setSize(width, height); _scaleX = (float)_screenSize.width / _designResolutionSize.width; _scaleY = (float)_screenSize.height / _designResolutionSize.height; if (resolutionPolicy == kResolutionNoBorder) { _scaleX = _scaleY = MAX(_scaleX, _scaleY); } if (resolutionPolicy == kResolutionShowAll) { _scaleX = _scaleY = MIN(_scaleX, _scaleY); } if ( resolutionPolicy == kResolutionFixedHeight) { _scaleX = _scaleY; _designResolutionSize.width = ceilf(_screenSize.width/_scaleX); } if ( resolutionPolicy == kResolutionFixedWidth) { _scaleY = _scaleX; _designResolutionSize.height = ceilf(_screenSize.height/_scaleY); } // calculate the rect of viewport float viewPortW = _designResolutionSize.width * _scaleX; float viewPortH = _designResolutionSize.height * _scaleY; _viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH); _resolutionPolicy = resolutionPolicy; // reset director's member variables to fit visible rect Director::sharedDirector()->_winSizeInPoints = getDesignResolutionSize(); Director::sharedDirector()->createStatsLabel(); Director::sharedDirector()->setGLDefaultValues(); }
void GLView::updateDesignResolutionSize() { if (_screenSize.width > 0 && _screenSize.height > 0 && _designResolutionSize.width > 0 && _designResolutionSize.height > 0) { _scaleX = (float)_screenSize.width / _designResolutionSize.width; _scaleY = (float)_screenSize.height / _designResolutionSize.height; if (_resolutionPolicy == ResolutionPolicy::NO_BORDER) { _scaleX = _scaleY = MAX(_scaleX, _scaleY); } else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL) { _scaleX = _scaleY = MIN(_scaleX, _scaleY); } else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) { _scaleX = _scaleY; _designResolutionSize.width = ceilf(_screenSize.width/_scaleX); } else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) { _scaleY = _scaleX; _designResolutionSize.height = ceilf(_screenSize.height/_scaleY); } // calculate the rect of viewport float viewPortW = _designResolutionSize.width * _scaleX; float viewPortH = _designResolutionSize.height * _scaleY; _viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH); // reset director's member variables to fit visible rect auto director = Director::getInstance(); director->_winSizeInPoints = getDesignResolutionSize(); director->_isStatusLabelUpdated = true; director->setProjection(director->getProjection()); } }
cocos2d::Sprite* GfxUtils::PrintScreen(cocos2d::Texture2D** pTexture) { auto glview = Director::getInstance()->getOpenGLView(); const cocos2d::Size& visibleSzie = glview->getFrameSize(); int nWidth = (int)visibleSzie.width; int nHeight = (int)visibleSzie.height; int bytePerPixel = 4; GLubyte *pTempData = NULL; Image* pShotImage = new Image(); do { pTempData = new GLubyte[nWidth * nHeight * bytePerPixel]; HDC hDCScreen = GetDC(GetDesktopWindow()); HBITMAP hBitmap = ::CreateCompatibleBitmap(hDCScreen, nWidth, nHeight); HDC hDCMem = CreateCompatibleDC(hDCScreen); HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hDCMem, hBitmap); ::BitBlt(hDCMem, 0, 0, nWidth, nHeight, hDCScreen, 0, 0, SRCCOPY); ::SelectObject(hDCMem, hOldBitmap); BITMAP bm; ::GetObject(hBitmap, sizeof(BITMAP), &bm);// 获取位图信息 DWORD dwImageSize = bm.bmWidthBytes * bm.bmHeight; BITMAPINFOHEADER bih;// 设置位图信息头 bih.biSize = sizeof(BITMAPINFOHEADER); // 位图信息头尺寸 bih.biWidth = bm.bmWidth; // 位图宽度 bih.biHeight = bm.bmHeight; // 位图高度 bih.biBitCount = bm.bmBitsPixel; // 设置每像素所用字节数 bih.biCompression = BI_RGB; bih.biPlanes = 1; bih.biSizeImage = dwImageSize; bih.biXPelsPerMeter = 0; bih.biYPelsPerMeter = 0; bih.biClrImportant = 0; bih.biClrUsed = 0; int ret = GetDIBits(hDCMem, hBitmap, 0, bih.biHeight, pTempData, (BITMAPINFO*)&bih, DIB_RGB_COLORS); /*glPixelStorei(GL_PACK_ALIGNMENT, 1); glReadPixels(0, 0, nSavedBufferWidth, nSavedBufferHeight, GL_RGBA, GL_UNSIGNED_BYTE, pTempData);*/ pShotImage->initWithRawData(pTempData, nWidth * nHeight * bytePerPixel, nWidth, nHeight, 8); // 删除创建的资源 ::DeleteDC(hDCMem); ::DeleteObject(hBitmap); ::DeleteDC(hDCScreen); } while (0); //pShotImage->saveToFile("abc.png"); CC_SAFE_DELETE_ARRAY(pTempData); (*pTexture)->initWithImage(pShotImage); CC_SAFE_DELETE(pShotImage); Sprite* pScreenShotSprite = Sprite::createWithTexture(*pTexture); pScreenShotSprite->setAnchorPoint(Vec2(0.5f, 0.5f)); cocos2d::Size resolution = glview->getDesignResolutionSize(); // 缩放至适合屏幕大小 cocos2d::Size spriteSize = pScreenShotSprite->getContentSize(); float scale = resolution.width / spriteSize.width; pScreenShotSprite->setScaleX(scale); pScreenShotSprite->setScaleY(-scale); // return pScreenShotSprite; }
Size TDDHelper::getScreenSize() { auto glView = Director::getInstance()->getOpenGLView(); return glView->getDesignResolutionSize(); }