diff --git a/src/gfx/Tilesheet.png b/src/data/gfx/Tilesheet.png similarity index 100% rename from src/gfx/Tilesheet.png rename to src/data/gfx/Tilesheet.png diff --git a/src/data/gfx/test.png b/src/data/gfx/test.png new file mode 100644 index 0000000..6d48b7d Binary files /dev/null and b/src/data/gfx/test.png differ diff --git a/src/data/map/level1.csv b/src/data/map/level1.csv new file mode 100644 index 0000000..b677007 --- /dev/null +++ b/src/data/map/level1.csv @@ -0,0 +1,25 @@ +0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,0,1,2,3,-1,-1,-1,-1,-1 +96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,96,97,98,99,-1,-1,-1,-1,-1 +192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,192,193,194,195,-1,-1,-1,-1,-1 +288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,288,289,290,291,-1,-1,-1,-1,-1 +384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,384,385,386,387,-1,-1,-1,-1,-1 +2152,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2171,-1,-1,-1,-1,-1 +2248,2249,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2267,-1,-1,-1,-1,-1 +2344,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2363,-1,-1,-1,-1,-1 +2440,2441,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,2459,-1,-1,-1,-1,-1 +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1 +-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1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/src/map/level1.json b/src/data/map/level1.json similarity index 100% rename from src/map/level1.json rename to src/data/map/level1.json diff --git a/src/map/level1.tmx b/src/data/map/level1.tmx similarity index 100% rename from src/map/level1.tmx rename to src/data/map/level1.tmx diff --git a/src/entity.h b/src/entity.h new file mode 100644 index 0000000..22580b8 --- /dev/null +++ b/src/entity.h @@ -0,0 +1,74 @@ +#ifndef ENTITY_H +#define ENTITY_H + +#include +#include + +class Entity : public sf::Sprite +{ +public: + Entity() { + this->active = 1; + this->texture = new sf::Texture(); + } + + Entity(std::string fileName) { + this->active = 1; + this->texture = new sf::Texture; + this->Load(fileName); + } + + Entity(std::string fileName, sf::IntRect rect) { + this->active = 1; + this->texture = new sf::Texture(); + this->Load(fileName, rect); + } + + void Load(std::string fileName) { + this->texture->loadFromFile(fileName, sf::IntRect()); + this->setTexture(*this->texture); + } + + void Load(std::string fileName, sf::IntRect rect) { + this->texture->loadFromFile(fileName, rect); + this->setTexture(*this->texture); + } + + bool Collision(Entity *entity) { + if(entity != NULL) { + return this->getGlobalBounds().intersects(entity->getGlobalBounds()); + } + + return false; + } + + void SetActive(int active) { + this->active = active; + } + + int Active() { + return this->active; + } + + int Group() { + return this->groupId; + } + + virtual void Update() { + this->move(this->velocity); + } + + ~Entity() { + delete this->texture; + } + + sf::Vector2f velocity; + +protected: + int active, groupId; + +private: + sf::Texture* texture; +}; + +#endif // ENTITY_H diff --git a/src/entitymanager.h b/src/entitymanager.h new file mode 100644 index 0000000..24a46db --- /dev/null +++ b/src/entitymanager.h @@ -0,0 +1,98 @@ +#ifndef ENTITYMANAGER +#define ENTITYMANAGER + +#include +#include + +#include "entity.h" + +typedef void CollisionUpdateEvent(Entity* entityA, Entity* entityB); + +class EntityManager +{ +public: + EntityManager() { + + } + + void SetCollisionMethod(CollisionUpdateEvent collisionsEvent) { + this->collisionsEvent = collisionsEvent; + } + + void AddEntity(std::string name, Entity* entity) { + std::unordered_map::const_iterator found = this->entities.find(name); + while(found != this->entities.end()) { + name += "0"; + found = this->entities.find(name); + } + + this->entities.insert(std::make_pair(name, entity)); + } + + Entity* Get(std::string name) { + std::unordered_map::const_iterator found = this->entities.find(name); + if(found != this->entities.end()) { + return found->second; + } + + return NULL; + } + + void Update() { + std::vector toRemove; + + for (auto& iterator : this->entities) { + if (iterator.second != NULL) { + if (this->collisionsEvent != NULL) { + for (auto& iterator2 : this->entities) { + if (iterator != iterator2) { + if (iterator.second->Collision(iterator2.second)) { + this->collisionsEvent(iterator.second, iterator2.second); + } + } + } + } + + if (iterator.second->Active()) { + iterator.second->Update(); + } + else { + toRemove.push_back(iterator.first); + } + } + } + + while (toRemove.size() > 0) { + this->entities.erase(toRemove[toRemove.size() - 1]); + toRemove.pop_back(); + } + + toRemove.clear(); + } + + + void Render(sf::RenderWindow* window) { + for (auto& iterator : this->entities) { + if (iterator.second != NULL) { + if (iterator.second->Active()) { + window->draw(*iterator.second); + } + } + } + } + + ~EntityManager() { + for (auto& iterator : this->entities) { + delete iterator.second; + } + + this->entities.clear(); + } + +private: + std::unordered_map entities; + CollisionUpdateEvent* collisionsEvent; +}; + +#endif // ENTITYMANAGER + diff --git a/src/include/SFML/lib/libsfml-graphics.a b/src/include/SFML/lib/libsfml-graphics.a new file mode 100644 index 0000000..9859e26 Binary files /dev/null and b/src/include/SFML/lib/libsfml-graphics.a differ diff --git a/src/inputmanager.cpp b/src/inputmanager.cpp new file mode 100644 index 0000000..c03914d --- /dev/null +++ b/src/inputmanager.cpp @@ -0,0 +1,98 @@ +#include "inputmanager.h" + +InputManager::InputManager() { + +} + +InputManager::~InputManager() { + +} + +sf::Keyboard::Key InputManager::KeyboardConfiguration(Input key) { +// Left = 0, +// Right, +// Up, +// Down, + + if(key == Left) return sf::Keyboard::A; + if(key == Right) return sf::Keyboard::E; + if(key == Up) return sf::Keyboard::Comma; + if(key == Down) return sf::Keyboard::O; + + return sf::Keyboard::Unknown; +} + +int InputManager::JoystickConfiguration(Input key) { +// if(key == Left) return 2; +// if(key == Right) return 1; +// if(key == Up) return 3; +// if(key == Down) return 0; + + return -1; +} + +void InputManager::JoystickAxisConfiguration(Input key, sf::Joystick::Axis& axis, float& position) { + if(key == Left) { + axis = sf::Joystick::PovY; + position = -100; + } + + if(key == Right) { + axis = sf::Joystick::PovY; + position = 100; + } + + if(key == Up) { + axis = sf::Joystick::PovX; + position = 100; + } + + if(key == Down) { + axis = sf::Joystick::PovX; + position = -100; + } +} + +bool InputManager::IsKeyBoardPressed(sf::Keyboard::Key key) { + return sf::Keyboard::isKeyPressed(key); +} + +bool InputManager::IsJoystickPressed(unsigned int joystick, unsigned int button) { + return sf::Joystick::isButtonPressed(joystick, button); +} + +bool InputManager::IsJoystickAxis(unsigned int joystick, sf::Joystick::Axis axis, float position) { + return sf::Joystick::getAxisPosition(joystick, axis) == position; +} + +bool InputManager::IsPressed(Input key, unsigned int joystick) { + sf::Keyboard::Key myKeyboardKey = KeyboardConfiguration(key); + if(myKeyboardKey != sf::Keyboard::Unknown) { + if(IsKeyBoardPressed(myKeyboardKey)) { + return true; + } + } + + + int myJoystickKey = JoystickConfiguration(key); + if(myJoystickKey != -1) { + if(IsJoystickPressed(joystick, myJoystickKey)) { + return true; + } + } + + sf::Joystick::Axis axis; + float position = 0; + JoystickAxisConfiguration(key, axis, position); + if(position != 0) { + if(IsJoystickAxis(joystick, axis, position)) { + return true; + } + } + + return false; +} + +//bool sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) +//bool sf::Joystick::isButtonPressed(0, 1); +//float sf::Joystick::getAxisPosition(0, sf::Joystick::Y); diff --git a/src/inputmanager.h b/src/inputmanager.h new file mode 100644 index 0000000..3a7d47a --- /dev/null +++ b/src/inputmanager.h @@ -0,0 +1,39 @@ +#ifndef INPUTMANAGER_H +#define INPUTMANAGER_H + +#include +#include + +class InputManager +{ +public: + InputManager(); + ~InputManager(); + + enum Input + { + Left = 0, + Right, + Up, + Down, + + KeyCount ///< Keep last -- the total number of inputs + }; + + sf::Keyboard::Key KeyboardConfiguration(Input key); + int JoystickConfiguration(Input key); + void JoystickAxisConfiguration(Input key, sf::Joystick::Axis& axis, float& position); + bool IsKeyBoardPressed(sf::Keyboard::Key); + bool IsJoystickPressed(unsigned int joystick, unsigned int button); + bool IsJoystickAxis(unsigned int joystick, sf::Joystick::Axis axis, float position); + bool IsPressed(Input key, unsigned int joystick = 0); +}; + +extern InputManager inputManager; + +#endif // INPUTMANAGER_H + + +//bool sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) +//bool sf::Joystick::isButtonPressed(0, 1); +//float sf::Joystick::getAxisPosition(0, sf::Joystick::Y); diff --git a/src/main.cpp b/src/main.cpp index b269330..cb203e8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,6 +1,15 @@ #include #include +#include "inputmanager.h" +#include "entitymanager.h" + +InputManager inputManager; + +void UpdateCollisions(Entity* entityA, Entity* entityB) { + std::cout << "Collisions!" << std::endl; +} + int main() { std::cout << "Hello World!" << std::endl; @@ -17,6 +26,17 @@ int main() sf::Clock timer; sf::Time timeElapsed; + EntityManager entityManager; + entityManager.AddEntity("test", new Entity("data\\gfx\\test.png")); + entityManager.AddEntity("test", new Entity("data\\gfx\\test.png")); + entityManager.SetCollisionMethod(UpdateCollisions); + +// Entity entity; +// entity.Load("data\\gfx\\test.png"); + +// Entity entity2; +// entity2.Load("data\\gfx\\test.png"); + // Run the program as long as the window is open while (window.isOpen()) { @@ -25,8 +45,9 @@ int main() while (window.pollEvent(event)) { // 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(); + } // Enter fullscreen mode : key combination : Alt+Enter if (event.type == sf::Event::KeyPressed) { @@ -44,9 +65,27 @@ int main() } } } + + if(inputManager.IsPressed(InputManager::Left)) { + std::cout << "LEFT" << std::endl; + } + + if(inputManager.IsPressed(InputManager::Down)) { + std::cout << "DOWN" << std::endl; + } } +// if(entity.Collision(&entity2)) { +// std::cout << "COLLISION!" << std::endl; +// } + window.clear(sf::Color(72, 152, 72)); + +// window.draw(entity); +// window.draw(entity2); + + entityManager.Render(&window); + window.display(); timeElapsed = timer.getElapsedTime(); diff --git a/src/maploader.cpp b/src/maploader.cpp deleted file mode 100644 index 4f582a4..0000000 --- a/src/maploader.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "maploader.h" - -MapLoader::MapLoader() -{ - -} - -MapLoader::~MapLoader() -{ - -} - diff --git a/src/maploader.h b/src/maploader.h deleted file mode 100644 index 30e4193..0000000 --- a/src/maploader.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef MAPLOADER_H -#define MAPLOADER_H - - -class MapLoader -{ -public: - MapLoader(); - ~MapLoader(); -}; - -#endif // MAPLOADER_H diff --git a/src/rpg.pro b/src/rpg.pro index cc91460..295ea39 100644 --- a/src/rpg.pro +++ b/src/rpg.pro @@ -18,8 +18,10 @@ DEPENDPATH += $$PWD/include TEMPLATE = app SOURCES += main.cpp \ - maploader.cpp + inputmanager.cpp HEADERS += \ - maploader.h + inputmanager.h \ + entity.h \ + entitymanager.h