在SDL2,C ++中,TTF_SizeText没有为“ i”,“ j”和“ 1”到目前为止发现返回正确的值

问题描述

我有一个textbox类,可以很好地与a,b,c等较宽的字符配合使用,但是对于像'f'和'l'这样的字符,它似乎错误地获得了这些字符的大小,但正确地获得了其他尺寸?这是用于textbox类的文本的“突出显示”代码,稍后会对其进行修正,但是应该对其进行记录以易于理解。

void Textbox::Highlight_Text(SDL_Renderer *renderer)
{
    if (clickedOn == true){


        int currentCharacterWidth = 0;
        int currentCharacterHeight = 0;
        int totalSize = 0;
        SDL_Rect currentCharacterRect;
        string currentCharacter,tempText;



        if (highlightedCharacters.size() >= 1){ ///To make sure only 1 thing is highlighted,in conjunction with next part
            highlighted = true;
        }

        if (highlighted == true){   /// if a part is highlighted,and is left highlighted,next time clicked,remove the highlighting and redo it
            if (EVENTS.mouseClicked == false){
                resetHighlightingNextClick = true;
            }
        }

        if (resetHighlightingNextClick == true){
            if (highlighted == true){
                if (EVENTS.mouseClicked == true){       ///actually remove the highlighting
                    highlightedCharacters.clear();
                    indexOfCharactersHighlighted.clear();
                    highlighted = false;
                    resetHighlightingNextClick = false;
                }
            }
        }



        for (int i=0; i < textboxText.Get_Text().size(); i++){
            currentCharacter =  textboxText.Get_Text()[i];
            TTF_SizeText(textboxText.fonts[textboxText.fontIndex],currentCharacter.c_str(),&currentCharacterWidth,&currentCharacterHeight);

            ///the totalSize added to rectangle is not making it wider,its adjusting its x value offset
            currentCharacterRect = {textboxText.x + totalSize,textboxText.y + int(textboxText.textSize*0.1),currentCharacterWidth,currentCharacterHeight};
            totalSize += currentCharacterWidth; ///"current" size of text in loop to get x value of specific character clicked on

            ///If mouse is touching any of the characters in the text
            if ( SDL_PointInRect(&EVENTS.mousePos,&currentCharacterRect) ){
                EVENTS.Change_Cursor(SDL_SYSTEM_CURSOR_IBEAM);

                if (EVENTS.mouseClicked == true){   ///Clicking on the text to highlight
                    if (In_Array(highlightedCharacters,currentCharacterRect.x) == false  ){
                        highlightedCharacters.push_back(currentCharacterRect);  ///If there is no duplicates
                        indexOfCharactersHighlighted.push_back(i); ///Get index of text being highlighted,its always in order too

                    }

                    if (  currentCharacterRect.x != highlightedCharacters[highlightedCharacters.size()-1].x){ ///So they don't stack up highlights,ie,you can remove them
                        /// If the mouse is not highlighting the last one,say second last on the right for example,delete the one in front of it (last one)
                        ///Like when highlighting text with mouse,it adapts to how you move it,so it unhighlights text not being highlighted
                        highlightedCharacters.pop_back();
                        indexOfCharactersHighlighted.pop_back();

                    }
                }

            }

        }///End for loop




        if (highlighted == true ){
            if (EVENTS.backspacePressed == true || EVENTS.currentKey != ""){
                tempText = textboxText.Get_Text();

                ///remove highlighted characters
                if (indexOfCharactersHighlighted.size() != 0){
                    ///the range of values highlighted will always be in a sorted order
                    tempText.erase( Min(indexOfCharactersHighlighted),Max(indexOfCharactersHighlighted)-Min(indexOfCharactersHighlighted)+1  );  ///erase the range of values highlighted
                    textboxText.Change_Text(renderer,tempText);

                    ///once removed text,clear every highlighted related thing
                    highlightedCharacters.clear();
                    indexOfCharactersHighlighted.clear();
                    highlighted = false;
                    resetHighlightingNextClick = false;

                    EVENTS.backspacePressed = false;
                    EVENTS.currentKey = "";
                }

            }
        }



    }   ///End if for clicked on



    ///fit with scrolling offsets
    if (EVENTS.scrolled == true){
        for (int p=0; p < highlightedCharacters.size(); p++){
            highlightedCharacters[p].y += EVENTS.scrollVal;
        }
    }




    ///Drawing the highlighted text
    if (highlighted == true   &&   clickedOn == true){
        SDL_SetRenderDrawBlendMode(renderer,SDL_BLENDMODE_BLEND);
        SDL_SetRenderDrawColor(renderer,55,60,65,75);
        for (int j=0; j < highlightedCharacters.size(); j++){
            SDL_RenderFillRect(renderer,&highlightedCharacters[j]);
        }
        SDL_SetRenderDrawBlendMode(renderer,SDL_BLENDMODE_NONE);
    }


    ///when clicked off textbox,clear everything/highlighting
    if (clickedOn == false){
        highlightedCharacters.clear();
        indexOfCharactersHighlighted.clear();
        highlighted = false;
    }



}

要以传入的字体作为参考,这是我在文本类中获取它的方式

    fontIndex = textSize-lowestFontSize  -1;

    ///One time setups
    if (numOfInstances == 1){
        try{
            TTF_Init();
            //cout << "Initialised ttf" << endl;
        }
        catch (exception &err){
            cout << "Could not initialise ttf for text \"" << text << "\". Error from SDL is: " << TTF_GetError() << ". Error from C++ is: " << err.what() << endl;
        }

        for (int i=lowestFontSize; i <= highestFontSize; i++){
            TTF_Font *currentFont = TTF_OpenFont(fontType.c_str(),i);
            if (!currentFont){
                cout << "Error with font in text \"" << txt << "\" Error is: " << SDL_GetError() << endl;
            }

            fonts.push_back(currentFont);
        }

    }

,所以如果我传入,例如说25作为我的文本大小,则我有我的lowestFontSize = 10和highestFontSize = 100,所以我需要25号的索引是(25-10 -1 = 14)从0开始,这是我在文本类中创建字体的静态向量之前的第一行。这是我要解释的摘录:

enter image description here

这显然可以正常工作。

enter image description here

但是现在,这是完全不准确的。如果我在文本末尾选择了一个随机字符,则该字符没有正确突出显示,只是从头开始看起来就很完美,但随后看起来好像不准确了,因此使总的灰色突出显示范围更宽了比预期的要好。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)