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

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

Slightly improve the documentation in a few files, add the missing
joystick class, and put a header in each source file.

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