簡體   English   中英

CSFML頂點數組和繪圖

[英]CSFML Vertex Array and drawing

幾個星期以來,我一直在為我的學校做一個項目,現在我需要研究粒子。 我一直在研究頂點,它看起來是制作它們的好方法。

我開始嘗試在屏幕上打印至少一個頂點並打印它,但我不知道我做錯了什么。

CSFML 是一個非常受限制的庫,因為沒有多少人使用它,因此試圖找到 SFML 示例並找出 C 函數的派生非常困難,並且給我帶來了一些麻煩。

這是我的代碼:

{
    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);
}

在這個例子中,我試圖創建一個頂點,給它一個 position,一種顏色,然后創建一個頂點數組,它取點頂點並將 append 我的頂點指向頂點數組。 我認為這里唯一的問題是將它打印在屏幕上,如sfRenderWindow_drawVertexArray(window, array, 0); 不打印任何東西,如果我將渲染 state 設置為 1,我的程序甚至在打開我的 window 之前就崩潰了。

我試圖找到有關此 function 的示例和解釋,但我現在幾乎迷路了。

我認為您的錯誤是您沒有在代碼中設置sfPoints 這是一個繪制 4 個點的簡單代碼。

#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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM