The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL2 drawing black inside window (from GtkWidget)
ScottRichards


Joined: 25 Apr 2015
Posts: 1
Location: UK
I'm attempting to get SDL2 to render into a gtk_drawing_area in GTK3 on linux (although evidentially i want this to work on windows too).

However after giving SDL2 access to the widget's window, and telling GTK not to render to the widget. I am unable to actually draw anything in the widget with SDL2, as everything appears black.

I have set up the following example to demostrate the problem. It is suppose to open a small GTK3 window, and let SDL2 draw in it with a coloured box.

Has anyone else had this problem? Or have any ideas how to resolve this drawing issue?

Code:
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <SDL2/SDL.h>

GtkWidget *window;
GtkWidget *sdlArea;
SDL_Window *SDLwin;
SDL_Renderer *SDLgfx;

void doDrawing(GtkWidget *widget, gpointer *data) {
    SDL_Rect myRect = {10, 10, 20, 20};
    SDL_SetRenderDrawColor(SDLgfx, 255, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(SDLgfx);
    SDL_SetRenderDrawColor(SDLgfx, 0, 255, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(SDLgfx, &myRect);
    SDL_RenderPresent(SDLgfx);
}

int main( int argc, char *argv[] ) {
   
    SDL_Init(SDL_INIT_VIDEO);
    gtk_init(&argc, &argv);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    g_signal_connect( window, "destroy", G_CALLBACK (gtk_main_quit), NULL );
   
    sdlArea = gtk_drawing_area_new();
    g_signal_connect (sdlArea, "draw", G_CALLBACK (doDrawing), NULL );
    gtk_widget_set_app_paintable(sdlArea, True);
    gtk_container_add( GTK_CONTAINER(window), sdlArea );
   
    gtk_widget_show_all(window);

    GdkWindow *gdk_window = gtk_widget_get_window(sdlArea);
    Window x11_window = gdk_x11_window_get_xid(GDK_X11_WINDOW(gdk_window));
   
    SDLwin = SDL_CreateWindowFrom((const void*) x11_window);
    SDLgfx = SDL_CreateRenderer(SDLwin, -1, SDL_RENDERER_SOFTWARE | SDL_RENDERER_TARGETTEXTURE);
   
    SDL_Rect myRect = {10, 10, 20, 20};
    SDL_SetRenderDrawColor(SDLgfx, 255, 0, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderClear(SDLgfx);
    SDL_SetRenderDrawColor(SDLgfx, 0, 255, 0, SDL_ALPHA_OPAQUE);
    SDL_RenderFillRect(SDLgfx, &myRect);
    SDL_RenderPresent(SDLgfx);
   
    gtk_main();
   
    SDL_Quit();
    return 0;
}


I am compiling in GCC with the following command

Code:
gcc `pkg-config --cflags gtk+-3.0` `sdl2-config --cflags` -o mySDLtest mySDLtest.c `pkg-config --libs gtk+-3.0` `sdl2-config --libs`

Many thanks to all those who can help me.