Skip to content

Instantly share code, notes, and snippets.

@gfokkema
Last active December 6, 2019 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gfokkema/a0c73de5c9df476b5b31057ebb63394f to your computer and use it in GitHub Desktop.
Save gfokkema/a0c73de5c9df476b5b31057ebb63394f to your computer and use it in GitHub Desktop.
#include <epoxy/gl.h>
#include <gtkmm/window.h>
#include <gtkmm/box.h>
#include <gtkmm/glarea.h>
#include <iostream>
class Example : public Gtk::Window
{
public:
Example();
~Example() override;
bool do_render(const Glib::RefPtr<Gdk::GLContext>&);
void do_realize();
void do_unrealize();
private:
Gtk::GLArea* m_glarea;
GLuint m_vao;
};
Example::Example()
: m_glarea()
{
set_default_size(600, 400);
set_border_width(10);
m_glarea = Gtk::make_managed<Gtk::GLArea>();
m_glarea->set_required_version(4, 1);
m_glarea->signal_realize().connect(sigc::mem_fun(*this, &Example::do_realize));
m_glarea->signal_unrealize().connect(sigc::mem_fun(*this, &Example::do_unrealize), false);
m_glarea->signal_render().connect(sigc::mem_fun(*this, &Example::do_render), false);
add(*m_glarea);
show_all();
}
Example::~Example()
{
printf("Destructor context: %p\n", reinterpret_cast<void*>(m_glarea->get_context().get()));
// m_glarea.make_current();
}
bool
Example::do_render(const Glib::RefPtr<Gdk::GLContext>&)
{
m_glarea->make_current();
try {
m_glarea->throw_if_error();
glClearColor(0.5, 0.5, 0.5, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.5, 0.5, 0.5, 1.0);
glFlush();
return true;
} catch (const Gdk::GLError& gle) {
std::cerr << "An error occurred in the render callback of the GLArea" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
return false;
}
}
void
Example::do_realize()
{
printf("Realize context: %p\n", reinterpret_cast<void*>(m_glarea->get_context().get()));
m_glarea->make_current();
try {
m_glarea->throw_if_error();
glGenVertexArrays(1, &m_vao);
glBindVertexArray(m_vao);
} catch(const Gdk::GLError& gle) {
std::cerr << "An error occured making the context current during realize:" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
}
}
void
Example::do_unrealize()
{
printf("Unrealize context: %p\n", reinterpret_cast<void*>(m_glarea->get_context().get()));
m_glarea->make_current();
try {
m_glarea->throw_if_error();
glDeleteBuffers(1, &m_vao);
} catch (const Gdk::GLError& gle) {
std::cerr << "An error occured making the context current during unrealize" << std::endl;
std::cerr << gle.domain() << "-" << gle.code() << "-" << gle.what() << std::endl;
}
}
int main(int argc, char *argv[])
{
auto app = Gtk::Application::create("org.gtkmm.Example");
auto example = std::make_unique<Example>();
return app->run(*example, argc, argv);
}
@gfokkema
Copy link
Author

gfokkema commented Dec 5, 2019

Compile with: g++ $(pkg-config --cflags --libs gtkmm-3.0 epoxy) example.cpp

@AlexB67
Copy link

AlexB67 commented Dec 5, 2019

Hi I saw your post on the mailing list. I found that the Error message goes away for me when you assign the window on the heap. ( Not the first time with gtkmm widgets when assigning on the stack I got issues, for example Gtk::Switch can even segfault I found, but as long as assign them dynamically it works.

So instead of

Example example;

Do
Example *example = new Example(); or use a smart pointer, but you get my drift.
....
delete example

then when I run it I no longer get the error message.

Cheers :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment