Permalink
Browse files

Added Input class and rotation

  • Loading branch information...
1 parent e5e14c6 commit ba0f66a19e41c9ea605b4026ea802c482e0fcf85 @rejcx committed Mar 10, 2013
Showing with 128 additions and 37 deletions.
  1. +0 −3 Application.h
  2. +17 −2 Camera.cpp
  3. +4 −0 Camera.h
  4. +79 −0 Input.cpp
  5. +22 −0 Input.h
  6. +6 −32 main.cpp
View
@@ -5,9 +5,6 @@
#include <string>
-#include <iostream>
-using namespace std;
-
namespace ursa
{
View
@@ -8,8 +8,8 @@ Camera::Camera()
m_position.x = m_position.y = m_position.z = 0;
m_rotation.x = m_rotation.y = m_rotation.z = 0;
- m_moveSpeed = 2;
- m_rotateSpeed = 2;
+ m_moveSpeed = 1;
+ m_rotateSpeed = 1;
}
void Camera::Setup( const sf::Vector3f& pos, const sf::Vector3f& rot )
@@ -38,4 +38,19 @@ void Camera::MoveZ( int inc )
m_position.z += inc * m_moveSpeed;
}
+void Camera::RotateX( int inc )
+{
+ m_rotation.x += inc * m_moveSpeed;
+}
+
+void Camera::RotateY( int inc )
+{
+ m_rotation.y += inc * m_moveSpeed;
+}
+
+void Camera::RotateZ( int inc )
+{
+ m_rotation.z += inc * m_moveSpeed;
+}
+
}
View
@@ -18,6 +18,10 @@ class Camera
void MoveY( int inc );
void MoveZ( int inc );
+ void RotateX( int inc );
+ void RotateY( int inc );
+ void RotateZ( int inc );
+
float X() { return m_position.x; }
float Y() { return m_position.y; }
float Z() { return m_position.z; }
View
@@ -0,0 +1,79 @@
+#include "Input.h"
+
+#include <iostream>
+
+namespace ursa
+{
+
+void Input::HandleInput( const sf::Input& input, Application& app, Camera& camera )
+{
+ /* SYSTEM KEYS */
+ if ( input.IsKeyDown( sf::Key::Q ) )
+ {
+ app.Quit( true );
+ }
+
+ /* MOVEMENT KEYS */
+ if ( input.IsKeyDown( sf::Key::A ) )
+ {
+ camera.MoveX( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::D ) )
+ {
+ camera.MoveX( -1 );
+ }
+ if ( input.IsKeyDown( sf::Key::S ) )
+ {
+ camera.MoveY( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::W ) )
+ {
+ camera.MoveY( -1 );
+ }
+ if ( input.IsKeyDown( sf::Key::Up ) )
+ {
+ camera.MoveZ( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::Down ) )
+ {
+ camera.MoveZ( -1 );
+ }
+
+ /* CAMERA ROTATION */
+ if ( input.IsKeyDown( sf::Key::J ) )
+ {
+ camera.RotateY( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::L ) )
+ {
+ camera.RotateY( -1 );
+ }
+ if ( input.IsKeyDown( sf::Key::I ) )
+ {
+ camera.RotateX( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::K ) )
+ {
+ camera.RotateX( -1 );
+ }
+ if ( input.IsKeyDown( sf::Key::U ) )
+ {
+ camera.RotateZ( 1 );
+ }
+ else if ( input.IsKeyDown( sf::Key::O ) )
+ {
+ camera.RotateZ( -1 );
+ }
+
+ /* DEBUG KEYS */
+ if ( input.IsKeyDown( sf::Key::F1 ) )
+ {
+ std::cout << std::endl;
+ std::cout << "Camera Coordinates:\t";
+ std::cout << camera.X() << ", " << camera.Y() << ", " << camera.Z() << std::endl;
+ std::cout << "Camera Rotation:\t";
+ std::cout << camera.RX() << ", " << camera.RY() << ", " << camera.RZ() << std::endl;
+ }
+}
+
+}
View
22 Input.h
@@ -0,0 +1,22 @@
+#ifndef _INPUT
+#define _INPUT
+
+#include <SFML/Window.hpp>
+
+#include "Application.h"
+#include "Camera.h"
+
+namespace ursa
+{
+
+class Input
+{
+ public:
+ void HandleInput( const sf::Input& input, Application& app, Camera& camera );
+
+ private:
+};
+
+}
+
+#endif
View
@@ -2,6 +2,7 @@
#include "Application.h"
#include "Camera.h"
+#include "Input.h"
using namespace std;
@@ -10,51 +11,24 @@ int main()
ursa::Application app;
ursa::Camera camera;
camera.Setup( sf::Vector3f( 0, 0, -100 ) );
+ ursa::Input input;
while ( !app.Quit() )
{
// Get Events
sf::Event event;
while ( app.GetEvent( event ) )
{
- const sf::Input& input = app.GetInput();
- if ( input.IsKeyDown( sf::Key::Q ) )
- {
- app.Quit( true );
- }
-
- // TODO: Move to its own class
- if ( input.IsKeyDown( sf::Key::A ) )
- {
- camera.MoveX( 1 );
- }
- else if ( input.IsKeyDown( sf::Key::D ) )
- {
- camera.MoveX( -1 );
- }
- if ( input.IsKeyDown( sf::Key::S ) )
- {
- camera.MoveY( 1 );
- }
- else if ( input.IsKeyDown( sf::Key::W ) )
- {
- camera.MoveY( -1 );
- }
- if ( input.IsKeyDown( sf::Key::Up ) )
- {
- camera.MoveZ( 1 );
- }
- else if ( input.IsKeyDown( sf::Key::Down ) )
- {
- camera.MoveZ( -1 );
- }
+ input.HandleInput( app.GetInput(), app, camera );
}
// Render
app.PrepDraw();
glTranslatef( camera.X(), camera.Y(), camera.Z() );
-// cout << camera.X() << ", " << camera.Y() << ", " << camera.Z() << endl;
+ glRotatef( camera.RX(), 1.0f, 0.0f, 0.0f );
+ glRotatef( camera.RY(), 0.0f, 1.0f, 0.0f );
+ glRotatef( camera.RZ(), 0.0f, 0.0f, 1.0f );
glBegin( GL_QUADS );

0 comments on commit ba0f66a

Please sign in to comment.