08_C++精灵库之inputbox.h头文件源代码(2026年1月15日版)

#ifndef INPUTBOX_H
#define INPUTBOX_H

#include <string>
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>

// 前置声明
class Screen;

class InputBox {
public:
    // 构造函数,接收屏幕对象指针
    InputBox(Screen* screen);
    ~InputBox();

    // 显示输入框并返回输入结果
    std::string show(std::string title, std::string prompt);

private:
    Screen* m_screen;               // 关联的屏幕对象
    SDL_Window* m_window;           // 输入框窗口
    SDL_Renderer* m_renderer;       // 输入框渲染器
    TTF_Font* m_font;               // 字体
    std::string m_inputText;        // 输入的文本
    bool m_quit;                    // 是否退出输入框
    bool m_confirmed;               // 是否确认输入
    const int m_windowWidth = 300;  // 窗口宽度
    const int m_windowHeight = 180; // 窗口高度

    // 初始化输入框窗口和渲染器
    bool init(const std::string& title);
    // 处理事件
    void handleEvents();
    // 渲染输入框界面
    void render(const std::string& prompt);
    // 释放资源
    void cleanup();
};

#endif // INPUTBOX_H

关于李兴球

李兴球的博客是Python创意编程原创博客
此条目发表在C++分类目录。将固定链接加入收藏夹。

发表回复