Hi. I wrote a program. but she does not draw a widget. but if this widget is drawn in another game, then it is drawn correctly. but in this program it is not visible. here is the code.
#include <stdio.h>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#include <gdk/x11/gdkx11window.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_video.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_events.h>
#include <stdlib.h>
#include “conf.h”
#include “box.h”
#include “shaders.h”
GtkApplication *app;
struct conf cf;
struct box *box;
static GdkWindow *gdk_window;
static long unsigned int window_id;
GtkWidget *main_window;
GtkWidget *gl_area;
static void gl_area_resize_cb ( GtkGLArea *area, gint width, gint height, gpointer data ) {
cf.screen_width = width;
cf.screen_height = height;
glViewport ( 0, 0, width, height );
}
static gboolean gl_area_render_cb ( GtkGLArea *area, GdkGLContext *ctx, gpointer data ) {
glClearColor ( 0xff / 255.0, 0xff / 255.0, 0x1c / 255.0, 1.0 );
glClear ( GL_COLOR_BUFFER_BIT );
box_render ( box );
SDL_GL_SwapWindow ( cf.window );
SDL_Delay ( 16 );
return TRUE;
}
static void gl_area_realize_cb ( GtkGLArea *area, gpointer data ) {
gtk_gl_area_make_current ( ( GtkGLArea * ) gl_area );
GdkGLContext *ctx = gtk_gl_area_get_context ( area );
gdk_window = gdk_gl_context_get_window ( ctx );
window_id = GDK_WINDOW_XID ( gdk_window );
#if 0
gdk_window = gtk_widget_get_window ( gl_area );
#endif
int ret = SDL_Init ( SDL_INIT_VIDEO );
printf ( "ret=%d\n", ret );
#if 0
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES );
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
SDL_GL_SetAttribute ( SDL_GL_CONTEXT_MINOR_VERSION, 0 );
SDL_GL_SetAttribute ( SDL_GL_ACCELERATED_VISUAL, 1 );
SDL_GL_SetAttribute ( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute ( SDL_GL_DEPTH_SIZE, 24 );
#endif
#if 0
char SDL_windowhack[32];
snprintf ( SDL_windowhack, 32, "SDL_WINDOWID=%ld", (long unsigned int) window_id );
putenv ( SDL_windowhack );
#endif
cf.window = SDL_CreateWindowFrom ( gdk_window );
if ( !cf.window ) {
printf ( "error window\n" );
}
SDL_GLContext glc;
#if 1
glc = SDL_GL_CreateContext ( cf.window );
SDL_GL_MakeCurrent ( cf.window, glc );
#endif
shaders_init ( );
box = calloc ( 1, sizeof ( struct box ) );
box_init ( box );
box_set_size ( box, 400, 400, 0xff7c7cff, 0x7cff7cff );
box_transform ( box, 30, 100 );
}
static void main_window_destroy ( GtkWidget *widget, gpointer data ) {
g_application_quit ( ( GApplication * ) app );
exit ( EXIT_SUCCESS );
}
static void create_window ( ) {
cf.screen_width = 1024;
cf.screen_height = 600;
main_window = gtk_application_window_new ( app );
gl_area = gtk_gl_area_new ( );
gtk_widget_set_size_request ( gl_area, 1024, 600 );
g_signal_connect ( gl_area, "realize", G_CALLBACK ( gl_area_realize_cb ), NULL );
g_signal_connect ( gl_area, "resize", G_CALLBACK ( gl_area_resize_cb ), NULL );
g_signal_connect ( gl_area, "render", G_CALLBACK ( gl_area_render_cb ), NULL );
g_signal_connect ( main_window, "destroy", G_CALLBACK( main_window_destroy ), NULL );
gtk_container_add ( ( GtkContainer * ) main_window, gl_area );
gtk_widget_show_all ( main_window );
}
static void g_startup ( GtkApplication *app, gpointer data ) {
GMainLoop *loop = g_main_loop_new ( NULL, FALSE );
create_window ( );
g_main_loop_run ( loop );
}
int main ( int argc, char **argv ) {
app = gtk_application_new ( "org.xverizex.cengine", G_APPLICATION_FLAGS_NONE );
g_application_register ( ( GApplication * ) app, NULL, NULL );
g_signal_connect ( app, "activate", G_CALLBACK ( g_startup ), NULL );
return g_application_run ( ( GApplication * ) app, argc, argv );
}