Advertisement
Guest User

My First SFML Program Rev 0.3.1

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