Skip to content

Commit

Permalink
locked camera view and temp tileable background (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
MorphewPrime authored Oct 31, 2021
1 parent 53b47ee commit d3f0847
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 40 deletions.
Binary file added assets/textures/temp_floor_128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 43 additions & 39 deletions src/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,11 @@ void GameManager::drawFrame()
// Clear current buffer
_gameWindow.clear();

// need to update the camera before drawing anything
// updateView();
// Now update the position of the view as nessisary.
updateViewLocked();

// Draw the temporary background before anything else
drawMap();

// Drawing an entity has two steps: calling the onDraw method to update the entity's sprite
// and calling the game window draw function
Expand All @@ -200,43 +203,44 @@ void GameManager::drawFrame()
_gameWindow.display();
}

void GameManager::updateView()
void GameManager::updateViewLocked()
{
// TODO: this is a mess, clean it up

// I want to define a 100x50 rectangle in the middle of the view.
// If the player walks outside of this rectangle, then we should
// move the view to follow it and keep it in the rectangle.
const int centerRectWidth = 100;
const int centerRectHeight = 50;
sf::Vector2i posInView = _gameWindow.mapCoordsToPixel(static_cast<sf::Vector2f>(this->_player.getPosition()));
sf::Vector2i viewCenter = static_cast<sf::Vector2i>(_view.getSize() * 0.5f);
sf::Vector2i displFromCenter = posInView - viewCenter;
sf::Vector2i outsideRect {0, 0}; // Vector that gives us how outside of the rectangle we are
if (displFromCenter.x < -(centerRectWidth/2))
outsideRect.x = displFromCenter.x + (centerRectWidth/2);
else if (displFromCenter.x > (centerRectWidth/2))
outsideRect.x = displFromCenter.x - (centerRectWidth/2);

if (displFromCenter.y < -(centerRectHeight/2))
outsideRect.y = displFromCenter.y + (centerRectHeight/2);
else if (displFromCenter.y > (centerRectHeight/2))
outsideRect.y = displFromCenter.y - (centerRectHeight/2);

sf::Vector2i topLeft = static_cast<sf::Vector2i>(_gameWindow.mapPixelToCoords({0, 0}));
sf::Vector2i bottomRight = static_cast<sf::Vector2i>(_gameWindow.mapPixelToCoords(static_cast<sf::Vector2i>(_view.getSize())));
sf::Vector2i translateView = outsideRect;
if (topLeft.x + outsideRect.x < 0)
translateView.x = -topLeft.x;
else if (bottomRight.x + outsideRect.x > _gameWindow.getSize().x)
translateView.x = _gameWindow.getSize().x - bottomRight.x;

if (topLeft.y + outsideRect.y < 0)
translateView.y = -topLeft.y;
else if (bottomRight.y + outsideRect.y > _gameWindow.getSize().y)
translateView.y = _gameWindow.getSize().y - bottomRight.y;

_view.move(static_cast<sf::Vector2f>(translateView));
sf::View view = _gameWindow.getView();
const sf::Vector2f &playerLocation = this->_player.getPosition();
const sf::Vector2f &viewSize = _view.getSize();
sf::Vector2f mapSize{1500.0, 1125.0}; // this can probably be moved to a member variable later when the map is made.

_gameWindow.setView(_view);
view.setCenter(this->_player.getPosition());

if (playerLocation.x < viewSize.x / 2) // If camera view is extends past left side of the map.
{
view.setCenter(sf::Vector2f{viewSize.x / 2, view.getCenter().y});
}
else if (playerLocation.x + viewSize.x / 2 > mapSize.x) // If camera view is extends past right side of the map.
{
view.setCenter(sf::Vector2f{mapSize.x - (viewSize.x / 2), view.getCenter().y});
}

if (playerLocation.y < viewSize.y / 2) // If camera view is extends past top side of the map.
{
view.setCenter(sf::Vector2f{view.getCenter().x, viewSize.y / 2});
}
else if (playerLocation.y + viewSize.y / 2 > mapSize.y) // If camera view is extends past bottom side of the map.
{
view.setCenter(sf::Vector2f{view.getCenter().x, mapSize.y - (viewSize.y / 2)});
}

_gameWindow.setView(view);
}

void GameManager::drawMap()
{
sf::Texture texture;
texture.loadFromFile("assets/textures/temp_floor_128.png");
texture.setRepeated(true);

sf::IntRect rectSourceSprite(0, 0, 1500, 1125);
sf::Sprite sprite(texture, rectSourceSprite);

this->_gameWindow.draw(sprite);
}
8 changes: 7 additions & 1 deletion src/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,11 @@ class GameManager
* @brief Called from drawFrame(),
* will move the current view based off of the player's location
*/
void updateView();
void updateViewLocked();

/**
* @brief Called from drawFrame(),
* Temporary function to draw a basic background of our map
*/
void drawMap();
};

0 comments on commit d3f0847

Please sign in to comment.