Advertisement
Guest User

Untitled

a guest
Jan 31st, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/System.hpp>
  3.  
  4. #include <vector>
  5. #include <cmath>
  6.  
  7. using namespace sf;
  8.  
  9. int main()
  10. {
  11. RenderWindow window{{1024, 768}, ""};
  12. View view{{0, 0, 1024, 768}};
  13.  
  14. std::vector<RectangleShape> rects;
  15.  
  16. while (window.isOpen())
  17. {
  18.  
  19. Event event;
  20. while (window.pollEvent(event))
  21. {
  22. switch (event.type)
  23. {
  24. case Event::Closed:
  25. window.close();
  26. break;
  27.  
  28. case Event::Resized:
  29. {
  30. float width = static_cast<float>(event.size.width);
  31. float height = static_cast<float>(event.size.height);
  32.  
  33. view.reset({0.0f, 0.0f, width, height});
  34.  
  35. break;
  36. }
  37.  
  38. default:
  39. break;
  40. }
  41. }
  42.  
  43. Vector2f offset;
  44. if (Keyboard::isKeyPressed(Keyboard::Left))
  45. {
  46. offset.x += -2.0f;
  47. }
  48. if (Keyboard::isKeyPressed(Keyboard::Right))
  49. {
  50. offset.x += 2.0f;
  51. }
  52. if (Keyboard::isKeyPressed(Keyboard::Down))
  53. {
  54. offset.y += 2.0f;
  55. }
  56. if (Keyboard::isKeyPressed(Keyboard::Up))
  57. {
  58. offset.y += -2.0f;
  59. }
  60. view.move(offset);
  61.  
  62. if (Mouse::isButtonPressed(Mouse::Left))
  63. {
  64. auto pixelPos = Mouse::getPosition(window);
  65. auto coordPos = window.mapPixelToCoords(pixelPos);
  66.  
  67. int x = static_cast<int>(std::trunc(coordPos.x));
  68. int y = static_cast<int>(std::trunc(coordPos.y));
  69.  
  70. x -= x % 48;
  71. y -= y % 48;
  72.  
  73. coordPos.x = static_cast<float>(x);
  74. coordPos.y = static_cast<float>(y);
  75.  
  76. RectangleShape rect{{48.0f, 48.0f}};
  77. rect.setPosition(coordPos);
  78. rects.push_back(std::move(rect));
  79. }
  80.  
  81. window.clear();
  82. window.setView(view);
  83. for (const auto &it: rects) window.draw(it);
  84. window.display();
  85. }
  86.  
  87. return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement