Browse Source

Only render entities on screen

master
Riyyi 10 years ago
parent
commit
52361bc8e0
  1. 11
      src/camera.cpp
  2. 1
      src/camera.h
  3. BIN
      src/data/gfx/Collision.png
  4. 16
      src/data/map/level1.json
  5. 6
      src/data/map/level1.tmx
  6. 10
      src/entitymanager.cpp
  7. 5
      src/entitymanager.h
  8. 1
      src/gamestate.h
  9. 7
      src/main.cpp
  10. 79
      src/map.cpp
  11. 13
      src/map.h
  12. 23
      src/mapload.cpp
  13. 10
      src/state/maingame.cpp

11
src/camera.cpp

@ -92,6 +92,17 @@ void Camera::Update(sf::RenderWindow *window, Map *map, sf::Vector2f position) {
} }
} }
bool Camera::IsOnScreen(sf::RenderWindow *window, Entity *entity) {
if(entity->getPosition().x + entity->getGlobalBounds().width / 2 > this->view.getCenter().x - window->getSize().x / 2
&& entity->getPosition().x - entity->getGlobalBounds().width / 2 < this->view.getCenter().x + window->getSize().x / 2
&& entity->getPosition().y + entity->getGlobalBounds().height / 2 > this->view.getCenter().y - window->getSize().y / 2
&& entity->getPosition().y - entity->getGlobalBounds().height / 2 < this->view.getCenter().y + window->getSize().y / 2) {
return true;
}
return false;
}
Camera::~Camera() Camera::~Camera()
{ {

1
src/camera.h

@ -12,6 +12,7 @@ public:
void MoveCamera(sf::RenderWindow *window, sf::Vector2f move); void MoveCamera(sf::RenderWindow *window, sf::Vector2f move);
void SetCenter(sf::RenderWindow *window, sf::Vector2f position); void SetCenter(sf::RenderWindow *window, sf::Vector2f position);
void Update(sf::RenderWindow *window, Map *map, sf::Vector2f position); void Update(sf::RenderWindow *window, Map *map, sf::Vector2f position);
bool IsOnScreen(sf::RenderWindow *window, Entity *entity);
~Camera(); ~Camera();
private: private:

BIN
src/data/gfx/Collision.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 2.8 KiB

After

Width:  |  Height:  |  Size: 2.9 KiB

16
src/data/map/level1.json

File diff suppressed because one or more lines are too long

6
src/data/map/level1.tmx

File diff suppressed because one or more lines are too long

10
src/entitymanager.cpp

@ -59,14 +59,14 @@ void EntityManager::Update() {
} }
void EntityManager::Render(sf::RenderWindow* window) { void EntityManager::Render(sf::RenderWindow* window, Camera *camera) {
for (auto& iterator : this->entities) { for(auto& iterator : this->entities) {
if (iterator.second != NULL) { if(iterator.second != NULL
if (iterator.second->Active()) { && iterator.second->Active()
&& camera->IsOnScreen(window, iterator.second)) {
window->draw(*iterator.second); window->draw(*iterator.second);
} }
} }
}
} }
EntityManager::~EntityManager() { EntityManager::~EntityManager() {

5
src/entitymanager.h

@ -4,6 +4,7 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include "camera.h"
#include "entity.h" #include "entity.h"
typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB); typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB);
@ -13,10 +14,10 @@ class EntityManager
public: public:
EntityManager(); EntityManager();
void SetCollisionMethod(CollisionUpdateEvent collisionsEvent); void SetCollisionMethod(CollisionUpdateEvent collisionsEvent);
void AddEntity(std::string name, Entity* entity); void AddEntity(std::string name, Entity *entity);
Entity* Get(std::string name); Entity* Get(std::string name);
void Update(); void Update();
void Render(sf::RenderWindow* window); void Render(sf::RenderWindow *window, Camera *camera);
~EntityManager(); ~EntityManager();
private: private:

1
src/gamestate.h

@ -59,7 +59,6 @@ public:
} }
} }
private: private:
sf::RenderWindow* window; sf::RenderWindow* window;
GameState* state; GameState* state;

7
src/main.cpp

@ -1,5 +1,4 @@
#include <iostream> #include <iostream>
#include <SFML/Graphics.hpp>
#include "state/maingame.h" #include "state/maingame.h"
@ -29,12 +28,10 @@ int main()
sf::Time timeElapsed; sf::Time timeElapsed;
// Run the program as long as the window is open // Run the program as long as the window is open
while (window.isOpen()) while (window.isOpen()) {
{
// Check all the window's events that were triggered since the last iteration of the loop // Check all the window's events that were triggered since the last iteration of the loop
sf::Event event; sf::Event event;
while (window.pollEvent(event)) while (window.pollEvent(event)) {
{
// Close window : exit // Close window : exit
if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) { if (event.type == sf::Event::Closed || sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close(); window.close();

79
src/map.cpp

@ -35,63 +35,85 @@ void Map::CheckCollision(Entity *entity) {
// Check collision // Check collision
int index; int index;
sf::Sprite tile;
bool collided = false; bool collided = false;
sf::Vector2i collidedTile;
for (int y = checkHeight.x; y < checkHeight.y; y++) { for (int y = checkHeight.x; y < checkHeight.y; y++) {
for (int x = checkWidth.x; x < checkWidth.y; x++) { for (int x = checkWidth.x; x < checkWidth.y; x++) {
index = this->collision->data[y][x]; index = this->collision->data[y][x];
// If collision tile // If collision tile
if(index != 0) { if(index != 0) {
if (entity->getGlobalBounds().intersects(sf::FloatRect(x * this->tileSet->tileWidth, tile.setTexture(*this->tileSet->tile[index]);
y * this->tileSet->tileHeight, tile.setPosition(sf::Vector2f(x * this->tileSet->tileWidth, y * this->tileSet->tileHeight));
this->tileSet->tileWidth, // Rotate oblique collisions
this->tileSet->tileHeight))) { // if(i == 1) {
// switch(x) {
// case 3:
// break;
// case 4:
// break;
// case 5:
// break;
// }
// }
if (tile.getGlobalBounds().intersects(entity->getGlobalBounds())) {
collided = true; collided = true;
collidedTile.x = x;
collidedTile.y = y;
break;
} }
} }
} }
} }
// Move back if entity collided) // Move back if entity collided
if(collided) { if(collided) {
// Get Entities center x, y position;
sf::Vector2f entityCenter = sf::Vector2f(
(int)((entity->getPosition().x + entity->getGlobalBounds().width / 2) / this->tileSet->tileWidth),
(int)((entity->getPosition().y + entity->getGlobalBounds().height / 2) / this->tileSet->tileHeight)
);
// DOWN_RIGHT // DOWN_RIGHT
if(entity->velocity.x > 0 if(collidedTile.x > entityCenter.x + 1
&& entity->velocity.y > 0) { && collidedTile.y > entityCenter.y + 1) {
entity->velocity.x -= entity->velocity.x * 2;
entity->velocity.y = 0;
} }
// UP_RIGHT // UP_RIGHT
else if(entity->velocity.x > 0 else if(collidedTile.x > entityCenter.x + 1
&& entity->velocity.y < 0) { && collidedTile.y < entityCenter.y - 1) {
entity->velocity.x = 0;
entity->velocity.y -= entity->velocity.y * 2;
} }
// UP_LEFT // UP_LEFT
else if(entity->velocity.x < 0 else if(collidedTile.x < entityCenter.x - 1
&& entity->velocity.y < 0) { && collidedTile.y < entityCenter.y - 1) {
entity->velocity.x = 0;
entity->velocity.y -= entity->velocity.y * 2;
} }
// DOWN_LEFT // DOWN_LEFT
else if(entity->velocity.x < 0 else if(collidedTile.x < entityCenter.x - 1
&& entity->velocity.y > 0) { && collidedTile.y > entityCenter.y + 1) {
entity->velocity.x -= entity->velocity.x * 2;
entity->velocity.y = 0;
} }
// RIGHT // RIGHT
else if(entity->velocity.x > 0) { if(collidedTile.x > entityCenter.x
&& collidedTile.y == entityCenter.y) {
entity->velocity.x -= entity->velocity.x * 2; entity->velocity.x -= entity->velocity.x * 2;
} }
// LEFT // LEFT
else if(entity->velocity.x < 0) { if(collidedTile.x < entityCenter.x
entity->velocity.x -= entity->velocity.x * 2; && collidedTile.y == entityCenter.y) {
} }
// DOWN // DOWN
else if(entity->velocity.y > 0) { if(collidedTile.x == entityCenter.x
entity->velocity.y -= entity->velocity.y * 2; && collidedTile.y > entityCenter.y) {
} }
// UP // UP
else if(entity->velocity.y < 0) { if(collidedTile.x == entityCenter.x
&& collidedTile.y < entityCenter.y) {
entity->velocity.y -= entity->velocity.y * 2; entity->velocity.y -= entity->velocity.y * 2;
} }
@ -119,6 +141,9 @@ void Map::RenderAbove(sf::RenderWindow *window) {
layer = this->above3; layer = this->above3;
this->Render(window, layer); this->Render(window, layer);
// layer = this->collision;
// this->Render(window, layer);
} }
void Map::Render(sf::RenderWindow *window, Layer *layer) { void Map::Render(sf::RenderWindow *window, Layer *layer) {

13
src/map.h

@ -60,6 +60,19 @@ public:
void RenderAbove(sf::RenderWindow *window); void RenderAbove(sf::RenderWindow *window);
~Map(); ~Map();
enum Direction
{
NONE = -1,
LEFT = 0,
RIGHT = 1,
UP = 2,
DOWN = 3,
TOP_LEFT = 4,
TOP_RIGHT = 5,
BOTTOM_LEFT = 6,
BOTTOM_RIGHT = 7
};
int height; int height;
Layer* ground1; Layer* ground1;
Layer* ground2; Layer* ground2;

23
src/mapload.cpp

@ -47,7 +47,7 @@ void MapLoad::Parser(std::string *content, rapidjson::Document *document) {
void MapLoad::LoadLayer(const rapidjson::Document& document, Map *map) { void MapLoad::LoadLayer(const rapidjson::Document& document, Map *map) {
int loopCount = document["layers"].Size(); int loopCount = document["layers"].Size();
Layer* current; Layer* current;
for(unsigned int i = 0; i < loopCount; i++) { for(int i = 0; i < loopCount; i++) {
current = NULL; current = NULL;
if(document["layers"][i]["properties"]["name"] == "Ground1") { if(document["layers"][i]["properties"]["name"] == "Ground1") {
current = map->ground1; current = map->ground1;
@ -108,15 +108,21 @@ void MapLoad::LoadTileSet(const rapidjson::Document& document, Map *map) {
map->tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt(); map->tileSet->tileHeight = document["tilesets"][0]["tileheight"].GetInt();
map->tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt(); map->tileSet->tileWidth = document["tilesets"][0]["tilewidth"].GetInt();
int height = map->tileSet->imageHeight / map->tileSet->tileHeight; int height;
int width = map->tileSet->imageWidth / map->tileSet->tileWidth; int width;
// Add NULL texture to fill position '0' // Add NULL texture to fill position '0'
sf::Texture* texture = new sf::Texture(); sf::Texture* texture = new sf::Texture();
map->tileSet->tile.push_back(texture); map->tileSet->tile.push_back(texture);
int loopCount = document["tilesets"].Size();
for(int i = 0; i < loopCount; i++) {
height = document["tilesets"][i]["imageheight"].GetInt() / document["tilesets"][i]["tileheight"].GetInt();
width = document["tilesets"][i]["imagewidth"].GetInt() / document["tilesets"][i]["tilewidth"].GetInt();
// Load map tilesheet // Load map tilesheet
std::string tileSheetLocation = map->tileSet->image; std::string tileSheetLocation = document["tilesets"][i]["image"].GetString();
tileSheetLocation.replace(0, 2, "data"); tileSheetLocation.replace(0, 2, "data");
sf::Image tileSheet; sf::Image tileSheet;
tileSheet.loadFromFile(tileSheetLocation); tileSheet.loadFromFile(tileSheetLocation);
@ -124,13 +130,14 @@ void MapLoad::LoadTileSet(const rapidjson::Document& document, Map *map) {
for(int y = 0; y < height; y++) { for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) { for(int x = 0; x < width; x++) {
sf::Texture* texture = new sf::Texture(); sf::Texture* texture = new sf::Texture();
texture->loadFromImage(tileSheet, sf::IntRect(x * map->tileSet->tileWidth, texture->loadFromImage(tileSheet, sf::IntRect(x * document["tilesets"][i]["tilewidth"].GetInt(),
y * map->tileSet->tileHeight, y * document["tilesets"][i]["tileheight"].GetInt(),
map->tileSet->tileWidth, document["tilesets"][i]["tilewidth"].GetInt(),
map->tileSet->tileHeight)); document["tilesets"][i]["tileheight"].GetInt()));
map->tileSet->tile.push_back(texture); map->tileSet->tile.push_back(texture);
} }
} }
}
} }
void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) { void MapLoad::LoadMap(const rapidjson::Document& document, Map *map) {

10
src/state/maingame.cpp

@ -37,15 +37,21 @@ void MainGame::Update(sf::RenderWindow* window) {
this->map->CheckCollision(this->player); this->map->CheckCollision(this->player);
this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y)); this->camera->Update(window, this->map, sf::Vector2f(this->player->getPosition().x, this->player->getPosition().y));
if(inputManager.IsPressed(InputManager::LoadMap)) { // Reloading of the map
if(inputManager.IsPressed(InputManager::LoadMap)
&& inputManager.IsKeyBoardPressed(sf::Keyboard::LControl)) {
std::cout << "Loading Map..." << std::endl; std::cout << "Loading Map..." << std::endl;
MapLoad(this->map, "data/map/level1.json", 0);
}
else if(inputManager.IsPressed(InputManager::LoadMap)) {
std::cout << "Updating Map..." << std::endl;
MapLoad(this->map, "data/map/level1.json", 1); MapLoad(this->map, "data/map/level1.json", 1);
} }
} }
void MainGame::Render(sf::RenderWindow* window) { void MainGame::Render(sf::RenderWindow* window) {
this->map->RenderGround(window); this->map->RenderGround(window);
this->entityManager->Render(window); this->entityManager->Render(window, this->camera);
this->map->RenderAbove(window); this->map->RenderAbove(window);
} }

Loading…
Cancel
Save