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

Author Topic: For loop in event loop.  (Read 1872 times)

0 Members and 1 Guest are viewing this topic.

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
For loop in event loop.
« on: February 26, 2019, 09:53:23 pm »
So i'm probably just overlooking something very obvious here, but basically I have a for loop inside the event loop. There's a switch and under the case of left clicking there is a for loop. After the for loop there is an if statement and the program seems to completely ignore the for loop and jumps straight to the if statement.

case sf::Event::MouseButtonPressed:  // the left click case in the switch
                    if(event.mouseButton.button == sf::Mouse::Left)
                    {
                        for(int i = 0; i < circles.size(); i++) //the for loop
                        {
                            if(circles[i].getGlobalBounds().contains(mouseCoords))
                            {
                                Circle newCircle;
                                int radius = circles[i].getCircleRadius();

                                std::cout<<"Hello"<<std::endl;

                                circles.pop_back();

                                newCircle.CreateCircle(placementCoords, radius, r, g, b, 124, 170, 204);

                                circles.push_back(newCircle);
                            }
                        }

                        //if requarments are met a new instance of shapes will be created, then given info(position, color, etc..) and be saved into a vector
                        if(canvas.getGlobalBounds().contains(mouseCoords)) //if statement I was talking about
                        {
                            Circle newCircle;

                            newCircle.CreateCircle(mouseCoords, defaultRadius, 176, 214, 242, 124, 170, 204);

                            circles.push_back(newCircle);

                            placementCoords = mouseCoords;

                            selectingSize = true;
                        }
                    }

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: For loop in event loop.
« Reply #1 on: February 26, 2019, 10:57:35 pm »
Use a debugger and step through your code while checking that all the values are set to what you expect. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: For loop in event loop.
« Reply #2 on: February 27, 2019, 01:06:03 am »
Thanks for telling me to check if the values were correct. I found multiple critical bugs in my code. All that solved it! ;D

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: For loop in event loop.
« Reply #3 on: February 27, 2019, 05:37:02 am »
Btw since this thread is still fresh - could you please tell me why is the window going down after I draw something? When I set the window size to 1080p and the texture size to 1080p after drawing something it moves down a couple of pixels - again and again.. Doesn't happen if I set them to 800x600 for example.

#include <SFML/Graphics.hpp>
#include <vector>
#include <iostream>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
    std::vector <sf::Vertex> lines;
    sf::Texture texture;
    sf::Sprite sprite;
    sprite.setPosition(0, 0);
    texture.create(800, 600);

    sf::Event event;

    int mousedown = 0;
    // run the program as long as the window is open
    window.setFramerateLimit(60);

    while (window.isOpen())
    {
        // check all the window's events that were triggered since the last iteration of the loop
        while (window.pollEvent(event))
        {
            // "close requested" event: we close the window
            if (event.type == sf::Event::Closed)
                window.close();
            else if ((event.type == sf::Event::MouseMoved) && (mousedown == 1))
            {
                sf::Vertex point;
                point.color = sf::Color::Black;
                point.position = sf::Vector2f(sf::Mouse::getPosition(window));

                lines.push_back(point);
            }
            else if (event.type == sf::Event::MouseButtonPressed)
            {
                mousedown = 1;
            }
            else if (event.type == sf::Event::MouseButtonReleased)
            {
                mousedown = 0;
                texture.update(window);
                lines.clear();
            }
        }

        window.clear(sf::Color::White);

        sprite.setTexture(texture);

        window.draw(sprite);

        window.draw(&lines[0], lines.size(), sf::LinesStrip);

        window.display();

    }

    return 0;
}
 

Xrey274

  • Jr. Member
  • **
  • Posts: 76
    • View Profile
Re: For loop in event loop.
« Reply #4 on: February 27, 2019, 05:50:17 am »
Also resizing the window and trying to draw completely messes it up.