Advertisement
Guest User

My First SFML Program Rev 0.4

a guest
Aug 13th, 2016
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 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. std::vector<sf::Sprite*> spriteContainer;
  18.  
  19. for (int i = 0; i < 3; i++)
  20. {
  21. sf::Sprite* sprite = new sf::Sprite;
  22. spriteContainer.push_back(sprite);
  23. sprite->setTexture(texture);
  24. sprite->setPosition(100.0f * (i+1), 100.0f);
  25. }
  26.  
  27. std::vector<sf::FloatRect*> rectangleContainer;
  28.  
  29. for (int i = 0; i < 3; i++)
  30. {
  31. sf::FloatRect* tileRect0 = new sf::FloatRect(100.0f * (i+1), 100.0f, 48.0f, 48.0f);
  32. rectangleContainer.push_back(tileRect0);
  33. }
  34.  
  35. // run the program as long as the window is open
  36. while (window.isOpen())
  37. {
  38. // check all the window's events that were triggered since the last iteration of the loop
  39. sf::Event event;
  40. while (window.pollEvent(event))
  41. {
  42. // "close requested" event: we close the window
  43. if (event.type == sf::Event::Closed)
  44. window.close();
  45. }
  46. window.clear();
  47. cursorPosition = window.mapPixelToCoords(sf::Mouse::getPosition(window));
  48.  
  49. if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
  50. {
  51. for (auto iter = rectangleContainer.begin(); iter != rectangleContainer.end(); iter++, id++)
  52. {
  53. if ((*iter)->contains(cursorPosition))
  54. {
  55. std::cout << "THEY INTERSECT" << std::endl;
  56. std::cout << "ID: " << id << std::endl;
  57. break;
  58. }
  59. else
  60. {
  61. std::cout << "NO INTERSECTION" << std::endl;
  62. }
  63. }
  64. id = 0;
  65.  
  66. std::cout << sf::Mouse::getPosition(window).x << std::endl;
  67. std::cout << sf::Mouse::getPosition(window).y << std::endl;
  68. }
  69.  
  70. for (auto iter = spriteContainer.begin(); iter != spriteContainer.end(); iter++)
  71. {
  72. window.draw(**iter);
  73. }
  74.  
  75. window.display();
  76.  
  77. }
  78.  
  79. for (int i = 0; i < 3; i++)
  80. {
  81. delete spriteContainer[i];
  82. delete rectangleContainer[i];
  83. }
  84.  
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement