#include <SFML/Window.hpp>
#include "gl/glew.h"
class Renderer
{
sf::Window* m_window;
sf::Thread m_drawThread;
public:
Renderer(sf::Window* window);
virtual ~Renderer(){};
void onThink();
void onDrawFrame();
void onInitialize();
void event_resized(sf::Event& evt);
void setViewport();
};
Renderer::Renderer(sf::Window* window)
: m_drawThread(&Renderer::onDrawFrame, this)
{
m_window = window;
}
void Renderer::onInitialize()
{
sf::Context gl;
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
setViewport();
}
void Renderer::onDrawFrame(){
m_window->setActive(true);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
m_window->display();
}
void Renderer::onThink()
{
m_window->setActive(false);
m_drawThread.launch();
}
void Renderer::event_resized(sf::Event& evt)
{
setViewport();
}
void Renderer::setViewport()
{
int width = m_window->getSize().x;
int height = m_window->getSize().y;
glViewport(0, 0, (GLsizei)width, (GLsizei)height); // Set our viewport to the size of our window
glMatrixMode(GL_PROJECTION); // Switch to the projection matrix so that we can manipulate how our scene is viewed
glLoadIdentity(); // Reset the projection matrix to the identity matrix so that we don't get any artifacts (cleaning up)
gluPerspective(60, (GLfloat)width / (GLfloat)height, 1.0, 100.0); // Set the Field of view angle (in degrees), the aspect ratio of our window, and the new and far planes
glMatrixMode(GL_MODELVIEW);
}
int main()
{
sf::Window window(sf::VideoMode(500, 500), "Test");
Renderer myrenderer(&window);
myrenderer.onInitialize();
sf::Event evt;
while(window.isOpen()){
while(window.pollEvent(evt)){
if(evt.type == sf::Event::KeyPressed){
window.close();
}
}
myrenderer.onThink();
}
}