hx3d  1
2D/3D Simple Game Framework
viewport.cpp
1 /*
2  Viewport.
3  Inspired by LibGDX viewport.
4 
5  Copyright (C) 2015 Denis BOURGE
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public
9  License as published by the Free Software Foundation; either
10  version 2.1 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License along with this library; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20  USA
21 */
22 
23 #include "hx3d/graphics/viewports/viewport.hpp"
24 
25 #include "hx3d/graphics/cameras/camera.hpp"
26 #include "hx3d/graphics/gl.hpp"
27 
28 namespace hx3d {
29 namespace graphics {
30 namespace viewports {
31 
32 Viewport::Viewport():
33  _worldWidth(0), _worldHeight(0), _screenX(0), _screenY(0), _screenWidth(0), _screenHeight(0) {}
34 
35 Viewport::~Viewport() {}
36 
37 void Viewport::setScreenPosition(const float x, const float y) {
38  _screenX = x;
39  _screenY = y;
40 }
41 
42 void Viewport::apply(Camera& camera) {
44 
45  camera.viewportWidth = _worldWidth;
47 
48  // Centering
49  camera.position = glm::vec3(_worldWidth / 2, _worldHeight / 2, camera.position.z);
50 
51  camera.update();
52 }
53 
54 void Viewport::update(Camera& camera, const int screenWidth, const int screenHeight) {
55  _screenWidth = screenWidth;
56  _screenHeight = screenHeight;
57 
58  internalUpdate(camera);
59 }
60 
61 glm::vec2 Viewport::screenToWorld(const glm::vec2 screenPoint) {
62  glm::vec2 worldPoint = glm::vec2(0);
63  float ratioX = (_screenWidth / _worldWidth);
64  float ratioY = (_screenHeight / _worldHeight);
65 
66  worldPoint.x = screenPoint.x / ratioX - _screenX / ratioX;
67  worldPoint.y = screenPoint.y / ratioY - _screenY / ratioY;
68  return worldPoint;
69 }
70 
72  return glm::vec2(_worldWidth, _worldHeight);
73 }
74 
75 } /* viewports */
76 } /* graphics */
77 } /* hx3d */
float viewportHeight
Viewport height.
Definition: camera.hpp:111
2D/3D camera.
Definition: camera.hpp:41
hx3d framework namespace
Definition: audio.hpp:26
void apply(Camera &camera)
Apply the viewport on the screen, centering the camera.
Definition: viewport.cpp:42
float _worldHeight
World height.
Definition: viewport.hpp:95
virtual void update()=0
Update the camera.
float viewportWidth
Viewport width.
Definition: camera.hpp:109
glm::vec3 position
Camera position.
Definition: camera.hpp:92
virtual void internalUpdate(Camera &camera) override
Update the viewport, centering the camera (internal).
int _screenHeight
Screen height.
Definition: viewport.hpp:104
void setScreenPosition(const float x, const float y)
Set the viewport position.
Definition: viewport.cpp:37
void update(Camera &camera, const int screenWidth, const int screenHeight)
Update the viewport with a new screen width and height, centering the camera.
Definition: viewport.cpp:54
glm::vec2 screenToWorld(const glm::vec2 screenPoint)
Convert a screen point to a world point.
Definition: viewport.cpp:61
float _worldWidth
World width.
Definition: viewport.hpp:93
glm::vec2 getWorldSize()
Get the world size.
Definition: viewport.cpp:71