You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SFML中叠加透明视图无法绘制问题求助

Fixing Your SFML Overlay View Issue

Hey there, let's dig into why your overlay view isn't showing up and get it working properly! I've gone through your code and spotted a few key mistakes that are causing the problem.

Key Issues in Your Original Code

  • Incorrect Viewport Setup: sf::View::setViewport() expects normalized coordinates (values between 0 and 1) that represent a fraction of the window's size/position. You passed in projectsBox.getGlobalBounds() which uses pixel coordinates—this pushes your viewport completely outside the window, so nothing drawn to it ever appears.
  • Misconfigured Origin: Using negative values for setOrigin() was shoving your randomBox outside the viewable area of your overlay view.
  • Missing Event Handling: You didn't include an event loop to handle window closure (small but important for usability!).

Fixed Code with Explanations

Here's the corrected version of your code, with comments breaking down each change:

#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow mainWindow;
    // Removed the invalid 300 parameter in VideoMode (it's bits per pixel, optional and usually 32)
    mainWindow.create(sf::VideoMode(800, 900), "SFML Works", sf::Style::Close);

    // Visual border for our overlay area (only for reference)
    sf::RectangleShape projectsBox(sf::Vector2f(400, 225));
    // Use setPosition() instead of negative setOrigin() to place the box
    projectsBox.setPosition(10, 130);
    projectsBox.setOutlineColor(sf::Color::Black);
    projectsBox.setOutlineThickness(10);
    projectsBox.setFillColor(sf::Color::Transparent);

    // Create our overlay view
    sf::View projectsView;
    // Convert pixel coordinates to normalized viewport values
    float winWidth = mainWindow.getSize().x;
    float winHeight = mainWindow.getSize().y;
    sf::FloatRect viewport(
        projectsBox.getPosition().x / winWidth,
        projectsBox.getPosition().y / winHeight,
        projectsBox.getSize().x / winWidth,
        projectsBox.getSize().y / winHeight
    );
    projectsView.setViewport(viewport);
    // Set the view's visible area to match our projectsBox size/position
    projectsView.setSize(projectsBox.getSize());
    projectsView.setCenter(projectsBox.getPosition() + projectsBox.getSize() / 2.f);

    // Box to draw inside the overlay view
    sf::RectangleShape randomBox(sf::Vector2f(100, 100));
    // Set origin to the center so positioning is intuitive
    randomBox.setOrigin(randomBox.getSize() / 2.f);
    // Place it at the center of our overlay view
    randomBox.setPosition(projectsView.getCenter());
    randomBox.setOutlineColor(sf::Color::Black);
    randomBox.setOutlineThickness(10);
    randomBox.setFillColor(sf::Color::Yellow);

    while (mainWindow.isOpen()) {
        sf::Event event;
        // Handle window events (like closing the window)
        while (mainWindow.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                mainWindow.close();
        }

        mainWindow.clear(sf::Color::White);

        // First draw default view content (our border box)
        mainWindow.setView(mainWindow.getDefaultView());
        mainWindow.draw(projectsBox);

        // Switch to overlay view and draw its content
        mainWindow.setView(projectsView);
        mainWindow.draw(randomBox);

        mainWindow.display();
    }

    return 0;
}

Why This Works

  • Normalized Viewport: By dividing the box's pixel position/size by the window's dimensions, we tell SFML exactly where on the window the overlay view should appear.
  • Proper View Configuration: setSize() and setCenter() ensure the view's coordinate space matches our overlay area, so content drawn to it aligns correctly with the border box.
  • Intuitive Positioning: Setting randomBox's origin to its center makes it easy to place the box exactly where we want it in the overlay.
  • Transparent Empty Areas: Since we only draw content within the overlay view's viewport, any empty space in the view stays transparent (showing the default view's content behind it).

内容的提问来源于stack exchange,提问作者Jacob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 16:52:47