Advertisement
Guest User

My First SFML Program

a guest
Aug 11th, 2016
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <SFML\System.hpp>
  4. #include "SFML Files\include\SFML\Window.hpp"
  5. #include "SFML Files\include\SFML\Graphics.hpp"
  6.  
  7. int main()
  8. {
  9. sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
  10.  
  11. sf::Texture texture;
  12. texture.loadFromFile("tile1.png");
  13.  
  14. sf::Sprite sprite;
  15. sprite.setTexture(texture);
  16. sprite.setPosition(100, 100);
  17.  
  18. sf::Mouse mouse;
  19.  
  20. sf::Rect<float> mouseRect;
  21. mouseRect.width = 1.0f;
  22. mouseRect.height = 1.0f;
  23.  
  24. sf::Rect<float> tileRect;
  25. tileRect.left = 100.0f;
  26. tileRect.width = 48.0f;
  27. tileRect.top = 100.0f;
  28. tileRect.height = 48.0f;
  29.  
  30. // run the program as long as the window is open
  31. while (window.isOpen())
  32. {
  33. // check all the window's events that were triggered since the last iteration of the loop
  34. sf::Event event;
  35. while (window.pollEvent(event))
  36. {
  37. // "close requested" event: we close the window
  38. if (event.type == sf::Event::Closed)
  39. window.close();
  40. }
  41. window.clear();
  42.  
  43. mouseRect.left = mouse.getPosition(window).x;
  44.  
  45. mouseRect.top = mouse.getPosition(window).y;
  46.  
  47.  
  48. if (mouse.isButtonPressed(mouse.Left))
  49. {
  50. if (mouseRect.intersects(tileRect))
  51. {
  52. std::cout << "THEY INTERSECT" << std::endl;
  53. }
  54. else
  55. {
  56. std::cout << "NO INTERSECTION" << std::endl;
  57. }
  58. std::cout << mouseRect.left << ", " << mouseRect.top << std::endl;
  59. }
  60.  
  61. window.draw(sprite);
  62. window.display();
  63. }
  64. return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement