SFML community forums

Help => Window => Topic started by: kurasu1415 on October 09, 2012, 03:18:16 am

Title: (SFML2) Possible to render OpenGL to a Window in a separate class?
Post by: kurasu1415 on October 09, 2012, 03:18:16 am
Hey guys,
So I am using OpenGL to draw to an sf::Window in SFML2. The general working setup is something like this :

while(window->isOpen())
{
    sf::Event event;
    while (window->pollEvent(event))
    {
        // Request for closing the window
        if (event.type == sf::Event::Closed)
            window->close();
    }
    window->setActive();
     
    //DO OPENGL STUFF HERE
           
    window->display();
}  

What I need to do is have a function inside another class that can actually handle the OpenGL portion of the code. Is this even possible in OpenGL? I tried doing something like this, but I get a SegFault when I try to call localWindow->setActive(); :

Renderer.h
class Renderer
{
private:
    Window* localWindow;

public:  
    Renderer(sf::Window* inWindow)
    {
        localWindow = &inWindow;
    }

    void draw()
    {
        localWindow->setActive();

        //OPENGL STUFF HERE!
    }  
}

Main.cpp
#include "Renderer.h"

sf::Window* window;
Renderer renderer(window);

void main()
{    
    //Create new Settings and Window to point "window" to.
    sf::ContextSettings settings;
    settings.antialiasingLevel = 4;
    settings.majorVersion = 3;
    settings.minorVersion = 3;
   
    sf::Window localWindow(sf::VideoMode(800,600), "OpenGL", sf::Style::Default, settings);
   
    window = &localWindow;
   
    graphics();
}

void graphics()
{
    while(window->isOpen())
    {
        sf::Event event;
        while (window->pollEvent(event))
        {
            // Request for closing the window
            if (event.type == sf::Event::Closed)
                window->close();
        }
        window->setActive();
         
        renderer.draw();
           
        window->display();
    }
}
Title: Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
Post by: G. on October 09, 2012, 07:01:16 am
In renderer.h, inWindow is an sf::Window*, localWindow also is an sf::Window*. Then, why do you use an &?
Title: Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
Post by: Laurent on October 09, 2012, 08:12:25 am
You pass the window pointer to your Renderer class before it is initialized. You pointer has a random value.
Title: Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
Post by: Nexus on October 09, 2012, 05:22:56 pm
Declare the renderer locally, then you can also pass a valid window to it.

By the way: In C++, main() must have return type int, not void.