如何将SDL2纹理渲染到GTK3 +窗口?

时间:2017-11-14 11:11:01

标签: gtk3 sdl-2

我正在创建一个音乐播放器并尝试使用GTK3 +来创建用户界面。我正在使用SDL_CreateWindowFrom函数让SDL2使用GTK3 +窗口而不是创建一个窗口但是无法弄清楚我需要遵循的步骤才能将SDL2纹理渲染到GTK3 +窗口。

获取GTK3窗口ID的代码

public class MyNotificationService extends Service {
@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}


static Calendar eve,morn;

@Override
public void onCreate() {
    super.onCreate();
    morn=Calendar.getInstance();
    morn.set(Calendar.HOUR_OF_DAY, 4);
    morn.set(Calendar.MINUTE, 0);
    morn.set(Calendar.SECOND, 0);
    morn.set(Calendar.AM_PM,1);

    eve=Calendar.getInstance();
    eve.set(Calendar.HOUR_OF_DAY, 6);
    eve.set(Calendar.MINUTE, 0);
    eve.set(Calendar.SECOND, 0);
    eve.set(Calendar.AM_PM,1);

    Log.d("Notification", "MORN "+morn.getTime().toString() +" vs EVE "+eve.getTime().toString());

}

@Override
public void onStart(Intent intent, int startId) {
    super.onStart(intent, startId);

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

    setTimer();

    return Service.START_STICKY;
}



void setTimer(){

    Timer timer = new Timer ();
    TimerTask hourlyTask = new TimerTask () {

        @Override
        public void run () {
            Calendar now=Calendar.getInstance();
            now.set(Calendar.MINUTE,0);
            now.set(Calendar.SECOND,0);


            Log.d("Notification MORN", morn.getTime().toString() +" vs "+now.getTime().toString());
            if (now.compareTo(morn)==0||now.getTime().equals(morn.getTime())) {
                Log.d("Notification Func", eve.getTime().toString() + " MORNING exe");
                Handler mainHandler = new Handler(Looper.getMainLooper());
                Runnable myRunnable = new Runnable() {
                    @Override
                    public void run() {
                        if (isInternetOn(getApplicationContext())){
                            getCaseDetails();
                        }else {
                            setNotification("Internet not available","Please check your Cases");
                        }
                    }
                };
                mainHandler.post(myRunnable);
            }
            Log.d("Notification EVE", eve.getTime().toString() +" vs "+now.getTime().toString());
            if (now.compareTo(eve)==0||now.getTime().equals(eve.getTime())) {
                    Log.d("Notification Func", eve.getTime().toString() + " EVENING exe");
                Handler mainHandler = new Handler(Looper.getMainLooper());
                Runnable myRunnable = new Runnable() {
                    @Override
                    public void run() {
                        getCaseDetails();
                    }
                };
                mainHandler.post(myRunnable);
            }
        }
    };
//        timer.schedule (hourlyTask, 0L, 1000*60*60);
    timer.schedule (hourlyTask, 0L, 30*1000);
}


void getDetails() {
   }



@Override
public void onDestroy() {
    super.onDestroy();
    Toast.makeText(this, "Notification Service Stopped", Toast.LENGTH_LONG).show();
}



public void setNotification(String titleStr,String descStr) {

   setting alarm here.
}

}

代码设置SDL2窗口

window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "---");
gtk_widget_show(window);
gdkWin = gtk_widget_get_window(GTK_WIDGET(window));
data->playContext->winID = GDK_WINDOW_XID(gdkWin);
gtk_main(); 

代码渲染SDL2纹理

playContext->display->window = SDL_CreateWindowFrom((const void *)playContext->winID);
playContext->display->renderer = SDL_CreateRenderer(playContext->display->window, -1, playContext->display->render_flags);

1 个答案:

答案 0 :(得分:2)

因为即使在最重要的位(例如渲染器创建标志)上,您放置的示例实际上也是不完整的,并且您没有指定哪个部分给您一个问题,这是我的示例(在我的系统上,它可用于gtk2和gtk3 - 我不会保证它完全正常但是:)

#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <SDL.h>
#include <stdbool.h>

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

static gboolean idle(void *ud) {
    (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");

    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;
}