软件逻辑狗子 2024-07-27 17:55 采纳率: 0%
浏览 5

C++SFML编程输入控件乱码

C++SFML编程,想用Text做个输入控件,但是无法显示,并且中文乱码代码页936。,这个怎么修改或者有什么其他的替代方法?

#include <SFML/Graphics.hpp>
#include <string>

class InputText {
public:
    InputText(const std::string& fontFile, int x, int y, int charSize) {
        if (!font.loadFromFile(fontFile)) {
            throw std::runtime_error("Failed to load font");
        }
        text.setFont(font);
        text.setCharacterSize(charSize);
        text.setFillColor(sf::Color::Black);
        text.setPosition(x, y);

        cursor.setSize(sf::Vector2f(1, text.getCharacterSize())); // 设置光标大小
        cursor.setFillColor(sf::Color::Black); // 设置光标颜色
        cursor.setPosition(text.getPosition().x + text.getGlobalBounds().width, text.getPosition().y); // 设置光标位置

        background.setSize(sf::Vector2f(text.getGlobalBounds().width + 10, text.getGlobalBounds().height)); // 设置背景大小
        background.setFillColor(sf::Color::White); // 设置背景颜色
        background.setPosition(text.getPosition().x - 5, text.getPosition().y); // 设置背景位置
    }

    void handleEvent(const sf::Event& event) {
        if (event.type == sf::Event::TextEntered) {
            if (event.text.unicode < 128 || event.text.unicode >= 0x4E00 && event.text.unicode <= 0x9FFF) {
                userInput += static_cast<char>(event.text.unicode);
                text.setString(userInput);
                updateCursorPosition(); // 更新光标位置
            }
        }
    }

    void draw(sf::RenderWindow& window) {
        window.draw(background); // 绘制背景
        window.draw(text);
        window.draw(cursor); // 绘制光标
    }

private:
    void updateCursorPosition() {
        cursor.setPosition(text.getPosition().x + text.getGlobalBounds().width, text.getPosition().y);
    }

    sf::Font font;
    sf::Text text;
    sf::RectangleShape cursor; // 光标形状
    sf::RectangleShape background; // 背景形状
    std::string userInput;
};

int main()
{
    // 创建一个窗口
    sf::RenderWindow window(sf::VideoMode(800, 600), "Input Text Example");

    // 创建输入文本控件实例
    InputText inputText("your_chinese_font.ttf", 10, 10, 24); // 替换为你支持中文的字体文件路径

    // 主循环
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            else {
                inputText.handleEvent(event);
            }
        }

        window.clear();
        inputText.draw(window);
        window.display();
    }

    return 0;
}
  • 写回答

1条回答 默认 最新

  • 专家-司宇[在线] 2024-07-27 17:55
    关注

    引自免费微信小程序:皆我百晓生

    这个问题是由于字符编码不一致导致的,特别是在使用std::string时,它默认使用Unicode编码,而sf::Text则使用UTF-8编码。为了解决这个问题,我们需要将字符串转换为UTF-8编码。

    以下是修改后的代码示例:

    #include <SFML/Graphics.hpp>
    #include <string>
    
    class InputText {
    public:
        InputText(const std::string& fontFile, int x, int y, int charSize) {
            if (!font.loadFromFile(fontFile)) {
                throw std::runtime_error("Failed to load font");
            }
            text.setFont(font);
            text.setCharacterSize(charSize);
            text.setFillColor(sf::Color::Black);
            text.setPosition(x, y);
    
            cursor.setSize(sf::Vector2f(1, text.getCharacterSize())); // 设置光标大小
            cursor.setFillColor(sf::Color::Black); // 设置光标颜色
            cursor.setPosition(text.getPosition().x + text.getGlobalBounds().width, text.getPosition().y); // 设置光标位置
    
            background.setSize(sf::Vector2f(text.getGlobalBounds().width + 10, text.getGlobalBounds().height)); // 设置背景大小
            background.setFillColor(sf::Color::White); // 设置背景颜色
            background.setPosition(text.getPosition().x - 5, text.getPosition().y); // 设置背景位置
        }
    
        void handleEvent(const sf::Event& event) {
            if (event.type == sf::Event::TextEntered) {
                if (event.text.unicode < 128 || event.text.unicode >= 0x4e00 && event.text.unicode <= 0x9fff) {
                    userInput += static_cast<char>(event.text.unicode);
                    text.setString(userInput);
                    updateCursorPosition(); // 更新光标位置
                }
            }
        }
    
        void draw(sf::RenderWindow& window) {
            window.draw(background); // 绘制背景
            window.draw(text);
            window.draw(cursor); // 绘制光标
        }
    
    private:
        void updateCursorPosition() {
            cursor.setPosition(text.getPosition().x + text.getGlobalBounds().width, text.getPosition().y);
        }
    
        sf::Font font;
        sf::Text text;
        sf::RectangleShape cursor; // 光标形状
        sf::RectangleShape background; // 背景形状
        std::string userInput;
    };
    
    int main()
    {
        // 创建一个窗口
        sf::RenderWindow window(sf::VideoMode(800, 600), "Input Text Example");
    
        // 创建输入文本控件实例
        InputText inputText("your_chinese_font.ttf", 10, 10, 24); // 替换为你支持中文的字体文件路径
    
        // 主循环
        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed) {
                    window.close();
                }
                else {
                    inputText.handleEvent(event);
                }
            }
    
            window.clear();
            inputText.draw(window);
            window.display();
        }
    
        return 0;
    }
    

    在这个版本中,我们使用了std::string::operator[](int index)方法来获取Unicode字符,并将其转换为UTF-8编码。这样就可以在sf::Text上正确地显示中文字符了。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 7月27日

悬赏问题

  • ¥15 35114 SVAC视频验签的问题
  • ¥15 impedancepy
  • ¥15 在虚拟机环境下完成以下,要求截图!
  • ¥15 求往届大挑得奖作品(ppt…)
  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见