我正在尝试使用gtk#和opengl编写一个简单的基本建模程序。为了能够同时使用gtk gui和opengl,我正在使用glwidget。最近我遇到了在同一个vbox中同时拥有菜单栏和glwidget的问题(它们位于不同的插槽中,但是相同的vbox)。这是我的代码:
//MainWindow.cs
using System;
using Gtk;
using CubeGameModeler;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using GLib;
public partial class MainWindow: Gtk.Window
{
public MainWindow (): base (Gtk.WindowType.Toplevel)
{
Build ();
}
protected void OnDeleteEvent (object sender, DeleteEventArgs a)
{
Application.Quit ();
a.RetVal = true;
}
protected void OnExit (object sender, EventArgs e)
{
Application.Quit ();
}
protected void OnSaveAs (object sender, EventArgs e)
{
SaveWindow savewindow = new SaveWindow ();
savewindow.Show ();
}
protected void OnSettings (object sender, EventArgs e)
{
SettingsWindow settingswindow = new SettingsWindow ();
settingswindow.Show ();
}
protected void OnAbout (object sender, EventArgs e)
{
AboutWindow aboutwindow = new AboutWindow ();
aboutwindow.Show ();
}
protected void OnOpen (object sender, EventArgs e)
{
OpenWindow openwindow = new OpenWindow ();
openwindow.Show ();
}
protected void OnHelp (object sender, EventArgs e)
{
HelpWindow helpwindow = new HelpWindow ();
helpwindow.Show ();
}
protected void glwidget2_RenderFrame (object sender, EventArgs e)
{
GL.Viewport (0, 0, 100, 100);
GL.ClearColor (0.5f, 0.5f, 1.0f, 1.0f);
GL.Clear (ClearBufferMask.ColorBufferBit);
GL.MatrixMode (MatrixMode.Modelview);
GL.LoadIdentity ();
}
}
这是我的MainWindow.cs类中的代码。我并不担心opengl渲染,我只是想让窗口保持打开超过一瞬间。我不知道这是不是一个错误,或者它只是与我糟糕的笔记本电脑有关。我在Debian Wheezy i386上使用MonoDevelop。当我尝试运行程序时,它会打开一瞬间然后关闭。当我查看应用程序输出时,它就是这样说的:
Loaded assembly: /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/bin/Debug/CubeGameModeler.exe
Loaded assembly: /usr/lib/mono/gac/gtk-sharp/2.12.0.0__35e10195dab3c99f/gtk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/glib-sharp/2.12.0.0__35e10195dab3c99f/glib-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/atk-sharp/2.12.0.0__35e10195dab3c99f/atk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/System/4.0.0.0__b77a5c561934e089/System.dll [External]
Loaded assembly: /usr/lib/mono/gac/gdk-sharp/2.12.0.0__35e10195dab3c99f/gdk-sharp.dll [External]
Loaded assembly: /usr/lib/mono/gac/Mono.Posix/4.0.0.0__0738eb9f132ed756/Mono.Posix.dll [External]
Loaded assembly: /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/bin/Debug/OpenTK.dll [External]
Loaded assembly: /usr/lib/mono/gac/System.Drawing/4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll [External]
Stacktrace:
at (wrapper managed-to-native) Gtk.Application.gtk_main () <IL 0x00022, 0xffffffff>
at Gtk.Application.Run () <IL 0x00000, 0x0001f>
at CubeGameModeler.MainClass.Main (string[]) [0x00011] in /home/mprogrammer/Documents/Monodevelop_Projects/CubeGameModeler/CubeGameModeler/Main.cs:13
at (wrapper runtime-invoke) <Module>.runtime_invoke_void_object (object,intptr,intptr,intptr) <IL 0x00050, 0xffffffff>
Native stacktrace:
/usr/bin/mono() [0x80e863d]
/usr/bin/mono() [0x8128bac]
/usr/bin/mono() [0x8064d1f]
[0xb76f240c]
/lib/i386-linux-gnu/i686/cmov/libpthread.so.0(__pthread_mutex_lock+0x1d) [0xb769618d]
/usr/lib/i386-linux-gnu/libX11.so.6(+0x2999d) [0xb64d599d]
[0x156]
Debug info from gdb:
=================================================================
Got a SIGSEGV while executing native code. This usually indicates
a fatal error in the mono runtime or one of the native libraries
used by your application.
=================================================================
我已经通过反复试验确定问题出在菜单栏上,因为如果我把它留在窗口上,但是让它不可见,那么glwidget没有问题,程序继续运行。
如果需要更多信息,请告诉我。
答案 0 :(得分:0)
我遇到了同样的问题。而且不仅有菜单,还有Button和Entry小部件。我找到了解决问题的方法。 在程序启动时初始化工具包为我解决了问题。 这是代码:
public static void Main (string[] args)
{
using(Init())
{
Application.Init ();
MainWindow win = new MainWindow ();
win.Show ();
Application.Run ();
}
}
private static OpenTK.Toolkit Init()
{
return Toolkit.Init(new ToolkitOptions
{
Backend = PlatformBackend.PreferNative
});
}