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

Author Topic: Beakpoint in visual studio  (Read 2623 times)

0 Members and 2 Guests are viewing this topic.

pasierdamian

  • Newbie
  • *
  • Posts: 2
    • View Profile
Beakpoint in visual studio
« on: August 22, 2015, 06:38:30 pm »
Hello i have problem with one function in my multi moving circle app.


#include <SFML/Graphics.hpp>
#include <vector>
#include <string>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <string>
#include "Ball.h"
#include  "Rectangle.h"

#include <SFML/Graphics/Text.hpp>
#include <ctime>
using namespace sf;
using namespace std;
Font font;
//tablica zawieraj&#261;ca wielko&#347;&#263; promieni kul
float radius[49];
// Tablice poczatkwoych warto&#347;ci kul
float beginningPositionX[49];
float beginningPositionY[49];
//predkosc poruszania sie kuli
float xSpeed[49];
float ySpeed[49];
//tablica liczb losowanych z przedzia&#322;u 0-49

// Wywo&#322;&#261;nie timera to losowania
Clock deltaClock;
Time myEventTimer;
//odbicie kuli od kraw&#281;dzi prostokata rectangle

int random[6];
void balls_Reflection(vector<Ball> & ball)
{

        for (int i = 0; i < 49; i++)
        {
                if (ball[i].get_Position().x < 620){
                        int positionY = ball[i].get_Position().y;
                        if (positionY <= 20){ ySpeed[i] = -ySpeed[i]; }
                        if (positionY >= 520.0 - 2 * ball[i].get_Radius()){ ySpeed[i] = -ySpeed[i]; }
                        int positionX = ball[i].get_Position().x;
                        if (positionX <= 20){ xSpeed[i] = -xSpeed[i]; }
                        if (positionX >= 620.0 - 2 * ball[i].get_Radius()){ xSpeed[i] = -xSpeed[i]; }

                }
                }


}
//losownaie kul nie diza&#322;a poprawnie


void random_Balls(vector<Ball> & ball, int random[6])
{



        if (deltaClock.getElapsedTime() >= seconds(5)){

                ball[random[0]].set_Position(650, 40 + ball[random[0]].get_Radius());
                ball[random[0]].set_CharacterPosition(ball[random[0]].get_Position().x + ball[random[0]].get_Radius() * 2 / 3, ball[random[0]].get_Position().y + ball[random[0]].get_Radius() / 4);
                if (deltaClock.getElapsedTime() >= seconds(8)){
                        ball[1].set_Position(650, 80 + ball[1].get_Radius() * 2);
                        ball[1].set_CharacterPosition(ball[1].get_Position().x + ball[1].get_Radius() * 2 / 3, ball[1].get_Position().y + ball[1].get_Radius() / 4);
                        if (deltaClock.getElapsedTime() >= seconds(11)){
                                ball[2].set_Position(650, 120 + ball[2].get_Radius() * 2);
                                ball[2].set_CharacterPosition(ball[2].get_Position().x + ball[2].get_Radius() * 2 / 3, ball[2].get_Position().y + ball[2].get_Radius() / 4);
                                if (deltaClock.getElapsedTime() >= seconds(14)){
                                        ball[3].set_Position(650, 160 + ball[3].get_Radius() * 2);
                                        ball[3].set_CharacterPosition(ball[3].get_Position().x + ball[3].get_Radius() * 2 / 3, ball[3].get_Position().y + ball[3].get_Radius() / 4);
                                        if (deltaClock.getElapsedTime() >= seconds(17)){
                                                ball[4].set_Position(650, 200 + ball[4].get_Radius() * 2);
                                                ball[4].set_CharacterPosition(ball[4].get_Position().x + ball[4].get_Radius() * 2 / 3, ball[4].get_Position().y + ball[4].get_Radius() / 4);
                                                if (deltaClock.getElapsedTime() >= seconds(20)){
                                                        ball[5].set_Position(650, 240 + ball[5].get_Radius() * 2);
                                                        ball[5].set_CharacterPosition(ball[5].get_Position().x + ball[5].get_Radius() * 2 / 3, ball[5].get_Position().y + ball[5].get_Radius() / 4);
                                                        deltaClock.restart();
                                                }
                                        }
                                }
                        }


                }


        }
}
//Ustawianie wartosci poczatkowych kuli
void settingUp_Balls(vector<Ball> & ball)
{

        font.loadFromFile("arial.ttf");
        for (int i = 0; i < 49; i++)
        {
                //      Zamiana wartosci liczbowej i na Stringa
                std::string s = std::to_string(i + 1);
                //losowanie ustwie&#324; poszczególnych kul;

                //losowanie koloru wy&#347;wietlanje kuli
                Color color1 = Color(rand() % 255, rand() % 255, rand() % 255, 255);
                //losowanie wielkosci promienia kuli
                radius[i] = (std::rand() % 40) + 30;
                // losownaie pozycji poczatkowej kuli
                beginningPositionX[i] = (std::rand() % 450) + 10;
                beginningPositionY[i] = (std::rand() % 320) + 10;


                ball[i].set_Position(radius[i] + beginningPositionX[i], radius[i] + beginningPositionY[i]);
                ball[i].set_Color(color1, Color::Black);
                ball[i].set_Radius(radius[i]);
                ball[i].set_Font(font);
                ball[i].set_String(s);
                ball[i].set_CharacterSize(radius[i]);
                ball[i].set_CharacterPosition(ball[i].get_Position().x + ball[i].get_Radius() * 2 / 3, ball[i].get_Position().y + ball[i].get_Radius() / 4);

                // predkosci poruszania si&#281; kuli
                ySpeed[i] = (std::rand() % 100) + 10;
                xSpeed[i] = (std::rand() % 100) + 10;
        }

}

int main()
{
       

        RenderWindow win(VideoMode(800, 600), L"Losowanie");
        vector < Ball > ball(49, Ball(win));
        Rectangle rectangle(win);
        rectangle.set_Position(20, 20);
        rectangle.set_Size(600, 500);
        // obs&#322;uga poruszania si&#281; kul
        Clock stoper;

        srand(time(NULL));

        for (int i = 0; i < 7; i++)
        {
                random[i] = (std::rand() % 49);
        }
        // wywosanie fukcji, definiujacej ustawienia kuli
        settingUp_Balls(ball);
        // funkcja nie dzia&#322;a popranie

        while (win.isOpen())
        {

                win.clear(Color::White);
                Event e;
                while (win.pollEvent(e))
                {
                        if (e.type == Event::Closed)
                                win.close();
                }

                //poruszanie sie kul
                auto dt = stoper.restart().asSeconds();
                for (int i = 0; i < 49; i++){
                        if (ball[i].get_Position().x < 620)
                                ball[i].Move(xSpeed[i] * dt, ySpeed[i] * dt);

                }


//Rysownaie prostokata masyzny losuj&#261;cej
                rectangle.draw_Rectangle();

// Rysowanie kul
                for (int i = 0; i < 49; i++)
                {
                        ball[i].draw_Ball();
                }
// sprawdzanie odbicie kuli
                balls_Reflection(ball);
               

                random_Balls(ball, random);
                        win.display();
        }
}
 

I have problem with function


void random_Balls(vector<Ball> & ball, int random[6])
{



        if (deltaClock.getElapsedTime() >= seconds(5)){

                ball[random[0]].set_Position(650, 40 + ball[random[0]].get_Radius());
                ball[random[0]].set_CharacterPosition(ball[random[0]].get_Position().x + ball[random[0]].get_Radius() * 2 / 3, ball[random[0]].get_Position().y + ball[random[0]].get_Radius() / 4);
                if (deltaClock.getElapsedTime() >= seconds(8)){
                        ball[1].set_Position(650, 80 + ball[1].get_Radius() * 2);
                        ball[1].set_CharacterPosition(ball[1].get_Position().x + ball[1].get_Radius() * 2 / 3, ball[1].get_Position().y + ball[1].get_Radius() / 4);
                        if (deltaClock.getElapsedTime() >= seconds(11)){
                                ball[2].set_Position(650, 120 + ball[2].get_Radius() * 2);
                                ball[2].set_CharacterPosition(ball[2].get_Position().x + ball[2].get_Radius() * 2 / 3, ball[2].get_Position().y + ball[2].get_Radius() / 4);
                                if (deltaClock.getElapsedTime() >= seconds(14)){
                                        ball[3].set_Position(650, 160 + ball[3].get_Radius() * 2);
                                        ball[3].set_CharacterPosition(ball[3].get_Position().x + ball[3].get_Radius() * 2 / 3, ball[3].get_Position().y + ball[3].get_Radius() / 4);
                                        if (deltaClock.getElapsedTime() >= seconds(17)){
                                                ball[4].set_Position(650, 200 + ball[4].get_Radius() * 2);
                                                ball[4].set_CharacterPosition(ball[4].get_Position().x + ball[4].get_Radius() * 2 / 3, ball[4].get_Position().y + ball[4].get_Radius() / 4);
                                                if (deltaClock.getElapsedTime() >= seconds(20)){
                                                        ball[5].set_Position(650, 240 + ball[5].get_Radius() * 2);
                                                        ball[5].set_CharacterPosition(ball[5].get_Position().x + ball[5].get_Radius() * 2 / 3, ball[5].get_Position().y + ball[5].get_Radius() / 4);
                                                        deltaClock.restart();
                                                }
                                        }
                                }
                        }


                }


        }
}

I can't code this is shorter, but in ball[2] visual studio stopped

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Beakpoint in visual studio
« Reply #1 on: August 22, 2015, 06:58:05 pm »
i have problem with one function
I have problem with function
Would you like to elaborate? What is the problem?

I can't code this is shorter, but in ball[2] visual studio stopped
Stopped? Crashed, froze, went to sleep, threw an exception, stopped at a breakpoint? If it's the breakpoint (as suggesting in your title), try removing the breakpoint first before starting them program.


This is not only minimal, but not complete. What is in "ball.h" and "rectangle.h"?

I'm going to avoid commenting on your code (I'm sure others will find things to say) other than avoid using namespace so casually, avoid using global variables, and use a std::vector of sf::Vector2f instead of two separate arrays of floats. All this before I got to any functions  ;D
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

pasierdamian

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Beakpoint in visual studio
« Reply #2 on: August 22, 2015, 07:08:40 pm »
Yes it stopped at a breakpoint. I trying remove breakpoint first but it's not working

My Ball class

#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include "BallShape.h"

using namespace sf;

class Ball
{
public:
        Ball(RenderWindow&);
        Ball();
        void set_Position(float, float);
        void set_OutlineColor(Color);
        void draw_Ball();
        void set_Color(Color, Color);
        void set_Thickness();
        void set_Radius(float);
        void Move(float, float);
        const sf::Vector2f& Ball::get_Position() const;
        float get_Radius();
        void set_String(String);
        void set_CharacterSize(int);
        void set_Font(Font &);
        void set_Scale(float, float);
        void set_CharacterPosition(float, float);


private:
        CircleShape Circley;
        RenderWindow& Render;
        Text txt;

};


Ball::Ball(sf::RenderWindow& app) : Render(app)
{
       
}



void Ball::set_Position(float x, float y )
{
        Circley.setPosition(x, y);
       
}

void Ball::set_OutlineColor(Color color)
{
        Circley.setOutlineColor(color);
}
void Ball::set_Thickness()
{
        Circley.setOutlineThickness(5);
}
void Ball::set_Color(Color ball, Color text)
{
        Circley.setFillColor(ball);
        txt.setColor(text);
}
void Ball::draw_Ball(){
        Render.draw(Circley);
        Render.draw(txt);
}
void Ball::set_Radius(float radius)
{
        Circley.setRadius(radius);
}

void Ball::set_Scale(float x, float y)
{
        Circley.setScale(x, y);
}

void Ball::Move(float x, float y)
{
        Circley.move(x, y);
        txt.move(x, y);
}
const sf::Vector2f& Ball::get_Position() const
{
        return Circley.getPosition();
}
float Ball::get_Radius()
{
        return Circley.getRadius();
}
void Ball::set_String(String number)
{
txt.setString(number);
}
void Ball::set_CharacterSize(int size)
{
        txt.setCharacterSize(size);
}
void Ball::set_Font(Font &fon)
{
        txt.setFont(fon);
}
void Ball::set_CharacterPosition(float x, float y)
{
        txt.setPosition(x, y);
}
 

and Rectangle class
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>

using namespace std;
using namespace sf;

class Rectangle
{
public: Rectangle(RenderWindow& );

                void set_Position(int, int);
                void draw_Rectangle();
                void set_Thickness(float);
                void set_Size(float, float);

private:
        RectangleShape rectangle;
        RenderWindow& Render;


};
Rectangle::Rectangle(sf::RenderWindow& app) : Render(app)
{
       
        rectangle.setOutlineColor(Color::Red);
        rectangle.setOutlineThickness(2);


}
void Rectangle::set_Position(int x, int y)
{
        rectangle.setPosition(x, y);
}
void Rectangle::set_Thickness(float thic)
{
        rectangle.setOutlineColor(Color::Red);
        rectangle.setOutlineThickness(thic);
}
void Rectangle::set_Size(float length, float height)
{
        rectangle.setSize(Vector2f(length, height));
}
void Rectangle::draw_Rectangle(){
        Render.draw(rectangle);
}


It's really so bad? I trying share it to more classes , but i had problems with it, now at least working.


First i want fix problem with random_balls function, but i waiting for any suggestion. I should not  using global variables and namespaces ? And what is wrong with vector but i dont undestand ?

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Beakpoint in visual studio
« Reply #3 on: August 22, 2015, 10:07:38 pm »
what is wrong with vector but i dont undestand ?
I didn't say std::vector was bad. I was suggesting to use sf::Vector2fs instead of just floats. A sf::Vector2f stores two floats - x and y - in the same variable. I also suggested using an std::vector of those rather than using the array that you are currently using.
e.g.
Instead of:
float beginningPositionX[49];
float beginningPositionY[49];
use:
std::vector<sf::Vector2f> beginningPositions(49);

Your code is rather large and broken into many pieces. If you need to debug it, you'll have to do that yourself. If you have a problem with something in particular, people would be more likely to help you.
Best way to get people's help is posting a minimal and complete example. It would serve you well to read the entire article so that you know what to write to help others to help you.

I do have a question, though. Why did you post this in the Window section of Help? Are you having trouble with SFML's windows?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything