source: trunk/deushax/gtk/glmapview.cpp @ 616

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

The client application no longer needs to call Video::Clea().

  • Property svn:keywords set to Id
File size: 7.9 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 <gtk/gtk.h>
11#include <gtkgl/gtkglarea.h>
12#include <gdk/gdkkeysyms.h>
13
14#include "core.h"
15#include "glmapview.h"
16
17static float const FPS = 30.0f;
18
19GlMapView::GlMapView(GtkBuilder *builder)
20  : hadj(GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_hadj"))),
21    vadj(GTK_ADJUSTMENT(gtk_builder_get_object(builder, "gl_vadj"))),
22    ticking(FALSE), panning(FALSE),
23    mapviewer(0),
24    xpan(0.0), ypan(0.0)
25{
26    /* Create new OpenGL widget */
27    int attrlist[] =
28    {
29        GDK_GL_RGBA,
30        GDK_GL_RED_SIZE, 1,
31        GDK_GL_GREEN_SIZE, 1,
32        GDK_GL_BLUE_SIZE, 1,
33        GDK_GL_DEPTH_SIZE, 16,
34        GDK_GL_DOUBLEBUFFER,
35        GDK_GL_NONE
36    };
37
38    glarea = gtk_gl_area_new(attrlist);
39    gtk_widget_set_usize(glarea, 400, 300);
40    gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK |
41                                  GDK_POINTER_MOTION_MASK |
42                                  GDK_BUTTON_PRESS_MASK |
43                                  GDK_BUTTON_RELEASE_MASK);
44    gtk_widget_set_can_focus(glarea, TRUE);
45
46    GtkContainer *cont = GTK_CONTAINER(gtk_builder_get_object(builder,
47                                                              "gl_container"));
48    gtk_container_add(cont, glarea);
49
50    /* We tick from the idle function instead of a timeout to avoid
51     * stealing time from the GTK loop when the callback time exceeds
52     * the timeout value. */
53    g_idle_add((GSourceFunc)IdleTickSignal, this);
54
55    /* We must divert gtk_main_quit() to release our resources when the
56     * GL widget is still realised. We'll call gtk_main_quit() when we
57     * are sure that everything has been cleaned. */
58    gtk_quit_add(0, (GtkFunction)ShutdownSignal, this);
59
60    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
61                       GTK_SIGNAL_FUNC(SetupSignal), this);
62    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
63                       GTK_SIGNAL_FUNC(DrawSignal), this);
64    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
65                       GTK_SIGNAL_FUNC(ReshapeSignal), this);
66
67    gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
68                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
69    gtk_signal_connect(GTK_OBJECT(glarea), "button_release_event",
70                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
71    gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
72                       GTK_SIGNAL_FUNC(MouseMotionSignal), this);
73
74    gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
75                       GTK_SIGNAL_FUNC(KeyPressSignal), this);
76}
77
78void GlMapView::LoadMap(char const *path)
79{
80    // FIXME: detect when the map viewer is killed
81    mapviewer = new MapViewer(path);
82    Ticker::Ref(mapviewer);
83
84    UpdateAdjustments();
85}
86
87void GlMapView::CloseMap()
88{
89    if (mapviewer)
90        Ticker::Unref(mapviewer);
91    mapviewer = NULL;
92
93    UpdateAdjustments();
94}
95
96gboolean GlMapView::IdleTick()
97{
98    if (Ticker::Finished())
99    {
100        gtk_main_quit();
101        return FALSE;
102    }
103
104    ticking = TRUE;
105
106    if (mapviewer)
107        mapviewer->SetPOV(gtk_adjustment_get_value(hadj),
108                          mapviewer->GetHeight() - glarea->allocation.height
109                           - gtk_adjustment_get_value(vadj));
110
111    /* Tick the game */
112    Ticker::TickGame();
113
114    gtk_widget_draw(GTK_WIDGET(glarea), NULL);
115
116    return TRUE;
117}
118
119gboolean GlMapView::Setup()
120{
121    /* Set up display */
122    gtk_widget_grab_focus(glarea);
123    if (gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
124        Video::Setup(glarea->allocation.width, glarea->allocation.height);
125
126    UpdateAdjustments();
127
128    return TRUE;
129}
130
131gboolean GlMapView::Shutdown()
132{
133    CloseMap();
134    Ticker::Shutdown();
135    /* Hijack the exit process by adding another level of gtk_main */
136    gtk_widget_set_sensitive(gtk_widget_get_toplevel(glarea), FALSE);
137    gtk_main();
138    return TRUE;
139}
140
141gboolean GlMapView::Draw(GdkEventExpose *e)
142{
143    if (e->count > 0)
144        return TRUE;
145
146    /* OpenGL functions can be called only if make_current returns true */
147    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
148    {
149        ticking = FALSE;
150
151        /* Tick the renderer, show the frame and clamp to desired framerate */
152        Ticker::TickDraw();
153        gtk_gl_area_swapbuffers(GTK_GL_AREA(glarea));
154        while (g_main_context_iteration(NULL, FALSE))
155            ;
156
157        // FIXME: do some GTK stuff in here
158        Ticker::ClampFps(1000.0f / FPS);
159    }
160
161    return TRUE;
162}
163
164void GlMapView::Scroll(double dx, double dy)
165{
166    gtk_adjustment_set_value(hadj, gtk_adjustment_get_value(hadj) + dx);
167    gtk_adjustment_set_value(vadj, gtk_adjustment_get_value(vadj) + dy);
168
169    UpdateAdjustments();
170}
171
172void GlMapView::UpdateAdjustments()
173{
174    float w = mapviewer ? mapviewer->GetWidth() : glarea->allocation.width;
175    float h = mapviewer ? mapviewer->GetHeight() : glarea->allocation.height;
176
177    /* Manage adjustments */
178    struct { GtkAdjustment *adj; float map_size, sw_size; } s[2] =
179    {
180        { hadj, w, glarea->allocation.width },
181        { vadj, h, glarea->allocation.height },
182    };
183
184    for (int i = 0; i < 2; i++)
185    {
186        gtk_adjustment_set_lower(s[i].adj, 0);
187        gtk_adjustment_set_upper(s[i].adj, s[i].map_size);
188        gtk_adjustment_set_step_increment(s[i].adj, 1);
189        gtk_adjustment_set_page_increment(s[i].adj, s[i].sw_size);
190        gtk_adjustment_set_page_size(s[i].adj, s[i].sw_size);
191
192        float val = gtk_adjustment_get_value(s[i].adj);
193        if (val + s[i].sw_size > s[i].map_size)
194        {
195            gtk_adjustment_set_value(s[i].adj,
196                                     s[i].map_size - s[i].sw_size);
197            gtk_adjustment_value_changed(s[i].adj);
198        }
199    }
200}
201
202gboolean GlMapView::MouseButton(GdkEventButton *e)
203{
204    if (e->type == GDK_BUTTON_PRESS && e->button == 2)
205    {
206        panning = TRUE;
207        xpan = e->x;
208        ypan = e->y;
209        GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
210        gdk_window_set_cursor(glarea->window, cursor);
211        gdk_cursor_unref(cursor);
212        return FALSE;
213    }
214    else if (e->type == GDK_BUTTON_RELEASE && e->button == 2)
215    {
216        panning = FALSE;
217        gdk_window_set_cursor(glarea->window, NULL);
218        return FALSE;
219    }
220
221    return TRUE;
222}
223
224gboolean GlMapView::MouseMotion(GdkEventMotion *e)
225{
226    if (panning)
227    {
228        Scroll(xpan - e->x, ypan - e->y);
229        xpan = e->x;
230        ypan = e->y;
231        return TRUE;
232    }
233
234    return FALSE;
235}
236
237gboolean GlMapView::KeyPress(GdkEventKey *e)
238{
239    switch (e->keyval)
240    {
241    case GDK_Up:    Scroll(  0.0, -10.0); return TRUE;
242    case GDK_Down:  Scroll(  0.0,  10.0); return TRUE;
243    case GDK_Left:  Scroll(-10.0,   0.0); return TRUE;
244    case GDK_Right: Scroll( 10.0,   0.0); return TRUE;
245    }
246
247    return FALSE;
248}
249
250/* Private signal slots */
251gboolean GlMapView::IdleTickSignal(GlMapView *that)
252{
253    return that->IdleTick();
254}
255
256gboolean GlMapView::SetupSignal(GtkWidget *w, GlMapView *that)
257{
258    (void)w;
259    return that->Setup();
260}
261
262gboolean GlMapView::ShutdownSignal(GlMapView *that)
263{
264    return that->Shutdown();
265}
266
267gboolean GlMapView::DrawSignal(GtkWidget *w, GdkEventExpose *e,
268                               GlMapView *that)
269{
270    (void)w;
271    return that->Draw(e);
272}
273
274gboolean GlMapView::ReshapeSignal(GtkWidget *w, GdkEventConfigure *e,
275                                  GlMapView *that)
276{
277    (void)w;
278    (void)e;
279    return that->Setup();
280}
281
282gboolean GlMapView::MouseButtonSignal(GtkWidget *w, GdkEventButton *e,
283                                      GlMapView *that)
284{
285    (void)w;
286    return that->MouseButton(e);
287}
288
289gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *e,
290                                      GlMapView *that)
291{
292    (void)w;
293    return that->MouseMotion(e);
294}
295
296gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *e,
297                                   GlMapView *that)
298{
299    (void)w;
300    return that->KeyPress(e);
301}
302
Note: See TracBrowser for help on using the repository browser.