C++ SFML 2.0-绘制星域

C++ SFML 2.0-绘制星域,c++,performance,optimization,sfml,C++,Performance,Optimization,Sfml,我正在使用SFML2.0制作一个游戏。背景将是一个星域。我认为在每次游戏开始时随机生成一个星域可能会很好,而不是使用图像,因此每次背景看起来都略有不同。问题是,我现在这样做会让我的比赛慢很多。有人知道是什么导致了速度下降吗?我查看了文档,想知道是什么,但我没有发现任何东西 这是我的密码: #include <SFML/Graphics.hpp> #include "sfml helper/initialization_helpers.h" #include "sfml helper/

我正在使用SFML2.0制作一个游戏。背景将是一个星域。我认为在每次游戏开始时随机生成一个星域可能会很好,而不是使用图像,因此每次背景看起来都略有不同。问题是,我现在这样做会让我的比赛慢很多。有人知道是什么导致了速度下降吗?我查看了文档,想知道是什么,但我没有发现任何东西

这是我的密码:

#include <SFML/Graphics.hpp>
#include "sfml helper/initialization_helpers.h"
#include "sfml helper/cursor_functions.h"
#include "sfml helper/global_event_handler.h"
#include "sfml helper/globals.h"
#include <cstdio>

int main(int argc, char* argv[])
{
    try {
        // some non-related preparations...
        PrepareApplication(argv[0]);
        float width = 640, height = 480;
        sf::RenderWindow window(sf::VideoMode(width, height), "SFML Test", sf::Style::Close);
        window.setFramerateLimit(60);

        // Textures is a global object that has an internal std::map<string, sf::Texture>
        Textures.add("ball.png", "ball");
        sf::Sprite sprite;
        sprite.setTexture(Textures.get("ball"));

        // This is the snippet that generates the starfield
        srand(time(NULL));
        sf::Image starsImg;
        starsImg.create(width, height, sf::Color::Black);
        int numStars = rand() % 20 + 490;
        for (int i = 0; i < numStars; i++) {
            int x = rand() % (int)width;
            int y = rand() % (int)height;
            sf::Color color(255, 255, 255, rand() % 75 + 25);
            starsImg.setPixel(x, y, color);
        }
        sf::Texture starsTexture;
        starsTexture.loadFromImage(starsImg);
        sf::Sprite stars;
        stars.setTexture(starsTexture);

        // main loop
        while (window.isOpen()) {
            if (Flags.isActive && Flags.inFocus) {
                confineCursorToWindow(window);
            } else {
                freeCursor();
            }
            sf::Event event;
            while (window.pollEvent(event)) {
                if (event.type == sf::Event::KeyPressed) {
                    if (event.key.code == sf::Keyboard::Escape) {
                        Flags.isActive = (!Flags.isActive);
                    }
                }
                if (event.type == sf::Event::MouseMoved) {
                    sprite.setPosition(event.mouseMove.x, event.mouseMove.y);
               }
                // handles default events like sf::Event::WindowClosed
                handleDefaultEventsForWindow(event, window);
            }
            window.clear();
            window.draw(stars); // here's where I draw the stars
            window.draw(sprite);
            window.display();
        }
    } catch (const char* error) {
        printf("%s\n", error);
        exit(1);
    }

    return 0;
}
#包括
#包括“sfml helper/initialization\u helpers.h”
#包括“sfml助手/cursor_functions.h”
#包括“sfml帮助程序/全局事件处理程序.h”
#包括“sfml助手/globals.h”
#包括
int main(int argc,char*argv[])
{
试一试{
//一些无关的准备工作。。。
准备应用程序(argv[0]);
浮动宽度=640,高度=480;
sf::RenderWindow窗口(sf::VideoMode(宽度、高度),“SFML测试”,sf::Style::Close);
设置帧率限制(60);
//纹理是具有内部std::贴图的全局对象
添加(“ball.png”、“ball”);
雪碧雪碧;
sprite.setTexture(Textures.get(“ball”));
//这是生成starfield的代码段
srand(时间(空));
sf::Image starsImg;
创建(宽度、高度、sf::颜色::黑色);
int numStars=rand()%20+490;
for(int i=0;i
编辑

我试着加载一个星域的图像,并尝试绘制,游戏仍然运行缓慢


另外,我所说的慢,是指被称为“精灵”的精灵在跟随鼠标时会滞后。这实际上是一个速度问题吗?

你确定你只在游戏开始时生成星光场,而不是每一帧吗?它在
while(window.isOpened){…}
循环之外,所以它应该是。。。但是,为了更好地检查,我将发布完整的代码。您是否尝试过创建渲染窗口并直接绘制纹理?这可能不是你的问题的原因,但我认为图像->纹理->精灵是一个开销;当你处理纹理和其他东西的时候,RenderWindow也更好。我能够自己完美地运行星际一代。您应该使用探查器查看是否有其他东西阻碍了程序的运行。