Commits

Javier Aravena  committed e17dfc9

Port a sfml

  • Participants
  • Parent commits 32f45b6

Comments (0)

Files changed (14)

+#include "CApp.h"
+
+CApp::CApp() 
+{
+    window = NULL;
+    running = true;
+}
+
+int CApp::OnExecute() 
+{
+    if(OnInit() == false)
+        return -1;
+
+    sf::Event event;
+
+    while(running) {
+        while(window->PollEvent(event)) {
+            OnEvent(event);
+        }
+
+        OnLoop();
+        OnRender();
+    }
+
+    OnCleanup();
+
+    return EXIT_SUCCESS;
+}
+
+int main(int argc, char * argv[])
+{
+    CApp app;
+    return app.OnExecute();
+}

File CApp.cpp

-#include "CApp.h"
-
-CApp::CApp() 
-{
-    Surf_Display = NULL;
-    Running = true;
-}
-
-int CApp::OnExecute() 
-{
-    if(OnInit() == false)
-        return -1;
-
-    SDL_Event Event;
-
-    while(Running) {
-        while(SDL_PollEvent(&Event)) {
-            OnEvent(Event);
-        }
-
-        OnLoop();
-        OnRender();
-    }
-
-    OnCleanup();
-
-    return 0;
-}
-
-int main(int argc, char * argv[])
-{
-    CApp app;
-    return app.OnExecute();
-}
 #ifndef _CAPP_H_
 #define _CAPP_H_
-#include <SDL.h>
+#include <SFML/Window.hpp>
 #include <GL/gl.h>
 #include <GL/glu.h>
+#include <iostream>
 
 class CApp {
     private:
-        bool Running;
-        SDL_Surface * Surf_Display;
+        bool running;
+        sf::Window * window;
 
-        void OnKeyEvent(SDL_keysym & keysym);
+        float points[4][3];
     public:
         CApp();
         int OnExecute();
         bool OnInit();
-        void OnEvent(SDL_Event & Event);
+        
+        void OnEvent(sf::Event & Event);
+        void OnKeyPress(sf::Event::KeyEvent key);
+        
         void OnLoop();
         void OnRender();
         void OnCleanup();

File CApp_OnCleanup.cc

+#include "CApp.h"
+
+void CApp::OnCleanup()
+{
+    window->Close();
+}

File CApp_OnCleanup.cpp

-#include "CApp.h"
-
-void CApp::OnCleanup()
-{
-    SDL_FreeSurface(Surf_Display);
-    SDL_Quit();
-}

File CApp_OnEvent.cc

+#include "CApp.h"
+
+void CApp::OnEvent(sf::Event & event)
+{
+    switch(event.Type) {
+        case sf::Event::Closed:
+            running = false;
+            break;
+        case sf::Event::KeyPressed:
+            OnKeyPress(event.Key);
+            break;
+        default:
+            break;
+    }
+    
+}
+
+void CApp::OnKeyPress(sf::Event::KeyEvent key) {
+    switch(key.Code){
+        case sf::Keyboard::Escape:
+        case sf::Keyboard::Q:
+            running = false;
+            break;
+        case sf::Keyboard::A:
+            points[0][0] = 0.f;
+            break;
+        default:
+            break;
+    }
+}
+

File CApp_OnEvent.cpp

-#include "CApp.h"
-
-void CApp::OnEvent(SDL_Event & event)
-{
-    switch(event.type) {
-        case SDL_QUIT:
-            Running = false;
-            break;
-        case SDL_KEYDOWN:
-            OnKeyEvent(event.key.keysym);
-            break;
-    }
-
-}
-
-void CApp::OnKeyEvent(SDL_keysym & keysym) {
-    switch(keysym.sym) {
-        case SDLK_q:
-        case SDLK_ESCAPE:
-            Running = false;
-            break;
-        default:
-            break;
-    }
-}
-

File CApp_OnInit.cc

+#include "CApp.h"
+
+bool CApp::OnInit()
+{
+    sf::VideoMode videoMode(800, 600, 32);
+    if(!videoMode.IsValid()) {
+        std::cerr << "Invalid video mode" << std::endl;
+        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");
+
+    glClearColor(0, 0, 0, 0);
+    glClearDepth(1.0f);
+
+    glViewport(0, 0, 640, 480);
+
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();
+
+    glOrtho(0, 640, 480, 0, 1, -1); //2D, use gluPerspective for 3D.
+
+    glMatrixMode(GL_MODELVIEW);
+
+    glEnable(GL_TEXTURE_2D);
+
+
+    return true;
+}

File CApp_OnInit.cpp

-#include "CApp.h"
-
-bool CApp::OnInit()
-{
-    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
-        return false;
-    }
-    /*
-    * SDL_GL_SetAttribute(SDL_GL_RED_SIZE,            8);
-    * SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,          8);
-    * SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,           8);
-    * SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,          8);
-    * SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,          16);
-    * SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,         32);
-    * SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,      8);
-    * SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,    8);
-    * SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE,     8);
-    * SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,    8);
-    * SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1); //Antialiasing
-    * SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2); //Antialiasing samples
-    */
-    if((Surf_Display = SDL_SetVideoMode(
-                    640, 480, 32, SDL_HWSURFACE | SDL_GL_DOUBLEBUFFER | SDL_OPENGL))  == NULL) {
-        return false;
-    }
-
-    glClearColor(0, 0, 0, 0);
-    glClearDepth(1.0f);
-
-    glViewport(0, 0, 640, 480);
-
-    glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-
-    glOrtho(0, 640, 480, 0, 1, -1); //2D, use gluPerspective for 3D.
-
-    glMatrixMode(GL_MODELVIEW);
-
-    glEnable(GL_TEXTURE_2D);
-
-    glLoadIdentity();
-
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-    glLoadIdentity();
-
-    glBegin(GL_QUADS);
-        glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
-        glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(100.0f, 0.0f, 0.0f);
-        glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(100.0f, 100.0f, 0.0f);
-        glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(0.0f, 100.0f, 0.0f);
-    glEnd();
-
-    return true;
-}

File CApp_OnLoop.cc

+#include "CApp.h"
+
+void CApp::OnLoop()
+{
+}

File CApp_OnLoop.cpp

-#include "CApp.h"
-
-void CApp::OnLoop()
-{
-}

File CApp_OnRender.cc

+#include "CApp.h"
+
+void CApp::OnRender()
+{
+
+    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();
+
+    window->Display();
+
+}

File CApp_OnRender.cpp

-#include "CApp.h"
-
-void CApp::OnRender()
-{
-    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
-    glLoadIdentity();
-
-    //No es necesario cargar siempre los puntos (de hecho, debería ser malo), pero así lo hace el tutorial.
-    glBegin(GL_QUADS);
-        glColor3f(1.0f, 0.0f, 0.0f); glVertex3f(0.0f, 0.0f, 0.0f);
-        glColor3f(1.0f, 1.0f, 0.0f); glVertex3f(100.0f, 0.0f, 0.0f);
-        glColor3f(1.0f, 0.0f, 1.0f); glVertex3f(100.0f, 100.0f, 0.0f);
-        glColor3f(1.0f, 1.0f, 1.0f); glVertex3f(0.0f, 100.0f, 0.0f);
-    glEnd();
-
-    SDL_GL_SwapBuffers();
-}
 env = Environment()
 
 # determine compiler and linker flags for SDL
-env.ParseConfig('sdl-config --cflags')
-env.ParseConfig('sdl-config --libs')
+#env.ParseConfig('sdl-config --cflags')
+#env.ParseConfig('sdl-config --libs')
 
 # gather a list of source files
-SOURCES = glob.glob('*.cpp')
+SOURCES = glob.glob('*.cc')
 
 # add additional compiler flags
 env.Append(CCFLAGS = ['-g', '-Wall'])
 # add additional libraries to link against
-env.Append(LIBS = ['SDL_mixer', 'SDL_image', 'GL', 'GLU'])
+env.Append(LIBS = ['sfml-window' ,'sfml-system', 'GL'])
 
 # build target
 # output executable will be "game"