Skip to content

Instantly share code, notes, and snippets.

@czak
Created January 12, 2022 14:27
Show Gist options
  • Save czak/ea73329cec7b521bdb85cc9462e2e65a to your computer and use it in GitHub Desktop.
Save czak/ea73329cec7b521bdb85cc9462e2e65a to your computer and use it in GitHub Desktop.
Testing GtkGLArea framerate in GTK3/GTK4
// GTK3 variant: │drwxr-xr-x 2 czak czak 4096 Jan 12 15:24 .
// gcc -DGTK3 $(pkg-config --cflags gtk+-3.0) -o glarea-gtk3 glarea.c $(pkg-config --libs gtk+-3.0) │drwxr-xr-x 18 czak czak 4096 Jan 12 11:15 ..
// │-rw-r--r-- 1 czak czak 24 Jan 12 13:24 .gitignore
// GTK4 variant: │-rw-r--r-- 1 czak czak 1012 Jan 12 13:33 .vimlocal
// gcc -DGTK4 $(pkg-config --cflags gtk4) -o glarea-gtk4 glarea.c $(pkg-config --libs gtk4) │-rw-r--r-- 1 czak czak 551 Jan 12 15:00 Makefile
│-rwxr-xr-x 1 czak czak 17152 Jan 12 15:24 glarea-gtk3
#include <stdio.h> │-rwxr-xr-x 1 czak czak 17096 Jan 12 15:24 glarea-gtk4
#include <gtk/gtk.h> │-rw-r--r-- 1 czak czak 1421 Jan 12 14:37 main.c
│czak@cube ~/projects/learngtk/15-gtk-gl-area (git)-[master] % ./glarea-gtk3
static int ticks = 0; │150 ticks in 2.499396 = 60.014500 FPS
static GTimer *timer = 0; │czak@cube ~/projects/learngtk/15-gtk-gl-area (git)-[master] % ./glarea-gtk4
│200 ticks in 3.333831 = 59.991043 FPS
static gboolean tick(GtkWidget* widget, GdkFrameClock* frame_clock, gpointer user_data) │czak@cube ~/projects/learngtk/15-gtk-gl-area (git)-[master] % ls -la
{ │total 64
gtk_widget_queue_draw(widget); │drwxr-xr-x 2 czak czak 4096 Jan 12 15:24 .
++ticks; │drwxr-xr-x 18 czak czak 4096 Jan 12 11:15 ..
return G_SOURCE_CONTINUE; │-rw-r--r-- 1 czak czak 24 Jan 12 13:24 .gitignore
} │-rw-r--r-- 1 czak czak 1012 Jan 12 13:33 .vimlocal
│-rw-r--r-- 1 czak czak 551 Jan 12 15:00 Makefile
static void activate(GtkApplication *app, gpointer user_data) │-rwxr-xr-x 1 czak czak 17152 Jan 12 15:24 glarea-gtk3
{ │-rwxr-xr-x 1 czak czak 17096 Jan 12 15:24 glarea-gtk4
GtkWidget *window; │-rw-r--r-- 1 czak czak 1421 Jan 12 14:37 main.c
GtkWidget *glarea; │czak@cube ~/projects/learngtk/15-gtk-gl-area (git)-[master] %
glarea = gtk_gl_area_new(); │
gtk_widget_add_tick_callback(glarea, tick, NULL, NULL); │
window = gtk_application_window_new(app); │
gtk_window_set_default_size(GTK_WINDOW(window), 800, 600); │
#if defined(GTK3) │
gtk_container_add(GTK_CONTAINER(window), glarea); │
gtk_widget_show_all(window); │
#elif defined(GTK4) │
gtk_window_set_child(GTK_WINDOW(window), glarea); │
gtk_widget_show(window); │
#endif │
timer = g_timer_new(); │
} │
int main(int argc, char **argv) │
{ │
GtkApplication *app; │
int res; │
app = gtk_application_new(NULL, G_APPLICATION_FLAGS_NONE); │
g_signal_connect(app, "activate", G_CALLBACK(activate), NULL); │
res = g_application_run(G_APPLICATION(app), argc, argv); │
g_object_unref(app); │
double elapsed = g_timer_elapsed(timer, NULL); │
fprintf(stderr, "%d ticks in %f = %f FPS\n", ticks, elapsed, ticks / elapsed); │
return res; │
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment