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

Author Topic: dynamic arrays with circleshapes  (Read 2200 times)

0 Members and 1 Guest are viewing this topic.

barata

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
dynamic arrays with circleshapes
« 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;
}

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: dynamic arrays with circleshapes
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

barata

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: dynamic arrays with circleshapes
« Reply #2 on: June 06, 2014, 02:27:15 pm »
But why the problem appears with the second circleshapes fill?
items[size - 1].setFillColor(Color::Blue);

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: dynamic arrays with circleshapes
« Reply #3 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".
Laurent Gomila - SFML developer

barata

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: dynamic arrays with circleshapes
« Reply #4 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" 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: dynamic arrays with circleshapes
« Reply #5 on: June 06, 2014, 03:15:58 pm »
Run a debugger and check the values.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: dynamic arrays with circleshapes
« Reply #6 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).
« Last Edit: June 06, 2014, 06:06:43 pm by Laurent »
Laurent Gomila - SFML developer

barata

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: dynamic arrays with circleshapes
« Reply #7 on: June 06, 2014, 03:36:31 pm »
It works! Thanks :)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: dynamic arrays with circleshapes
« Reply #8 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 (article I have written).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

barata

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: dynamic arrays with circleshapes
« Reply #9 on: June 06, 2014, 05:05:48 pm »
Thanks, i'll read this article as soon as possible