gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:40:12 26 4
gpt4 key购买 nike

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

获取 GTK3 窗口 ID 的代码

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

代码渲染SDL2纹理

SDL_RenderClear(playContext->display->renderer);
SDL_RenderCopy(playContext->display->renderer, playContext->textureQ.head->bmp, NULL, NULL);
SDL_RenderPresent(playContext->display->renderer);

最佳答案

因为即使在最重要的位(例如渲染器创建标志)上,您放置的示例实际上也是不完整的,并且您没有指定哪一部分给您带来问题,这是我的示例(在我的系统上,它适用于 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;
}

关于gtk3 - 如何将 SDL2 纹理渲染到 GTK3+ 窗口中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47284284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com