Changeset 162


Ignore:
Timestamp:
Aug 22, 2010, 6:02:20 PM (10 years ago)
Author:
sam
Message:

Refactor editor to get the glarea object out of the main source file.

Location:
trunk/src
Files:
2 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile.am

    r155 r162  
    1919test_map_LDFLAGS = `pkg-config --libs sdl gl SDL_image` -lpipi
    2020
    21 editor_SOURCES = gtk/editor.cpp
     21editor_SOURCES = gtk/editor.cpp gtk/glmapview.cpp gtk/glmapview.h
    2222editor_CXXFLAGS = `pkg-config --cflags sdl gl SDL_image gtk+-2.0 gtkgl-2.0`
    2323editor_LDADD = libcommon.a
  • trunk/src/gtk/editor.cpp

    r159 r162  
    88#endif
    99
    10 #include <cstdio>
    1110#include <cstdlib>
    12 #include <cmath>
    1311
    1412#include <gtk/gtk.h>
     
    1614
    1715#include "core.h"
     16#include "glmapview.h"
    1817#include "debugfps.h"
    19 
    20 static volatile int quit = 0;
    21 
    22 static int ticking = 0;
    23 static int panning = 0;
    24 static double xpan = 0.0f, ypan = 0.0f;
    25 static float const FPS = 30.0f;
    26 
    27 static MapViewer *mv;
    28 static GtkWidget *glarea;
    29 static GtkAdjustment *hadj, *vadj;
    30 
    31 static gint main_quit(GtkWidget *widget, GdkEventExpose *event)
    32 {
    33     (void)widget;
    34     (void)event;
    35 
    36     quit = 1;
    37     gtk_main_quit();
    38     return FALSE;
    39 }
    40 
    41 static gboolean tick(void *widget)
    42 {
    43     // FIXME: do not do anything if the previous tick was too recent?
    44 
    45     // FIXME: only quit if all entities have been cleaned
    46     if (quit)
    47         return FALSE;
    48 
    49     ticking = 1;
    50 
    51     mv->SetPOV(gtk_adjustment_get_value(hadj), gtk_adjustment_get_value(vadj));
    52 
    53     /* Tick the game */
    54     Ticker::TickGame();
    55 
    56     gtk_widget_draw(GTK_WIDGET(widget), NULL);
    57 
    58     return TRUE;
    59 }
    60 
    61 static gboolean mouse_button(GtkWidget *widget, GdkEventButton *event,
    62                                                 gpointer user_data)
    63 {
    64     if (event->type == GDK_BUTTON_PRESS && event->button == 2)
    65     {
    66         panning = 1;
    67         xpan = event->x;
    68         ypan = event->y;
    69         GdkCursor *cursor = gdk_cursor_new(GDK_HAND1);
    70         gdk_window_set_cursor(widget->window, cursor);
    71         gdk_cursor_unref(cursor);
    72         return FALSE;
    73     }
    74     else if (event->type == GDK_BUTTON_RELEASE && event->button == 2)
    75     {
    76         panning = 0;
    77         gdk_window_set_cursor(widget->window, NULL);
    78         return FALSE;
    79     }
    80 
    81     return TRUE;
    82 }
    83 
    84 static gboolean mouse_motion(GtkWidget *widget, GdkEventMotion *event,
    85                                                 gpointer user_data)
    86 {
    87     if (panning)
    88     {
    89         if (event->x != xpan)
    90         {
    91             double val = gtk_adjustment_get_value(hadj);
    92             val += xpan - event->x;
    93             xpan = event->x;
    94             if (val + widget->allocation.width > mv->GetWidth())
    95                 val = mv->GetWidth() - widget->allocation.width;
    96             gtk_adjustment_set_value(hadj, val);
    97             gtk_adjustment_value_changed(hadj);
    98         }
    99 
    100         if (event->y != ypan)
    101         {
    102             double val = gtk_adjustment_get_value(vadj);
    103             val += ypan - event->y;
    104             ypan = event->y;
    105             if (val + widget->allocation.height > mv->GetHeight())
    106                 val = mv->GetHeight() - widget->allocation.height;
    107             gtk_adjustment_set_value(vadj, val);
    108             gtk_adjustment_value_changed(vadj);
    109         }
    110     }
    111 
    112     return TRUE;
    113 }
    114 
    115 static gint init(GtkWidget *widget)
    116 {
    117     /* Manage adjustments */
    118     struct
    119     {
    120         GtkAdjustment *adj;
    121         float map_size, sw_size;
    122     }
    123     s[2] =
    124     {
    125         { hadj, mv->GetWidth(), widget->allocation.width },
    126         { vadj, mv->GetHeight(), widget->allocation.height },
    127     };
    128 
    129     for (int i = 0; i < 2; i++)
    130     {
    131         gtk_adjustment_set_lower(s[i].adj, 0);
    132         gtk_adjustment_set_upper(s[i].adj, s[i].map_size);
    133         gtk_adjustment_set_step_increment(s[i].adj, 1);
    134         gtk_adjustment_set_page_increment(s[i].adj, s[i].sw_size);
    135         gtk_adjustment_set_page_size(s[i].adj, s[i].sw_size);
    136 
    137         float val = gtk_adjustment_get_value(s[i].adj);
    138         if (val + s[i].sw_size > s[i].map_size)
    139         {
    140             gtk_adjustment_set_value(s[i].adj, s[i].map_size - s[i].sw_size);
    141             gtk_adjustment_value_changed(s[i].adj);
    142         }
    143     }
    144 
    145     /* Set up display */
    146     if (gtk_gl_area_make_current(GTK_GL_AREA(widget)))
    147         Video::Setup(widget->allocation.width, widget->allocation.height);
    148 
    149     return TRUE;
    150 }
    151 
    152 static gint reshape(GtkWidget *widget, GdkEventConfigure *event)
    153 {
    154     (void)event;
    155 
    156     return init(widget);
    157 }
    158 
    159 static gint draw(GtkWidget *widget, GdkEventExpose *event)
    160 {
    161     if (event->count > 0)
    162         return TRUE;
    163 
    164     /* OpenGL functions can be called only if make_current returns true */
    165     if (ticking && gtk_gl_area_make_current(GTK_GL_AREA(widget)))
    166     {
    167         ticking = 0;
    168 
    169         /* Clear the screen, tick the renderer, show the frame and
    170          * clamp to desired framerate */
    171         Video::Clear();
    172         Ticker::TickDraw();
    173         gtk_gl_area_swapbuffers(GTK_GL_AREA(widget));
    174         while (g_main_context_iteration(NULL, FALSE))
    175             ;
    176         Ticker::ClampFps(1000.0f / FPS);
    177     }
    178 
    179     return TRUE;
    180 }
    18118
    18219int main(int argc, char **argv)
     
    19229    }
    19330
    194     /* Build the application interface and keep a few member pointers */
     31    /* Build the application interface */
    19532    GtkBuilder *builder = gtk_builder_new();
    19633    if (!gtk_builder_add_from_file(builder, "src/gtk/editor.xml", NULL))
     
    19936        return EXIT_FAILURE;
    20037    }
     38    gtk_builder_connect_signals(builder, NULL);
    20139
    202     GtkWidget *window = GTK_WIDGET(gtk_builder_get_object(builder, "window1"));
    203     GtkWidget *viewport = GTK_WIDGET(
    204                               gtk_builder_get_object(builder, "viewport1"));
    205     hadj = gtk_range_get_adjustment(GTK_RANGE(
    206                               gtk_builder_get_object(builder, "hscrollbar1")));
    207     vadj = gtk_range_get_adjustment(GTK_RANGE(
    208                               gtk_builder_get_object(builder, "vscrollbar1")));
     40    /* Add our custom GL map viewer */
     41    GlMapView *glmapview = new GlMapView(builder);
     42
     43    /* Show window. We're good to go! */
     44    gtk_widget_show_all(GTK_WIDGET(gtk_builder_get_object(builder, "window1")));
    20945    g_object_unref(G_OBJECT(builder));
    21046
    211     /* Create new OpenGL widget */
    212     int attrlist[] =
    213     {
    214         GDK_GL_RGBA,
    215         GDK_GL_RED_SIZE, 1,
    216         GDK_GL_GREEN_SIZE, 1,
    217         GDK_GL_BLUE_SIZE, 1,
    218         GDK_GL_DOUBLEBUFFER,
    219         GDK_GL_NONE
    220     };
     47    new DebugFps();
     48    glmapview->LoadMap("maps/testmap.tmx");
    22149
    222     glarea = gtk_gl_area_new(attrlist);
    223     gtk_widget_set_usize(glarea, 400, 300);
    224     gtk_widget_set_events(glarea, GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK
    225                            | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
    226     gtk_container_add(GTK_CONTAINER(viewport), glarea);
     50    gtk_main();
    22751
    228     /* We tick from the idle function instead of a timeout to avoid
    229      * stealing time from the GTK loop when the callback time exceeds
    230      * the timeout value. */
    231     gtk_idle_add(tick, glarea);
    232 
    233     /* Connect signals and show window */
    234     gtk_signal_connect(GTK_OBJECT(window), "delete_event",
    235                        GTK_SIGNAL_FUNC(main_quit), NULL);
    236 
    237     gtk_signal_connect(GTK_OBJECT(glarea), "expose_event",
    238                        GTK_SIGNAL_FUNC(draw), NULL);
    239     gtk_signal_connect(GTK_OBJECT(glarea), "configure_event",
    240                        GTK_SIGNAL_FUNC(reshape), NULL);
    241     gtk_signal_connect(GTK_OBJECT(glarea), "realize",
    242                        GTK_SIGNAL_FUNC(init), NULL);
    243     gtk_signal_connect(GTK_OBJECT(glarea), "button_press_event",
    244                        GTK_SIGNAL_FUNC(mouse_button), NULL);
    245     gtk_signal_connect(GTK_OBJECT(glarea), "button_release_event",
    246                        GTK_SIGNAL_FUNC(mouse_button), NULL);
    247     gtk_signal_connect(GTK_OBJECT(glarea), "motion_notify_event",
    248                        GTK_SIGNAL_FUNC(mouse_motion), NULL);
    249 
    250     // FIXME: detect when the map viewer is killed
    251     mv = new MapViewer("maps/testmap.tmx");
    252     new DebugFps();
    253 
    254     gtk_widget_show_all(window);
    255     gtk_main();
     52    delete glmapview;
    25653
    25754    return EXIT_SUCCESS;
  • trunk/src/gtk/editor.xml

    r157 r162  
    55  <object class="GtkWindow" id="window1">
    66    <property name="title" translatable="yes">Deus Hax Editor</property>
     7    <signal name="delete_event" handler="gtk_main_quit"/>
    78    <child>
    89      <object class="GtkVBox" id="vbox1">
     
    161162                  <object class="GtkHScrollbar" id="hscrollbar1">
    162163                    <property name="visible">True</property>
     164                    <property name="adjustment">gl_hadj</property>
    163165                  </object>
    164166                  <packing>
     
    172174                    <property name="visible">True</property>
    173175                    <property name="orientation">vertical</property>
     176                    <property name="adjustment">gl_vadj</property>
    174177                  </object>
    175178                  <packing>
     
    180183                </child>
    181184                <child>
    182                   <object class="GtkViewport" id="viewport1">
     185                  <object class="GtkViewport" id="gl_container">
    183186                    <property name="visible">True</property>
    184187                    <property name="resize_mode">queue</property>
     
    230233    </child>
    231234  </object>
     235  <object class="GtkAdjustment" id="gl_hadj">
     236    <property name="upper">100</property>
     237    <property name="step_increment">1</property>
     238    <property name="page_increment">10</property>
     239    <property name="page_size">10</property>
     240  </object>
     241  <object class="GtkAdjustment" id="gl_vadj">
     242    <property name="upper">100</property>
     243    <property name="step_increment">1</property>
     244    <property name="page_increment">10</property>
     245    <property name="page_size">10</property>
     246  </object>
    232247</interface>
Note: See TracChangeset for help on using the changeset viewer.