Advertisement
Guest User

Untitled

a guest
Jul 4th, 2013
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.83 KB | None | 0 0
  1. #include "Drawer.h"
  2. #include "BlockSizeCalculator.h"
  3. #include <iostream>
  4.  
  5. Drawer::Drawer() : neededToBlend(60)
  6. {
  7. blending = false;
  8.  
  9. blendCount = 0;
  10.  
  11. firstBlendPane = nullptr;
  12. secondBlendPane = nullptr;
  13.  
  14. window.create(sf::VideoMode::getFullscreenModes()[0] , "", sf::Style::Fullscreen);
  15.  
  16. view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
  17.  
  18. view.setViewport(sf::FloatRect(0, 0, 1, 1));
  19. }
  20.  
  21. float Drawer::calculateScrollSpeed(const World & world)
  22. {
  23. //TODO actual calculations
  24.  
  25. return 10; //magic number. Oh yes.
  26. }
  27.  
  28. bool Drawer::drawWorld(const World & world)
  29. {
  30. bool done = false;
  31.  
  32. float scrollSpeed = calculateScrollSpeed(world);
  33.  
  34. int viewLeftBound = view.getCenter().x - view.getSize().x/2;
  35. int viewRightBound = viewLeftBound + view.getSize().x;
  36.  
  37. int left = world.getWidth()*BlockSizeCalculator::getBlockSize() - (view.getSize().x + viewLeftBound);
  38.  
  39. if (scrollSpeed < left)
  40. view.move(scrollSpeed, 0);
  41. else
  42. {
  43. view.move(left, 0);
  44.  
  45. done = true;
  46. }
  47.  
  48. window.setView(view);
  49.  
  50. window.clear();
  51.  
  52. int blockSize = BlockSizeCalculator::getBlockSize();
  53.  
  54. sf::RectangleShape rectangle;
  55.  
  56. rectangle.setSize(sf::Vector2f(blockSize, blockSize));
  57.  
  58. for (int d = 0; d < world.getDepth(); ++d)
  59. for (int x = 0; x < world.getWidth() ; ++x)
  60. for (int y = 0; y < world.getHeight() ; ++y)
  61. {
  62. rectangle.setFillColor(world.getColor(x, y, d));
  63.  
  64. rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));
  65.  
  66. window.draw(rectangle);
  67. }
  68.  
  69. window.display();
  70.  
  71. if (done)
  72. {
  73. blending = true;
  74.  
  75. return true;
  76. }
  77. else
  78. return false;
  79. }
  80.  
  81.  
  82. //refactored out, keeping in case anything is needed later
  83. /*
  84.  
  85. bool Drawer::draw(const World & world, const World & nextWorld)
  86. {
  87. if (!blending)
  88. {
  89. bool done = false;
  90.  
  91. int viewLeftBound = view.getCenter().x - view.getSize().x/2;
  92. int viewRightBound = viewLeftBound + view.getSize().x;
  93.  
  94. int left = world.getWidth()*BlockSizeCalculator::getBlockSize() - (view.getSize().x + viewLeftBound);
  95.  
  96. float scrollStartPoint = 0.33; //point where it reaches full speed
  97. float scrollEndPoint = 0.66; //point where it starts to slow down
  98. float startRate = 0.5;
  99. float endRate = 0.5;
  100.  
  101. float desiredScrollSpeed = 10;
  102.  
  103. //both of these must be at least one or it stops
  104.  
  105. float minimumStartScrollSpeed = 4;
  106. float minimumEndScrollSpeed = 0.1;
  107.  
  108. int scrollStartInt = scrollStartPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
  109. int scrollEndInt = scrollEndPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
  110.  
  111. float moveSpeed;
  112.  
  113. if (viewLeftBound < scrollStartInt)
  114. {
  115. //speeding up
  116.  
  117. moveSpeed = (((float) viewLeftBound)/scrollStartInt)*desiredScrollSpeed;
  118.  
  119. if (moveSpeed < minimumStartScrollSpeed)
  120. moveSpeed = minimumStartScrollSpeed;
  121. }
  122. else if (viewRightBound > scrollEndPoint)
  123. {
  124. //slowing down
  125.  
  126. int rightBoundSize = (BlockSizeCalculator::getBlockSize()*world.getWidth()) - scrollEndInt;
  127.  
  128. int progress = -(scrollEndInt - viewRightBound);
  129.  
  130. moveSpeed = (1 - (((float) progress)/rightBoundSize))*desiredScrollSpeed;
  131.  
  132. if (moveSpeed < minimumEndScrollSpeed)
  133. moveSpeed = minimumEndScrollSpeed;
  134. }
  135. else
  136. {
  137. //in the middle
  138.  
  139. moveSpeed = desiredScrollSpeed;
  140. }
  141.  
  142. if (left > moveSpeed)
  143. view.move(moveSpeed, 0);
  144. else
  145. {
  146. view.move(left, 0);
  147.  
  148. done = true;
  149. }
  150.  
  151. window.setView(view);
  152.  
  153. drawWorld(world);
  154.  
  155. if (done)
  156. {
  157. blending = true;
  158. }
  159. }
  160. else
  161. {
  162. //blending
  163.  
  164. ++blendCount;
  165.  
  166. drawBlend(world, nextWorld);
  167.  
  168. return blendCount >= neededToBlend;
  169. }
  170. }
  171. */
  172.  
  173. void Drawer::reset()
  174. {
  175. view.setCenter(view.getSize().x/2 + 1, view.getSize().y/2 + 1);
  176. }
  177.  
  178. bool Drawer::drawBlend(const World & world, const World & nextWorld)
  179. {
  180. if (world.getWidth() != nextWorld.getWidth() || world.getHeight() != nextWorld.getHeight() || world.getDepth() != nextWorld.getDepth())
  181. return true; //bad things happen if we try to blend that
  182.  
  183. int width = BlockSizeCalculator::getDesiredWidth();
  184. int height = BlockSizeCalculator::getDesiredHeight();
  185. int depth = BlockSizeCalculator::getDesiredDepth();
  186.  
  187. if (firstBlendPane == nullptr)
  188. {
  189.  
  190.  
  191. firstBlendPane = new sf::Color ** [depth];
  192. secondBlendPane = new sf::Color ** [depth];
  193.  
  194. for (int d = 0; d < depth; ++d)
  195. {
  196. firstBlendPane[d] = new sf::Color * [width];
  197. secondBlendPane[d] = new sf::Color * [width];
  198. }
  199.  
  200. for (int d = 0; d < depth; ++d)
  201. for (int x = 0; x < width; ++x)
  202. {
  203. firstBlendPane[d][x] = new sf::Color [height];
  204. secondBlendPane[d][x] = new sf::Color [height];
  205. }
  206.  
  207. for (int d = 0; d < depth; ++d)
  208. for (int w = 0; w < width; ++w)
  209. for (int h = 0; h < height; ++h)
  210. {
  211. secondBlendPane[d][w][h] = nextWorld.getColor(w, h, d);
  212. }
  213.  
  214. for (int d = 0; d < BlockSizeCalculator::getDesiredDepth(); ++d)
  215. for (int w = world.getWidth() - width, x = 0; w < world.getWidth(); ++w, ++x)
  216. for (int h = world.getHeight() - height, y = 0; h < world.getHeight(); ++h, ++y)
  217. {
  218. firstBlendPane[d][x][y] = world.getColor(w, h, d);
  219. }
  220.  
  221.  
  222.  
  223.  
  224.  
  225. }
  226.  
  227. window.clear();
  228.  
  229. ++blendCount;
  230.  
  231. float blendPercent = ((float) blendCount/neededToBlend);
  232.  
  233. sf::RectangleShape rectangle;
  234.  
  235. rectangle.setSize(sf::Vector2f(BlockSizeCalculator::getBlockSize(), BlockSizeCalculator::getBlockSize()));
  236.  
  237. for (int d = 0; d < depth; ++d)
  238. for (int x = 0; x < width; ++x)
  239. for (int y = 0; y < height; ++y)
  240. {
  241. sf::Color worldColor = firstBlendPane[d][x][y];
  242. sf::Color nextWorldColor = secondBlendPane[d][x][y];
  243.  
  244. int redDif = nextWorldColor.r - worldColor.r;
  245. int greenDif = nextWorldColor.g - worldColor.g;
  246. int blueDif = nextWorldColor.b - worldColor.b;
  247. int alphaDif = nextWorldColor.a - worldColor.a;
  248.  
  249. sf::Color color = worldColor;
  250.  
  251. color.r += (int) (blendPercent*redDif);
  252. color.g += (int) (blendPercent*greenDif);
  253. color.b += (int) (blendPercent*blueDif);
  254. color.a += (int) (blendPercent*alphaDif);
  255.  
  256. rectangle.setFillColor(sf::Color::Yellow);
  257.  
  258. int blockSize = BlockSizeCalculator::getBlockSize();
  259.  
  260. //rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));
  261. rectangle.setPosition(0, 0);
  262.  
  263. sf::Vector2f pos = rectangle.getPosition();
  264.  
  265. window.draw(rectangle);
  266. }
  267.  
  268. sf::RectangleShape rectangle2(sf::Vector2f(100, 100));
  269.  
  270. rectangle2.setFillColor(sf::Color::Yellow);
  271.  
  272. window.draw(rectangle2);
  273.  
  274. window.display();
  275.  
  276. if (blendPercent ==1) //done blending
  277. {
  278. blendCount = 0;
  279.  
  280. blending = false;
  281.  
  282. firstBlendPane = nullptr;
  283. secondBlendPane = nullptr;
  284.  
  285. reset();
  286.  
  287. return true;
  288. }
  289. else
  290. return false;
  291. }
  292.  
  293. bool Drawer::isBlending() {return blending;}
  294.  
  295. sf::RenderWindow & Drawer::getWindow() {return window;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement