1. Javier Aravena
  2. Dancing Cubes Demo

Commits

Javier Aravena  committed a1dd607

Picking

  • Participants
  • Parent commits e17dfc9
  • Branches default

Comments (0)

Files changed (5)

File .hgignore

View file
 *.o
 game
 .sconsign.dblite
+tags

File CApp.h

View file
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <iostream>
+#include <cmath>
 
 class CApp {
     private:
         bool running;
         sf::Window * window;
 
-        float points[4][3];
+        static const unsigned int pointsLimit = 4;
+        float points[pointsLimit][3];
+
+        int draggingPoint;
     public:
         CApp();
         int OnExecute();

File CApp_OnEvent.cc

View file
 
 void CApp::OnEvent(sf::Event & event)
 {
+    const int dragTolerance = 10;
+
     switch(event.Type) {
         case sf::Event::Closed:
             running = false;
         case sf::Event::KeyPressed:
             OnKeyPress(event.Key);
             break;
+        case sf::Event::MouseButtonPressed:
+            if(event.MouseButton.Button == sf::Mouse::Left) {
+                for(unsigned int i=0; i < pointsLimit; ++i) {
+                    if(abs(event.MouseButton.X - points[i][0]) < dragTolerance &&
+                            abs(event.MouseButton.Y - points[i][1]) < dragTolerance) {
+                        draggingPoint = i;
+                    }
+                }
+            }
+            break;
+        case sf::Event::MouseButtonReleased:
+        //case sf::Event::MouseLeft:
+            draggingPoint = -1;
+            break;
+        // Case sf::Event::MouseMoved:
+        //     if(draggingPoint != -1) {
+        //         points[draggingPoint][0] = event.MouseMove.X;
+        //         points[draggingPoint][1] = event.MouseMove.Y;
+        //     }
+        //     break;
         default:
             break;
     }

File CApp_OnInit.cc

View file
 
 bool CApp::OnInit()
 {
-    sf::VideoMode videoMode(800, 600, 32);
+    const int width=800;
+    const int height=600;
+
+    sf::VideoMode videoMode(width, height, 32);
     if(!videoMode.IsValid()) {
         std::cerr << "Invalid video mode" << std::endl;
         return false;
     glClearColor(0, 0, 0, 0);
     glClearDepth(1.0f);
 
-    glViewport(0, 0, 640, 480);
+    glViewport(0, 0, width, height);
 
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
 
-    glOrtho(0, 640, 480, 0, 1, -1); //2D, use gluPerspective for 3D.
+    glOrtho(0, width, height, 0, 1, -1); //2D, use gluPerspective for 3D.
 
     glMatrixMode(GL_MODELVIEW);
 
     glEnable(GL_TEXTURE_2D);
 
+    draggingPoint = -1;
 
     return true;
 }

File CApp_OnLoop.cc

View file
 
 void CApp::OnLoop()
 {
+    if(draggingPoint >= 0) {
+        sf::Vector2i position(sf::Mouse::GetPosition(*window));
+        points[draggingPoint][0] = position.x;
+        points[draggingPoint][1] = position.y;
+    }
 }