Commits

Javier Aravena  committed 21a7f8a

Refactoring: separated the entity.

  • Participants
  • Parent commits cc4d5f4

Comments (0)

Files changed (7)

 #ifndef _CAPP_H_
 #define _CAPP_H_
+
 #include <SFML/Window.hpp>
 #include <GL/gl.h>
 #include <GL/glu.h>
 #include <iostream>
 #include <cmath>
+#include "FakeCube.hh"
 
 class CApp {
     private:
         bool running;
         sf::Window * window;
 
-        static const unsigned int pointsLimit = 4;
-        float points[pointsLimit][3];
-
-        int draggingPoint;
+        FakeCube fakeCube;
     public:
         CApp();
         int OnExecute();

File CApp_OnEvent.cc

 
 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:
-            draggingPoint = -1;
-            break;
         case sf::Event::Resized:
             glViewport(0, 0, event.Size.Width, event.Size.Height);
         default:
             break;
     }
+
+    fakeCube.OnEvent(event);
     
 }
 
         case sf::Keyboard::Q:
             running = false;
             break;
-        case sf::Keyboard::A:
-            points[0][0] = 0.f;
-            break;
         default:
             break;
     }

File CApp_OnInit.cc

         return false;
     }
 
-    float initial_points[][3] = {
-        {320.f, 120.f, 0.f},
-        {480.f, 100.f, 0.f},
-        {480.f, 400.f, 0.f},
-        {320.f, 360.f, 0.f}
-    };
 
-    for(int i=0; i<4; ++i) {
-        for(int j=0; j<3; ++j) {
-            points[i][j] = initial_points[i][j];
-        }
-    }
-
-    window = new sf::Window(videoMode, "SFML Window");
+    window = new sf::Window(videoMode, "Config Cubo");
 
     glClearColor(0, 0, 0, 0);
     glClearDepth(1.0f);
 
     glEnable(GL_TEXTURE_2D);
 
-    draggingPoint = -1;
-
     return true;
 }

File CApp_OnLoop.cc

 
 void CApp::OnLoop()
 {
-    if(draggingPoint >= 0) {
-        sf::Vector2i position(sf::Mouse::GetPosition(*window));
-        points[draggingPoint][0] = position.x * window->GetWidth() / 800;
-        points[draggingPoint][1] = position.y * window->GetHeight() / 600;
-    }
+    sf::Vector2i position(sf::Mouse::GetPosition(*window));
+    fakeCube.dragCurrentPoint(
+            position.x * window->GetWidth() / 800,
+            position.y * window->GetHeight() / 600
+            );
 }

File CApp_OnRender.cc

     glLoadIdentity();
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity();
-
-    glPolygonMode(GL_FRONT, GL_LINE);
-    glPolygonMode(GL_BACK, GL_LINE);
-
-
-    glBegin(GL_QUADS);
-        glColor3f(1.0f, 1.0f, 1.0f);
-        glVertex3f(points[0][0], points[0][1], points[0][2]);
-        glVertex3f(points[1][0], points[1][1], points[1][2]);
-        glVertex3f(points[2][0], points[2][1], points[2][2]);
-        glVertex3f(points[3][0], points[3][1], points[4][2]);
-    glEnd();
+    
+    fakeCube.OnRender();
 
     window->Display();
 
+#include "FakeCube.hh"
+
+FakeCube::FakeCube()
+{
+    float initial_points[][3] = {
+        {320.f, 120.f, 0.f},
+        {480.f, 100.f, 0.f},
+        {480.f, 400.f, 0.f},
+        {320.f, 360.f, 0.f}
+    };
+
+    for(int i=0; i<4; ++i) {
+        for(int j=0; j<3; ++j) {
+            points[i][j] = initial_points[i][j];
+        }
+    }
+
+    draggingPoint = -1;
+    
+}
+
+bool FakeCube::OnEvent(sf::Event & event)
+{
+    const int dragTolerance = 10;
+
+    switch(event.Type) {
+        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;
+                    }
+                }
+            }
+            return false;
+            break;
+        case sf::Event::MouseButtonReleased:
+            draggingPoint = -1;
+            break;
+        default:
+            break;
+    }
+    return true;
+}
+
+void FakeCube::OnRender()
+{
+    glPolygonMode(GL_FRONT, GL_LINE);
+    glPolygonMode(GL_BACK, GL_LINE);
+
+    glBegin(GL_QUADS);
+        glColor3f(1.0f, 1.0f, 1.0f);
+        glVertex3f(points[0][0], points[0][1], points[0][2]);
+        glVertex3f(points[1][0], points[1][1], points[1][2]);
+        glVertex3f(points[2][0], points[2][1], points[2][2]);
+        glVertex3f(points[3][0], points[3][1], points[4][2]);
+    glEnd();
+}
+
+void FakeCube::dragCurrentPoint(int x, int y) 
+{
+    if(draggingPoint >= 0) {
+        points[draggingPoint][0] = x;
+        points[draggingPoint][1] = y;
+    }
+}
+#ifndef _FAKECUBE_H_
+#define _FAKECUBE_H_
+
+#include <SFML/Window.hpp>
+#include <GL/gl.h>
+#include <GL/glu.h>
+#include <iostream>
+#include <cmath>
+
+class FakeCube {
+    private:
+        static const unsigned int pointsLimit = 4;
+        float points[pointsLimit][3];
+        int draggingPoint;
+    public: //TODO: Estas cosas deberían estar definidas en clases virtuales (aka interfases)
+        bool OnEvent(sf::Event & Event);
+        void OnRender();
+        FakeCube();
+        void dragCurrentPoint(int x, int y);
+};
+
+#endif
+