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

Author Topic: if statement and drawing to window problem...  (Read 1631 times)

0 Members and 1 Guest are viewing this topic.

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
if statement and drawing to window problem...
« on: December 14, 2012, 05:40:32 pm »
Having a problem with drawing a rectangle to the window if my variable touching is equal to true.   The collision detection works fine.  If I put a printf statement within the if statement at the end it prints it out. I don't understand why it's not drawing.
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Network.hpp>
#define RAND_MAX 100;

   //Must get object to attach to player and follow player.
int main()
{
   
   sf::RectangleShape player;
   player.setSize(sf::Vector2f(30,30));
   player.setFillColor(sf::Color::Blue);
   sf::RectangleShape object;
   object.setSize(sf::Vector2f(30,30));
   object.setFillColor(sf::Color::Red);
   sf::RectangleShape object2;
   object2.setSize(sf::Vector2f(30,30));
   object2.setFillColor(sf::Color::Red);
   int x = rand();
   int y = rand();
   object2.setPosition(100,100);
   sf::RenderWindow window(sf::VideoMode(1024, 600), "Game");
   bool touching = false;
   
   player.setPosition(0,0);
   object.setPosition(100,100);

   while (window.isOpen())
   {
      sf::Event event;
      

      while(window.pollEvent(event))
      {
         if (event.type == sf::Event::Closed)
            window.close();
         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)){
            player.move(-3,0);
            
         }
         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)){
            player.move(3,0);
         }
         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)){
            player.move(0,3);
         }
         else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){
            player.move(0,-3);
         }
         
         if (object.getGlobalBounds().intersects( player.getGlobalBounds())){
            touching = true;
         }
         
         
         
         
      }

      
      
      window.clear();
      window.draw(player);
      window.draw(object);
      if (touching){
            window.draw(object2);
            
            
         }
      window.display();
      
      


   

      }
   return 0;
      


}
Noob C++ Programmer.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10800
    • View Profile
    • development blog
    • Email
Re: if statement and drawing to window problem...
« Reply #1 on: December 14, 2012, 06:14:10 pm »
You should use the code=cpp tag in the forum to post code... ;)

sf::Keyboard::isKeyPressed as well as sf::Mouse don't have anything to do with events, so don't process them in the event loop! Also the collision check has nothing to do with events, so don't put it in the event loop (while(window.pollEvent(event))).
You're aware though that object and object2 are rendered to the same position and thus only one might get displayed?

Additional tips:
  • Always use forward slashes!
  • You have to call srand() before rand() and if you're using a compiler with C++11 support you might want to look into the new random generator.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Christopher Jozwiak

  • Newbie
  • *
  • Posts: 48
    • View Profile
    • Blog
    • Email
Re: if statement and drawing to window problem...
« Reply #2 on: December 14, 2012, 06:27:44 pm »
Hm... I always miss those stupid things.  It works now.  I'll try to refrain from posting an issue without staring at it for at least 3 hours next time.  Thanks for pointing the other things out though. 
Noob C++ Programmer.