Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 4 commits
  • 4 files changed
  • 0 comments
  • 1 contributor

Showing 4 changed files with 245 additions and 49 deletions. Show diff stats Hide diff stats

  1. +3 4 Geometry.cpp
  2. +227 15 Input.cpp
  3. +12 2 Input.h
  4. +3 28 main.cpp
7 Geometry.cpp
@@ -39,7 +39,6 @@ void Geometry::Draw()
39 39 glEnable( GL_TEXTURE_2D );
40 40 glBegin( GL_QUADS );
41 41
42   -
43 42 DrawXY();
44 43 DrawXZ();
45 44 DrawYZ();
@@ -52,9 +51,9 @@ void Geometry::Draw()
52 51
53 52 bool Geometry::CheckCursor( const sf::Vector3f& cursor )
54 53 {
55   - if ( (int)(cursor.x) == m_index.x &&
56   - (int)(cursor.y) == m_index.y &&
57   - (int)(cursor.z) == m_index.z )
  54 + if ( (int)(cursor.x) == m_index.x*10 &&
  55 + (int)(cursor.y) == m_index.y*10 &&
  56 + (int)(cursor.z) == m_index.z*10 )
58 57 {
59 58 m_cursorOn = true;
60 59 }
242 Input.cpp
@@ -6,8 +6,41 @@ using namespace std;
6 6 namespace ursa
7 7 {
8 8
9   -void Input::HandleInput( Application& app, Camera& camera, std::vector<Geometry>& lstBoxes, sf::Vector3f& cursor, int activeBox, GLuint lstTextures[5] )
  9 +Input::Input() : m_timeoutMax( 5.0 )
10 10 {
  11 + m_cursorPosition.x = m_cursorPosition.y = m_cursorPosition.z = 0.0;
  12 + m_cursorTimeout = 0.0;
  13 +}
  14 +
  15 +void Input::CreateNewBox( std::vector<Geometry>& lstBoxes, GLuint& texture )
  16 +{
  17 + // Create a new one!
  18 + // Position, Dimensions, Index
  19 + Geometry newg(
  20 + sf::Vector3f(
  21 + m_cursorPosition.x,
  22 + m_cursorPosition.y,
  23 + m_cursorPosition.z
  24 + ),
  25 + sf::Vector3f( 10, 10, 10 ),
  26 + sf::Vector3i(
  27 + m_cursorPosition.x / 10,
  28 + m_cursorPosition.y / 10,
  29 + m_cursorPosition.z / 10
  30 + )
  31 + );
  32 +
  33 + newg.SetTexture( texture );
  34 +
  35 + lstBoxes.push_back( newg );
  36 +}
  37 +
  38 +void Input::HandleInput( Application& app, Camera& camera, std::vector<Geometry>& lstBoxes, GLuint lstTextures[5], float mapWidth, float mapDepth )
  39 +{
  40 + if ( m_cursorTimeout > 0 )
  41 + {
  42 + m_cursorTimeout--;
  43 + }
11 44
12 45 sf::Event event;
13 46 while ( app.GetEvent( event ) )
@@ -18,42 +51,121 @@ void Input::HandleInput( Application& app, Camera& camera, std::vector<Geometry>
18 51 HandleCameraControls( input, app, camera );
19 52 HandleMouseControls( input, app, lstBoxes );
20 53
  54 + // Find active box
  55 + int activeBox = -1;
  56 + for ( int i = 0; i < lstBoxes.size(); i++ )
  57 + {
  58 + if ( lstBoxes[i].CheckCursor( CursorPosition() ) )
  59 + {
  60 + activeBox = i;
  61 + }
  62 + }
  63 +
21 64 if ( input.IsKeyDown( sf::Key::Num0 ) )
22 65 {
23   - lstBoxes[activeBox].SetTexture( lstTextures[0] );
  66 + if ( activeBox == -1 )
  67 + {
  68 + CreateNewBox( lstBoxes, lstTextures[0] );
  69 + }
  70 + else
  71 + {
  72 + lstBoxes[activeBox].SetTexture( lstTextures[0] );
  73 + }
24 74 }
25 75 else if ( input.IsKeyDown( sf::Key::Num1 ) )
26 76 {
27   - lstBoxes[activeBox].SetTexture( lstTextures[1] );
  77 + if ( activeBox == -1 )
  78 + {
  79 + CreateNewBox( lstBoxes, lstTextures[1] );
  80 + }
  81 + else
  82 + {
  83 + lstBoxes[activeBox].SetTexture( lstTextures[1] );
  84 + }
28 85 }
29 86 else if ( input.IsKeyDown( sf::Key::Num2 ) )
30 87 {
31   - lstBoxes[activeBox].SetTexture( lstTextures[2] );
  88 + if ( activeBox == -1 )
  89 + {
  90 + CreateNewBox( lstBoxes, lstTextures[2] );
  91 + }
  92 + else
  93 + {
  94 + lstBoxes[activeBox].SetTexture( lstTextures[2] );
  95 + }
32 96 }
33 97 else if ( input.IsKeyDown( sf::Key::Num3 ) )
34 98 {
35   - lstBoxes[activeBox].SetTexture( lstTextures[3] );
  99 + if ( activeBox == -1 )
  100 + {
  101 + CreateNewBox( lstBoxes, lstTextures[3] );
  102 + }
  103 + else
  104 + {
  105 + lstBoxes[activeBox].SetTexture( lstTextures[3] );
  106 + }
36 107 }
37 108 else if ( input.IsKeyDown( sf::Key::Num4 ) )
38 109 {
39   - lstBoxes[activeBox].SetTexture( lstTextures[4] );
  110 + if ( activeBox == -1 )
  111 + {
  112 + CreateNewBox( lstBoxes, lstTextures[4] );
  113 + }
  114 + else
  115 + {
  116 + lstBoxes[activeBox].SetTexture( lstTextures[4] );
  117 + }
40 118 }
41 119
42   - if ( input.IsKeyDown( sf::Key::Numpad8 ) )
  120 + if ( m_cursorTimeout <= 0 )
43 121 {
44   - cursor.z -= 0.5;
  122 + if ( input.IsKeyDown( sf::Key::Numpad8 ) )
  123 + {
  124 + m_cursorPosition.z -= 10;
  125 + m_cursorTimeout = m_timeoutMax;
  126 + }
  127 + else if ( input.IsKeyDown( sf::Key::Numpad2 ) )
  128 + {
  129 + m_cursorPosition.z += 10;
  130 + m_cursorTimeout = m_timeoutMax;
  131 + }
  132 + if ( input.IsKeyDown( sf::Key::Numpad4 ) )
  133 + {
  134 + m_cursorPosition.x -= 10;
  135 + m_cursorTimeout = m_timeoutMax;
  136 + }
  137 + else if ( input.IsKeyDown( sf::Key::Numpad6 ) )
  138 + {
  139 + m_cursorPosition.x += 10;
  140 + m_cursorTimeout = m_timeoutMax;
  141 + }
  142 + if ( input.IsKeyDown( sf::Key::Numpad9 ) )
  143 + {
  144 + m_cursorPosition.y += 10;
  145 + m_cursorTimeout = m_timeoutMax;
  146 + }
  147 + else if ( input.IsKeyDown( sf::Key::Numpad3 ) )
  148 + {
  149 + m_cursorPosition.y -= 10;
  150 + m_cursorTimeout = m_timeoutMax;
  151 + }
45 152 }
46   - else if ( input.IsKeyDown( sf::Key::Numpad2 ) )
  153 +
  154 + if ( m_cursorPosition.x >= mapWidth )
47 155 {
48   - cursor.z += 0.5;
  156 + m_cursorPosition.x = 0;
49 157 }
50   - if ( input.IsKeyDown( sf::Key::Numpad4 ) )
  158 + else if ( m_cursorPosition.x < 0 )
51 159 {
52   - cursor.x -= 0.5;
  160 + m_cursorPosition.x = mapWidth - 1;
53 161 }
54   - else if ( input.IsKeyDown( sf::Key::Numpad6 ) )
  162 + if ( m_cursorPosition.z >= mapDepth )
55 163 {
56   - cursor.x += 0.5;
  164 + m_cursorPosition.z = 0;
  165 + }
  166 + else if ( m_cursorPosition.z < 0 )
  167 + {
  168 + m_cursorPosition.z = mapDepth - 1;
57 169 }
58 170
59 171 /* DEBUG KEYS */
@@ -71,7 +183,7 @@ void Input::HandleInput( Application& app, Camera& camera, std::vector<Geometry>
71 183
72 184 std::cout << std::endl;
73 185 std::cout << "Cursor:" << std::endl;
74   - std::cout << cursor.x << ", " << cursor.y << ", " << cursor.z << std::endl;
  186 + std::cout << m_cursorPosition.x << ", " << m_cursorPosition.y << ", " << m_cursorPosition.z << std::endl;
75 187 }
76 188
77 189 }
@@ -145,5 +257,105 @@ void Input::HandleMouseControls( const sf::Input& input, Application& app, std::
145 257 {
146 258 }
147 259
  260 +void Input::DrawCursor()
  261 +{
  262 + glColor3f( 1.0f, 0.0f, 1.0f );
  263 +
  264 + float tileWidth = 10.0;
  265 +
  266 + sf::Vector3f lstPoints[8];
  267 + lstPoints[0].x = m_cursorPosition.x;
  268 + lstPoints[0].y = m_cursorPosition.y;
  269 + lstPoints[0].z = m_cursorPosition.z;
  270 +
  271 + lstPoints[1].x = m_cursorPosition.x + tileWidth;
  272 + lstPoints[1].y = m_cursorPosition.y;
  273 + lstPoints[1].z = m_cursorPosition.z;
  274 +
  275 + lstPoints[2].x = m_cursorPosition.x + tileWidth;
  276 + lstPoints[2].y = m_cursorPosition.y + tileWidth;
  277 + lstPoints[2].z = m_cursorPosition.z;
  278 +
  279 + lstPoints[3].x = m_cursorPosition.x;
  280 + lstPoints[3].y = m_cursorPosition.y + tileWidth;
  281 + lstPoints[3].z = m_cursorPosition.z;
  282 +
  283 + lstPoints[4].x = m_cursorPosition.x;
  284 + lstPoints[4].y = m_cursorPosition.y;
  285 + lstPoints[4].z = m_cursorPosition.z + tileWidth;
  286 +
  287 + lstPoints[5].x = m_cursorPosition.x + tileWidth;
  288 + lstPoints[5].y = m_cursorPosition.y;
  289 + lstPoints[5].z = m_cursorPosition.z + tileWidth;
  290 +
  291 + lstPoints[6].x = m_cursorPosition.x + tileWidth;
  292 + lstPoints[6].y = m_cursorPosition.y + tileWidth;
  293 + lstPoints[6].z = m_cursorPosition.z + tileWidth;
  294 +
  295 + lstPoints[7].x = m_cursorPosition.x;
  296 + lstPoints[7].y = m_cursorPosition.y + tileWidth;
  297 + lstPoints[7].z = m_cursorPosition.z + tileWidth;
  298 +
  299 + // XY bottom
  300 + glBegin( GL_LINES );
  301 + glVertex3f( lstPoints[0].x, lstPoints[0].y, lstPoints[0].z );
  302 + glVertex3f( lstPoints[1].x, lstPoints[1].y, lstPoints[1].z );
  303 + glVertex3f( lstPoints[5].x, lstPoints[5].y, lstPoints[5].z );
  304 + glVertex3f( lstPoints[4].x, lstPoints[4].y, lstPoints[4].z );
  305 + glVertex3f( lstPoints[0].x, lstPoints[0].y, lstPoints[0].z );
  306 + glEnd();
  307 +
  308 + // XY top
  309 + glBegin( GL_LINES );
  310 + glVertex3f( lstPoints[3].x, lstPoints[3].y + 0.1, lstPoints[3].z );
  311 + glVertex3f( lstPoints[2].x, lstPoints[2].y + 0.1, lstPoints[2].z );
  312 + glVertex3f( lstPoints[6].x, lstPoints[6].y + 0.1, lstPoints[6].z );
  313 + glVertex3f( lstPoints[7].x, lstPoints[7].y + 0.1, lstPoints[7].z );
  314 + glVertex3f( lstPoints[3].x, lstPoints[3].y + 0.1, lstPoints[3].z );
  315 + glEnd();
  316 +
  317 + // ZY Lines
  318 + glBegin( GL_LINES );
  319 + glVertex3f( lstPoints[0].x, lstPoints[0].y, lstPoints[0].z );
  320 + glVertex3f( lstPoints[3].x, lstPoints[3].y, lstPoints[3].z );
  321 + glEnd();
  322 +
  323 + glBegin( GL_LINES );
  324 + glVertex3f( lstPoints[1].x, lstPoints[1].y, lstPoints[1].z );
  325 + glVertex3f( lstPoints[2].x, lstPoints[2].y, lstPoints[2].z );
  326 + glEnd();
  327 +
  328 + glBegin( GL_LINES );
  329 + glVertex3f( lstPoints[5].x, lstPoints[5].y, lstPoints[5].z );
  330 + glVertex3f( lstPoints[6].x, lstPoints[6].y, lstPoints[6].z );
  331 + glEnd();
  332 +
  333 + glBegin( GL_LINES );
  334 + glVertex3f( lstPoints[4].x, lstPoints[4].y, lstPoints[4].z );
  335 + glVertex3f( lstPoints[7].x, lstPoints[7].y, lstPoints[7].z );
  336 + glEnd();
  337 +
  338 + // XZ Lines
  339 + glBegin( GL_LINES );
  340 + glVertex3f( lstPoints[0].x, lstPoints[0].y, lstPoints[0].z );
  341 + glVertex3f( lstPoints[4].x, lstPoints[4].y, lstPoints[4].z );
  342 + glEnd();
  343 +
  344 + glBegin( GL_LINES );
  345 + glVertex3f( lstPoints[1].x, lstPoints[1].y, lstPoints[1].z );
  346 + glVertex3f( lstPoints[5].x, lstPoints[5].y, lstPoints[5].z );
  347 + glEnd();
  348 +
  349 + glBegin( GL_LINES );
  350 + glVertex3f( lstPoints[2].x, lstPoints[2].y, lstPoints[2].z );
  351 + glVertex3f( lstPoints[6].x, lstPoints[6].y, lstPoints[6].z );
  352 + glEnd();
  353 +
  354 + glBegin( GL_LINES );
  355 + glVertex3f( lstPoints[3].x, lstPoints[3].y, lstPoints[3].z );
  356 + glVertex3f( lstPoints[7].x, lstPoints[7].y, lstPoints[7].z );
  357 + glEnd();
  358 +}
  359 +
148 360
149 361 }
14 Input.h
@@ -15,14 +15,24 @@ namespace ursa
15 15 class Input
16 16 {
17 17 public:
  18 + Input();
18 19 void HandleInput( Application& app, Camera& camera,
19   - std::vector<Geometry>& lstBlocks, sf::Vector3f& cursor,
20   - int activeBox, GLuint lstTextures[5] );
  20 + std::vector<Geometry>& lstBlocks,
  21 + GLuint lstTextures[5], float mapWidth, float mapDepth );
  22 +
  23 + void DrawCursor();
  24 + void CreateNewBox( std::vector<Geometry>& lstBlocks, GLuint& texture );
  25 +
  26 + sf::Vector3f CursorPosition() { return m_cursorPosition; }
21 27
22 28 private:
23 29 void HandleCameraControls( const sf::Input& input, Application& app, Camera& camera );
24 30 void HandleAppControls( const sf::Input& input, Application& app );
25 31 void HandleMouseControls( const sf::Input& input, Application& app, std::vector<Geometry>& lstBlocks );
  32 +
  33 + sf::Vector3f m_cursorPosition;
  34 + float m_cursorTimeout;
  35 + const float m_timeoutMax;
26 36 };
27 37
28 38 }
31 main.cpp
@@ -29,7 +29,6 @@ int main()
29 29 const int mapDepth = 10;
30 30
31 31 vector<ursa::Geometry> lstBoxes;
32   - int activeBox = 0;
33 32 for ( int z = 0; z < mapDepth; z++ )
34 33 {
35 34 for ( int x = 0; x < mapWidth; x++ )
@@ -44,36 +43,10 @@ int main()
44 43 lstBoxes[i].SetTexture( lstTextures[0] );
45 44 }
46 45
47   - sf::Vector3f cursor(0, 0, 0);
48   -
49 46 while ( !app.Quit() )
50 47 {
51 48 // Get Events
52   - input.HandleInput( app, camera, lstBoxes, cursor, activeBox, lstTextures );
53   - if ( cursor.x >= mapWidth )
54   - {
55   - cursor.x = 0;
56   - }
57   - else if ( cursor.x < 0 )
58   - {
59   - cursor.x = mapWidth - 1;
60   - }
61   - if ( cursor.z >= mapDepth )
62   - {
63   - cursor.z = 0;
64   - }
65   - else if ( cursor.z < 0 )
66   - {
67   - cursor.z = mapDepth - 1;
68   - }
69   -
70   - for ( int i = 0; i < lstBoxes.size(); i++ )
71   - {
72   - if ( lstBoxes[i].CheckCursor( cursor ) )
73   - {
74   - activeBox = i;
75   - }
76   - }
  49 + input.HandleInput( app, camera, lstBoxes, lstTextures, mapWidth * 10, mapDepth * 10 );
77 50
78 51 // Render
79 52 app.PrepDraw();
@@ -84,6 +57,8 @@ int main()
84 57 lstBoxes[i].Draw();
85 58 }
86 59
  60 + input.DrawCursor();
  61 +
87 62 app.FinishDraw();
88 63 }
89 64

No commit comments for this range

Something went wrong with that request. Please try again.