Update ifdef condition for MCST-LCC compiler (E2K)
[0ad.git] / source / tools / atlas / GameInterface / InputProcessor.cpp
blobebb736d468710e8b7b6cc9dcd04f98fcb4b44ad4
1 /* Copyright (C) 2010 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. 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 General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 #include "precompiled.h"
20 #include "InputProcessor.h"
22 #include "ps/Game.h"
23 #include "graphics/Camera.h"
24 #include "graphics/GameView.h"
25 #include "maths/Quaternion.h"
27 static float g_ViewZoomSmoothness = 0.02f;
28 static float g_ViewRotateScale = 0.02f;
30 static void Rotate(CCamera& camera, float speed)
32 CVector3D upwards(0.0f, 1.0f, 0.0f);
33 CVector3D origin = camera.m_Orientation.GetTranslation();
34 CVector3D pivot = camera.GetFocus();
35 CVector3D delta = origin - pivot;
37 CQuaternion r;
38 r.FromAxisAngle(upwards, speed);
39 delta = r.Rotate(delta);
40 camera.m_Orientation.Translate(-origin);
41 camera.m_Orientation.Rotate(r);
42 camera.m_Orientation.Translate(pivot + delta);
45 bool InputProcessor::ProcessInput(GameLoopState* state)
47 if (! g_Game)
48 return false;
50 CCamera* camera = g_Game->GetView()->GetCamera();
52 CVector3D leftwards = camera->m_Orientation.GetLeft();
54 CVector3D inwards = camera->m_Orientation.GetIn();
56 // Calculate a vector pointing forwards, parallel to the ground
57 CVector3D forwards = inwards;
58 forwards.Y = 0.0f;
59 if (forwards.Length() < 0.001f) // be careful if the camera is looking straight down
60 forwards = CVector3D(1.f, 0.f, 0.f);
61 else
62 forwards.Normalize();
64 bool moved = false;
66 GameLoopState::Input& input = state->input;
68 if (state->input.scrollSpeed[0] != 0.0f)
70 camera->m_Orientation.Translate(forwards * (input.scrollSpeed[0] * state->realFrameLength));
71 moved = true;
74 if (state->input.scrollSpeed[1] != 0.0f)
76 camera->m_Orientation.Translate(forwards * (-input.scrollSpeed[1] * state->realFrameLength));
77 moved = true;
80 if (state->input.scrollSpeed[2] != 0.0f)
82 camera->m_Orientation.Translate(leftwards * (input.scrollSpeed[2] * state->realFrameLength));
83 moved = true;
86 if (state->input.scrollSpeed[3] != 0.0f)
88 camera->m_Orientation.Translate(leftwards * (-input.scrollSpeed[3] * state->realFrameLength));
89 moved = true;
92 if (state->input.scrollSpeed[4] != 0.0f)
94 Rotate(*camera, input.scrollSpeed[4] * state->realFrameLength * g_ViewRotateScale);
95 moved = true;
98 if (state->input.scrollSpeed[5] != 0.0f)
100 Rotate(*camera, -input.scrollSpeed[5] * state->realFrameLength * g_ViewRotateScale);
101 moved = true;
104 if (state->input.zoomDelta != 0.0f)
106 float zoom_proportion = powf(g_ViewZoomSmoothness, state->realFrameLength);
107 camera->m_Orientation.Translate(inwards * (input.zoomDelta * (1.0f - zoom_proportion)));
108 input.zoomDelta *= zoom_proportion;
110 if (fabsf(input.zoomDelta) < 0.1f)
111 input.zoomDelta = 0.0f;
113 moved = true;
116 if (moved)
117 camera->UpdateFrustum();
119 return moved;