Embedding SDL widget in GTK application

 

Here's an updated version of Embedding SDL widget in GTK application using SDL2 and GTK 3.0.

embed.c
#include <stdio.h>
#include <gtk/gtkx.h>
#include <SDL.h>
#include <stdbool.h>

static SDL_Window *sdl_window;
static SDL_Renderer *sdl_renderer;
static GtkWindow *gtk_window;
static GtkWidget *gtk_da;
static void *gdk_window;
static void *window_id;

static gboolean idle(void *ud) {
    if(!sdl_window) {
        printf("creating SDL window for window id %p\n", window_id);
        sdl_window = SDL_CreateWindowFrom(window_id);
        printf("sdl_window=%p\n", sdl_window);
        if(!sdl_window) {
            printf("%s\n", SDL_GetError());
        }
        sdl_renderer = SDL_CreateRenderer(sdl_window, -1, 0);
        printf("sdl_renderer=%p\n", sdl_renderer);
        if(!sdl_renderer) {
            printf("%s\n", SDL_GetError());
        }
    } else {
        SDL_SetRenderDrawColor(sdl_renderer, 255, 0, 0, 255);
        SDL_RenderClear(sdl_renderer);
        SDL_RenderPresent(sdl_renderer);
    }
    return true;
}

int main(int argc, char **argv) {
    gtk_init(&argc, &argv);
    gtk_window = (GtkWindow*)gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(gtk_window, "test");
    g_signal_connect(gtk_window,  "delete-event", gtk_main_quit, NULL);

    gtk_da = gtk_drawing_area_new();
    gtk_container_add(GTK_CONTAINER(gtk_window), gtk_da);
    gtk_widget_show_all(GTK_WIDGET(gtk_window));

    gdk_window = gtk_widget_get_window(GTK_WIDGET(gtk_da));
    window_id = (void*)(intptr_t)GDK_WINDOW_XID(gdk_window);

    SDL_Init(SDL_INIT_VIDEO);

    g_idle_add(&idle, 0);
    gtk_main();
    return 0;
}

 
 
Compile the file using following command to generate executable embed.

gcc embed.c $(pkg-config --libs --cflags gtk+-3.0 sdl2) -o embed


Comments

Popular posts from this blog

GNU Emacs as a Comic Book Reader

Mozilla Readability in GNU Emacs

Data Visualization with GNU Emacs