Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: (SFML2) Possible to render OpenGL to a Window in a separate class?  (Read 3782 times)

0 Members and 1 Guest are viewing this topic.

kurasu1415

  • Newbie
  • *
  • Posts: 37
    • View Profile
    • Email
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();
    }
}

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
« Reply #1 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 &?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
« Reply #2 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.
Laurent Gomila - SFML developer

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: (SFML2) Possible to render OpenGL to a Window in a separate class?
« Reply #3 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything