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

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

Minor code simplifications.

  • 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
144void 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
152void GlMapView::UpdateAdjustments()
153{
154    float w = mapviewer ? mapviewer->GetWidth() : glarea->allocation.width;
155    float h = mapviewer ? mapviewer->GetHeight() : glarea->allocation.height;
156
157    /* Manage adjustments */
158    struct { GtkAdjustment *adj; float map_size, sw_size; } s[2] =
159    {
160        { hadj, w, glarea->allocation.width },
161        { vadj, h, glarea->allocation.height },
162    };
163
164    for (int i = 0; i < 2; i++)
165    {
166        gtk_adjustment_set_lower(s[i].adj, 0);
167        gtk_adjustment_set_upper(s[i].adj, s[i].map_size);
168        gtk_adjustment_set_step_increment(s[i].adj, 1);
169        gtk_adjustment_set_page_increment(s[i].adj, s[i].sw_size);
170        gtk_adjustment_set_page_size(s[i].adj, s[i].sw_size);
171
172        float val = gtk_adjustment_get_value(s[i].adj);
173        if (val + s[i].sw_size > s[i].map_size)
174        {
175            gtk_adjustment_set_value(s[i].adj,
176                                     s[i].map_size - s[i].sw_size);
177            gtk_adjustment_value_changed(s[i].adj);
178        }
179    }
180}
181
182gboolean GlMapView::MouseButton(GdkEventButton *e)
183{
184    if (e->type == GDK_BUTTON_PRESS && e->button == 2)
185    {
186        panning = TRUE;
187        xpan = e->x;
188        ypan = e->y;
189        GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
190        gdk_window_set_cursor(glarea->window, cursor);
191        gdk_cursor_unref(cursor);
192        return FALSE;
193    }
194    else if (e->type == GDK_BUTTON_RELEASE && e->button == 2)
195    {
196        panning = FALSE;
197        gdk_window_set_cursor(glarea->window, NULL);
198        return FALSE;
199    }
200
201    return TRUE;
202}
203
204gboolean GlMapView::MouseMotion(GdkEventMotion *e)
205{
206    if (panning)
207    {
208        Scroll(xpan - e->x, ypan - e->y);
209        xpan = e->x;
210        ypan = e->y;
211        return TRUE;
212    }
213
214    return FALSE;
215}
216
217gboolean GlMapView::KeyPress(GdkEventKey *e)
218{
219    switch (e->keyval)
220    {
221    case GDK_Up:    Scroll(  0.0, -10.0); return TRUE;
222    case GDK_Down:  Scroll(  0.0,  10.0); return TRUE;
223    case GDK_Left:  Scroll(-10.0,   0.0); return TRUE;
224    case GDK_Right: Scroll( 10.0,   0.0); return TRUE;
225    }
226
227    return FALSE;
228}
229
230/* Private signal slots */
231gboolean GlMapView::IdleTickSignal(GlMapView *that)
232{
233    return that->IdleTick();
234}
235
236gboolean GlMapView::SetupSignal(GtkWidget *w, GlMapView *that)
237{
238    (void)w;
239    return that->Setup();
240}
241
242gboolean GlMapView::DestroySignal(GtkWidget *w, GlMapView *that)
243{
244    (void)w;
245    return that->Destroy();
246}
247
248gboolean GlMapView::DrawSignal(GtkWidget *w, GdkEventExpose *e,
249                               GlMapView *that)
250{
251    (void)w;
252    return that->Draw(e);
253}
254
255gboolean GlMapView::ReshapeSignal(GtkWidget *w, GdkEventConfigure *e,
256                                  GlMapView *that)
257{
258    (void)w;
259    (void)e;
260    return that->Setup();
261}
262
263gboolean GlMapView::MouseButtonSignal(GtkWidget *w, GdkEventButton *e,
264                                      GlMapView *that)
265{
266    (void)w;
267    return that->MouseButton(e);
268}
269
270gboolean GlMapView::MouseMotionSignal(GtkWidget *w, GdkEventMotion *e,
271                                      GlMapView *that)
272{
273    (void)w;
274    return that->MouseMotion(e);
275}
276
277gboolean GlMapView::KeyPressSignal(GtkWidget *w, GdkEventKey *e,
278                                   GlMapView *that)
279{
280    (void)w;
281    return that->KeyPress(e);
282}
283
Note: See TracBrowser for help on using the repository browser.