source: trunk/src/gtkvideo.cpp @ 91

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

Start of the GtkVideo implementation.

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