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

Author Topic: Memory Issue due a lot of Rect Objects  (Read 3156 times)

0 Members and 1 Guest are viewing this topic.

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Memory Issue due a lot of Rect Objects
« on: October 08, 2014, 07:23:11 am »
Hey guys!

Code:
namespace CM_Commander
{
    class Hexagon
    {
        RectangleShape pixel;
        List<List<RectangleShape>> hexfieldL;

        int width;
        int height;
        int pos_x = 0;
        int pos_y = 0;

        public Hexagon(Vector2f size)
        {
            this.width = (int)size.X;
            this.height = (int)size.Y;
            pixel = new RectangleShape(new Vector2f(1,1));
        }

        public void DrawHexagon(RenderWindow renderwindow)
        {
            for(int y = pos_y; y <= pos_y+height; y++)
                for(int x = pos_x; x <= pos_x+width; x++)
                {
                    if (y == pos_y || y == pos_y+height)
                    {
                        pixel.Position = new Vector2f(x, y);
                        renderwindow.Draw(pixel);
                        continue;
                    }
                    pixel.Position = new Vector2f(pos_x, y);
                    renderwindow.Draw(pixel);
                    pixel.Position = new Vector2f(pos_x+width, y);
                    renderwindow.Draw(pixel);
                }
        }
    }
}

Above you can see my class for drawing a rectangle (actually its the base for drawing a hexagon later - need to start with a simpler form first)
I have a big issue with memory here - It seems that the drawing of the rectangle affects FPS or Game Speed at all...because moving the map around (which u can't see) lags hardcore.

In fact I have a bunch of opinions how this memory littering occurs.

1) I always thought the Draw Buffer (if there exists one) will be cleared after .Display() has been called - if not I might produce a big load of buffer positions due the fact that I draw a simple rectangle out of a lot of rectangle objects.

2) Drawing a rectangle is anyhow a big issue anyway - but can't find a way to draw a pixel which is hierached by "Drawable" Class


What u guys know about these problems?

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Memory Issue due a lot of Rect Objects
« Reply #1 on: October 08, 2014, 07:59:20 am »
Quote
1) I always thought the Draw Buffer (if there exists one) will be cleared after .Display() has been called - if not I might produce a big load of buffer positions due the fact that I draw a simple rectangle out of a lot of rectangle objects.

No, it gets cleared when you call clear().  And I don't think "buffer position" is a real thing.

Quote
2) Drawing a rectangle is anyhow a big issue anyway - but can't find a way to draw a pixel which is hierached by "Drawable" Class

http://sfml-dev.org/tutorials/2.1/graphics-vertex-array.php shows exactly how to do this.


The main thing that jumps out to me from your code snippet is that you're using a list of lists of rectangle shapes...when you should really only need a single VertexArray to represent a polygon.

It almost looks like you're trying to store each individual pixel of the final image as a rectangle shape, which is an extremely terrible idea.  Is this class meant to represent a single hexagon or an entire board? (if the latter, you misnamed it)

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Memory Issue due a lot of Rect Objects
« Reply #2 on: October 08, 2014, 09:32:21 am »
Quote
The main thing that jumps out to me from your code snippet is that you're using a list of lists of rectangle shapes...when you should really only need a single VertexArray to represent a polygon.

It almost looks like you're trying to store each individual pixel of the final image as a rectangle shape, which is an extremely terrible idea.  Is this class meant to represent a single hexagon or an entire board? (if the latter, you misnamed it)

Meh sorry for that - the List actually is an ancient code which I didn't deleted yet. In fact I only created that Rectangle Object called "pixel" and this I drew with every new position iterated by the loop - so no multiple instances here.

I will try Clear() to solve the issue - parallely checking the Link you showed me bro - thx

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Memory Issue due a lot of Rect Objects
« Reply #3 on: October 08, 2014, 09:49:40 am »
meh sadly I already am using Clear() ;P

Falke88

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
Re: Memory Issue due a lot of Rect Objects
« Reply #4 on: October 08, 2014, 10:38:00 am »
namespace CM_Commander
{
    class Hexagon
    {
        VertexArray pixelL;

        int width;
        int height;
        int pos_x = 0;
        int pos_y = 0;

        public Hexagon(Vector2f size)
        {
            this.width = (int)size.X;
            this.height = (int)size.Y;
            pixelL = new VertexArray();
        }

        public void DrawHexagon(RenderWindow renderwindow)
        {
            for (int y = pos_y; y <= pos_y + height; y++)
                for (int x = pos_x; x <= pos_x + width; x++)
                {
                    if (y == pos_y || y == pos_y + height)
                    {
                        pixelL.Append(new Vertex(new Vector2f(x, y), Color.White));
                        continue;
                    }
                    pixelL.Append(new Vertex(new Vector2f(pos_x, y), Color.White));
                    pixelL.Append(new Vertex(new Vector2f(pos_x + width, y), Color.White));
                }
            renderwindow.Draw(pixelL);
            pixelL.Clear();
        }
    }
}

Just changed the code and now it works fine ! Thanks for the help bro

 

anything