source: trunk/src/gtkvideo.cpp @ 93

Last change on this file since 93 was 93, checked in by sam, 10 years ago

The GTK loop now works. Sort of.

  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1
2#include <gtk/gtk.h>
3#include <gtkgl/gtkglarea.h>
4
5#ifdef WIN32
6#   define WIN32_LEAN_AND_MEAN
7#   include <windows.h>
8#endif
9#if defined __APPLE__ && defined __MACH__
10#   include <OpenGL/gl.h>
11#else
12#   define GL_GLEXT_PROTOTYPES
13#   include <GL/gl.h>
14#endif
15
16#include <stdlib.h>
17
18#include "gtkvideo.h"
19
20/*
21 * Gtk Video implementation class
22 */
23
24class GtkVideoData
25{
26    friend class GtkVideo;
27
28private:
29    static gint init(GtkWidget *widget)
30    {
31        GtkVideoData *data = (GtkVideoData *)
32            gtk_object_get_data(GTK_OBJECT(widget), "data");
33
34        /* OpenGL functions can be called only if make_current returns true */
35        if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
36        {
37            glViewport(0, 0, widget->allocation.width,
38                             widget->allocation.height);
39            glMatrixMode(GL_PROJECTION);
40            glLoadIdentity();
41            glOrtho(0,100, 100,0, -1,1);
42            glMatrixMode(GL_MODELVIEW);
43            glLoadIdentity();
44
45    glMatrixMode(GL_PROJECTION);
46    glLoadIdentity();
47    glOrtho(0, data->widget->allocation.width,
48               data->widget->allocation.height, 0, -1, 10);
49    glMatrixMode(GL_MODELVIEW);
50    glLoadIdentity();
51
52    glEnable(GL_TEXTURE_2D);
53    glShadeModel(GL_SMOOTH);
54    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
55    glClearDepth(1.0);
56    glEnable(GL_DEPTH_TEST);
57    glDepthFunc(GL_LEQUAL);
58    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
59
60    glEnable(GL_BLEND);
61    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
62
63        }
64        return TRUE;
65    }
66
67    static gint draw(GtkWidget *widget, GdkEventExpose *event)
68    {
69        GtkVideoData *data = (GtkVideoData *)
70            gtk_object_get_data(GTK_OBJECT(widget), "data");
71
72        if (event->count == 0 && data->drawing == 2
73             && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
74        {
75#if 0
76            /* Draw simple triangle */
77            glClearColor(0,0,0,1);
78            glClear(GL_COLOR_BUFFER_BIT);
79            glColor3f(1,1,1);
80            glBegin(GL_TRIANGLES);
81            glVertex2f(10,10);
82            glVertex2f(10,90);
83            glVertex2f(90,90);
84            glEnd();
85#endif
86
87            data->drawing = 0;
88
89            /* Swap backbuffer to front */
90            gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
91        }
92
93        return TRUE;
94    }
95
96    static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
97    {
98        GtkVideoData *data = (GtkVideoData *)
99            gtk_object_get_data(GTK_OBJECT(widget), "data");
100
101        if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
102        {
103            glViewport(0,0, widget->allocation.width,
104                            widget->allocation.height);
105
106    glMatrixMode(GL_PROJECTION);
107    glLoadIdentity();
108    glOrtho(0, data->widget->allocation.width,
109               data->widget->allocation.height, 0, -1, 10);
110    glMatrixMode(GL_MODELVIEW);
111    glLoadIdentity();
112
113    glEnable(GL_TEXTURE_2D);
114    glShadeModel(GL_SMOOTH);
115    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
116    glClearDepth(1.0);
117    glEnable(GL_DEPTH_TEST);
118    glDepthFunc(GL_LEQUAL);
119    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
120
121    glEnable(GL_BLEND);
122    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
123
124        }
125        return TRUE;
126    }
127
128    GtkWidget *widget;
129    int drawing;
130};
131
132/*
133 * Public GtkVideo class
134 */
135
136GtkVideo::GtkVideo(char const *title, int width, int height)
137{
138    data = new GtkVideoData();
139
140    int attrlist[] =
141    {
142        GDK_GL_RGBA,
143        GDK_GL_RED_SIZE, 1,
144        GDK_GL_GREEN_SIZE, 1,
145        GDK_GL_BLUE_SIZE, 1,
146        GDK_GL_DOUBLEBUFFER,
147        GDK_GL_NONE
148    };
149
150    if (gdk_gl_query() == FALSE)
151    {
152        // FIXME: implement a panic() mode
153        g_print("OpenGL not supported\n");
154        exit(1);
155    }
156
157    data->widget = gtk_gl_area_new(attrlist);
158    data->drawing = 0;
159
160    gtk_object_set_data(GTK_OBJECT(data->widget), "data", data);
161    gtk_widget_set_events(GTK_WIDGET(data->widget),
162                          GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
163
164    gtk_signal_connect(GTK_OBJECT(data->widget), "expose_event",
165                       GTK_SIGNAL_FUNC(GtkVideoData::draw), NULL);
166    gtk_signal_connect(GTK_OBJECT(data->widget), "configure_event",
167                       GTK_SIGNAL_FUNC(GtkVideoData::reshape), NULL);
168    gtk_signal_connect(GTK_OBJECT(data->widget), "realize",
169                       GTK_SIGNAL_FUNC(GtkVideoData::init), NULL);
170
171    // FIXME: is this needed?
172    gtk_widget_set_usize(GTK_WIDGET(data->widget), 100, 100);
173
174    /* Initialise OpenGL */
175    glViewport(0, 0, data->widget->allocation.width,
176                     data->widget->allocation.height);
177    glMatrixMode(GL_PROJECTION);
178    glLoadIdentity();
179    glOrtho(0, data->widget->allocation.width,
180               data->widget->allocation.height, 0, -1, 10);
181    glMatrixMode(GL_MODELVIEW);
182    glLoadIdentity();
183
184    glEnable(GL_TEXTURE_2D);
185    glShadeModel(GL_SMOOTH);
186    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
187    glClearDepth(1.0);
188    glEnable(GL_DEPTH_TEST);
189    glDepthFunc(GL_LEQUAL);
190    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
191
192    glEnable(GL_BLEND);
193    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
194}
195
196void *GtkVideo::GetWidget()
197{
198    return data->widget;
199}
200
201int GtkVideo::GetWidth() const
202{
203    return data->widget->allocation.width;
204}
205
206int GtkVideo::GetHeight() const
207{
208    return data->widget->allocation.height;
209}
210
211void GtkVideo::PreRender()
212{
213/// XXX: is this right?
214gtk_gl_area_make_current(GTK_GL_AREA(data->widget));
215
216    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
217    glLoadIdentity();
218
219    data->drawing = 1;
220}
221
222void GtkVideo::PostRender(float milliseconds)
223{
224    data->drawing = 2;
225
226    gtk_widget_draw(GTK_WIDGET(data->widget), NULL);
227#if 0
228    if (milliseconds > 0.0f)
229        while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
230            SDL_Delay(1);
231    data->ticks = SDL_GetTicks();
232    data->frames++;
233
234    SDL_GL_SwapBuffers();
235#endif
236}
237
238void GtkVideo::FullScreen()
239{
240    // FIXME
241}
242
243GtkVideo::~GtkVideo()
244{
245    // FIXME
246}
247
Note: See TracBrowser for help on using the repository browser.