Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Editor Line Problem  (Read 1752 times)

0 Members and 1 Guest are viewing this topic.

Meerjel01

  • Newbie
  • *
  • Posts: 28
    • View Profile
Editor Line Problem
« on: May 06, 2020, 03:19:01 pm »
Hey again. I got back into SFML development and are working on an editor for my Raycaster engine. I got the grid and vertex placement via mouse working but I haven't gotten edges(Lines) to work yet.

Here's the editor's main script
int main(int, char const**)
{
   
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window", sf::Style::Resize);
   
    int gridSize = 16;
   
    sf::RectangleShape gridBox(sf::Vector2f(gridSize, gridSize));
    gridBox.setFillColor(sf::Color(0, 0, 0));
   
    MapPoint pnt;
   
    gridBox.setOutlineThickness(1);
    gridBox.setOutlineColor(sf::Color(155, 155, 155));
   
    const int nofRows = 128;
    const int nofCols = 128;
    const int distance = gridSize;
    const float offset = distance/2.f;
    const float height = std::sqrt(std::pow(distance,2.f) - std::pow(offset,2.f));

    sf::Font font;
    if (!font.loadFromFile(resourcePath() + "sansation.ttf"))
    {
        // error...
    }
   
    sf::Texture dotTex;
    if(!dotTex.loadFromFile(resourcePath() + "dot.png"))
    {
        // error...
    }
   
    sf::Text text1;
    sf::Text text2;
   
    text1.setFont(font);
    text2.setFont(font);
    text1.setCharacterSize(8);
    text2.setCharacterSize(8);
    text1.setFillColor(sf::Color::Red);
    text2.setFillColor(sf::Color::Blue);
    text2.setPosition(0, 8);
   
    int currentVertIndex = 0;
    int currentEdgeIndex = 0;
   
    sf::Vector2f firstPointForEdge;
   
    std::vector<Edge> edgeNodes;
    std::vector<Vertex> vertNodes;
    bool addingLine;
   
    // Start the game loop
    while (window.isOpen())
    {
       
        sf::Vector2i pixelPos = sf::Mouse::getPosition(window);
        sf::Vector2f worldPos = window.mapPixelToCoords(pixelPos);
       
       
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Escape pressed: exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
           
            if(event.type == event.MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) {
                if(!addingLine) {
                    firstPointForEdge = worldPos;
                    addingLine = true;
                } else {
                   
                    edgeNodes.push_back(*new Edge(firstPointForEdge, worldPos));
                    addingLine = false;
                }
               
                currentVertIndex++;
                vertNodes.push_back(*new Vertex(worldPos));
            }
           
        }
       
       
       
        text1.setString("World point is " + std::to_string(worldPos.x) + " " + std::to_string(worldPos.y));
        text2.setString("Vertices " + std::to_string(vertNodes.size()) + " " + std::to_string(currentVertIndex));

        // Clear screen
        window.clear();

        for (int i=0; i<nofRows; ++i) {
            for (int j=0; j<nofCols; ++j){
                gridBox.setPosition(j*distance, i*height);
                pnt.setPosition(j*distance - 1.5, i*height - 1.5);
                window.draw(gridBox);

            }
        }
       
        for(int x = 0; x < vertNodes.size(); x++) {
            vertNodes[x].onDraw(window);
        }
        for(int x = 0; x < edgeNodes.size(); x++) {
            edgeNodes[x].onDraw(window);
        }
       

        window.draw(text1);
        window.draw(text2);
       
        // Update the window
        window.display();
    }

    return EXIT_SUCCESS;
}
 

And here's the Edge header
#include <SFML/Graphics.hpp>

class Edge : public sf::Transformable
{
public:
   
    Edge(sf::Vector2f pos1, sf::Vector2f pos2)
    {
        line[0] = sf::Vertex(pos1);
        line[1] = sf::Vertex(pos2);
    }
   
    virtual void onDraw(sf::RenderTarget& target) const
    {
        target.draw(line, 2, sf::Lines);
    }
   
    sf::Vertex line[];
};
 
It will not display and can crash the editor. So what do I have to change?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Editor Line Problem
« Reply #1 on: May 06, 2020, 04:16:27 pm »
Quote
sf::Vertex line[];
This is equivalent to sf::Vertex* line, ie. a pointer to some unallocated memory. You probably wanted to do that:
sf::Vertex line[2];
Laurent Gomila - SFML developer

Meerjel01

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Editor Line Problem
« Reply #2 on: May 06, 2020, 05:15:37 pm »
I figured it out myself. But thanks for trying to help!

 

anything