Browse Source

Added Input and Enity

Added InputManager
Added Entity
Added EntityManager
master
Riyyi 9 years ago
parent
commit
e5ee39e736
  1. 0
      src/data/gfx/Tilesheet.png
  2. BIN
      src/data/gfx/test.png
  3. 25
      src/data/map/level1.csv
  4. 0
      src/data/map/level1.json
  5. 0
      src/data/map/level1.tmx
  6. 74
      src/entity.h
  7. 98
      src/entitymanager.h
  8. BIN
      src/include/SFML/lib/libsfml-graphics.a
  9. 98
      src/inputmanager.cpp
  10. 39
      src/inputmanager.h
  11. 41
      src/main.cpp
  12. 12
      src/maploader.cpp
  13. 12
      src/maploader.h
  14. 6
      src/rpg.pro

0
src/gfx/Tilesheet.png → src/data/gfx/Tilesheet.png

Before

Width:  |  Height:  |  Size: 301 KiB

After

Width:  |  Height:  |  Size: 301 KiB

BIN
src/data/gfx/test.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 388 B

25
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,-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,-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
1 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
2 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
3 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
4 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
5 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
6 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
7 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
8 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
9 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
10 -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
11 -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
12 -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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/data/map/level1.json

0
src/map/level1.tmx → src/data/map/level1.tmx

74
src/entity.h

@ -0,0 +1,74 @@
#ifndef ENTITY_H
#define ENTITY_H
#include <string>
#include <SFML/Graphics.hpp>
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

98
src/entitymanager.h

@ -0,0 +1,98 @@
#ifndef ENTITYMANAGER
#define ENTITYMANAGER
#include <unordered_map>
#include <vector>
#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<std::string, Entity*>::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<std::string, Entity*>::const_iterator found = this->entities.find(name);
if(found != this->entities.end()) {
return found->second;
}
return NULL;
}
void Update() {
std::vector<std::string> 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<std::string, Entity*> entities;
CollisionUpdateEvent* collisionsEvent;
};
#endif // ENTITYMANAGER

BIN
src/include/SFML/lib/libsfml-graphics.a

Binary file not shown.

98
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);

39
src/inputmanager.h

@ -0,0 +1,39 @@
#ifndef INPUTMANAGER_H
#define INPUTMANAGER_H
#include <SFML/Window/Keyboard.hpp>
#include <SFML/Window/Joystick.hpp>
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);

41
src/main.cpp

@ -1,6 +1,15 @@
#include <iostream>
#include <SFML/Graphics.hpp>
#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();

12
src/maploader.cpp

@ -1,12 +0,0 @@
#include "maploader.h"
MapLoader::MapLoader()
{
}
MapLoader::~MapLoader()
{
}

12
src/maploader.h

@ -1,12 +0,0 @@
#ifndef MAPLOADER_H
#define MAPLOADER_H
class MapLoader
{
public:
MapLoader();
~MapLoader();
};
#endif // MAPLOADER_H

6
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

Loading…
Cancel
Save