Skip to content

Instantly share code, notes, and snippets.

@gfokkema
Created December 6, 2019 20:54
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/3aa94c1fc645efad6dba488d3c2fe8f2 to your computer and use it in GitHub Desktop.
Save gfokkema/3aa94c1fc645efad6dba488d3c2fe8f2 to your computer and use it in GitHub Desktop.
#include <math.h>
#include <gtk/gtk.h>
#include <epoxy/gl.h>
static GLuint vao = 0;
/* We need to set up our state when we realize the GtkGLArea widget */
static void
realize (GtkWidget *widget)
{
gtk_gl_area_make_current (GTK_GL_AREA (widget));
glGenVertexArrays (1, &vao);
}
/* We should tear down the state when unrealizing */
static void
unrealize (GtkWidget *widget)
{
gtk_gl_area_make_current (GTK_GL_AREA (widget));
if (gtk_gl_area_get_error (GTK_GL_AREA (widget)) != NULL)
return;
glDeleteVertexArrays (1, &vao);
}
static gboolean
render (GtkGLArea *area,
GdkGLContext *context)
{
if (gtk_gl_area_get_error (area) != NULL)
return FALSE;
/* Clear the viewport */
glClearColor (0.5, 0.5, 0.5, 1.0);
glClear (GL_COLOR_BUFFER_BIT);
/* Flush the contents of the pipeline */
glFlush ();
return TRUE;
}
static void
activate (GtkApplication* app,
gpointer user_data)
{
GtkWidget *window, *box, *gl_area;
window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "OpenGL Area");
gtk_window_set_default_size (GTK_WINDOW (window), 400, 600);
gtk_container_set_border_width (GTK_CONTAINER (window), 12);
box = gtk_box_new (GTK_ORIENTATION_VERTICAL, FALSE);
gtk_box_set_spacing (GTK_BOX (box), 6);
gtk_container_add (GTK_CONTAINER (window), box);
gl_area = gtk_gl_area_new ();
gtk_widget_set_hexpand (gl_area, TRUE);
gtk_widget_set_vexpand (gl_area, TRUE);
gtk_container_add (GTK_CONTAINER (box), gl_area);
/* We need to initialize and free GL resources, so we use
* the realize and unrealize signals on the widget
*/
g_signal_connect (gl_area, "realize", G_CALLBACK (realize), NULL);
g_signal_connect (gl_area, "unrealize", G_CALLBACK (unrealize), NULL);
/* The main "draw" call for GtkGLArea */
g_signal_connect (gl_area, "render", G_CALLBACK (render), NULL);
gtk_widget_show_all (window);
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
@gfokkema
Copy link
Author

gfokkema commented Dec 6, 2019

Build with: gcc $(pkg-config --cflags --libs gtk+-3.0 epoxy) gtkexample.c

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