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

Author Topic: First drawing, the program crash after the 3rd rectangle  (Read 1484 times)

0 Members and 1 Guest are viewing this topic.

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
First drawing, the program crash after the 3rd rectangle
« on: May 06, 2020, 01:35:07 pm »
Hello, i'm exercising with drawing in SFML ...

I successfully drawn 2 rectangles that overlap.

What i want to do is a space here i can finally draw text (channel space for a lighting software)

i'm a first rectangle with borders, then another rectangle inside the first one, then another rectangle inside also.

The 2 first rectangles are OK, and the program runs, when i add the third rectangle in the code, the program crash immediatly. This drawing will result in a class later, but right now, i'm juste trying to draw things to see what i can do.

I'm not sure i'm doing the good thing to create what i want to do and if there is another best method for this, please tell me, i'm so new in using graphic libraries and kind of programming rookie who really want to learn to do the good things.

Wondering why is it crashing ?

Thx by advance.

Here is my code :

#include <SFML/Graphics.hpp>
#include <iostream>
#include "fonctionsdetexte.h"

using namespace std;

int main()
{
    sf::RenderWindow window(sf::VideoMode(800,600), "voici une fenêtre");
    window.setVerticalSyncEnabled(true);
    sf::Event event;
    sf::Font font;
    sf::Text text;
    sf::Vertex vertex;
if (!font.loadFromFile("OCRA_0.ttf"))
{
    cout << "font not loaded" << endl;
}

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

        while (window.pollEvent(event))
            {
            channel_job_core(event);
            }

    window.clear(sf::Color::Black);

    sf::RectangleShape unders(sf::Vector2f(50.f, 70.f));
    unders.setFillColor(sf::Color(0 , 0, 255));
    unders.setOutlineThickness(2.f);
    unders.setOutlineColor(sf::Color(255, 255, 255));
    unders.setPosition(10, 10);
    window.draw(unders);
    sf::RectangleShape channel_strip(sf::Vector2f(50.f, 20.f));
    channel_strip.setFillColor(sf::Color(255, 0, 0));
    channel_strip.setPosition(10, 10);
    window.draw(channel_strip);
    // RectangleShape 'info_strip' make the program crashes,
    // if commented, the program works and display the 2 first rectangle
    sf::RectangleShape info_strip(sf::Vector2f(50.f, 20.f));
    info_strip.setFillColor(sf::Color(255, 0, 0));
    info_strip.setPosition(10, 35);
    window.draw(info_strip);

    /*text.setFont(font);
    text.setString("wildcat");
    text.setCharacterSize(54);
    text.setFillColor(sf::Color::Red);
    text.setStyle(sf::Text::Bold | sf::Text::Underlined);
    window.draw(text);*/



    window.display();
    }

return 0;
}

 

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: First drawing, the program crash after the 3rd rectangle
« Reply #1 on: May 06, 2020, 02:49:30 pm »
Hello!

First question is: where does it crash and what errors are reported?

Next, I should inform you that you should not be testing "event" without it first being polled.
If the event hasn't been polled at all, it could have random data inside it and that could be considered a "close" event, which would close your program.
It should only be tested in the "pollEvent" loop. What does channel_job_core do?

Something else to consider is that you can create the drawable objects outside the window loop rather than being created on every loop although it shouldn't cause any problems (it does help to make it clearer later on though). It's often nicer to (mostly) only do the window drawing between the clear and update rather than creating and updating objects.

In conclusion, it's unlikely that the third rectangle is the actual source of the problem. Make sure you're processing the events properly first. Then, if it's actually crashing, check the error message and look at the stack.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: First drawing, the program crash after the 3rd rectangle
« Reply #2 on: May 06, 2020, 03:25:13 pm »
Ok, actually you are really a Hero ...

I moved the windows close event in the poll.event loop and it's not crashing anymore, you had the good intuition.

Channel_job_core does keyboard input treatment like filtering numbers, buffering number and number processing cases.

The drawing will be moved into a class, i'm just testing how to right now, it will not remain in the main but will be graphics of a particular object. Not really sure if i can process like that, but there will have an object named channel wich does several things and will contain it's own graphics. This object will repeat several times, up to 2048 times. Graphics for each channel will be repeated on a grid ...

do you think i should start right now with vertex array as it seems more complex to me ? i'm not sur i have understood everything yet. !!!

do i have to make a class with only the graphics of my channels or can it be contained in a class that also processes other things, even if they are related ?
« Last Edit: May 06, 2020, 03:28:34 pm by Riton_Lafouine »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: First drawing, the program crash after the 3rd rectangle
« Reply #3 on: May 06, 2020, 03:39:18 pm »
You are welcome!

If you are going to be drawing thousands of rectangles, yes, use a vertex array (or more than one, depending).

To use a vertex array to draw multiple rectangles, the simplest way is to just use the quads primitive type. Then, each four vertices are the corners of each rectangle.

e.g.
for rectangle A, rectangle B, rectangle C.
1 is top-left, 2 is bottom-left, 3 is bottom-right, 4 is top-right.

your vertices in your vertex array would look something like this:

A1, A2, A3, A4, B1, B2, B3, B4, C1, C2, C3, C4.

Notice that the number of vertices in the vertex array would be the number of rectangles multiplied by 4 (the number of vertices (corners) of the rectangle). Here it's 3 rectangles x 4 corners = 12 vertices.


I can't say I fully understand what you mean by "channel".


If you're considering these rectangles to represent a "tile map", you should first take a look at:
https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php#example-tile-map
And then, consider using the one in Selba Ward:
Tile Map (https://github.com/Hapaxia/SelbaWard/wiki/Tile-Map)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: First drawing, the program crash after the 3rd rectangle
« Reply #4 on: May 06, 2020, 04:32:31 pm »
Thanx for your answers,

i'm very grateful that you use your time to help.

I will try to be more clear but my english is not that fantastic !

First, the project on what i am working is a software lighting console used to control lighting during a show.

i am going to create a class named channel, this is the basic entity in a lighting console, a single channel is, to be simple, dedicated to manage one projector, basically adjusting its dimming level, or controling a moving light projector, wich is a little bit more tricky.

In the class i want to create, there will be all the code used to control level of the projector, if the channel is selected, its output ect ...

i want it as an object because we don't use only 1 channel, but several. My guess is that i can have the graphics code in the class, so i can make interact graphics with status of the channel :

for example : if selected, border is white, if not border is black ...

but i'm not sure if i can do that, if i have to create a single object for graphics or if my class with all the features of the channel can contain the codes for graphics.  This is actually my main question.

The interest graphics to be in the object is that if the object have not been created, mainly because there is no projector here to be controlled, i will not display anything.

i will work on vertex array, but i think i will need to fry a few neurons with  that ... but as usual i need to experiment ...

Thx
RLF

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: First drawing, the program crash after the 3rd rectangle
« Reply #5 on: May 06, 2020, 04:37:40 pm »
Normally, yes, you could have multiple "channels" or class objects, each with a rectangle (or other drawable object) stored inside it.
This, in fact, should be fine for however many you want to store.
If, however, you draw more than a few hundred at once, you'll need to "batch" them together to draw at once with a vertex array.

Basically, storing some thousands of objects, each with a rectangle is fine. Drawing thousands of rectangles is not without batching. If you only draw some at once, you should be okay.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Riton_Lafouine

  • Newbie
  • *
  • Posts: 16
    • View Profile
Re: First drawing, the program crash after the 3rd rectangle
« Reply #6 on: May 06, 2020, 05:22:37 pm »
Ok,

Thank you very much for your answers ... Time for coding all of this now. All 3 neurons at max activity !