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

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

Remove duplicate code in the GL map viewer.

  • Property svn:keywords set to Id
File size: 7.4 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    gtk_signal_connect(GTK_OBJECT(glarea), "realize",
55                       GTK_SIGNAL_FUNC(SetupSignal), this);
56    gtk_signal_connect(GTK_OBJECT(glarea), "destroy",
57                       GTK_SIGNAL_FUNC(DestroySignal), this);
58    gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
59                       GTK_SIGNAL_FUNC(DrawSignal), this);
60    gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
61                       GTK_SIGNAL_FUNC(ReshapeSignal), this);
62
63    gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
64                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
65    gtk_signal_connect(GTK_OBJECT(glarea), "button_release_event",
66                       GTK_SIGNAL_FUNC(MouseButtonSignal), this);
67    gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
68                       GTK_SIGNAL_FUNC(MouseMotionSignal), this);
69
70    gtk_signal_connect(GTK_OBJECT(glarea), "key_press_event",
71                       GTK_SIGNAL_FUNC(KeyPressSignal), this);
72}
73
74void GlMapView::LoadMap(char const *path)
75{
76    // FIXME: detect when the map viewer is killed
77    mapviewer = new MapViewer(path);
78
79    UpdateAdjustments();
80}
81
82void GlMapView::SetFocus()
83{
84    gtk_widget_grab_focus(glarea);
85}
86
87gboolean GlMapView::IdleTick()
88{
89    // FIXME: do not do anything if the previous tick was too recent?
90    ticking = TRUE;
91
92    if (mapviewer)
93        mapviewer->SetPOV(gtk_adjustment_get_value(hadj),
94                          gtk_adjustment_get_value(vadj));
95
96    /* Tick the game */
97    Ticker::TickGame();
98
99    gtk_widget_draw(GTK_WIDGET(glarea), NULL);
100
101    return TRUE;
102}
103
104gboolean GlMapView::Setup()
105{
106    /* Set up display */
107    if (gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
108        Video::Setup(glarea->allocation.width, glarea->allocation.height);
109
110    UpdateAdjustments();
111
112    return TRUE;
113}
114
115gboolean GlMapView::Destroy()
116{
117    g_idle_remove_by_data(this);
118    return TRUE;
119}
120
121gboolean GlMapView::Draw(GdkEventExpose *e)
122{
123    if (e->count > 0)
124        return TRUE;
125
126    /* OpenGL functions can be called only if make_current returns true */
127    if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(glarea)))
128    {
129        ticking = FALSE;
130
131        /* Clear the screen, tick the renderer, show the frame and
132         * clamp to desired framerate */
133        Video::Clear();
134        Ticker::TickDraw();
135        gtk_gl_area_swapbuffers(GTK_GL_AREA(glarea));
136        while (g_main_context_iteration(NULL, FALSE))
137            ;
138        Ticker::ClampFps(1000.0f / FPS);
139    }
140
141    return TRUE;
142}
143
144gboolean GlMapView::Scroll(double dx, double dy)
145{
146    gtk_adjustment_set_value(hadj, gtk_adjustment_get_value(hadj) + dx);
147    gtk_adjustment_set_value(vadj, gtk_adjustment_get_value(vadj) + dy);
148
149    UpdateAdjustments();
150
151    return TRUE;
152}
153
154gboolean GlMapView::UpdateAdjustments()
155{
156    float w = mapviewer ? mapviewer->GetWidth() : glarea->allocation.width;
157    float h = mapviewer ? mapviewer->GetHeight() : glarea->allocation.height;
158
159    /* Manage adjustments */
160    struct { GtkAdjustment *adj; float map_size, sw_size; } s[2] =
161    {
162        { hadj, w, glarea->allocation.width },
163        { vadj, h, glarea->allocation.height },
164    };
165
166    for (int i = 0; i < 2; i++)
167    {
168        gtk_adjustment_set_lower(s[i].adj, 0);
169        gtk_adjustment_set_upper(s[i].adj, s[i].map_size);
170        gtk_adjustment_set_step_increment(s[i].adj, 1);
171        gtk_adjustment_set_page_increment(s[i].adj, s[i].sw_size);
172        gtk_adjustment_set_page_size(s[i].adj, s[i].sw_size);
173
174        float val = gtk_adjustment_get_value(s[i].adj);
175        if (val + s[i].sw_size > s[i].map_size)
176        {
177            gtk_adjustment_set_value(s[i].adj,
178                                     s[i].map_size - s[i].sw_size);
179            gtk_adjustment_value_changed(s[i].adj);
180        }
181    }
182
183    return TRUE;
184}
185
186gboolean GlMapView::MouseButton(GdkEventButton *e)
187{
188    if (e->type == GDK_BUTTON_PRESS && e->button == 2)
189    {
190        panning = TRUE;
191        xpan = e->x;
192        ypan = e->y;
193        GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
194        gdk_window_set_cursor(glarea->window, cursor);
195        gdk_cursor_unref(cursor);
196        return FALSE;
197    }
198    else if (e->type == GDK_BUTTON_RELEASE && e->button == 2)
199    {
200        panning = FALSE;
201        gdk_window_set_cursor(glarea->window, NULL);
202        return FALSE;
203    }
204
205    return TRUE;
206}
207
208gboolean GlMapView::MouseMotion(GdkEventMotion *e)
209{
210    if (panning)
211    {
212        Scroll(xpan - e->x, ypan - e->y);
213        xpan = e->x;
214        ypan = e->y;
215    }
216
217    return TRUE;
218}
219
220gboolean GlMapView::KeyPress(GdkEventKey *e)
221{
222    switch (e->keyval)
223    {
224    case GDK_Up:    Scroll(  0.0, -10.0); break;
225    case GDK_Down:  Scroll(  0.0,  10.0); break;
226    case GDK_Left:  Scroll(-10.0,  0.0); break;
227    case GDK_Right: Scroll( 10.0,  0.0); break;
228    default: return FALSE;
229    }
230
231    return TRUE;
232}
233
234/* Private signal slots */
235gboolean GlMapView::IdleTickSignal(GlMapView *that)
236{
237    return that->IdleTick();
238}
239
240gboolean GlMapView::SetupSignal(GtkWidget *w, GlMapView *that)
241{
242    (void)w;
243    return that->Setup();
244}
245
246gboolean GlMapView::DestroySignal(GtkWidget *w, GlMapView *that)
247{
248    (void)w;
249    return that->Destroy();
250}
251
252gboolean GlMapView::DrawSignal(GtkWidget *w, GdkEventExpose *e,
253                               GlMapView *that)
254{
255    (void)w;
256    return that->Draw(e);
257}
258
259gboolean GlMapView::ReshapeSignal(GtkWidget *w, GdkEventConfigure *e,
260                                  GlMapView *that)
261{
262    (void)w;
263    (void)e;
264    return that->Setup();
265}
266
267gboolean GlMapView::MouseButtonSignal(GtkWidget *w, GdkEventButton *e,
268                                      GlMapView *that)
269{
270    (void)w;
271    return that->MouseButton(e);
272}
273
274gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *e,
275                                      GlMapView *that)
276{
277    (void)w;
278    return that->MouseMotion(e);
279}
280
281gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *e,
282                                   GlMapView *that)
283{
284    (void)w;
285    return that->KeyPress(e);
286}
287
Note: See TracBrowser for help on using the repository browser.