void fzDrawCircle( const fzVec2& center, fzFloat r, fzFloat a, fzUInt segs, bool drawLineToCenter)
    {
        int additionalSegment = 1;
        if (drawLineToCenter)
            additionalSegment++;
        
        const float coef = 2.0f * (float)M_PI/segs;
        
        fzVec2 *vertices = new fzVec2[segs+2];
        
        for(fzUInt i = 0; i <= segs; i++)
        {
            float rads  = i * coef;
            GLfloat j   = r * fzMath_cos(rads + a) + center.x;
            GLfloat k   = r * fzMath_sin(rads + a) + center.y;
            
            vertices[i] = fzPoint(j, k);
        }
        vertices[segs+1] = center;
        
        fzGLSetMode(kFZGLMode_Primitives);            
#if FZ_GL_SHADERS
        lazyInitialize();
        _fzShader->use();
        FZ_SAFE_APPLY_MATRIX(_fzShader);
        glVertexAttribPointer(kFZAttribPosition, 2, GL_FLOAT, GL_FALSE, 0, vertices);
#else
        glVertexPointer(2, GL_FLOAT, 0, vertices);
#endif
        
        glDrawArrays(GL_LINE_STRIP, 0, (GLsizei) segs+additionalSegment);
        
        delete [] vertices;
    }
Exemple #2
0
 void Menu::alignItemsInColumns(fzPoint paddings, fzUInt *sizes, fzUInt nuColumns)
 {
     // Calculate total size
     fzSize size(-paddings.x, 0);
     Node *node = (Node*)m_children.front();
     
     fzFloat widths[nuColumns];
     
     
     for(fzUInt i = 0; i < nuColumns; ++i) {
         
         fzFloat width = 0;
         fzFloat height = -paddings.y;
         for(fzUInt w = 0; w < sizes[i]; ++i)
         {
             if(node == NULL) {
                 FZ_ASSERT(false, "Too many items.");
                 return;
             }
             
             width = MAX(width, node->getContentSize().width);
             height += node->getContentSize().height + paddings.y;
             node = (Node*)node->next();
         }
         
         size.width += width + paddings.x;
         widths[i] = size.width + width/2 + paddings.x;
         size.height = MAX(height, size.height);
     }
     setContentSize(size);
     
     
     fzUInt index = 0;
     for(fzUInt i = 0; i < nuColumns; ++i) {
         alignVertically(paddings.y, fzPoint(widths[i], m_contentSize.height/2), fzRange(index, sizes[i]));
         index += sizes[i];
     }
 }
Exemple #3
0
    void Label::createFontChars()
    {
        // Get string length
        fzUInt m_stringLen = m_string.size();
    
        Sprite *fontChar = static_cast<Sprite*>(m_children.front());
        if(m_stringLen > 0)
        {
            // Is font available?
            if(p_font == NULL) {
                FZLOGERROR("Label: Impossible to generate label, font config is missing.");
                return;
            }
            
            // Precalculate label size
            fzFloat longestLine = 0;
            fzUInt currentLine = 0;
            char charId = 0, prevId = 0;
            fzFloat lineWidth[100];
            memset(lineWidth, 0, sizeof(fzFloat) * 100);
            
            for(fzUInt i = 0; i < m_stringLen; ++i) {
                charId = m_string.at(i);
                if(charId < 0)
                    FZ_RAISE("Label: CHAR[] doesn't exist. It's negative.");
                
                if( charId == '\n') {
                    longestLine = fzMax(longestLine, lineWidth[currentLine]);
                    ++currentLine;
                }else {
                    lineWidth[currentLine] += p_font->getCharInfo(charId).xAdvance + m_horizontalPadding + p_font->getKerning(prevId, charId);
                    prevId = charId;
                }
            }
            longestLine = fzMax(longestLine, lineWidth[currentLine]);
            
            
            fzFloat lineHeight = p_font->getLineHeight() + m_verticalPadding;
            fzFloat totalHeight = lineHeight * (currentLine+1);
            
            fzInt nextFontPositionY = totalHeight - lineHeight;
            fzInt nextFontPositionX = 0;
            
            prevId = 0; currentLine = 0;
            for(fzUInt i = 0; i < m_stringLen; ++i)
            {
                charId = m_string.at(i);

                // line jump
                if (charId == '\n') {
                    nextFontPositionY -= lineHeight;
                    nextFontPositionX = 0;
                    ++currentLine;
                    continue;
                }
                
                // config line start point
                if (nextFontPositionX == 0)
                {
                    switch (m_alignment) {
                        case kFZLabelAlignment_left:
                            nextFontPositionX = 0;
                            break;
                        case kFZLabelAlignment_right:
                            nextFontPositionX = longestLine - lineWidth[currentLine];
                            //nextFontPositionX = (longestLine - lineWidth[currentLine])/2.0f;
                            break;
                        case kFZLabelAlignment_center:
                            nextFontPositionX = (longestLine - lineWidth[currentLine])/2.0f;
                            break;
                        default:
                            break;
                    }
                }
                
                // get font def
                const fzCharDef& fontDef = p_font->getCharInfo(static_cast<unsigned char>(charId));
                if(fontDef.xAdvance == 0) {
                    char toPrint[3];
                    printChar(toPrint, charId);
                    FZLOGERROR("Label: CHAR[%d] '%s' is not included.", charId, toPrint);
                    nextFontPositionX += p_font->getLineHeight();
                    continue;
                }
                
                // get sprite
                if( fontChar == NULL ) {
                    fontChar = new Sprite();
                    addChild(fontChar);
                    
                }else {
                    // reusing sprites
                    fontChar->setIsVisible(true);
                }
                
                // config sprite
                nextFontPositionX += p_font->getKerning(prevId, charId);
                fzFloat yOffset = p_font->getLineHeight() - fontDef.yOffset;
                fzPoint fontPos = fzPoint(nextFontPositionX + fontDef.xOffset + fontDef.width * 0.5f,
                                          nextFontPositionY + yOffset - fontDef.height * 0.5f );
                
                fontChar->setTextureRect(fontDef.getRect());
                fontChar->setPosition(fontPos);
                fontChar->setColor(m_color);
                
                
                // next sprite
                nextFontPositionX += fontDef.xAdvance + m_horizontalPadding;
                prevId = charId;
                fontChar = static_cast<Sprite*>(fontChar->next());
            }
            
            // new content size
            setContentSize(fzSize(longestLine, totalHeight));
            
        }else{
            
            setContentSize(FZSizeZero);
        }
        
        // make sprites not longer used hidden.
        for(; fontChar; fontChar = static_cast<Sprite*>(fontChar->next()))
            fontChar->setIsVisible(false);
    }
Exemple #4
0
    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();
    }
Exemple #5
0
 Place::Place(fzFloat x, fzFloat y)
 : Place(fzPoint(x, y))
 { }