Bubba-3D  0.9.0
Awesome game engine!
HudRenderer.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 //
18 // Created by johan on 2015-12-12.
19 //
20 
21 #include <ResourceManager.h>
22 #include <GLSquare.h>
23 #include <Layout.h>
24 #include <Globals.h>
25 #include "HudRenderer.h"
26 #include <vector>
27 #include <IHudDrawable.h>
28 #include <HUDGraphic.h>
29 #include <SFML/Window/Mouse.hpp>
30 #include <RelativeIHudDrawable.h>
31 #include "Dimension.h"
32 
33 using namespace std;
34 
35 HudRenderer::HudRenderer(){
36 
37  ResourceManager::loadShader("shaders/hud.vert", "shaders/hud.frag", "hudShader");
38  shaderProgram = ResourceManager::getShader("hudShader");
39 
40 }
41 
43  rootLayout = layout;
44  updateLayout();
45 }
46 
48  return rootLayout->findById(id);
49 }
50 
52 
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)));
59  }
60  rootLayout->getGLSquares(0,0,(float)w,(float)h, &squares);
61 
62 }
63 
64 void HudRenderer::render() {
65 
66  chag::float4x4 orthographicProjection = createOrthographicProjection();
67  for(auto child : squares)
68  child.second->render(shaderProgram,&orthographicProjection);
69 
70 }
71 
73  std::map<string,IHudDrawable*>::iterator it = squares.find(id);
74  return it == squares.end() ? nullptr : it->second;
75 }
76 
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);
85  }
86  return orthoMatrix;
87 }
88 
89 HudRenderer::~HudRenderer(){
90 
91 }
92 
93 
94 void HudRenderer::renderShadow(ShaderProgram *shaderProgram) {}
95 
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);
101  else
102  rootLayout->invokeListenersInternal(x,y,Layout::ON_CLICK_LISTENER,false);
103  rootLayout->invokeListeners(x,y,Layout::ON_HOVER_LISTENER);
104 }
105 
106 void HudRenderer::setWorldCamera(Camera *worldCamera) {
107  this->worldCamera = worldCamera;
108 }
109 
110 void HudRenderer::addRelativeLayout(GameObject *relativeTo, Layout *layout) {
111  this->relativeLayouts.push_back(pair<GameObject*, Layout*>(relativeTo, layout));
112  updateLayout();
113 }
virtual void setLayout(Layout *layout)
Definition: HudRenderer.cpp:42
virtual IHudDrawable * getHudDrawableById(std::string id)
Definition: HudRenderer.cpp:72
Class for maintaining OpenGL shader programs.
Definition: ShaderProgram.h:39
virtual Layout * getLayoutById(std::string id)
Definition: HudRenderer.cpp:47
virtual void updateLayout()
Definition: HudRenderer.cpp:51
A class for containing all information about a object in the game world.
Definition: GameObject.h:67
Definition: Layout.h:33
Definition: Camera.h:26