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

Author Topic: Drawing Lists of RectangleShapes.  (Read 1553 times)

0 Members and 1 Guest are viewing this topic.

MartinVilche

  • Newbie
  • *
  • Posts: 2
    • View Profile
Drawing Lists of RectangleShapes.
« on: July 09, 2017, 12:34:38 am »
Hello, I'm doing some test on SFML to know how the Engine of the game I'm making will work and i found a problem:
    When I try to draw the elements on a list of shapes it only draws the first one.
int main(){
   
    sf::RenderWindow window(sf::VideoMode(640,480), "TESTING", sf::Style::Default);
    window.setFramerateLimit(60);
   
    window.setKeyRepeatEnabled(false);
   
    struct Node{
       
        sf::RectangleShape rect;
       
        Node* next;
   
    };
   
    int q = 0;
   
    Node* list = NULL;
    Node* d = list;
    Node* r = list;
   
    sf::Event event;
   
    while(window.isOpen()){
       
        while(window.pollEvent(event))
            if(event.type == sf::Event::Closed)
                window.close();
       
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space)){
           
            r = list;
           
            while(r != NULL){
                printf("in loop\n");
                r = r->next;
            }
           
            if(r == list){
               
                list = new Node;
                printf("created a new one. (first one)\n");
                list->rect.setSize(sf::Vector2f(25,25));
                list->rect.setPosition(25*q,25*q);
                q++;
               
                list->next = NULL;
            }
            else if(r == NULL){
               
                r = new Node;
                printf("created a new one.\n");
                r->rect.setSize(sf::Vector2f(25,25));
                r->rect.setPosition(25*q,25*q);
                q++;
               
                r->next = NULL;
            }
       
        }
               
       
        window.clear();
       
        while(d != NULL){
           
            window.draw(d->rect);
            d=d->next;
       
        }
       
        if(d == NULL)
            d = list;
       
        window.display();
    }
   
    return 0;
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing Lists of RectangleShapes.
« Reply #1 on: July 09, 2017, 01:22:32 am »
Each node in a list should point to the next one. At no time are you setting next to anything but null.

Is there a reason that you aren't just using std::list or std::forward_list?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

MartinVilche

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Drawing Lists of RectangleShapes.
« Reply #2 on: July 09, 2017, 01:59:09 am »
Each node in a list should point to the next one. At no time are you setting next to anything but null.

Is there a reason that you aren't just using std::list or std::forward_list?

I just prefer using pointers myself.
I thought that if i called "r = new Node" when r is pointing at NULL, it would create a new Node pointer.
I will try to learn and use std::list.
Thx

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Drawing Lists of RectangleShapes.
« Reply #3 on: July 09, 2017, 04:36:13 am »
q, d and r are not very descriptive so I can't tell for certain what your intentions are for them.

However, r is just another single pointer. Assigning to it "new Node" allocates memory and creates a new one in the same way it does when you assign it to list. This is completely separate from the list's node and will be lost when a new one is assigned to r then following time, probably leading to memory leakage.
A node's next member is surely the one that should point to the next node?
Maybe (I'm not sure as I haven't learnt and stepped through your code) instead of creating a new node where the node is null, create it when then next member is null. For example (untested):
// iterate until you reach the first null "next"
while (r->next) // (!= NULL)
    r = r->next;

// create new node directly on the next pointer
r->next = new Node;

// point r at the new node
r = r->next;

// rect setup
r->rect.setSize(sf::Vector2f(25,25));
r->rect.setPosition(25*q,25*q);

// nullify the next member of the new node
r->next = NULL;

// counter?
q++;

std::list is very easy - much easier than safe manual pointer work ;) However, if you don't need a linked list, the default storage container should most likely be an std::vector.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything