21 #include <ResourceManager.h> 25 #include "HudRenderer.h" 27 #include <IHudDrawable.h> 28 #include <HUDGraphic.h> 29 #include <SFML/Window/Mouse.hpp> 30 #include <RelativeIHudDrawable.h> 31 #include "Dimension.h" 35 HudRenderer::HudRenderer(){
37 ResourceManager::loadShader(
"shaders/hud.vert",
"shaders/hud.frag",
"hudShader");
38 shaderProgram = ResourceManager::getShader(
"hudShader");
48 return rootLayout->findById(
id);
53 int w = Globals::get(Globals::WINDOW_WIDTH), h = Globals::get(Globals::WINDOW_HEIGHT);
54 squares = map<string,IHudDrawable*>();
55 for(
auto relLayout : relativeLayouts){
56 map<string,IHudDrawable*> tempSquares = relLayout.second->getGLSquares(0,0,relLayout.second->getWidth().getSize(w),relLayout.second->getHeight().getSize(h));
57 for(
auto square : tempSquares)
58 squares.insert(squares.end(),pair<string,IHudDrawable*>(square.first,
new RelativeIHudDrawable(worldCamera,relLayout.first,square.second)));
60 rootLayout->getGLSquares(0,0,(
float)w,(
float)h, &squares);
64 void HudRenderer::render() {
66 chag::float4x4 orthographicProjection = createOrthographicProjection();
67 for(
auto child : squares)
68 child.second->render(shaderProgram,&orthographicProjection);
73 std::map<string,IHudDrawable*>::iterator it = squares.find(
id);
74 return it == squares.end() ?
nullptr : it->second;
77 chag::float4x4 HudRenderer::createOrthographicProjection() {
78 static int w = -1, h = -1;
79 static chag::float4x4 orthoMatrix;
80 if(Globals::get(Globals::WINDOW_WIDTH) != w || Globals::get(Globals::WINDOW_HEIGHT) != h){
81 w = Globals::get(Globals::WINDOW_WIDTH);
82 h = Globals::get(Globals::WINDOW_HEIGHT);
83 float r = (float)w, l = 0.0f, b = -(
float)h, t = 0.0f, f = 1.0f, n = -1.0f;
84 orthoMatrix = chag::make_ortho(r, l, t, b, f, n);
89 HudRenderer::~HudRenderer(){
94 void HudRenderer::renderShadow(
ShaderProgram *shaderProgram) {}
96 void HudRenderer::update(
float dt){
97 int x = Globals::get(Globals::MOUSE_WINDOW_X);
98 int y = Globals::get(Globals::MOUSE_WINDOW_Y);
99 if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
100 rootLayout->invokeListenersInternal(x,y,Layout::ON_CLICK_LISTENER,
true);
102 rootLayout->invokeListenersInternal(x,y,Layout::ON_CLICK_LISTENER,
false);
103 rootLayout->invokeListeners(x,y,Layout::ON_HOVER_LISTENER);
106 void HudRenderer::setWorldCamera(
Camera *worldCamera) {
107 this->worldCamera = worldCamera;
110 void HudRenderer::addRelativeLayout(
GameObject *relativeTo,
Layout *layout) {
111 this->relativeLayouts.push_back(pair<GameObject*, Layout*>(relativeTo, layout));
virtual void setLayout(Layout *layout)
virtual IHudDrawable * getHudDrawableById(std::string id)
Class for maintaining OpenGL shader programs.
virtual Layout * getLayoutById(std::string id)
virtual void updateLayout()
A class for containing all information about a object in the game world.