source: trunk/src/gtk/glmapview.cpp @ 180

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

Fix a few minor FIXMEs in the code.

  • 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_DOUBLEBUFFER,
34        GDK_GL_NONE
35    };
36
37    glarea = gtk_gl_area_new(attrlist);
38    gtk_widget_set_usize(glarea, 400, 300);
39    gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK |
40                                  GDK_POINTER_MOTION_MASK |
41                                  GDK_BUTTON_PRESS_MASK |
42                                  GDK_BUTTON_RELEASE_MASK);
43    gtk_widget_set_can_focus(glarea, TRUE);
44
45    GtkContainer *cont = GTK_CONTAINER(gtk_builder_get_object(builder,
46                                                              "gl_container"));
47    gtk_container_add(cont, glarea);
48
49    /* We tick from the idle function instead of a timeout to avoid
50     * stealing time from the GTK loop when the callback time exceeds
51     * the timeout value. */
52    g_idle_add((GSourceFunc)IdleTickSignal, this);
53
54    /* We must divert gtk_main_quit() to release our resources when the
55     * GL widget is still realised. We'll call gtk_main_quit() when we
56     * are sure that everything has been cleaned. */
57    gtk_quit_add(0, (GtkFunction)ShutdownSignal, this);
58
59    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
60                       GTK_SIGNAL_FUNC(SetupSignal), this);
61    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
62                       GTK_SIGNAL_FUNC(DrawSignal), this);
63    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
64                       GTK_SIGNAL_FUNC(ReshapeSignal), this);
65
66    gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
67                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
68    gtk_signal_connect(GTK_OBJECT(glarea), "button_release_event",
69                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
70    gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
71                       GTK_SIGNAL_FUNC(MouseMotionSignal), this);
72
73    gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
74                       GTK_SIGNAL_FUNC(KeyPressSignal), this);
75}
76
77void GlMapView::LoadMap(char const *path)
78{
79    // FIXME: detect when the map viewer is killed
80    mapviewer = new MapViewer(path);
81    Ticker::Ref(mapviewer);
82
83    UpdateAdjustments();
84}
85
86void GlMapView::CloseMap()
87{
88    if (mapviewer)
89        Ticker::Unref(mapviewer);
90    mapviewer = NULL;
91
92    UpdateAdjustments();
93}
94
95gboolean GlMapView::IdleTick()
96{
97    if (Ticker::Finished())
98    {
99        gtk_main_quit();
100        return FALSE;
101    }
102
103    ticking = TRUE;
104
105    if (mapviewer)
106        mapviewer->SetPOV(gtk_adjustment_get_value(hadj),
107                          gtk_adjustment_get_value(vadj));
108
109    /* Tick the game */
110    Ticker::TickGame();
111
112    gtk_widget_draw(GTK_WIDGET(glarea), NULL);
113
114    return TRUE;
115}
116
117gboolean GlMapView::Setup()
118{
119    /* Set up display */
120    gtk_widget_grab_focus(glarea);
121    if (gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
122        Video::Setup(glarea->allocation.width, glarea->allocation.height);
123
124    UpdateAdjustments();
125
126    return TRUE;
127}
128
129gboolean GlMapView::Shutdown()
130{
131    CloseMap();
132    Ticker::Shutdown();
133    /* Hijack the exit process by adding another level of gtk_main */
134    gtk_widget_set_sensitive(gtk_widget_get_toplevel(glarea), FALSE);
135    gtk_main();
136    return TRUE;
137}
138
139gboolean GlMapView::Draw(GdkEventExpose *e)
140{
141    if (e->count > 0)
142        return TRUE;
143
144    /* OpenGL functions can be called only if make_current returns true */
145    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
146    {
147        ticking = FALSE;
148
149        /* Clear the screen, tick the renderer, show the frame and
150         * clamp to desired framerate */
151        Video::Clear();
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.