简体   繁体   中英

CSFML Vertex Array and drawing

It's been a few weeks i've been working a project for my school and I now need to work on particles. I've been looking at vertices and it looks like to be a good way to make them.

I've started by trying to print at least one vertex on the screen and to print it, but I don't know what I'm doing wrong.

CSFML is a very restricted library as not many people use it, so trying to find SFML examples and to figure out the derivates of the functions to C is quite hard and giving me some troubles.

Here's my code:

{
    sfVertex a;
    sfVector2f apos = {200, 100};
    a.color = sfRed;
    a.position = apos;

    sfVertexArray *array = sfVertexArray_create();
    sfVertexArray_setPrimitiveType(array, sfPoints);
    sfVertexArray_append(array, a);

    sfRenderWindow_drawVertexArray(window, array, 0);
}

In this example, I'm trying to create a vertex, give it a position, a color, and then create a vertex array that takes point vertices and to append my vertex to the vertex array. I think the only problem here is to print it on the screen, as sfRenderWindow_drawVertexArray(window, array, 0); doesn't print anything, and if I put the render state to 1 my program just crashes before even opening my window.

I tried to find examples and explanations about this function but I'm pretty much lost now.

I think your error was that you did not set sfPoints in your code. Here is a simple code that draws 4 points.

#include <iostream>
#include <SFML/Graphics.hpp>


int main(){
  sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");

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

    sf::VertexArray vertexArray (sf::Points, 4); 

    vertexArray[0].position = sf::Vector2f(10, 10);
    vertexArray[1].position = sf::Vector2f(10, 20);
    vertexArray[2].position = sf::Vector2f(20, 10);
    vertexArray[3].position = sf::Vector2f(20, 20);

    // Set colour for all vertices
    for(int i = 0; i < 4.; i++){
      vertexArray[i].color=sf::Color::Yellow;
    }

    window.clear();
    window.draw(vertexArray);
    window.display();
  }

  return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM