source: trunk/src/gtk/gtkvideo.cpp @ 106

Last change on this file since 106 was 105, checked in by sam, 11 years ago

Create a helper class for easy scene setup. Highly reduces the number
of includes in a few source files.

  • Property svn:keywords set to Id
File size: 3.6 KB
Line 
1//
2// Deus Hax (working title)
3// Copyright (c) 2010 Sam Hocevar <sam@hocevar.net>
4//
5
6#if defined HAVE_CONFIG_H
7#   include "config.h"
8#endif
9
10#include <cstdlib>
11
12#include <gtk/gtk.h>
13#include <gtkgl/gtkglarea.h>
14
15#include "gtkvideo.h"
16#include "video.h"
17
18/*
19 * Gtk Video implementation class
20 */
21
22class GtkVideoData
23{
24    friend class GtkVideo;
25
26private:
27    static gint init(GtkWidget *widget)
28    {
29        GtkVideoData *data = (GtkVideoData *)
30            gtk_object_get_data(GTK_OBJECT(widget), "data");
31
32        /* OpenGL functions can be called only if make_current returns true */
33        if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
34        {
35            Video::Setup(widget->allocation.width,
36                         widget->allocation.height);
37        }
38        return TRUE;
39    }
40
41    static gint draw(GtkWidget *widget, GdkEventExpose *event)
42    {
43        GtkVideoData *data = (GtkVideoData *)
44            gtk_object_get_data(GTK_OBJECT(widget), "data");
45
46        if (event->count == 0 && data->drawing == 2
47             && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
48        {
49            data->drawing = 0;
50
51            /* Swap backbuffer to front */
52            gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
53        }
54
55        return TRUE;
56    }
57
58    static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
59    {
60        GtkVideoData *data = (GtkVideoData *)
61            gtk_object_get_data(GTK_OBJECT(widget), "data");
62
63        if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
64        {
65            Video::Setup(widget->allocation.width,
66                         widget->allocation.height);
67        }
68        return TRUE;
69    }
70
71    GtkWidget *widget;
72    int drawing;
73};
74
75/*
76 * Public GtkVideo class
77 */
78
79GtkVideo::GtkVideo(char const *title, int width, int height)
80{
81    data = new GtkVideoData();
82
83    int attrlist[] =
84    {
85        GDK_GL_RGBA,
86        GDK_GL_RED_SIZE, 1,
87        GDK_GL_GREEN_SIZE, 1,
88        GDK_GL_BLUE_SIZE, 1,
89        GDK_GL_DOUBLEBUFFER,
90        GDK_GL_NONE
91    };
92
93    if (gdk_gl_query() == FALSE)
94    {
95        // FIXME: implement a panic() mode
96        g_print("OpenGL not supported\n");
97        exit(1);
98    }
99
100    data->widget = gtk_gl_area_new(attrlist);
101    data->drawing = 0;
102
103    gtk_object_set_data(GTK_OBJECT(data->widget), "data", data);
104    gtk_widget_set_events(GTK_WIDGET(data->widget),
105                          GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
106
107    gtk_signal_connect(GTK_OBJECT(data->widget), "expose_event",
108                       GTK_SIGNAL_FUNC(GtkVideoData::draw), NULL);
109    gtk_signal_connect(GTK_OBJECT(data->widget), "configure_event",
110                       GTK_SIGNAL_FUNC(GtkVideoData::reshape), NULL);
111    gtk_signal_connect(GTK_OBJECT(data->widget), "realize",
112                       GTK_SIGNAL_FUNC(GtkVideoData::init), NULL);
113
114    // FIXME: is this needed?
115    gtk_widget_set_usize(GTK_WIDGET(data->widget), 400, 300);
116}
117
118void *GtkVideo::GetWidget()
119{
120    return data->widget;
121}
122
123int GtkVideo::GetWidth() const
124{
125    return data->widget->allocation.width;
126}
127
128int GtkVideo::GetHeight() const
129{
130    return data->widget->allocation.height;
131}
132
133void GtkVideo::PreRender()
134{
135    /// XXX: is this right?
136    gtk_gl_area_make_current(GTK_GL_AREA(data->widget));
137
138    Video::Clear();
139
140    data->drawing = 1;
141}
142
143void GtkVideo::PostRender(float milliseconds)
144{
145    data->drawing = 2;
146
147    gtk_widget_draw(GTK_WIDGET(data->widget), NULL);
148#if 0
149    if (milliseconds > 0.0f)
150        while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
151            SDL_Delay(1);
152    data->ticks = SDL_GetTicks();
153    data->frames++;
154
155    SDL_GL_SwapBuffers();
156#endif
157}
158
Note: See TracBrowser for help on using the repository browser.