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

Author Topic: [WIN7] Odd problem with std::cout  (Read 2247 times)

0 Members and 1 Guest are viewing this topic.

geist

  • Newbie
  • *
  • Posts: 2
    • View Profile
[WIN7] Odd problem with std::cout
« on: January 26, 2013, 12:47:29 am »
I'm in an odd situation right now:
        //point playerRect at mouse pos
        std::cout << mouseX << "\n";

        dx = mouseX - playerX;
        dy = mouseY - playerY;
        trigresult = atan2(dy,dx) * 180/PI;
        playerRect.setRotation(trigresult + 90);
For some reason, whenever I comment out the "std::cout << mouseX << "\n"", the trigresult only outputs 90 or -90 degrees with cout(so playerRect is either pointing straight up or straight down). And whenever it is there/isn't commented out, it works correctly and trigresult prints out correct results. Anyone know why this is?

Thanks in advance.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
AW: [WIN7] Odd problem with std::cout
« Reply #1 on: January 26, 2013, 12:55:14 am »
Usually such strange behavior comes from uninitialized variables/ undefined behavior.

Does mouseX always hold a valid value?

You need to provide a complete and minimal example.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

geist

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: [WIN7] Odd problem with std::cout
« Reply #2 on: January 26, 2013, 04:25:18 am »
int main()
{
    //variables
    float playerX = 400;
    float playerY = 300;

    float mouseX = 0.0;
    float mouseY = 0.0;

    double dx = 0;
    double dy = 0;

    float trigresult = 0.0;

    //shapes
    sf::RectangleShape playerRect;
    playerRect.setSize(sf::Vector2f(BLOCK_SIZE,BLOCK_SIZE));
    playerRect.setPosition(playerX,playerY);
    playerRect.setOrigin(BLOCK_SIZE/2,BLOCK_SIZE/2);
    playerRect.setTexture(&tex_playerRect);

    //gameloop//////////////////////
    while(bPlay == true)
    {
        //EVENTS//
        while(window.pollEvent(event))
        {
            /////////////////////////////////////
            //mouse

            //check if mouse moved
            if(event.type == sf::Event::MouseMoved)
            {
                mouseX = event.mouseMove.x;
                mouseY = event.mouseMove.y;
            }
        }
        //LOGIC//

        //point playerRect at mouse pos
        std::cout << mouseX << "\n";
        std::cout << mouseX << "\n";

        dx = mouseX - playerX;
        dy = mouseY - playerY;
        trigresult = atan2(dy,dx) * 180/PI;
        playerRect.setRotation(trigresult + 90);
       
        //RENDERING//

        window.clear();

        window.draw(playerRect);

        window.display();
    }
}

I hope this is enough code for you to help(also note that the first std::cout for mouseX didn't actually output anything to the console, but when I inserted it twice it worked ( ??? )

io

  • Jr. Member
  • **
  • Posts: 52
  • z/OS by day, SFML by night
    • View Profile
Re: [WIN7] Odd problem with std::cout
« Reply #3 on: January 26, 2013, 05:29:24 am »
some quick modifications so your example would compile.. but I'm not exactly seeing what the problem you are having is.  the rectangle follows my mouse and outputs ?  also trig result is displaying correctly on my end?


http://i.imgur.com/axAAfBb.jpg


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


int main()
{

  //variables
    float playerX = 400;
    float playerY = 300;

    float mouseX = 0.0;
    float mouseY = 0.0;

    double dx = 0;
    double dy = 0;
    float PI = 3.14;
    float trigresult = 0.0;

    int BLOCK_SIZE = 10;
    sf::RenderWindow window(sf::VideoMode(1024, 768), "SFML works!");
    window.setFramerateLimit(30);

    //shapes
    sf::RectangleShape playerRect;
    playerRect.setSize(sf::Vector2f(BLOCK_SIZE,BLOCK_SIZE));
    playerRect.setPosition(playerX,playerY);
    playerRect.setOrigin(BLOCK_SIZE/2,BLOCK_SIZE/2);
    //playerRect.setTexture(&tex_playerRect);

    bool bPlay = true;
    //gameloop//////////////////////
    while(bPlay == true)
    {
         sf::Event event;
        //EVENTS//
        while(window.pollEvent(event))
        {
            /////////////////////////////////////
            //mouse

            //check if mouse moved
            if(event.type == sf::Event::MouseMoved)
            {
                mouseX = event.mouseMove.x;
                mouseY = event.mouseMove.y;
            }
        }
        //LOGIC//

        //point playerRect at mouse pos
        //std::cout << mouseX << "\n";
        //std::cout << mouseX << "\n";

        dx = mouseX - playerX;
        dy = mouseY - playerY;
        trigresult = atan2(dy,dx) * 180/PI;
        std::cout <<trigresult << std::endl;

        playerRect.setRotation(trigresult + 90);

        //RENDERING//

        window.clear();

        window.draw(playerRect);

        window.display();
    }


    return 0;
}
 
« Last Edit: January 26, 2013, 05:45:13 am by io »