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

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

Put everything in the "lol" namespace. Better late than never.

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