I simplified the example.
I put here a the attached code:
main.cpp
#include "Application.h"
int main()
{
gg::Application *application = new(std::nothrow) gg::Application("");
application->init();
application->run();
application->cleanup();
delete application;
}
Application.c
#include <thread>
#include "Application.h"
namespace gg
{
Application::Application(const std::string theTitle):
mTitle(theTitle),
mVideoMode(DEFAULT_VIDEO_WIDTH, DEFAULT_VIDEO_HEIGHT, DEFAULT_VIDEO_BPP),
mWindow(),
mContextSettings(),
mWindowStyle(sf::Style::Close | sf::Style::Resize)
{
}
void Application::init()
{
// Create the main window
//sf::RenderWindow window(sf::VideoMode(800, 600), "SFML OpenGL", sf::Style::Default, sf::ContextSettings(32));
mWindow.create(mVideoMode, mTitle, mWindowStyle, mContextSettings);
mWindow.setVerticalSyncEnabled(true);
mWindow.setActive(false);
// Set the color and depth clear values
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);
}
void Application::cleanup()
{
// Don't forget to destroy our texture
glDeleteTextures(1, &texture);
}
void Application::run()
{
// launch the rendering thread
//sf::Thread thread(&Application::renderingThread, this);
//thread.launch();
std::thread renderingThread(&Application::renderingThread, this);
// Start game loop
while (mWindow.isOpen())
{
// Process events
sf::Event event;
while (mWindow.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
mWindow.close();
// Escape key : exit
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
mWindow.close();
// Adjust the viewport when the window is resized
if (event.type == sf::Event::Resized)
glViewport(0, 0, event.size.width, event.size.height);
}
}
renderingThread.join();
}
void Application::renderingThread()
{
// Create a clock for measuring the time elapsed
sf::Clock clock;
// Activate the window before using OpenGL commands.
// This is useless here because we have only one window which is
// always the active one, but don't forget it if you use multiple windows
mWindow.setActive();
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(clock.getElapsedTime().asSeconds() * 50, 1.f, 0.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 30, 0.f, 1.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 90, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glColor3f(1.f, 0.f, 0.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f( 50.f, 50.f, 50.f);
glVertex3f( 50.f, -50.f, 50.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glColor3f(0.f, 1.f, 0.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, 50.f, -50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, -50.f, 50.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, 50.f);
glColor3f(0.f, 0.f, 1.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, 50.f);
glEnd();
// Finally, display the rendered frame on screen
mWindow.display();
}
Application::~Application(void)
{
}
}
Application.h
#ifndef _GG_APPLICATION
#define _GG_APPLICATION
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
namespace gg
{
class Application
{
public:
/// Default Video Width to use if config file not found
static const unsigned int DEFAULT_VIDEO_WIDTH = 800;
/// Default Video Height to use if config file not found
static const unsigned int DEFAULT_VIDEO_HEIGHT = 600;
/// Default Video bits per pixel (color depth) if config file not found
static const unsigned int DEFAULT_VIDEO_BPP = 32;
/// Title to use for Window
std::string mTitle;
/// Video Mode to use (width, height, bpp)
sf::VideoMode mVideoMode;
/// Render window to draw to
sf::RenderWindow mWindow;
/// Window settings to use when creating Render window
sf::ContextSettings mContextSettings;
/// Window style to use when creating Render window
unsigned long mWindowStyle;
sf::Texture backgroundTexture;
sf::Font font;
sf::Sprite background;
GLuint texture;
Application(const std::string theTitle);
virtual void init();
virtual void cleanup();
virtual void run();
virtual void renderingThread();
virtual ~Application(void);
};
}
#endif