SFML community forums

Help => Graphics => Topic started by: barata on June 06, 2014, 02:11:59 pm

Title: dynamic arrays with circleshapes
Post by: barata on June 06, 2014, 02:11:59 pm
Hello, here is my code. Im trying to write the programm which puts circleshapes on cursor position. I must use dynamic arrays. Could you say me, whats wrong and why my programm doesnt work? Thank you very much
#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

using namespace sf;
using namespace std;

RenderWindow app;

class CS
{
public:
        CircleShape *items;
        int size;

        void Add() {
                size++;
                CircleShape *newArray = new CircleShape(size);
                for (int i = 0; i < size - 1; i++)
                        newArray[i] = items[i];
                items = newArray;
                items[size - 1].setFillColor(Color::Blue);
                items[size - 1].setRadius(20);
        }
};
CS circleShape;
int main()
{      
        float xResolution = VideoMode::getDesktopMode().width;
        float yResolution = VideoMode::getDesktopMode().height;
        app.create(VideoMode(xResolution, yResolution), "1", Style::Fullscreen);
        while (true)
        {
                app.clear(Color::White);
                if (Keyboard::isKeyPressed(sf::Keyboard::D))
                {
                        circleShape.Add();
                        int mouseX = Mouse::getPosition().x, mouseY = Mouse::getPosition().y;
                        circleShape.items[circleShape.size - 1].setPosition(mouseX, mouseY);
                        for (int i = 0; i < circleShape.size; i++)
                                app.draw(circleShape.items[i]);
                }
                app.display();
        }
        return 0;
}
Title: AW: dynamic arrays with circleshapes
Post by: eXpl0it3r on June 06, 2014, 02:17:13 pm
There are a few things that could need some clean up, but for your problem you need to pass the window to the getPosition function.
Title: Re: dynamic arrays with circleshapes
Post by: barata on June 06, 2014, 02:27:15 pm
But why the problem appears with the second circleshapes fill?
items[size - 1].setFillColor(Color::Blue);
Title: Re: dynamic arrays with circleshapes
Post by: Laurent on June 06, 2014, 02:52:26 pm
What problem? You didn't give any detail in your first post, all we know is that "it doesn't work".
Title: Re: dynamic arrays with circleshapes
Post by: barata on June 06, 2014, 03:03:55 pm
The problem appears in this line: items[size - 1].setFillColor(Color::Blue); (line 23) when size = 2.
Error: "Access violation when writing to the address" 
Title: AW: dynamic arrays with circleshapes
Post by: eXpl0it3r on June 06, 2014, 03:15:58 pm
Run a debugger and check the values.
Title: Re: dynamic arrays with circleshapes
Post by: Laurent on June 06, 2014, 03:30:52 pm
CircleShape *newArray = new CircleShape(size);

==>

CircleShape *newArray = new CircleShape[size];

I'm not surprised you got problems with manual memory management. Avoid it, and use one of the abstractions of the standard library (std::vector for example).
Title: Re: dynamic arrays with circleshapes
Post by: barata on June 06, 2014, 03:36:31 pm
It works! Thanks :)
Title: Re: dynamic arrays with circleshapes
Post by: Nexus on June 06, 2014, 03:42:18 pm
It works!
You should really consider Laurent's advice. Use STL containers whenever possible. They're easy to use, more robust, more efficient and have more features than self-written dynamic data structures.

You might also be interested in why you should avoid new and delete (http://www.bromeon.ch/articles/raii.html) (article I have written).
Title: Re: dynamic arrays with circleshapes
Post by: barata on June 06, 2014, 05:05:48 pm
Thanks, i'll read this article as soon as possible