跳转到内容

Window 模块

类: WindowBase、Window、Context、Cursor、Event、VideoMode、GlResource
结构: ContextSettings
命名空间(原类改 ns): Clipboard、Keyboard、Mouse、Joystick、Sensor、Touch、Vulkan

sf::VideoMode({800, 600}); // size; bpp 可选
sf::VideoMode::getDesktopMode();
sf::VideoMode::getFullscreenModes();
// Style(位标志,非 scoped enum)
sf::Style::None | Titlebar | Resize | Close | Default(=Titlebar|Resize|Close)
// State(scoped)
sf::State::Windowed | Fullscreen

构造示例:

sf::Window window(sf::VideoMode({800, 600}), "Title", sf::Style::Default, sf::State::Windowed);
// 全屏:
sf::RenderWindow rw(sf::VideoMode::getFullscreenModes().at(0), "Title", sf::State::Fullscreen);

聚合初始化;无默认参数构造函数。

成员:depthBits、stencilBits、antiAliasingLevel(注意拼写 A 大写)、majorVersion、minorVersion、attributeFlags(Attribute::Default|Core|Debug),以及 sRGB 相关标志(见头文件完整定义)。

  • 生命周期:create(...)、close()、isOpen()
  • 事件:pollEvent() → std::optional<Event>;waitEvent(Time timeout=Zero);handleEvents(handlers...) 访问者模式
  • 几何:get/setPosition(Vector2i)、get/setSize(Vector2u)、setMinimumSize / setMaximumSize(optional<Vector2u>)
  • setTitle、setIcon、setVisible、setMouseCursorVisible/Grabbed、setMouseCursor
  • setKeyRepeatEnabled、setJoystickThreshold
  • requestFocus、hasFocus、getNativeHandle()(原 getSystemHandle)
  • Window 额外:setVerticalSyncEnabled、setFramerateLimit、setActive、display、getSettings
  • 勿同时开 VSync 与 framerate limit
  • 事件必须在创建窗口的同一线程轮询;macOS 要求主线程

子类型用 is<T>()(无数据)或 getIf<T>()(有数据);另有 visit(Visitor)。

子类型 主要字段
Closed (无)
Resized Vector2u size
FocusLost / FocusGained (无)
TextEntered char32_t unicode
KeyPressed / KeyReleased Keyboard::Key code、Keyboard::Scancode scancode、alt/control/shift/system
MouseWheelScrolled Mouse::Wheel wheel、float delta、Vector2i position
MouseButtonPressed/Released Mouse::Button button、Vector2i position
MouseMoved Vector2i position
MouseMovedRaw Vector2i delta(未加速;Win/Linux)
MouseEntered / MouseLeft (无)
JoystickButtonPressed/Released joystickId、button
JoystickMoved joystickId、Joystick::Axis axis、float position [-100,100]
JoystickConnected/Disconnected joystickId
TouchBegan/Moved/Ended finger、Vector2i position
SensorChanged Sensor::Type type、Vector3f value

实时输入均为命名空间。

  • Scoped enums: sf::Keyboard::Key::A、sf::Keyboard::Scancode::Escape、sf::Mouse::Button::Left、sf::Joystick::Axis::X …
  • Keyboard::isKeyPressed(Key) / isKeyPressed(Scancode);localize / delocalize / getDescription
  • Mouse::isButtonPressed、getPosition() / getPosition(window)、setPosition
  • 滚轮无实时状态,只能靠 MouseWheelScrolled 事件
  • Joystick:isConnected、getAxisPosition、isButtonPressed、getIdentification 等
  • 3.1 弃用: Touch::isDown / getPosition → 用 Touch 事件的 position

可从像素或系统类型加载;构造失败可抛 sf::Exception。Cursor::Type 为 scoped enum。

相关教程:Window 及同目录 Events / Inputs。