Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "Drawer.h"
- #include "BlockSizeCalculator.h"
- #include <iostream>
- Drawer::Drawer() : neededToBlend(60)
- {
- blending = false;
- blendCount = 0;
- firstBlendPane = nullptr;
- secondBlendPane = nullptr;
- 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));
- }
- float Drawer::calculateScrollSpeed(const World & world)
- {
- //TODO actual calculations
- return 10; //magic number. Oh yes.
- }
- bool Drawer::drawWorld(const World & world)
- {
- bool done = false;
- float scrollSpeed = calculateScrollSpeed(world);
- 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);
- if (scrollSpeed < left)
- view.move(scrollSpeed, 0);
- else
- {
- view.move(left, 0);
- done = true;
- }
- window.setView(view);
- 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();
- if (done)
- {
- blending = true;
- return true;
- }
- else
- return false;
- }
- //refactored out, keeping in case anything is needed later
- /*
- 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);
- }
- bool Drawer::drawBlend(const World & world, const World & nextWorld)
- {
- if (world.getWidth() != nextWorld.getWidth() || world.getHeight() != nextWorld.getHeight() || world.getDepth() != nextWorld.getDepth())
- return true; //bad things happen if we try to blend that
- int width = BlockSizeCalculator::getDesiredWidth();
- int height = BlockSizeCalculator::getDesiredHeight();
- int depth = BlockSizeCalculator::getDesiredDepth();
- if (firstBlendPane == nullptr)
- {
- firstBlendPane = new sf::Color ** [depth];
- secondBlendPane = new sf::Color ** [depth];
- for (int d = 0; d < depth; ++d)
- {
- firstBlendPane[d] = new sf::Color * [width];
- secondBlendPane[d] = new sf::Color * [width];
- }
- for (int d = 0; d < depth; ++d)
- for (int x = 0; x < width; ++x)
- {
- firstBlendPane[d][x] = new sf::Color [height];
- secondBlendPane[d][x] = new sf::Color [height];
- }
- for (int d = 0; d < depth; ++d)
- for (int w = 0; w < width; ++w)
- for (int h = 0; h < height; ++h)
- {
- secondBlendPane[d][w][h] = nextWorld.getColor(w, h, d);
- }
- for (int d = 0; d < BlockSizeCalculator::getDesiredDepth(); ++d)
- for (int w = world.getWidth() - width, x = 0; w < world.getWidth(); ++w, ++x)
- for (int h = world.getHeight() - height, y = 0; h < world.getHeight(); ++h, ++y)
- {
- firstBlendPane[d][x][y] = world.getColor(w, h, d);
- }
- }
- window.clear();
- ++blendCount;
- float blendPercent = ((float) blendCount/neededToBlend);
- sf::RectangleShape rectangle;
- rectangle.setSize(sf::Vector2f(BlockSizeCalculator::getBlockSize(), BlockSizeCalculator::getBlockSize()));
- for (int d = 0; d < depth; ++d)
- for (int x = 0; x < width; ++x)
- for (int y = 0; y < height; ++y)
- {
- sf::Color worldColor = firstBlendPane[d][x][y];
- sf::Color nextWorldColor = secondBlendPane[d][x][y];
- int redDif = nextWorldColor.r - worldColor.r;
- int greenDif = nextWorldColor.g - worldColor.g;
- int blueDif = nextWorldColor.b - worldColor.b;
- int alphaDif = nextWorldColor.a - worldColor.a;
- sf::Color color = worldColor;
- color.r += (int) (blendPercent*redDif);
- color.g += (int) (blendPercent*greenDif);
- color.b += (int) (blendPercent*blueDif);
- color.a += (int) (blendPercent*alphaDif);
- rectangle.setFillColor(sf::Color::Yellow);
- int blockSize = BlockSizeCalculator::getBlockSize();
- //rectangle.setPosition(x*blockSize, window.getSize().y - (y*blockSize + blockSize));
- rectangle.setPosition(0, 0);
- sf::Vector2f pos = rectangle.getPosition();
- window.draw(rectangle);
- }
- sf::RectangleShape rectangle2(sf::Vector2f(100, 100));
- rectangle2.setFillColor(sf::Color::Yellow);
- window.draw(rectangle2);
- window.display();
- if (blendPercent ==1) //done blending
- {
- blendCount = 0;
- blending = false;
- firstBlendPane = nullptr;
- secondBlendPane = nullptr;
- reset();
- return true;
- }
- else
- return false;
- }
- bool Drawer::isBlending() {return blending;}
- sf::RenderWindow & Drawer::getWindow() {return window;}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement