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

Author Topic: Giving sf::RenderWindow.Draw() a pointer inside an array  (Read 4411 times)

0 Members and 1 Guest are viewing this topic.

Frostbite

  • Newbie
  • *
  • Posts: 17
    • View Profile
Giving sf::RenderWindow.Draw() a pointer inside an array
« on: September 24, 2010, 12:20:14 am »
Hello, I've been trying to figure this one out, I have a function that loops through an array of pointers to classes which have sprites, and then tells the Render Window to draw them. I'm not sure whats wrong, but whenever the line
Code: [Select]
Window.Draw(Array[X][Y]->BlockSprite);
is in the function (or a similar line anywhere else) the sfml render window will crash. When this line is not there, it is fine.

Here is the code for the function, and ask if there is anything else I can give you guys, thanks in advance.

Code: [Select]

Square * Array[23][23];// class Square has sf::Sprite BlockSprite;

void DrawManager()
{
    for (int X = 0; X < 24; X = X + 1)
    {
        for (int Y = 0; Y < 24; Y = Y + 1)
        {
            if (Array[X][Y] != 0)
            {
                Window.Draw(Array[X][Y]->BlockSprite);
            }
        }
    }
}


The pointers are set to zero if they are not pointing to anything, so its fairly self explanatory what this does... I think XD
Also, originally this was written so that the sprite was private and there was a public function that returned it, I got rid of it in troubleshooting and made the sprite public

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #1 on: September 24, 2010, 01:54:57 am »
Compare the array size with your subscripts.

Why are you using pointers and not Square objects? Do you actually free the allocated memory?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Frostbite

  • Newbie
  • *
  • Posts: 17
    • View Profile
Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #2 on: September 24, 2010, 02:10:59 am »
forgive me but I'm not exactly sure what you mean by subscripts,

The reason I am using pointers rather than an array of classes is because I think it should have a lower memory cost. I don't need 576 squares, just up to that. So I can easily track where the squares are on the grid and access them by allocating memory during runtime when I need it and sticking the pointer in the grid.

Since it is a tetris game, I have a small array that stores the pointers of the four active squares. I can manipulate them and move them how I wish and then update where they are on my grid (the array) easily and they will always stack nicely. Collision checking is easy and fast too, just check underneath the active blocks for a non active block or the floor Once one of them touches the bottom or another block, it is easy to freeze them in place and forget about them, making new blocks up top with pointers in both arrays.

I only have to worry about deleting them in lines or the entire screen, so I can simply delete a line on the large array, both pointer and memory, and then shift all blocks above it down. When time comes to end, its easy to just do that to the entire array.

Does that explain why I am using an array of pointers?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #3 on: September 24, 2010, 02:25:48 am »
Quote from: "Frostbite"
forgive me but I'm not exactly sure what you mean by subscripts
Indexes. An array of size S can be accessed by the index 0 to S-1.

About the pointers: It's okay if you use them, but manual memory management is very error-prone (regarding memory leaks, double deletions, etc.). Smart pointers or generally RAII could be of help, especially in complex code, for example functions with different control paths and multiple exits.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Frostbite

  • Newbie
  • *
  • Posts: 17
    • View Profile
Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #4 on: September 24, 2010, 02:36:06 am »
well, I think my indexes match my loop. 24 index locations in each direction, and 24 iterations of it, all 0-23. Now I'm not sure if  for loop would iterate and then run, but that could be fixed by switching it from an ++X to an X++. But I don't really see how that could effect my window crashing.

And I'm sure I would use them if I knew how =) I'm always up for becoming a better programmer though, especially cause I'm not a good one.

--edit--
looked up smart pointers, I think if I may rewrite it from scratch and include them when I'm done. And certainty in larger projects.

Also,  could a memory leak or a dangling pointer be whats causing this?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #5 on: September 24, 2010, 02:58:23 am »
Quote from: "Frostbite"
well, I think my indexes match my loop. 24 index locations in each direction
No, your array is too small. You only have 23 locations in each direction. Hence the undefined behaviour at out-of-range access.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Frostbite

  • Newbie
  • *
  • Posts: 17
    • View Profile
Giving sf::RenderWindow.Draw() a pointer inside an array
« Reply #6 on: September 24, 2010, 03:08:09 am »
I see the light!

Thanks, if only I had thought to go and look up how array initilization works again. Thanks for pointing that out to me

Problem solved =)

//I feel like such a noob =P

btw, thank you for introducing me to Smart pointers and RAII. I'm a little short on programing concepts outside of the basic language tutorial

 

anything