Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2013
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. #include "Drawer.h"
  2. #include "BlockSizeCalculator.h"
  3. #include <iostream>
  4.  
  5. Drawer::Drawer()
  6. {
  7. blending = false;
  8.  
  9. blendCount = 0;
  10.  
  11. neededToBlend = 30;
  12.  
  13. window.create(sf::VideoMode::getFullscreenModes()[0] , "", sf::Style::Fullscreen);
  14.  
  15. view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
  16.  
  17. view.setViewport(sf::FloatRect(0, 0, 1, 1));
  18. }
  19.  
  20. void Drawer::drawWorld(const World & world)
  21. {
  22. window.clear();
  23.  
  24. int blockSize = BlockSizeCalculator::getBlockSize();
  25.  
  26. sf::RectangleShape rectangle;
  27.  
  28. rectangle.setSize(sf::Vector2f(blockSize, blockSize));
  29.  
  30. for (int d = 0; d < world.getDepth(); ++d)
  31. for (int x = 0; x < world.getWidth() ; ++x)
  32. for (int y = 0; y < world.getHeight() ; ++y)
  33. {
  34. rectangle.setFillColor(world.getColor(x, y, d));
  35.  
  36. rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));
  37.  
  38. window.draw(rectangle);
  39. }
  40.  
  41. window.display();
  42. }
  43.  
  44. bool Drawer::draw(const World & world, const World & nextWorld)
  45. {
  46. if (!blending)
  47. {
  48. bool done = false;
  49.  
  50. int viewLeftBound = view.getCenter().x - view.getSize().x/2;
  51. int viewRightBound = viewLeftBound + view.getSize().x;
  52.  
  53. int left = world.getWidth()*BlockSizeCalculator::getBlockSize() - (view.getSize().x + viewLeftBound);
  54.  
  55. float scrollStartPoint = 0.33; //point where it reaches full speed
  56. float scrollEndPoint = 0.66; //point where it starts to slow down
  57. float startRate = 0.5;
  58. float endRate = 0.5;
  59.  
  60. float desiredScrollSpeed = 10;
  61.  
  62. //both of these must be at least one or it stops
  63.  
  64. float minimumStartScrollSpeed = 4;
  65. float minimumEndScrollSpeed = 0.1;
  66.  
  67. int scrollStartInt = scrollStartPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
  68. int scrollEndInt = scrollEndPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
  69.  
  70. float moveSpeed;
  71.  
  72. if (viewLeftBound < scrollStartInt)
  73. {
  74. //speeding up
  75.  
  76. moveSpeed = (((float) viewLeftBound)/scrollStartInt)*desiredScrollSpeed;
  77.  
  78. if (moveSpeed < minimumStartScrollSpeed)
  79. moveSpeed = minimumStartScrollSpeed;
  80. }
  81. else if (viewRightBound > scrollEndPoint)
  82. {
  83. //slowing down
  84.  
  85. int rightBoundSize = (BlockSizeCalculator::getBlockSize()*world.getWidth()) - scrollEndInt;
  86.  
  87. int progress = -(scrollEndInt - viewRightBound);
  88.  
  89. moveSpeed = (1 - (((float) progress)/rightBoundSize))*desiredScrollSpeed;
  90.  
  91. if (moveSpeed < minimumEndScrollSpeed)
  92. moveSpeed = minimumEndScrollSpeed;
  93. }
  94. else
  95. {
  96. //in the middle
  97.  
  98. moveSpeed = desiredScrollSpeed;
  99. }
  100.  
  101. if (left > moveSpeed)
  102. view.move(moveSpeed, 0);
  103. else
  104. {
  105. view.move(left, 0);
  106.  
  107. done = true;
  108. }
  109.  
  110. window.setView(view);
  111.  
  112. drawWorld(world);
  113.  
  114. if (done)
  115. {
  116. blending = true;
  117. }
  118. }
  119. else
  120. {
  121. //blending
  122.  
  123. ++blendCount;
  124.  
  125. drawBlend(world, nextWorld);
  126.  
  127. return blendCount >= neededToBlend;
  128. }
  129. }
  130.  
  131. void Drawer::reset()
  132. {
  133. view.setCenter(view.getSize().x/2 + 1, view.getSize().y/2 + 1);
  134. }
  135.  
  136. sf::RenderWindow & Drawer::getWindow() {return window;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement