Optimize headers
[oweals/minetest.git] / src / camera.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010-2011 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef CAMERA_HEADER
21 #define CAMERA_HEADER
22
23 #include "irrlichttypes_extrabloated.h"
24 #include "inventory.h"
25 #include "mesh.h"
26 #include "tile.h"
27 #include "util/numeric.h"
28 #include <ICameraSceneNode.h>
29
30 class LocalPlayer;
31 struct MapDrawControl;
32 class IGameDef;
33
34 /*
35         Client camera class, manages the player and camera scene nodes, the viewing distance
36         and performs view bobbing etc. It also displays the wielded tool in front of the
37         first-person camera.
38 */
39 class Camera
40 {
41 public:
42         Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
43                         IGameDef *gamedef);
44         ~Camera();
45
46         // Get player scene node.
47         // This node is positioned at the player's torso (without any view bobbing),
48         // as given by Player::m_position. Yaw is applied but not pitch.
49         inline scene::ISceneNode* getPlayerNode() const
50         {
51                 return m_playernode;
52         }
53
54         // Get head scene node.
55         // It has the eye transformation and pitch applied,
56         // but no view bobbing.
57         inline scene::ISceneNode* getHeadNode() const
58         {
59                 return m_headnode;
60         }
61
62         // Get camera scene node.
63         // It has the eye transformation, pitch and view bobbing applied.
64         inline scene::ICameraSceneNode* getCameraNode() const
65         {
66                 return m_cameranode;
67         }
68
69         // Get the camera position (in absolute scene coordinates).
70         // This has view bobbing applied.
71         inline v3f getPosition() const
72         {
73                 return m_camera_position;
74         }
75
76         // Get the camera direction (in absolute camera coordinates).
77         // This has view bobbing applied.
78         inline v3f getDirection() const
79         {
80                 return m_camera_direction;
81         }
82
83         // Horizontal field of view
84         inline f32 getFovX() const
85         {
86                 return m_fov_x;
87         }
88
89         // Vertical field of view
90         inline f32 getFovY() const
91         {
92                 return m_fov_y;
93         }
94
95         // Get maximum of getFovX() and getFovY()
96         inline f32 getFovMax() const
97         {
98                 return MYMAX(m_fov_x, m_fov_y);
99         }
100
101         // Checks if the constructor was able to create the scene nodes
102         bool successfullyCreated(std::wstring& error_message);
103
104         // Step the camera: updates the viewing range and view bobbing.
105         void step(f32 dtime);
106
107         // Update the camera from the local player's position.
108         // frametime is used to adjust the viewing range.
109         void update(LocalPlayer* player, f32 frametime, v2u32 screensize,
110                         f32 tool_reload_ratio);
111
112         // Render distance feedback loop
113         void updateViewingRange(f32 frametime_in);
114
115         // Start digging animation
116         // Pass 0 for left click, 1 for right click
117         void setDigging(s32 button);
118
119         // Replace the wielded item mesh
120         void wield(const ItemStack &item);
121
122         // Draw the wielded tool.
123         // This has to happen *after* the main scene is drawn.
124         // Warning: This clears the Z buffer.
125         void drawWieldedTool();
126
127 private:
128         // Scene manager and nodes
129         scene::ISceneManager* m_smgr;
130         scene::ISceneNode* m_playernode;
131         scene::ISceneNode* m_headnode;
132         scene::ICameraSceneNode* m_cameranode;
133
134         scene::ISceneManager* m_wieldmgr;
135         scene::IMeshSceneNode* m_wieldnode;
136         u8 m_wieldlight;
137
138         // draw control
139         MapDrawControl& m_draw_control;
140         
141         IGameDef *m_gamedef;
142
143         // Absolute camera position
144         v3f m_camera_position;
145         // Absolute camera direction
146         v3f m_camera_direction;
147
148         // Field of view and aspect ratio stuff
149         f32 m_aspect;
150         f32 m_fov_x;
151         f32 m_fov_y;
152
153         // Stuff for viewing range calculations
154         f32 m_added_frametime;
155         s16 m_added_frames;
156         f32 m_range_old;
157         f32 m_frametime_old;
158         f32 m_frametime_counter;
159         f32 m_time_per_range;
160
161         // View bobbing animation frame (0 <= m_view_bobbing_anim < 1)
162         f32 m_view_bobbing_anim;
163         // If 0, view bobbing is off (e.g. player is standing).
164         // If 1, view bobbing is on (player is walking).
165         // If 2, view bobbing is getting switched off.
166         s32 m_view_bobbing_state;
167         // Speed of view bobbing animation
168         f32 m_view_bobbing_speed;
169
170         // Digging animation frame (0 <= m_digging_anim < 1)
171         f32 m_digging_anim;
172         // If -1, no digging animation
173         // If 0, left-click digging animation
174         // If 1, right-click digging animation
175         s32 m_digging_button;
176 };
177
178 #endif