[gtkmm] GtkmmSDL



Hi,

I am currently writing some sprite and level editors, based on gtk, for 
a platform game. I decided to use the gtksdl widget from sourceforge, 
and hacked it to get it to work with gtk. Now, after developing it for a 
while, I can see the merits of gtkmm and am actively trying to convert 
gtksdl to gtkmmsdl. I have tried my best and have the widget kind of 
adding, but I think I am missing some basic fundamentals about gtkmm. 
Can someone point me in the right direction for some related code, or a 
tutorial on the matter or something? I have googled to no end and have 
had no luck finding anything.

Thanks for any help,

Simon

code for gtksdl.c ( modified for our project )

static void gtk_sdl_class_init		(GtkSDLClass	*klass);
static void gtk_sdl_init		(GtkSDL		*sdl);
static void gtk_sdl_realize		(GtkWidget	*widget);
static void gtk_sdl_size_allocate	(GtkWidget	*widget,
					GtkAllocation	*allocation);

static void gtk_sdl_send_configure	(GtkSDL *sdl);
static void gtk_sdl_surface_attach	(GtkSDL *sdl);
static gint gtk_sdl_expose (GtkWidget *widget, GdkEventExpose *event);


static GtkWidgetClass *parent_class = NULL;

GtkType gtk_sdl_get_type (void)
{
  static GtkType sdl_type = 0;

	if (!sdl_type)
	{
		static const GtkTypeInfo sdl_info =
		{
			"GtkSDL",
			sizeof (GtkSDL),
			sizeof (GtkSDLClass),
			(GtkClassInitFunc) gtk_sdl_class_init,
			(GtkObjectInitFunc) gtk_sdl_init,
			/* reserved_1 */ NULL,
			/* reserved_2 */ NULL,
			(GtkClassInitFunc) NULL,
		};
		sdl_type = gtk_type_unique (GTK_TYPE_WIDGET, &sdl_info);
	}

	return sdl_type;
}

static void gtk_sdl_class_init (GtkSDLClass *class)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;


	widget_class = GTK_WIDGET_CLASS (class);
	widget_class->realize = gtk_sdl_realize;
	widget_class->size_allocate = gtk_sdl_size_allocate;

	widget_class->expose_event = gtk_sdl_expose;

	parent_class = gtk_type_class (gtk_widget_get_type ());

	object_class = GTK_OBJECT_CLASS (class);

}

static void gtk_sdl_init (GtkSDL *sdl)
{

	GTK_WIDGET_SET_FLAGS (sdl, GTK_CAN_FOCUS);
	if ( GTK_WIDGET_CAN_FOCUS(sdl) )
	{
		gtk_widget_grab_focus ( GTK_WIDGET (sdl) );
	}
	gtk_widget_set_events ( GTK_WIDGET (sdl), GDK_KEY_PRESS_MASK );

}

GtkWidget *gtk_sdl_new (gint width, gint height, gint bpp, Uint32 flags)
{
  GtkSDL *sdl;


	sdl = gtk_type_new (gtk_sdl_get_type ());

	sdl->width = width;
	sdl->height = height;
	sdl->bpp = bpp;
	sdl->flags = flags;

	gtk_sdl_size ( GTK_SDL (sdl), width, height);

	return GTK_WIDGET (sdl);
}

void gtk_sdl_size (GtkSDL *sdl, gint width, gint height)
{

	g_return_if_fail (GTK_IS_SDL (sdl));

	GTK_WIDGET (sdl)->requisition.width = width;
	GTK_WIDGET (sdl)->requisition.height = height;

	gtk_widget_queue_resize (GTK_WIDGET (sdl));

}

static void gtk_sdl_realize (GtkWidget *widget)
{
  GtkSDL *sdl;
  GdkWindowAttr attributes;
  gint attributes_mask;


	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_SDL (widget));

	sdl = GTK_SDL (widget);
	GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED);

	attributes.window_type = GDK_WINDOW_CHILD;
	attributes.x = widget->allocation.x;
	attributes.y = widget->allocation.y;
	attributes.width = widget->allocation.width;
	attributes.height = widget->allocation.height;
	attributes.wclass = GDK_INPUT_OUTPUT;
	attributes.visual = gtk_widget_get_visual (widget);
	attributes.colormap = gtk_widget_get_colormap (widget);
	attributes.event_mask = gtk_widget_get_events (widget) | 
GDK_EXPOSURE_MASK;

	attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | 
GDK_WA_COLORMAP;

	widget->window = gdk_window_new (gtk_widget_get_parent_window 
(widget), &attributes, attributes_mask);
	gdk_window_set_user_data (widget->window, sdl);

	widget->style = gtk_style_attach (widget->style, 
widget->window);
	gtk_style_set_background (widget->style, widget->window, 
GTK_STATE_NORMAL);

	gtk_sdl_send_configure (GTK_SDL (widget));

}

static void gtk_sdl_size_allocate (GtkWidget *widget, GtkAllocation 
*allocation)
{

	g_return_if_fail (widget != NULL);
	g_return_if_fail (GTK_IS_SDL (widget));
	g_return_if_fail (allocation != NULL);

	widget->allocation = *allocation;
	
	widget->allocation.width = MAX (1, widget->allocation.width);
	widget->allocation.height = MAX (1, widget->allocation.height);

	if (GTK_WIDGET_REALIZED (widget))
	{
		gdk_window_move_resize (widget->window,
					allocation->x, allocation->y,
					allocation->width, 
allocation->height);

		gtk_sdl_send_configure (GTK_SDL (widget));
	}
}

static void gtk_sdl_send_configure (GtkSDL *sdl)
{
  GtkWidget *widget;
  GdkEventConfigure event;

	widget = GTK_WIDGET (sdl);

	event.type = GDK_CONFIGURE;
	event.window = widget->window;
	event.send_event = TRUE;
	event.x = widget->allocation.x;
	event.y = widget->allocation.y;
	event.width = widget->allocation.width;
	event.height = widget->allocation.height;

	gtk_widget_event (widget, (GdkEvent*) &event);

}

static void gtk_sdl_surface_attach (GtkSDL *sdl)
{
  gchar SDL_windowhack[32];
  		#ifdef WIN32
			sprintf(SDL_windowhack, "SDL_WINDOWID=%ld",	
(long)GDK_WINDOW_HWND(GTK_WIDGET(sdl)->window));
			putenv("SDL_VIDEODRIVER=windib");
		#else
			sprintf(SDL_windowhack, "SDL_WINDOWID=%ld",	
GDK_WINDOW_XWINDOW(GTK_WIDGET(sdl)->window));
		#endif
		
		putenv(SDL_windowhack);

		SDL_QuitSubSystem (SDL_INIT_VIDEO);
		if ( SDL_InitSubSystem ( SDL_INIT_VIDEO ) < 0)
		{
			fprintf (stderr, "unable to init SDL: %s", 
SDL_GetError() );
			return;
		}

		if (sdl->flags &= (SDL_OPENGLBLIT | SDL_DOUBLEBUF))
		{
			SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
		}

		if (sdl->surface)
		{
			puts ("TODO: deallocate previus surface");
		}

		sdl->surface = SDL_SetVideoMode( sdl->width, 
sdl->height, sdl->bpp, sdl->flags);
		if (!sdl->surface)
		{
			fprintf (stderr, "Unable to set the video mode: 
%s", SDL_GetError() );
			return;
		}

}

static gint gtk_sdl_expose (GtkWidget *widget, GdkEventExpose *event)
{
	SDL_Surface *surface;

	surface = SDL_GetVideoSurface ();

	if (!surface)
	{
		gtk_sdl_surface_attach (GTK_SDL (widget));
	}

	return FALSE;
	
}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]