very early prototype of a spaceshooter in SFML
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

background.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include <SFML/Graphics.hpp>
  2. #include <SFML/System.hpp>
  3. #include <SFML/Window.hpp>
  4. #include "background.hpp"
  5. namespace Proto4 {
  6. void Background::init(sf::Texture bg0, sf::Texture bg1,
  7. sf::View &rootView) {
  8. bg0Tex = bg0;
  9. bg1Tex = bg1;
  10. bg0Tex.setRepeated(true);
  11. bg1Tex.setRepeated(true);
  12. bg0Tex.setSmooth(true);
  13. bg1Tex.setSmooth(true);
  14. bg0Sprite.setTexture(bg0Tex);
  15. bg1Sprite.setTexture(bg1Tex);
  16. bg0View = bg1View = rootView;
  17. bg0Size = bg0Tex.getSize().x;
  18. bg1Size = bg1Tex.getSize().x;
  19. sf::Vector2f rootViewSize = rootView.getSize();
  20. bg0Sprite.setTextureRect(sf::IntRect(0, 0,
  21. rootViewSize.x + 2 * bg0Size,
  22. rootViewSize.y + 2 * bg0Size));
  23. bg1Sprite.setTextureRect(sf::IntRect(0, 0,
  24. rootViewSize.x + 2 * bg0Size,
  25. rootViewSize.y + 2 * bg0Size));
  26. bg0Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
  27. bg1Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
  28. }
  29. void Background::update(sf::View &rootView, sf::RenderTarget &window) {
  30. // parallax
  31. bg0View.setCenter(rootView.getCenter() / 3.5f);
  32. bg1View.setCenter(rootView.getCenter() / 2.f);
  33. // move endless backgrounds if parallax has moved one -step, to offset
  34. // parallax-movement
  35. sf::Vector2i bg0Pos =
  36. window.mapCoordsToPixel(bg0Sprite.getPosition(), bg0View);
  37. if (bg0Pos.x >= 0)
  38. bg0Sprite.move(sf::Vector2f(-bg0Size, 0));
  39. else if (bg0Pos.x <= -2 * bg0Size)
  40. bg0Sprite.move(sf::Vector2f(bg0Size, 0));
  41. if (bg0Pos.y >= 0)
  42. bg0Sprite.move(sf::Vector2f(0, -bg0Size));
  43. else if (bg0Pos.y <= -2 * bg0Size)
  44. bg0Sprite.move(sf::Vector2f(0, bg0Size));
  45. sf::Vector2i bg1Pos =
  46. window.mapCoordsToPixel(bg1Sprite.getPosition(), bg1View);
  47. if (bg1Pos.x >= 0)
  48. bg1Sprite.move(sf::Vector2f(-bg1Size, 0));
  49. else if (bg1Pos.x <= -2 * bg1Size)
  50. bg1Sprite.move(sf::Vector2f(bg1Size, 0));
  51. if (bg1Pos.y >= 0)
  52. bg1Sprite.move(sf::Vector2f(0, -bg1Size));
  53. else if (bg1Pos.y <= -2 * bg1Size)
  54. bg1Sprite.move(sf::Vector2f(0, bg1Size));
  55. }
  56. void Background::draw(sf::RenderTarget &target,
  57. sf::RenderStates states) const {
  58. target.setView(bg0View);
  59. target.draw(bg0Sprite);
  60. target.setView(bg1View);
  61. target.draw(bg1Sprite);
  62. }
  63. void Background::resize(sf::Event &resizeEvent) {
  64. bg0View.setSize(resizeEvent.size.width, resizeEvent.size.height);
  65. bg1View.setSize(resizeEvent.size.width, resizeEvent.size.height);
  66. bg0Sprite.setTextureRect(
  67. sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg0Size,
  68. resizeEvent.size.height + 2 * bg0Size));
  69. bg1Sprite.setTextureRect(
  70. sf::IntRect(0, 0, resizeEvent.size.width + 2 * bg1Size,
  71. resizeEvent.size.height + 2 * bg1Size));
  72. }
  73. }