- Main Page
- Classes
- Files

- Search for |

# display.cpp

    00001 // "display.cpp"
    00002
    00003 // (c) OPAC Team, LIFL, January 2006
    00004
    00005 /*
    00006    Contact: paradiseo-help@lists.gforge.inria.fr
    00007 */
    00008
    00009 #include <iostream>
    00010 #include <fstream>
    00011
    00012 #include <X11/Xlib.h>
    00013
    00014 #include "display.h"
    00015 #include "node.h"
    00016 #include "opt_route.h"
    00017
    00018 #define BORDER 20
    00019 #define RATIO 0.5
    00020
    00021 #define screen_width 1024
    00022 #define screen_height 768
    00023
    00024 static const char * filename;
    00025
    00026 /* Computed coordinates */
    00027 static unsigned * X_new_coord, * Y_new_coord ;
    00028
    00029 /* this variable will contain the handle to the returned graphics context. */
    00030 static GC gc;
    00031
    00032 /* this variable will contain the pointer to the Display structure */
    00033 static Display* disp;
    00034
    00035 /* this variable will store the ID of the newly created window. */
    00036 static Window win;
    00037
    00038 static int screen;
    00039
    00040 /* Create a new backing pixmap of the appropriate size */
    00041
    00042   /* Best tour */
    00043   /*
    00044   gdk_gc_set_line_attributes (gc, 2,  GDK_LINE_ON_OFF_DASH, GDK_CAP_NOT_LAST, GDK_JOIN_MITER) ;
    00045
    00046   gdk_gc_set_foreground  (gc, & color_green) ;
    00047
    00048   for (int i = 0 ; i < (int) numNodes ; i ++) {
    00049
    00050     gdk_draw_line (pixmap, gc,
    00051                    X_new_coord [opt_route [i]],
    00052                    Y_new_coord [opt_route [i]],
    00053                    X_new_coord [opt_route [(i + 1) % numNodes]],
    00054                    Y_new_coord [opt_route [(i + 1) % numNodes]]);
    00055
    00056                    }*/
    00057
    00058 void openMainWindow (const char * __filename) {
    00059
    00060   filename = __filename;
    00061
    00062   /* Map */
    00063   int map_width = (int) (X_max - X_min);
    00064   int map_height = (int) (Y_max - Y_min);
    00065   int map_side = std :: max (map_width, map_height);
    00066
    00067   /* Calculate the window's width and height. */
    00068   int win_width = (int) (screen_width * RATIO * map_width / map_side);
    00069   int win_height = (int) (screen_height * RATIO * map_height / map_side);
    00070
    00071   /* Computing the coordinates */
    00072   X_new_coord = new unsigned [numNodes];
    00073   Y_new_coord = new unsigned [numNodes];
    00074
    00075   for (unsigned i = 0; i < numNodes; i ++) {
    00076     X_new_coord [i] = (unsigned) (win_width * (1.0 - (X_coord [i] - X_min) / map_width) + BORDER);
    00077     Y_new_coord [i] = (unsigned) (win_height * (1.0 - (Y_coord [i] - Y_min) / map_height) + BORDER);
    00078   }
    00079
    00080   /* Initialisation */
    00081   XGCValues val ;
    00082
    00083   disp = XOpenDisplay (NULL) ;
    00084   screen = DefaultScreen (disp) ;
    00085   win = XCreateSimpleWindow (disp, RootWindow (disp, screen), 0, 0, win_width + 2 * BORDER, win_height + 2 * BORDER, 2, BlackPixel (disp, screen), WhitePixel (disp, screen)) ;
    00086   val.foreground = BlackPixel(disp, screen) ;
    00087   val.background = WhitePixel(disp, screen) ;
    00088   gc = XCreateGC (disp, win, GCForeground | GCBackground, & val) ;
    00089
    00090   XMapWindow (disp, win) ;
    00091   XFlush (disp) ;
    00092
    00093   while (true) {
    00094     XClearWindow (disp, win) ;
    00095
    00096     /* Vertices as circles */
    00097     for (unsigned i = 1 ; i < numNodes ; i ++)
    00098       XDrawArc (disp, win, gc, X_new_coord [i] - 1, Y_new_coord [i] - 1, 3, 3, 0, 364 * 64) ;
    00099
    00100     /* New tour */
    00101     std :: ifstream f (filename);
    00102     if (f) {
    00103       Route route;
    00104       f >> route;
    00105       f.close ();
    00106
    00107       for (int i = 0; i < (int) numNodes; i ++)
    00108         XDrawLine (disp, win, gc,
    00109                    X_new_coord [route [i]],
    00110                  Y_new_coord [route [i]],
    00111                    X_new_coord [route [(i + 1) % numNodes]],
    00112                    Y_new_coord [route [(i + 1) % numNodes]]);
    00113     }
    00114     XFlush (disp) ;
    00115     sleep (1) ;
    00116   }
    00117 }