Commits

Javier Aravena  committed c004aaa Merge

Merged with Dance branch

  • Participants
  • Parent commits 912362f, b7c7217

Comments (0)

Files changed (8)

 *.so
 *.o
 game
+*.config
 .sconsign.dblite
 tags
 CMakeFiles
     return EXIT_SUCCESS;
 }
 
-inline void CApp::setTitle()
-{
-    //Really Slow!!
-    char * str = new char[30];
-    snprintf(str, 30, "CubeThingy %.4g", 1.f/frameTime);
-    window->SetTitle(str);
-}
-
 int main(int argc, char * argv[])
 {
     CApp app;
+    srandom(time(NULL));
     return app.OnExecute();
 }
 #include <cmath>
 #include <vector>
 #include <cstdio>
+#include <ctime>
 
+#include "CubeConfig.hh"
 #include "IEntity.hh"
 #include "Cube.hh"
+#include "DancingCube.hh"
 
 class CApp {
     private:
         sf::Clock clock;
         float frameTime;
 
-        inline void setTitle();
-
         float pickedColour[3];
 
     public:

File CApp_OnInit.cc

 
 bool CApp::OnInit()
 {
-    const int width=800;
-    const int height=600;
+    CubeConfig cubeConfig;
 
-    sf::VideoMode videoMode(width, height, 32);
+    const int grid_size = 5;
+    for(int i=0; i < grid_size; ++i) {
+        for(int j=0; j < grid_size; ++j) {
+            entities.push_back(new DancingCube(cubeConfig, i, j, grid_size));
+        }
+    }
+
+    //const int width=800;
+    //const int height=600;
+    //sf::VideoMode videoMode(width, height, 32);
+
+    sf::VideoMode videoMode = sf::VideoMode::GetDesktopMode();
+    int width = videoMode.Width;
+    int height = videoMode.Height;
+
+
     if(!videoMode.IsValid()) {
         std::cerr << "Invalid video mode" << std::endl;
         return false;
     }
 
-    entities.push_back(new Cube());
-
-    window = new sf::Window(videoMode, "Config Cubo", sf::Style::Default, *contextSsettings);
+    window = new sf::Window(videoMode, "Dancing Cube", sf::Style::Fullscreen, *contextSsettings);
 
     glClearColor(0, 0, 0, 0);
     glClearDepth(1.0f);

File CubeConfig.cc

+#include "CubeConfig.hh"
+
+CubeConfig::CubeConfig(const char * filename)
+{
+    angle_x = 0;
+    angle_y = 0;
+    angle_z = 0;
+    translate_x = 0;
+    translate_y = 0;
+    translate_z = 0;
+    scale = 1;
+
+    libconfig::Config cfg;
+    try {
+        cfg.readFile(filename);
+    }
+    catch(const libconfig::FileIOException &fioex)
+    {
+        std::cerr << "I/O error while reading file." << std::endl;
+        return;
+    }
+    
+    angle_x = cfg.lookup("angle_x");
+    angle_y = cfg.lookup("angle_y");
+    angle_z = cfg.lookup("angle_z");
+    translate_x = cfg.lookup("translate_x");
+    translate_y = cfg.lookup("translate_y");
+    translate_z = cfg.lookup("translate_z");
+    scale = cfg.lookup("scale");
+}

File CubeConfig.hh

+#ifndef _CUBECONFIG_H_
+#define _CUBECONFIG_H_
+
+#include <libconfig.h++>
+#include <iostream>
+
+struct CubeConfig {
+        float angle_x, angle_y, angle_z;
+        float translate_x, translate_y, translate_z;
+        float scale;
+        CubeConfig(const char * filename="cube.config");
+};
+
+#endif

File DancingCube.cc

+#include "DancingCube.hh"
+
+DancingCube::DancingCube (CubeConfig config, unsigned int row, unsigned int column, unsigned int size)
+{
+    grid_scale = 1.f / size;
+    grid_translate_x = row * grid_scale;
+    grid_translate_z = column * grid_scale;
+
+    new_target();
+    direction = 1;
+    scale_y = target_min;
+
+    speed = 0.3;
+    angle_x = config.angle_x;
+    angle_y = config.angle_y;
+    angle_z = config.angle_z;
+    translate_x = config.translate_x;
+    translate_y = config.translate_y;
+    translate_z = config.translate_z;
+    scale = 1;
+}
+
+void DancingCube::OnRender()
+{
+    calculateMatrix();
+
+    //Adelante
+    glBegin(GL_QUADS);
+        glNormal3f(0.f, 0.f, 1.f);
+
+        glColor3f(0.3f, 0.3f, 0.3f);
+        glVertex3f(0.f, 1.f, 0.f);
+        glVertex3f(1.f, 1.f, 0.f);
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(1.f, 0.f, 0.f);
+        glVertex3f(0.f, 0.f, 0.f);
+    glEnd();
+
+    //Atrás
+    glBegin(GL_QUADS);
+        glNormal3f(0.f, 0.f, -1.f);
+
+        glColor3f(0.3f, 0.3f, 0.3f);
+        glVertex3f(0.f, 1.f, -1.f);
+        glVertex3f(1.f, 1.f, -1.f);
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(1.f, 0.f, -1.f);
+        glVertex3f(0.f, 0.f, -1.f);
+    glEnd();
+
+    //Izquierda
+    glBegin(GL_QUADS);
+        glNormal3f(-1.f, 0.f, 0.f);
+
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(0.f, 0.f, -1.f);
+        glColor3f(0.3f, 0.3f, 0.3f);
+        glVertex3f(0.f, 1.f, -1.f);
+        glVertex3f(0.f, 1.f, 0.f);
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(0.f, 0.f, 0.f);
+    glEnd();
+
+    //Derecha
+    glBegin(GL_QUADS);
+        glNormal3f(1.f, 0.f, 0.f);
+
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(1.f, 0.f, -1.f);
+        glColor3f(0.3f, 0.3f, 0.3f);
+        glVertex3f(1.f, 1.f, -1.f);
+        glVertex3f(1.f, 1.f, 0.f);
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(1.f, 0.f, 0.f);
+    glEnd();
+
+    //Abajo
+    glBegin(GL_QUADS);
+        glNormal3f(0.f, -1.f, 0.f);
+
+        glColor3f(1.f, 0.1f, 0.1f);
+        glVertex3f(0.f, 0.f, -1.f);
+        glVertex3f(1.f, 0.f, -1.f);
+        glVertex3f(1.f, 0.f, 0.f);
+        glVertex3f(0.f, 0.f, 0.f);
+    glEnd();
+
+    //Arriba
+    glBegin(GL_QUADS);
+        glNormal3f(0.f, 1.f, 0.f);
+
+        glColor3f(0.3f, 0.3f, 0.3f);
+        glVertex3f(0.f, 1.f, -1.f);
+        glVertex3f(1.f, 1.f, -1.f);
+        glVertex3f(1.f, 1.f, 0.f);
+        glVertex3f(0.f, 1.f, 0.f);
+    glEnd();
+}
+
+void DancingCube::OnRenderPicking()
+{
+    OnRender();
+}
+
+void DancingCube::calculateMatrix()
+{
+    glTranslatef(-0.5f, -0.5f, -2.f);
+    glTranslatef(translate_x, translate_y, translate_z);
+    glScalef(scale, scale, scale);
+    glRotatef(45, 0, 1, 0);
+    glRotatef(angle_x, 0, -1, 0);
+    glRotatef(angle_z, 0, 0, 1);
+    glRotatef(angle_y, 1, 0, 0);
+
+    glScalef(1, scale_y, 1);
+    glTranslatef(grid_translate_x, 0, grid_translate_z);
+    glScalef(grid_scale, 1, grid_scale);
+}
+
+
+void DancingCube::OnLoop(float frameTime, int pointer_x, int pointer_y)
+{
+    scale_y += frameTime * speed * direction;
+    if(scale_y > target_max || scale_y < target_min) direction = -1 * direction;
+}
+
+bool DancingCube::OnEvent(sf::Event & event)
+{
+    return true;
+}
+
+void DancingCube::SetPickedColour(float r, float g, float b) {
+    pickedColour[0] = r;
+    pickedColour[1] = g;
+    pickedColour[2] = b;
+}
+
+void DancingCube::new_target() {
+    target_min = (float)(random() % 10)/ 30;
+    target_max = 0.5f + (float)(random() % 10)/20;
+}

File DancingCube.hh

+#ifndef _DANCINGCUBE_H_
+#define _DANCINGCUBE_H_
+
+#include <SFML/Window.hpp>
+#include <SFML/System.hpp>
+
+#include <GL/gl.h>
+#include <GL/glu.h>
+
+#include <iostream>
+#include <cmath>
+#include <vector>
+#include <cstdlib>
+
+#include "CubeConfig.hh"
+#include "IEntity.hh"
+
+class DancingCube : public IEntity 
+{
+    private: 
+        float target_min;
+        float target_max;
+        float scale_y;
+        float speed;
+        short int direction;
+
+        float grid_translate_x, grid_translate_z;
+        float grid_scale;
+
+        //TODO:Load this shit
+        float angle_x, angle_y, angle_z;
+        float translate_x, translate_y, translate_z;
+        float scale;
+
+        float pickedColour[3];
+
+        void calculateMatrix();
+        void new_target();
+    public:
+        DancingCube(CubeConfig config, unsigned int row, unsigned int column, unsigned int size);
+        bool OnEvent(sf::Event & Event);
+        void OnRender();
+        void OnRenderPicking();
+        void OnLoop(float frameTime, int pointer_x, int pointer_y);
+        void SetPickedColour(float r, float g, float b);
+};
+
+#endif
+