display.cpp

00001 // -*- mode: c++; c-indent-level: 4; c++-member-init-indent: 8; comment-column: 35; -*-
00002 
00003 // "display.cpp"
00004 
00005 // (c) OPAC Team, LIFL, January 2006
00006 
00007 /* This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Lesser General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011    
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Lesser General Public License for more details.
00016    
00017    You should have received a copy of the GNU Lesser General Public
00018    License along with this library; if not, write to the Free Software
00019    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00020    
00021    Contact: cahon@lifl.fr
00022 */
00023 
00024 #include <iostream>
00025 #include <fstream>
00026 
00027 #include <X11/Xlib.h>
00028 
00029 #include "display.h"
00030 #include "node.h"
00031 #include "opt_route.h"
00032 
00033 #define BORDER 20
00034 #define RATIO 0.5
00035 
00036 #define screen_width 1024
00037 #define screen_height 768
00038 
00039 static const char * filename;
00040 
00041 /* Computed coordinates */
00042 static unsigned * X_new_coord, * Y_new_coord ;
00043 
00044 /* this variable will contain the handle to the returned graphics context. */
00045 static GC gc;
00046   
00047 /* this variable will contain the pointer to the Display structure */
00048 static Display* disp;
00049 
00050 /* this variable will store the ID of the newly created window. */
00051 static Window win;
00052 
00053 static int screen;
00054 
00055 /* Create a new backing pixmap of the appropriate size */
00056 
00057   /* Best tour */
00058   /*
00059   gdk_gc_set_line_attributes (gc, 2,  GDK_LINE_ON_OFF_DASH, GDK_CAP_NOT_LAST, GDK_JOIN_MITER) ;
00060 
00061   gdk_gc_set_foreground  (gc, & color_green) ;      
00062 
00063   for (int i = 0 ; i < (int) numNodes ; i ++) {
00064 
00065     gdk_draw_line (pixmap, gc,
00066                    X_new_coord [opt_route [i]],
00067                    Y_new_coord [opt_route [i]],
00068                    X_new_coord [opt_route [(i + 1) % numNodes]],
00069                    Y_new_coord [opt_route [(i + 1) % numNodes]]);
00070     
00071                    }*/
00072 
00073 void openMainWindow (const char * __filename) {
00074 
00075   filename = __filename;
00076 
00077   /* Map */
00078   int map_width = (int) (X_max - X_min);
00079   int map_height = (int) (Y_max - Y_min);
00080   int map_side = std :: max (map_width, map_height);
00081   
00082   /* Calculate the window's width and height. */
00083   int win_width = (int) (screen_width * RATIO * map_width / map_side);
00084   int win_height = (int) (screen_height * RATIO * map_height / map_side);
00085 
00086   /* Computing the coordinates */
00087   X_new_coord = new unsigned [numNodes];
00088   Y_new_coord = new unsigned [numNodes];
00089 
00090   for (unsigned i = 0; i < numNodes; i ++) {
00091     X_new_coord [i] = (unsigned) (win_width * (1.0 - (X_coord [i] - X_min) / map_width) + BORDER);
00092     Y_new_coord [i] = (unsigned) (win_height * (1.0 - (Y_coord [i] - Y_min) / map_height) + BORDER);
00093   }
00094   
00095   /* Initialisation */
00096   XGCValues val ;
00097   
00098   disp = XOpenDisplay (NULL) ;
00099   screen = DefaultScreen (disp) ;
00100   win = XCreateSimpleWindow (disp, RootWindow (disp, screen), 0, 0, win_width + 2 * BORDER, win_height + 2 * BORDER, 2, BlackPixel (disp, screen), WhitePixel (disp, screen)) ;
00101   val.foreground = BlackPixel(disp, screen) ;
00102   val.background = WhitePixel(disp, screen) ;
00103   gc = XCreateGC (disp, win, GCForeground | GCBackground, & val) ; 
00104 
00105   XMapWindow (disp, win) ;
00106   XFlush (disp) ;
00107 
00108   while (true) {
00109     XClearWindow (disp, win) ;
00110 
00111     /* Vertices as circles */
00112     for (unsigned i = 1 ; i < numNodes ; i ++)
00113       XDrawArc (disp, win, gc, X_new_coord [i] - 1, Y_new_coord [i] - 1, 3, 3, 0, 364 * 64) ;
00114     
00115     /* New tour */
00116     std :: ifstream f (filename);
00117     if (f) {
00118       Route route;
00119       f >> route;
00120       f.close ();
00121       
00122       for (int i = 0; i < (int) numNodes; i ++) 
00123         XDrawLine (disp, win, gc,     
00124                    X_new_coord [route [i]],
00125                  Y_new_coord [route [i]],
00126                    X_new_coord [route [(i + 1) % numNodes]],
00127                    Y_new_coord [route [(i + 1) % numNodes]]);  
00128     }
00129     XFlush (disp) ;    
00130     sleep (1) ;
00131   }
00132 }

Generated on Fri Dec 22 16:54:58 2006 for ParadisEO by  doxygen 1.4.7