Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Drawer.h"
- #include "BlockSizeCalculator.h"
- #include <iostream>
- Drawer::Drawer()
- {
- blending = false;
- blendCount = 0;
- neededToBlend = 30;
- window.create(sf::VideoMode::getFullscreenModes()[0] , "", sf::Style::Fullscreen);
- view.reset(sf::FloatRect(0, 0, window.getSize().x, window.getSize().y));
- view.setViewport(sf::FloatRect(0, 0, 1, 1));
- }
- void Drawer::drawWorld(const World & world)
- {
- window.clear();
- int blockSize = BlockSizeCalculator::getBlockSize();
- sf::RectangleShape rectangle;
- rectangle.setSize(sf::Vector2f(blockSize, blockSize));
- for (int d = 0; d < world.getDepth(); ++d)
- for (int x = 0; x < world.getWidth() ; ++x)
- for (int y = 0; y < world.getHeight() ; ++y)
- {
- rectangle.setFillColor(world.getColor(x, y, d));
- rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));
- window.draw(rectangle);
- }
- window.display();
- }
- bool Drawer::draw(const World & world, const World & nextWorld)
- {
- if (!blending)
- {
- bool done = false;
- int viewLeftBound = view.getCenter().x - view.getSize().x/2;
- int viewRightBound = viewLeftBound + view.getSize().x;
- int left = world.getWidth()*BlockSizeCalculator::getBlockSize() - (view.getSize().x + viewLeftBound);
- float scrollStartPoint = 0.33; //point where it reaches full speed
- float scrollEndPoint = 0.66; //point where it starts to slow down
- float startRate = 0.5;
- float endRate = 0.5;
- float desiredScrollSpeed = 10;
- //both of these must be at least one or it stops
- float minimumStartScrollSpeed = 4;
- float minimumEndScrollSpeed = 0.1;
- int scrollStartInt = scrollStartPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
- int scrollEndInt = scrollEndPoint*(BlockSizeCalculator::getBlockSize()*world.getWidth());
- float moveSpeed;
- if (viewLeftBound < scrollStartInt)
- {
- //speeding up
- moveSpeed = (((float) viewLeftBound)/scrollStartInt)*desiredScrollSpeed;
- if (moveSpeed < minimumStartScrollSpeed)
- moveSpeed = minimumStartScrollSpeed;
- }
- else if (viewRightBound > scrollEndPoint)
- {
- //slowing down
- int rightBoundSize = (BlockSizeCalculator::getBlockSize()*world.getWidth()) - scrollEndInt;
- int progress = -(scrollEndInt - viewRightBound);
- moveSpeed = (1 - (((float) progress)/rightBoundSize))*desiredScrollSpeed;
- if (moveSpeed < minimumEndScrollSpeed)
- moveSpeed = minimumEndScrollSpeed;
- }
- else
- {
- //in the middle
- moveSpeed = desiredScrollSpeed;
- }
- if (left > moveSpeed)
- view.move(moveSpeed, 0);
- else
- {
- view.move(left, 0);
- done = true;
- }
- window.setView(view);
- drawWorld(world);
- if (done)
- {
- blending = true;
- }
- }
- else
- {
- //blending
- ++blendCount;
- drawBlend(world, nextWorld);
- return blendCount >= neededToBlend;
- }
- }
- void Drawer::reset()
- {
- view.setCenter(view.getSize().x/2 + 1, view.getSize().y/2 + 1);
- }
- sf::RenderWindow & Drawer::getWindow() {return window;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement