I'm currently learning about VertexArray and I'm currently struggling to fix the issue I am having. The issue is that the VertexArray would not draw to the screen. I have tested the code with sprites, and the sprite was drawn to screen successfully.
#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();
}
}
Here's the simple code that draws random color inside quads onto a screen. The tutorial I was following was this one by CodingMadeEasy. Thanks in advance everyone!
First of all you created an empty vertex array. Create one with a type and an initial number of vertices sf::VertexArray vArray (sf::Quads, 4);
And you cleared the window before you actually drawn it, Window.display();
goes before Window.clear();
.
Here is the working code:
#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();
}
}