Created
December 6, 2019 20:54
-
-
Save gfokkema/3aa94c1fc645efad6dba488d3c2fe8f2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Build with:
gcc $(pkg-config --cflags --libs gtk+-3.0 epoxy) gtkexample.c