started adding sparrowshell

This commit is contained in:
Lendemor 2016-03-28 21:43:12 +02:00
parent 7911e7bddf
commit 90a03b6f51
9 changed files with 183 additions and 13 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
build* build*
*.user *.user
data*

View File

@ -9,7 +9,7 @@
#include "tools/pathfinder.h" #include "tools/pathfinder.h"
int main(){ int main(){
CameraNode camera; /* CameraNode camera;
RESOURCE_ADD(&camera, SceneNode, "camera"); RESOURCE_ADD(&camera, SceneNode, "camera");
Scene scene("testScene"); Scene scene("testScene");
@ -30,14 +30,16 @@ int main(){
GraphNode n4 = GraphNode(); GraphNode n4 = GraphNode();
n4.setValue(4); n4.setValue(4);
n1.addNeighbours(&n2,2); n1.addNeighbours(&n2);
n1.addNeighbours(&n3,3); n1.addNeighbours(&n3);
n2.addNeighbours(&n3,5); n2.addNeighbours(&n3);
n3.addNeighbours(&n4,2); n3.addNeighbours(&n4);
std::vector<GraphNode*> path = PathFinder::a_star(&n1,&n4); std::vector<GraphNode*> path = PathFinder::a_star(&n1,&n4);
std::cout << "Path Size: " << path.size() << std::endl; std::cout << "Path Size: " << path.size() << std::endl;
for(GraphNode* gn: path){ for(GraphNode* gn: path){
std::cout << gn->getValue() << std::endl; std::cout << gn->getValue() << std::endl;
} }*/
} }

View File

@ -1,11 +1,29 @@
#include "messagebus.h" #include "messagebus.h"
#include "system.h"
MessageBus::MessageBus() MessageBus::MessageBus()
{ {
} }
void MessageBus::registerSystem(SystemType type, System* system){
systems[type] = system;
}
void MessageBus::update()
{
while (!message_list.empty()){
Message* msg = message_list.front();
message_list.pop();
for(auto const &entity : systems){
entity.second->handleMessage(msg);
}
}
}
void MessageBus::postMessage(Message *msg) void MessageBus::postMessage(Message *msg)
{ {
message_list.push_back(msg); message_list.push(msg);
} }

View File

@ -1,17 +1,25 @@
#ifndef MESSAGEBUS_H #ifndef MESSAGEBUS_H
#define MESSAGEBUS_H #define MESSAGEBUS_H
#include <vector> #include <queue>
#include <map>
#include <string>
#include "system.h"
class Message; class Message;
class System;
class MessageBus class MessageBus
{ {
std::vector<Message*> message_list; //message file std::map<SystemType,System*> systems;
std::queue<Message*> message_list; //message file
public: public:
MessageBus(); MessageBus();
void registerSystem(SystemType,System*);
void postMessage(Message* msg); void postMessage(Message* msg);
void update();
void handleMessage(); void handleMessage();
}; };

51
src/sparrowshell.cpp Normal file
View File

@ -0,0 +1,51 @@
#include "sparrowshell.h"
#include "message.h"
#include "input.h"
const int SparrowShell::BUFFER_MAX_LENGTH = 50;
const int SparrowShell::BUFFER_DISPLAYED_NUMBER = 10;
const int SparrowShell::SCROLLBAR_PIXEL_WIDTH = 2;
SparrowShell::SparrowShell(sf::Window* window, Input* input): m_position(glm::ivec2(0,0)),m_window(window),m_input(input),m_scrollbar(this)
{
//m_dimension = glm::ivec2();//
}
void SparrowShell::out(std::string s)
{
if (m_buffer.size() == BUFFER_MAX_LENGTH)
m_buffer.pop_back();
m_buffer.push_front(s);
}
void SparrowShell::scrollUp(){
if (m_index + BUFFER_DISPLAYED_NUMBER < m_buffer.size()) m_index++;
}
void SparrowShell::scrollDown(){
if (m_index > 0) m_index--;
}
void SparrowShell::update()
{
//TODO : update TextMesh
m_scrollbar.update();
}
SparrowShell::ScrollBar::ScrollBar(SparrowShell* shell):m_shell(shell){
m_position = glm::ivec2(m_shell->m_dimension.x - SCROLLBAR_PIXEL_WIDTH,0);
m_dimension = glm::ivec2(SCROLLBAR_PIXEL_WIDTH,m_shell->m_dimension.y);
}
void SparrowShell::ScrollBar::update(){
m_position.y = m_shell->m_position.y;
m_dimension.y = m_shell->m_dimension.y;
if (m_shell->m_buffer.size() > BUFFER_DISPLAYED_NUMBER){
float cran = ((float)m_shell->m_dimension.y/(float)m_shell->m_buffer.size());
int indexCursor = m_shell->m_buffer.size()-(m_shell->m_index+SCROLLBAR_PIXEL_WIDTH);
m_position.y += (int)(cran * indexCursor);
m_dimension.y = (int)(cran * BUFFER_DISPLAYED_NUMBER);
}
}

55
src/sparrowshell.h Normal file
View File

@ -0,0 +1,55 @@
#ifndef SPARROWSHELL_H
#define SPARROWSHELL_H
#include <list>
#include "system.h"
#include "scene.h"
#include "glm/glm.hpp"
class Input;
namespace sf {
class Window;
}
class SparrowShell : public SceneNode
{
private:
class ScrollBar{
SparrowShell* m_shell;
glm::ivec2 m_position;
glm::ivec2 m_dimension;
//TODO : Add rectangle mesh
public:
ScrollBar();
ScrollBar(SparrowShell* shell);
void update();
};
static const int BUFFER_MAX_LENGTH;
static const int BUFFER_DISPLAYED_NUMBER;
static const int SCROLLBAR_PIXEL_WIDTH;
std::list<std::string> m_buffer;
sf::Window* m_window;
Input* m_input;
int m_index = 0;
glm::ivec2 m_position;
glm::ivec2 m_dimension;
//textMesh
ScrollBar m_scrollbar;
public:
SparrowShell(sf::Window*, Input*);
void update();
void scrollUp();
void scrollDown();
void out(std::string);
};
#endif // SPARROWSHELL_H

View File

@ -1,7 +1,28 @@
#include "system.h" #include "system.h"
#include "messagebus.h"
#include "message.h"
#include "input.h"
#include <iostream>
System::System() System::System()
{ {
} }
InputSystem::InputSystem(){
}
void InputSystem::initInput(sf::Window *window){
m_input = new Input(window);
}
void InputSystem::update(){
int action;
Message* message;
while (action = m_input->getAction() != NO_ACTION){
message = new Message(std::to_string(action),SystemType::INPUT_SYSTEM);
m_msgBus->postMessage(message);
}
}

View File

@ -3,13 +3,19 @@
class MessageBus; class MessageBus;
class Message; class Message;
class Input;
namespace sf{
class Window;
}
//TODO:complete this part with other existing system, //TODO:complete this part with other existing system,
enum SystemType {INPUT_SYSTEM, RENDERER_SYSTEM, IA_SYSTEM}; enum SystemType {INPUT_SYSTEM, RENDERER_SYSTEM, IA_SYSTEM,GAME_SYSTEM,LENGTH};
class System class System
{ {
SystemType m_type; SystemType m_type;
protected:
MessageBus* m_msgBus; MessageBus* m_msgBus;
public: public:
@ -17,4 +23,14 @@ public:
virtual void handleMessage(Message* message) = 0; virtual void handleMessage(Message* message) = 0;
}; };
class InputSystem : public System{
private:
Input* m_input;
public:
InputSystem();
~InputSystem();
void initInput(sf::Window* window);
void update();
};
#endif // SYSTEM_H #endif // SYSTEM_H

View File

@ -64,5 +64,3 @@ std::vector<GraphNode*> PathFinder::a_star(GraphNode* start,GraphNode* goal)
std::reverse(path.begin(),path.end()); std::reverse(path.begin(),path.end());
return path; return path;
} }