VertexArray没有绘制到屏幕上

时间:2018-10-04 17:07:43

标签: sfml

我目前正在学习VertexArray,目前正在努力解决我遇到的问题。问题是VertexArray无法绘制到屏幕上。我已经用精灵测试了代码,并且精灵已成功绘制到屏幕上。

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
    sf::Vector2i screenDimensions(800,600);
    sf::Vector2i blockDimensions(10,10);

    srand(time(0));

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(screenDimensions.x,screenDimensions.y),"My first SFML game!");

    while(Window.isOpen())
    {
        sf::Event Evnt;
        while(Window.pollEvent(Evnt))
        {
            switch(Evnt.type)
            {
                case(sf::Event::Closed):
                    Window.close();
                    break;
            }
        }
        for(int i=0; i< screenDimensions.x/blockDimensions.x; i++)
        {
            for(int j=0; j<screenDimensions.y/blockDimensions.y;j++)
            {
                sf::VertexArray vArray;
                vArray[0].position = sf::Vector2f(i*blockDimensions.x,j*blockDimensions.y);
                vArray[1].position = sf::Vector2f(i*blockDimensions.x + blockDimensions.x, j*blockDimensions.y);
                vArray[2].position = sf::Vector2f(i*blockDimensions.x + blockDimensions.x, j*blockDimensions.y + blockDimensions.y);
                vArray[3].position = sf::Vector2f(i*blockDimensions.x,j*blockDimensions.y + blockDimensions.y);

                for(int k=0;k<4;k++)
                {
                    int red = rand() % 255;
                    int green = rand() % 255;
                    int blue = rand() % 255;

                    vArray[k].color = sf::Color(red,green,blue);
                }
                Window.draw(vArray);
            }
        }

        Window.clear();
        Window.display();
    }

}

这是简单的代码,可将四边形内部的随机颜色绘制到屏幕上。我关注的教程是CodingMadeEasy的this one。预先感谢大家!

1 个答案:

答案 0 :(得分:0)

首先,您创建了一个空的顶点数组。创建一个类型为顶点sf::VertexArray vArray (sf::Quads, 4);

的顶点

然后您在实际绘制窗口之前便清除了该窗口,Window.display();Window.clear();之前。

这是工作代码:

#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
#include <ctime>
#include <cstdlib>

int main()
{
    sf::Vector2i screenDimensions(800,600);
    sf::Vector2i blockDimensions(10,10);

    srand(time(0));

    sf::RenderWindow Window;
    Window.create(sf::VideoMode(screenDimensions.x,screenDimensions.y),"My first SFML game!");

    while(Window.isOpen())
    {
        sf::Event Evnt;
        while(Window.pollEvent(Evnt))
        {
            switch(Evnt.type)
            {
                case(sf::Event::Closed):
                    Window.close();
                    break;
            }
        }
        for(int i=0; i< screenDimensions.x/blockDimensions.x; i++)
        {
            for(int j=0; j<screenDimensions.y/blockDimensions.y;j++)
            {
                sf::VertexArray vArray (sf::Quads, 4);
                vArray[0].position = sf::Vector2f(i*blockDimensions.x,j*blockDimensions.y);
                vArray[1].position = sf::Vector2f(i*blockDimensions.x + blockDimensions.x, j*blockDimensions.y);
                vArray[2].position = sf::Vector2f(i*blockDimensions.x + blockDimensions.x, j*blockDimensions.y + blockDimensions.y);
                vArray[3].position = sf::Vector2f(i*blockDimensions.x,j*blockDimensions.y + blockDimensions.y);

                for(int k=0;k<4;k++)
                {
                    int red = rand() % 255;
                    int green = rand() % 255;
                    int blue = rand() % 255;

                    vArray[k].color = sf::Color(red,green,blue);
                }
                Window.draw(vArray);
            }
        }

        Window.display();
        Window.clear();
    }

}