Browse Source

Only render visible part of Map

master
Riyyi 9 years ago
parent
commit
5ff315c521
  1. 2
      src/data/map/level1.json
  2. 2
      src/data/map/level1.tmx
  3. 19
      src/map.cpp
  4. 2
      src/player.cpp

2
src/data/map/level1.json

File diff suppressed because one or more lines are too long

2
src/data/map/level1.tmx

File diff suppressed because one or more lines are too long

19
src/map.cpp

@ -18,16 +18,21 @@ Map::Map()
} }
void Map::Render(sf::RenderWindow* window) { void Map::Render(sf::RenderWindow* window) {
sf::Sprite tile; // Get render section
sf::Vector2f renderWidth = sf::Vector2f((window->getView().getCenter().x - window->getSize().x / 2) / this->tilewidth,
(window->getView().getCenter().x + window->getSize().x / 2) / this->tilewidth);
sf::Vector2f renderHeight = sf::Vector2f((window->getView().getCenter().y - window->getSize().y / 2) / this->tileheight,
(window->getView().getCenter().y + window->getSize().y / 2) / this->tileheight);
// Render map
int index; int index;
int width = this->layer1->width; sf::Sprite tile;
int height = this->layer1->height; for (int y = renderHeight.x; y < renderHeight.y; y++) {
for (int i = 0; i < height; i++) { for (int x = renderWidth.x; x < renderWidth.y; x++) {
for (int j = 0; j < width; j++) { index = this->layer1->data[y][x];
index = this->layer1->data[i][j];
if(index != 0) { if(index != 0) {
tile.setTexture(*this->tileSet->tile[index]); tile.setTexture(*this->tileSet->tile[index]);
tile.setPosition(sf::Vector2f(j * this->tileSet->tileWidth, i * this->tileSet->tileHeight)); tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight));
window->draw(tile); window->draw(tile);
} }
} }

2
src/player.cpp

@ -7,7 +7,7 @@ Player::Player(EntityManager* entityManager, Map *map, Camera *camera, float x,
this->Load("data/gfx/player.png"); this->Load("data/gfx/player.png");
this->setPosition(x, y); this->setPosition(x, y);
this->speed = 1.5f; this->speed = 2.5f;
} }
void Player::Update(sf::RenderWindow* window, InputManager inputManager) { void Player::Update(sf::RenderWindow* window, InputManager inputManager) {

Loading…
Cancel
Save