void Sprite::updateTextureCoords(fzRect rect) { Texture2D *texture = getTexture(); if(texture == NULL) return; fzFloat wide = texture->getPixelsWide(); fzFloat high = texture->getPixelsHigh(); rect *= texture->getFactor(); #if FZ_FIX_ARTIFACTS_BY_STRECHING_TEXEL wide *= 2; high *= 2; rect.origin.x = rect.origin.x*2+1; rect.origin.y = rect.origin.y*2+1; rect.size.width = rect.size.width*2-2; rect.size.height = rect.size.height*2-2; #endif // ! FZ_FIX_ARTIFACTS_BY_STRECHING_TEXEL fzFloat A = rect.origin.x / wide; fzFloat B = A + rect.size.width / wide; fzFloat C = rect.origin.y / high; fzFloat D = C + rect.size.height / high; if(m_rectRotated) { if( m_flipX ) FZ_SWAP(D, C); if( m_flipY ) FZ_SWAP(A, B); m_texCoords[0] = fzVec2(A, D); m_texCoords[1] = fzVec2(A, C); m_texCoords[2] = fzVec2(B, D); m_texCoords[3] = fzVec2(B, C); } else { if( m_flipX ) FZ_SWAP(A, B); if( m_flipY ) FZ_SWAP(C, D); m_texCoords[0] = fzVec2(A, D); m_texCoords[1] = fzVec2(B, D); m_texCoords[2] = fzVec2(A, C); m_texCoords[3] = fzVec2(B, C); } makeDirty(kFZDirty_texcoords); }
fzPoint Director::convertToGL(fzPoint point, bool isFlippedY) const { fzSize canvasSize = getCanvasSize(); if(getOrientation() == kFZOrientation_LandscapeLeft) FZ_SWAP(canvasSize.width, canvasSize.height); fzAffineTransform transform; if(isFlippedY) { transform.scale(1, -1); transform.translate(0, -canvasSize.height); } transform.scale(canvasSize.width / m_renderingRect.size.width, canvasSize.height / m_renderingRect.size.height); transform.concat(m_orientationTransform); return point.applyTransform(transform); }
void Director::updateProjection() { FZ_ASSERT(OSW::Instance(), "OS Wrapper is not defined."); updateViewRect(); if(!(m_dirtyFlags & kFZDDirty_projection)) return; // VIEW PORT // The view port must be the display size in pixels. // Display size is not equal to the screen size, an application could not use the whole screen. fzSize viewPort = getViewPort(); glViewport(0, 0, viewPort.width, viewPort.height); // PROJECTION // The projection must be calculated using the canvas size. No pixels here. fzSize canvasSize = getCanvasSize(); fzOrientation orientation = getOrientation(); if(orientation == kFZOrientation_LandscapeLeft || orientation == kFZOrientation_LandscapeRight) FZ_SWAP(canvasSize.width, canvasSize.height); switch (m_projection) { case kFZProjection2D: { fzMath_mat4OrthoProjection(0, canvasSize.width, 0, canvasSize.height, -1024, 1024, m_transformMV); break; } default: FZ_RAISE("Director: Unrecognized projection."); } m_orientationTransform = FZAffineTransformIdentity; if(orientation == kFZOrientation_LandscapeLeft) { m_orientationTransform.translate(canvasSize.width, 0); m_orientationTransform.rotate(FZ_DEGREES_TO_RADIANS(90)); fzMat4 mat; fzMath_mat4Multiply(m_transformMV, m_orientationTransform, mat); fzMath_mat4Copy(mat, m_transformMV); }else if(orientation == kFZOrientation_LandscapeRight) { m_orientationTransform.translate(0, canvasSize.height); m_orientationTransform.rotate(FZ_DEGREES_TO_RADIANS(-90)); fzMat4 mat; fzMath_mat4Multiply(m_transformMV, m_orientationTransform, mat); fzMath_mat4Copy(mat, m_transformMV); } m_orientationTransform = m_orientationTransform.getInverse(); m_dirtyFlags &= ~kFZDDirty_projection; if(p_runningScene) { p_runningScene->updateLayout(); if(p_hud) p_hud->updateLayout(); } }
void Director::updateViewRect() { if(!(m_dirtyFlags & kFZDDirty_viewPort)) return; if(m_windowSize == FZSizeZero && m_originalCanvasSize == FZSizeZero) setFullscreen(); else if(m_windowSize == FZSizeZero) setWindowSize(m_originalCanvasSize); fzSize windowSize = getWindowSize(); fzSize canvasSize = (m_originalCanvasSize == FZSizeZero) ? windowSize : m_originalCanvasSize; fzOrientation orientation = getOrientation(); if(orientation == kFZOrientation_LandscapeLeft || orientation == kFZOrientation_LandscapeRight) { FZ_SWAP(canvasSize.width, canvasSize.height); } fzFloat windowRate = windowSize.width/windowSize.height; fzFloat canvasRate = canvasSize.width/canvasSize.height; fzSize newCanvasSize = canvasSize; // could be the same if(windowRate == canvasRate) { // No resizing because the canvas and window rate is the same. m_renderingRect = fzRect(FZPointZero, windowSize); }else{ // The window and the canvas rate is different, so we have to apply // the proper resizing algorythm switch (m_resizeMode) { case kFZResizeMode_None: { m_renderingRect.size = canvasSize; m_renderingRect.origin = (windowSize - canvasSize)/2; break; } case kFZResizeMode_Expand: { m_renderingRect = fzRect(FZPointZero, windowSize); break; } case kFZResizeMode_Fit: { if(canvasRate > windowRate) { m_renderingRect.size.width = windowSize.width; m_renderingRect.size.height = canvasSize.height * windowSize.width/canvasSize.width; m_renderingRect.origin = fzPoint(0, (windowSize.height-m_renderingRect.size.height)/2); }else{ m_renderingRect.size.height = windowSize.height; m_renderingRect.size.width = canvasSize.width * windowSize.height/canvasSize.height; m_renderingRect.origin = fzPoint((windowSize.width-m_renderingRect.size.width)/2, 0); } break; } case kFZResizeMode_FitFill: { if(canvasRate > windowRate) newCanvasSize.height = canvasSize.width / windowRate; else newCanvasSize.width = canvasSize.height * windowRate; m_renderingRect = fzRect(FZPointZero, windowSize); break; } case kFZResizeMode_IntFit: { fzFloat factorX = windowSize.width / canvasSize.width; fzFloat factorY = windowSize.height / canvasSize.height; fzFloat factor = MIN(factorX, factorY); factor = (factor >= 1.0f) ? static_cast<fzInt>(factor) : factor; m_renderingRect.size = canvasSize * factor; m_renderingRect.origin = (windowSize - m_renderingRect.size)/2; break; } case kFZResizeMode_IntFitFill: { fzFloat factorX = windowSize.width / canvasSize.width; fzFloat factorY = windowSize.height / canvasSize.height; fzFloat factor = MIN(factorX, factorY); factor = (factor >= 1.0f) ? static_cast<fzInt>(factor) : factor; newCanvasSize = windowSize / factor; m_renderingRect = fzRect(FZPointZero, windowSize); break; } default: break; } } m_canvasSize = newCanvasSize; if(orientation == kFZOrientation_LandscapeLeft || orientation == kFZOrientation_LandscapeRight) { FZ_SWAP(m_canvasSize.width, m_canvasSize.height); } // FACTORS fzSize viewPort = getViewPort(); fzFloat factorX = viewPort.width / newCanvasSize.width; fzFloat factorY = viewPort.height / newCanvasSize.height; // COMPUTE FINAL FACTOR fzFloat newFactor = roundf(MAX(MAX(factorX, factorY), 1)); if(newFactor > DeviceConfig::Instance().getMaxFactor()) newFactor = DeviceConfig::Instance().getMaxFactor(); if(newFactor > m_resourcesFactor) { TextureCache::Instance().removeAllTextures(); FontCache::Instance().removeAllFonts(); } m_resourcesFactor = newFactor; m_dirtyFlags &= ~kFZDDirty_viewPort; m_dirtyFlags |= kFZDDirty_projection; // Notify changes to the OS Wrapper OSW::setOrientation(orientation); OSW::updateWindow(); }