You don't manually loop for events, but instead set up your drawing in the
expose handle of the drawing widget. You then connect to this handle
through:

    g_signal_connect(drawing_area, "expose-event",
                     G_CALLBACK(my_expose_handler),
                     user_data);

See the simple.c example in the gtkglarea source archive.

Regards,
Dov

2009/7/23 Mihai Draghicioiu <mihai.draghici...@gmail.com>

> Hi all! I'm making an OpenGL application based on GDK + GtkGLArea. My code
> so far works, but it has two issues:
>
> 1. It does not handle events in a blocking fashion.
> 2. Somehow the window does not receive an Expose event.
>
> I was told that for blocking until an event arrives, I'd have to use GLib
> main loop. I have looked at the GLib main loop reference page, but I have
> no
> idea how to make the this work. So I need some hints here.
>
> And for the GDK_EXPOSE event thing, I even set the all events mask, and
> there was still no expose event. I went around this by drawing outside the
> event switch, after each event is processed. This is not so important, but
> I'm curious why it happens. With Xlib programs, an Expose event is received
> when the window pops up, and every time it is 'made dirty' by other windows
> or dragging on-screen from off-screen or whatever.
>
> Here is my code:
>
> // g++ *gdkgl.cpp* `pkg-config gdk-2.0 gtkglext-1.0 --libs --cflags`
> -ogdkgl
>
> #include <stdlib.h>
> #include <gdk/gdk.h>
> #include <gdk/gdkgl.h>
> #include <GL/gl.h>
>
> 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);
>
>        GdkGLWindow *glwin = NULL;
>        GdkGLContext *glcontext = NULL;
>        glwin = gdk_window_set_gl_capability(win, glc, NULL);
>        glcontext = gdk_gl_context_new(GDK_GL_DRAWABLE(glwin), NULL, true,
> GDK_GL_RGBA_TYPE);
>
>        bool done = false;
>        while(!done) {
>                GdkEvent *ev = gdk_event_get();
>                if(ev) {
>                        switch(ev->type) {
>                                case GDK_MAP:
>                                        break;
>                                case GDK_DELETE:
>                                        done = true;
>                                        break;
>                                case GDK_KEY_PRESS:
>                                        printf("key pressed\n");
>                                        break;
>                                case GDK_EXPOSE:
>                                        printf("got expose\n");
>                                        break;
>                                case GDK_CONFIGURE:
>
> if(gdk_gl_drawable_gl_begin(gdk_window_get_gl_drawable(win), glcontext)) {
>                                                glViewport(0, 0,
> ev->configure.width, ev->configure.height);
>
> if(gdk_gl_drawable_is_double_buffered(gdk_window_get_gl_drawable(win))) {
>
> gdk_gl_drawable_swap_buffers(gdk_window_get_gl_drawable(win));
>                                                } else
>                                                        glFlush();
>
> gdk_gl_drawable_gl_end(gdk_window_get_gl_drawable(win));
>                                        }
>                                        break;
>                        }
>
> if(gdk_gl_drawable_gl_begin(gdk_window_get_gl_drawable(win), glcontext)) {
>                                                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();
>
>
> if(gdk_gl_drawable_is_double_buffered(gdk_window_get_gl_drawable(win))) {
>
> gdk_gl_drawable_swap_buffers(gdk_window_get_gl_drawable(win));
>                                                } else
>                                                        glFlush();
>
> gdk_gl_drawable_gl_end(gdk_window_get_gl_drawable(win));
>                                        }
>                        gdk_event_free(ev);
>                }
>        }
>
>        gdk_gl_window_destroy(glwin);
>        gdk_window_destroy(win);
>
>        return 0;
> }
> _______________________________________________
> gtk-app-devel-list mailing list
> gtk-app-devel-list@gnome.org
> http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
>
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list

Reply via email to