Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

drv.c

Go to the documentation of this file.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** drv.c - Driver of second assignment of course CIV 2801 - 2005.2
00004 **
00005 ** ---------------------------------------------------------------
00006 **
00007 ** Project:
00008 **    CIV 2801 - Fundamentos de Computacao Grafica Aplicada - 2005.2
00009 **    Semester Projects.
00010 **    See documentation on class notes.
00011 **
00012 ** ---------------------------------------------------------------
00013 */
00014 
00032 /*
00033 ** ---------------------------------------------------------------
00034 ** Global variables and definitions:
00035 */
00036 #include <stdlib.h>
00037 #include <stdio.h>
00038 #include <string.h>
00039 
00040 #include "iup.h"
00041 #include "iupkey.h"
00042 #include "iupgl.h"
00043 #include "iupcontrols.h"
00044 
00045 #include "prj.h"
00046 #include "prm.h"
00047 
00048 /*
00049 ** ---------------------------------------------------------------
00050 ** Local variables and definitions:
00051 */
00053 static Ihandle* Omain;
00055 static Ihandle *Otopmsg;
00057 static Ihandle* Oheightval;
00059 static int cv_height;
00061 static int cv_width;
00063 int doing_help = 0;
00065 static char file_name[256];
00066 
00067 
00068 /*
00069 ** ---------------------------------------------------------------
00070 ** Local auxiliary functions:
00071 */
00072 
00073 /* ======================  drvOpenFile  ======================= */
00074 
00083 static FILE *drvOpenFile( char *mode )
00084 {
00085  FILE    *fd;         /* file descriptor */
00086  Ihandle *dlg;        /* Ponteiro para o dialogo criado  */
00087  int     status;      /* return status of dialog to get file name */
00088 
00089  strcpy( file_name, "*.*" );
00090 
00091 /* Create a file open dialog.
00092  */
00093  dlg = IupFileDlg( );
00094  if( strcmp( mode, "r" ) == 0 )
00095  {
00096   IupSetAttribute( dlg, IUP_DIALOGTYPE, "OPEN" );
00097  }
00098  else
00099  {
00100   IupSetAttribute( dlg, IUP_DIALOGTYPE, "SAVE" );
00101  }
00102  IupSetAttribute( dlg, IUP_TITLE, "Trab3: Dialogo para selecionar arquivo" );
00103  IupSetAttribute( dlg, IUP_FILTER, "*.*" );
00104  IupSetAttribute( dlg, IUP_FILE, file_name );
00105 
00106 /* Display file open dialog and get file name.
00107  */
00108  IupPopup( dlg, IUP_ANYWHERE, IUP_ANYWHERE );
00109  status = IupGetInt( dlg, IUP_STATUS );
00110  if( status >= 0 )
00111  {
00112   strcpy( file_name, IupGetAttribute( dlg, IUP_VALUE ) );
00113  }
00114  else
00115  {
00116   return( NULL );
00117  }
00118 
00119 /* Open file.
00120  */
00121  fd = fopen( file_name, mode );
00122  if( fd == NULL )
00123  {
00124   IupMessage( " Mensagem de Erro ",
00125               "\n Nao foi possivel abrir arquivo! \n" );
00126   return( NULL );
00127  }
00128 
00129  return( fd );
00130 }
00131 
00132 
00133 /*
00134 ** ---------------------------------------------------------------
00135 ** Local VGL callback functions:
00136 */
00137 static void drvResize( int width, int height, void *h );
00138 static void drvRedisplay( void *h );
00139 static void drvMouseBt( int bt, int mode,
00140                         float x, float y, void *data );
00141 static void drvMouseMv( int bt, float x, float y, void *data );
00142 
00143 /* ========================  drvResize  ======================= */
00144 
00154 static void drvResize( int width, int height, void *h )
00155 {
00156  IupGLMakeCurrent( Odatascreen );
00157  cv_height = height;
00158  cv_width = width;
00159  prjResize( width, height );
00160 }
00161 
00162 /* ======================  drvRedisplay  ====================== */
00163 
00170 static void drvRedisplay( void *h )
00171 {
00172  if( (prmFirst( ) == NULL) || doing_help )
00173  {
00174 /* In case model is empty or doing help requested by user, 
00175    display help message */
00176   prjHelp( );
00177   IupSetAttribute( Otopmsg, IUP_TITLE,
00178                    " Os comandos abaixo podem ser acionados"
00179                    " de acordo com as teclas indicadas. " );
00180  }
00181  else /* Otherwise, resdiplay model and clear top message. */
00182  {
00183   prjRedisplay( );
00184  }
00185 }
00186 
00187 /* ========================  drvMouseBt  ====================== */
00188 
00197 static void drvMouseBt( int bt, int mode,
00198                         float x, float y, void *data )
00199 {
00200  double xw, yw;                     /* mouse position in world coordinates */
00201  double factor;                     /* primitive height factor */
00202  int    rx, ry;
00203  static char fac_string[20];
00204 
00205 /* Reject point if user did not hit left mouse button.
00206  * At button-press, check to see whether mouse point hit a primitive
00207  * (this will select a primitive) and update the primitive height 
00208  * factor valuator.
00209  * Clear top message at button-release event.
00210  */
00211  if( (bt == VGL_BUTTON_LEFT) && (mode == VGL_PRESS)  )
00212  {
00213 /* Translate point from device (raster) coordinates to 
00214  * world coordinates in the XY plane.
00215  */
00216   rx = (int) (x*(float)cv_width);
00217   ry = (int) (y*(float)cv_height);
00218   if( ! prjRaster2WorldXY( rx, ry, &xw, &yw ) )
00219    return;
00220 
00221   prjSelectPrm( xw, yw );
00222   factor = prjGetHeightFac( );
00223   sprintf( fac_string, "%.2f", factor );
00224   IupSetAttribute( Oheightval, IUP_VALUE, fac_string );
00225   doing_help = 0;
00226  }
00227  else
00228  {
00229   IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00230  }
00231 }
00232 
00233 /* ========================  drvMouseMv  ====================== */
00234 
00242 static void drvMouseMv( int bt, float x, float y, void *data )
00243 {
00244 }
00245 
00246 
00247 /*
00248 ** ---------------------------------------------------------------
00249 ** Local IUP callback functions:
00250 */
00251 static int drvSetHeightFac( Ihandle *val, double fac );
00252 static int drvKeyCrtl( Ihandle *dg, int key );
00253 
00254 /* =====================  drvSetHeightFac  ==================== */
00255  
00267 static int drvSetHeightFac( Ihandle *val, double fac )
00268 {
00269  prjSetHeightFac( fac );
00270  return( IUP_DEFAULT );
00271 }
00272 
00273 /* =======================  drvKeyCrtl  ======================= */
00274 
00286 static int drvKeyCrtl( Ihandle *dg, int key )
00287 {
00288  FILE *fd;       /* pointer to file descriptor */
00289 
00290 /* Clear top message in the general case.
00291  */
00292  IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00293 
00294 /* Take the appropriate action according to the pressed key.
00295  */
00296  switch( key )
00297  {
00298   case K_h:           /* h */
00299   case K_H:           /* H */
00300    doing_help =! doing_help;
00301    vglRedraw( vglcanvas );
00302    break;
00303 
00304   case K_cQ:          /* Ctrl+q */
00305    prjQuit( );
00306    return( IUP_CLOSE );
00307 
00308   case K_cI:          /* Ctrl+i */
00309    prjInfo( );
00310    break;
00311 
00312   case K_cO:          /* Ctrl+o */
00313    if( (fd = drvOpenFile( "r" )) != NULL )
00314    {
00315     if( ! prjOpenModel( fd ) )
00316     {
00317      IupMessage( " Mensagem de Erro ",
00318                  "\n Erro nos dados do arquivo lido!\n" );
00319     }
00320     else
00321     {
00322      IupSetAttribute( Otopmsg, IUP_TITLE,
00323                       " Use o mouse para manipular o modelo na tela. " );
00324     }
00325     fclose( fd );
00326    }
00327    break;
00328 
00329   case K_cS:          /* Ctrl+s */
00330    prjZbufferSnapShot( );
00331    break;
00332 
00333   case K_r:           /* r */
00334    prjResetView( );
00335    break;
00336 
00337   case K_f:           /* f */
00338    prjFit( );
00339    break;
00340 
00341   case K_p:           /* p */
00342    prjPersp( );
00343    break;
00344 
00345   case K_o:           /* o */
00346    prjOrtho( );
00347    break;
00348 
00349   case K_x:           /* x */
00350    prjProjX( );
00351    break;
00352 
00353   case K_y:           /* y */
00354    prjProjY( );
00355    break;
00356 
00357   case K_z:           /* z */
00358    prjProjZ( );
00359    break;
00360 
00361   case K_cM:          /* Ctrl+m => "(screen-based) view Manipulation mode" */
00362    prjManipMode( );
00363   break;
00364 
00365   case K_cN:          /* Ctrl+n => "(walk-through) view Navigation mode " */
00366    prjNavigMode( );
00367   break;
00368 
00369   case K_cZ:          /* Ctrl+z => "Zoom view manipulation mode" */
00370    prjZoomMode( );
00371    break;
00372 
00373   default:
00374    break;
00375  }
00376 
00377  return( IUP_DEFAULT );
00378 }
00379 
00380 
00381 /*
00382 ** ---------------------------------------------------------------
00383 ** Main driver:
00384 */
00385 
00386 /* --------------------------------------------------------------- */
00398 int main( int argc, char* argv[] )
00399 {
00400  static char msg_alarm[512];
00401  char *msg_error;
00402 
00403  IupOpen( );
00404  IupControlsOpen( );
00405  IupGLCanvasOpen( );
00406 
00407  msg_error = IupLoad( "trab3.led" );
00408  if( msg_error )
00409  {
00410   sprintf( msg_alarm, "\n%s\n", msg_error );
00411   IupAlarm( "Erro no arquivo trab3.led!", msg_alarm, " OK ", NULL, NULL );
00412   IupClose( );
00413   exit( 1 );
00414  }
00415 
00416 /* Get handle to interface objects.
00417  */
00418  Omain           = IupGetHandle( "main" );
00419  Otopmsg         = IupGetHandle( "topmsg" );
00420  Odatascreen     = IupGetHandle( "datascreen" );
00421  Oheightval      = IupGetHandle( "heightval" );
00422 
00423 /* Realize user interface dialog.
00424  */
00425  IupMap( Omain );
00426 
00427 /* Register IUP callback functions associated with each action.
00428  */
00429  IupSetFunction( "drvSetHeightFac",  (Icallback)drvSetHeightFac );
00430  IupSetFunction( "drvKeyCrtl",       (Icallback)drvKeyCrtl );
00431 
00432 /* Register main canvas with VGL package that
00433  * manages visualization through mouse interaction.
00434  * Initialize VGL functions for redisplay, resize, and 
00435  * mouse event on canvas.
00436  */
00437  vglcanvas = vglIupCanvas( Odatascreen );
00438  vglSetReshapeFunc( vglcanvas, drvResize, 0L );
00439  vglSetRedrawFunc( vglcanvas, drvRedisplay, 0L );
00440  vglSetMouseButtonFunc( vglcanvas, drvMouseBt, vglcanvas );
00441  vglSetMouseMotionFunc( vglcanvas, drvMouseMv, vglcanvas );
00442 
00443 /* Initialize project.
00444  */
00445  prjInit( );
00446 
00447 /* Exhibit user interface dialog.
00448  */
00449  IupShowXY( Omain, IUP_CENTER, IUP_CENTER );
00450 
00451 /* Pass control to user-interface system.
00452  */
00453  IupMainLoop( );
00454 
00455  IupClose( );
00456 
00457  return( 0 );
00458 }
00459 
00460 lSetReshapeFunc( vglcanvas, drvResize, 0L );
00461  vglSetRedrawFunc( vglcanvas, drvRedisplay, 0L );
00462  vglSetMouseButtonFunc( vglcanvas, drvMouseBt, vglcanvas );
00463  vglSetMouseMotionFunc( vglcanvas, drvMouseMv, vglcanvas );
00464 
00465 /* Initialize project.
00466  */
00467  prjInit( );
00468 
00469 /* Exhibit user interface dialog.
00470  */
00471  IupShowXY( Omain, IUP_CENTER, IUP_CENTER );
00472 
00473 /* Pass control to user-interface system.
00474  */
00475  IupMainLoop( );
00476 
00477  IupClose( );
00478 
00479  return( 0 );
00480 }
00481 

Generated on Tue Nov 8 10:57:59 2005 for Trab3 by doxygen1.2.18