|
该代码来自于Mihai Draghicioiu,我做了一些改动,主要是expose事件的响应。
假设源码文件名称为puregdk.c,使用
gcc puregdk.c -o puregdk`pkg-config --cflags --libs gtkglext-1.0`
编译。
在winxp mingw上测试通过。另外需要安装gtkglext软件包。- #include <stdlib.h>
- #include <glib.h>
- #include <gdk/gdk.h>
- #include <gdk/gdkgl.h>
- #include <GL/gl.h>
- GdkGLWindow *glwin = NULL;
- GdkGLContext *glcontext = NULL;
- GMainLoop *mainloop = NULL;
- static void do_draw()
- {
- glClearColor(1.0, .5, .2, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 800, 600, 0, -1, 1);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glBegin(GL_QUADS);
- glVertex2i(100, 100);
- glVertex2i(400, 100);
- glVertex2i(400, 500);
- glVertex2i(100, 500);
- glEnd();
- }
- static void event_func(GdkEvent *ev, gpointer data)
- {
- static int oldw = 0, oldh = 0;
- switch(ev->type) {
- case GDK_MAP:
- break;
- case GDK_DELETE:
- g_main_loop_quit(mainloop);
- break;
- case GDK_KEY_PRESS:
- g_print("key pressed\n");
- break;
- case GDK_EXPOSE:
- g_print("expose event\n");
- if(gdk_gl_drawable_gl_begin(gdk_window_get_gl_drawable(ev->any.window),
- glcontext)) {
- do_draw();
- if(gdk_gl_drawable_is_double_buffered(gdk_window_get_gl_drawable(ev->any.window)))
- gdk_gl_drawable_swap_buffers(gdk_window_get_gl_drawable(ev->any.window));
- else
- glFlush();
- gdk_gl_drawable_gl_end(gdk_window_get_gl_drawable(ev->any.window));
- }
- break;
- case GDK_CONFIGURE:
- g_print( "configure event\n");
- if (oldw != ev->configure.width || oldh != ev->configure.height){
- gdk_window_enable_synchronized_configure( ev->configure.window);
- if(gdk_gl_drawable_gl_begin(gdk_window_get_gl_drawable(ev->configure.window),
- glcontext)) {
- glViewport(0, 0, ev->configure.width, ev->configure.height);
- do_draw();
-
- if(gdk_gl_drawable_is_double_buffered(gdk_window_get_gl_drawable(ev->configure.window)))
- gdk_gl_drawable_swap_buffers(gdk_window_get_gl_drawable(ev->configure.window));
- else
- glFlush();
- gdk_gl_drawable_gl_end(gdk_window_get_gl_drawable(ev->configure.window));
- }
- gdk_window_configure_finished( ev->configure.window);
- oldw = ev->configure.width;
- oldh = ev->configure.height;
- }
- break;
- }
- }
- int main(int argc, char **argv)
- {
- gdk_init(&argc, &argv);
- gdk_gl_init(&argc, &argv);
- int config_attributes[] = {
- GDK_GL_DOUBLEBUFFER,
- GDK_GL_RGBA,
- GDK_GL_RED_SIZE, 1,
- GDK_GL_GREEN_SIZE, 1,
- GDK_GL_BLUE_SIZE, 1,
- GDK_GL_DEPTH_SIZE, 12,
- GDK_GL_ATTRIB_LIST_NONE
- };
- GdkGLConfig *glc = gdk_gl_config_new(config_attributes);
- GdkWindowAttr attr;
- attr.title = argv[0];
- attr.event_mask = GDK_KEY_PRESS_MASK | GDK_STRUCTURE_MASK |
- GDK_EXPOSURE_MASK;
- attr.window_type = GDK_WINDOW_TOPLEVEL;
- attr.wclass = GDK_INPUT_OUTPUT;
- attr.width = 800;
- attr.height = 600;
- GdkWindow *win = gdk_window_new(NULL, &attr, 0);
- gdk_window_show(win);
- glwin = gdk_window_set_gl_capability(win, glc, NULL);
- glcontext = gdk_gl_context_new(GDK_GL_DRAWABLE(glwin), NULL, TRUE,
- GDK_GL_RGBA_TYPE);
- gdk_event_handler_set(event_func, NULL, NULL); // this is used by GTK+ internally. We just use it for ourselves here
- mainloop = g_main_loop_new(g_main_context_default(), FALSE);
- g_main_loop_run(mainloop);
- gdk_gl_window_destroy(glwin);
- gdk_window_destroy(win);
- return 0;
- }
复制代码 |
|