使用 SFML 从另一个 class 中绘制主 window

Drawing in the main window from another class with SFML

我需要一些帮助,因为我正在尝试制作俄罗斯方块游戏,这是我使用 C++ 的第一个项目,但我遇到了一些问题。

我想在主文件中保留主要的 window 声明,并从 class BoxRenderer 中提取相同的 window 来绘制所有的四联骨牌, 背景等

但是我的 Sprite 没有出现,我有黑屏,这是我的代码:

main.cpp:

// SpaceOdyssey.cpp : définit le point d'entrée pour l'application console.
//

#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>
#include "BoxRenderer.h"

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 280), "Tetris-like by Orionss");
    sf::Texture bgTexture;

    if (!bgTexture.loadFromFile("sprites\background.png"))
        return EXIT_FAILURE;

    BoxRenderer renderer(bgTexture);

    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)
                window.close();

        window.clear();
        renderer.render(window);
        window.display();
    }

    return 0;
}

BoxRenderer.cpp:

#include "stdafx.h"
#include "BoxRenderer.h"

BoxRenderer::BoxRenderer(sf::Texture bgTexture)
{
    m_bgTexture = bgTexture;
}

void BoxRenderer::render(sf::RenderWindow& win)
{
    m_bgTexture.update(win);
    sf::Sprite background(m_bgTexture);
    win.draw(background);
}

来自void sf::Texture::update(const Window &window) reference

Update the texture from the contents of a window.

您正在清除之前的 window,这会覆盖您的纹理,因此它完全是黑色的。你根本不想打电话给 m_bgTexture.update(win);