Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

drv.cpp

Go to the documentation of this file.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** drv.c - Driver of fourth assignment of course CIV 2802 - 2010.1
00004 **
00005 ** ---------------------------------------------------------------
00006 **
00007 ** Project:
00008 **    CIV 2802 - Sistemas Graficos para Engenharia - 2010.1
00009 **    Semester Projects.
00010 **    See documentation on class notes.
00011 **
00012 ** ---------------------------------------------------------------
00013 */
00014 
00037 /*
00038 ** ---------------------------------------------------------------
00039 ** Global variables and definitions:
00040 */
00041 #include <stdlib.h>
00042 #include <stdio.h>
00043 #include <string.h>
00044 
00045 #include "iup.h"
00046 #include "iupkey.h"
00047 #include "iupgl.h"
00048 #include "iupcontrols.h"
00049 
00050 #include "prj.h"
00051 #include "prm.h"
00052 
00053 /*
00054 ** ---------------------------------------------------------------
00055 ** Local variables and definitions:
00056 */
00058 static Ihandle* Omain;
00060 static Ihandle* Otopmsg;
00062 static Ihandle* Oheightval;
00064 static int cv_height;
00066 static int cv_width;
00068 int doing_help = 0;
00070 static char file_name[256];
00071 
00072 
00073 /*
00074 ** ---------------------------------------------------------------
00075 ** Local auxiliary functions:
00076 */
00077 
00078 /* ======================  drvOpenFile  ======================= */
00079 
00088 static FILE* drvOpenFile( char* mode )
00089 {
00090  FILE*    fd;         /* file descriptor */
00091  Ihandle* dlg;        /* Ponteiro para o dialogo criado  */
00092  int     status;      /* return status of dialog to get file name */
00093 
00094  strcpy( file_name, "*.*" );
00095 
00096 /* Create a file open dialog.
00097  */
00098  dlg = IupFileDlg( );
00099  if( strcmp( mode, "r" ) == 0 )
00100  {
00101   IupSetAttribute( dlg, IUP_DIALOGTYPE, "OPEN" );
00102  }
00103  else
00104  {
00105   IupSetAttribute( dlg, IUP_DIALOGTYPE, "SAVE" );
00106  }
00107  IupSetAttribute( dlg, IUP_TITLE, "Trab4: Dialogo para selecionar arquivo" );
00108  IupSetAttribute( dlg, IUP_FILTER, "*.*" );
00109  IupSetAttribute( dlg, IUP_FILE, file_name );
00110 
00111 /* Display file open dialog and get file name.
00112  */
00113  IupPopup( dlg, IUP_CURRENT, IUP_CURRENT );
00114  status = IupGetInt( dlg, IUP_STATUS );
00115  if( status >= 0 )
00116  {
00117   strcpy( file_name, IupGetAttribute( dlg, IUP_VALUE ) );
00118  }
00119  else
00120  {
00121   return( NULL );
00122  }
00123 
00124 /* Open file.
00125  */
00126  fd = fopen( file_name, mode );
00127  if( fd == NULL )
00128  {
00129   IupMessage( " Mensagem de Erro ",
00130               "\n Nao foi possivel abrir arquivo! \n" );
00131   return( NULL );
00132  }
00133 
00134  return( fd );
00135 }
00136 
00137 
00138 /*
00139 ** ---------------------------------------------------------------
00140 ** Local VGL callback functions:
00141 */
00142 static void drvResize( int width, int height, void* h );
00143 static void drvRedisplay( void* h );
00144 static void drvMouseBt( int bt, int mode,
00145                         float x, float y, void* data );
00146 static void drvMouseMv( int bt, float x, float y, void* data );
00147 
00148 /* ========================  drvResize  ======================= */
00149 
00159 static void drvResize( int width, int height, void* h )
00160 {
00161         IupGLMakeCurrent( Prj::Odatascreen );
00162  cv_height = height;
00163  cv_width = width;
00164  Prj::Resize( width, height );
00165 }
00166 
00167 /* ======================  drvRedisplay  ====================== */
00168 
00175 static void drvRedisplay( void* h )
00176 {
00177  if( (Prm::First( ) == NULL) || doing_help )
00178  {
00179 /* In case model is empty or doing help requested by user, 
00180    display help message */
00181   Prj::Help( );
00182   IupSetAttribute( Otopmsg, IUP_TITLE,
00183                    " Os comandos abaixo podem ser acionados"
00184                    " de acordo com as teclas indicadas. " );
00185  }
00186  else /* Otherwise, resdiplay model and clear top message. */
00187  {
00188   Prj::Redisplay( );
00189  }
00190 }
00191 
00192 /* ========================  drvMouseBt  ====================== */
00193 
00202 static void drvMouseBt( int bt, int mode,
00203                         float x, float y, void* data )
00204 {
00205  double xw, yw;                     /* mouse position in world coordinates */
00206  double factor;                     /* primitive height factor */
00207  int    rx, ry;
00208  static char fac_string[20];
00209 
00210 /* Reject point if user did not hit left mouse button.
00211  * At button-press, check to see whether mouse point hit a primitive
00212  * (this will select a primitive) and update the primitive height 
00213  * factor valuator.
00214  * Clear top message at button-release event.
00215  */
00216  if( (bt == VGL_BUTTON_LEFT) && (mode == VGL_PRESS)  )
00217  {
00218 /* Translate point from device (raster) coordinates to 
00219  * world coordinates in the XY plane.
00220  */
00221   rx = (int) (x*(float)cv_width);
00222   ry = (int) (y*(float)cv_height);
00223   if( ! Prj::Raster2WorldXY( rx, ry, &xw, &yw ) )
00224    return;
00225 
00226   Prj::SelectPrm( xw, yw );
00227   factor = Prj::GetHeightFac( );
00228   sprintf( fac_string, "%.2f", factor );
00229   IupSetAttribute( Oheightval, IUP_VALUE, fac_string );
00230   doing_help = 0;
00231  }
00232  else
00233  {
00234   IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00235  }
00236 }
00237 
00238 /* ========================  drvMouseMv  ====================== */
00239 
00247 static void drvMouseMv( int bt, float x, float y, void* data )
00248 {
00249 }
00250 
00251 
00252 /*
00253 ** ---------------------------------------------------------------
00254 ** Local IUP callback functions:
00255 */
00256 static int drvSetHeightFac( Ihandle* val, double fac );
00257 static int drvKeyCrtl( Ihandle* dg, int key );
00258 
00259 /* =====================  drvSetHeightFac  ==================== */
00260  
00272 static int drvSetHeightFac( Ihandle* val, double fac )
00273 {
00274  Prj::SetHeightFac( fac );
00275  return( IUP_DEFAULT );
00276 }
00277 
00278 /* =======================  drvKeyCrtl  ======================= */
00279 
00291 static int drvKeyCrtl( Ihandle* dg, int key )
00292 {
00293  FILE* fd;       /* pointer to file descriptor */
00294 
00295 /* Clear top message in the general case.
00296  */
00297  IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00298 
00299 /* Take the appropriate action according to the pressed key.
00300  */
00301  switch( key )
00302  {
00303   case K_h:           /* h */
00304   case K_H:           /* H */
00305    doing_help =! doing_help;
00306    vglRedraw( Prj::vglcanvas );
00307    break;
00308 
00309   case K_cQ:          /* Ctrl+q */
00310    Prj::Quit( );
00311    return( IUP_CLOSE );
00312 
00313   case K_cI:          /* Ctrl+i */
00314    Prj::Info( );
00315    break;
00316 
00317   case K_cO:          /* Ctrl+o */
00318    if( (fd = drvOpenFile( "r" )) != NULL )
00319    {
00320     if( ! Prj::OpenModel( fd ) )
00321     {
00322      IupMessage( " Mensagem de Erro ",
00323                  "\n Erro nos dados do arquivo lido!\n" );
00324     }
00325     else
00326     {
00327      IupSetAttribute( Otopmsg, IUP_TITLE,
00328                       " Use o mouse para manipular o modelo na tela. " );
00329     }
00330     fclose( fd );
00331    }
00332    break;
00333 
00334   case K_cS:          /* Ctrl+s */
00335    Prj::ZbufferSnapShot( );
00336    break;
00337 
00338   case K_r:           /* r */
00339    Prj::ResetView( );
00340    break;
00341 
00342   case K_f:           /* f */
00343    Prj::Fit( );
00344    break;
00345 
00346   case K_p:           /* p */
00347    Prj::Persp( );
00348    break;
00349 
00350   case K_o:           /* o */
00351    Prj::Ortho( );
00352    break;
00353 
00354   case K_x:           /* x */
00355    Prj::ProjX( );
00356    break;
00357 
00358   case K_y:           /* y */
00359    Prj::ProjY( );
00360    break;
00361 
00362   case K_z:           /* z */
00363    Prj::ProjZ( );
00364    break;
00365 
00366   case K_cM:          /* Ctrl+m => "(screen-based) view Manipulation mode" */
00367    Prj::ManipMode( );
00368   break;
00369 
00370   case K_cN:          /* Ctrl+n => "(walk-through) view Navigation mode " */
00371    Prj::NavigMode( );
00372   break;
00373 
00374   case K_cZ:          /* Ctrl+z => "Zoom view manipulation mode" */
00375    Prj::ZoomMode( );
00376    break;
00377 
00378   default:
00379    break;
00380  }
00381 
00382  return( IUP_DEFAULT );
00383 }
00384 
00385 
00386 /*
00387 ** ---------------------------------------------------------------
00388 ** Main driver:
00389 */
00390 
00391 /* --------------------------------------------------------------- */
00403 int main( int argc, char* argv[] )
00404 {
00405  static char msg_alarm[512];
00406  char* msg_error;
00407 
00408  IupOpen( &argc, &argv );
00409  IupControlsOpen( );
00410  IupGLCanvasOpen( );
00411 
00412  msg_error = IupLoad( "trab4.led" );
00413  if( msg_error )
00414  {
00415   sprintf( msg_alarm, "\n%s\n", msg_error );
00416   IupAlarm( "Erro no arquivo trab4.led!", msg_alarm, " OK ", NULL, NULL );
00417   IupClose( );
00418   exit( 1 );
00419  }
00420 
00421 /* Get handle to interface objects.
00422  */
00423  Omain            = IupGetHandle( "main" );
00424  Otopmsg          = IupGetHandle( "topmsg" );
00425  Prj::Odatascreen = IupGetHandle( "datascreen" );
00426  Oheightval       = IupGetHandle( "heightval" );
00427 
00428 /* Realize user interface dialog.
00429  */
00430  IupMap( Omain );
00431 
00432 /* Register IUP callback functions associated with each action.
00433  */
00434  IupSetFunction( "drvSetHeightFac",  (Icallback)drvSetHeightFac );
00435  IupSetFunction( "drvKeyCrtl",       (Icallback)drvKeyCrtl );
00436 
00437 /* Register main canvas with VGL package that
00438  * manages visualization through mouse interaction.
00439  * Initialize VGL functions for redisplay, resize, and 
00440  * mouse event on canvas.
00441  */
00442  Prj::vglcanvas = vglIupCanvas( Prj::Odatascreen );
00443  vglSetReshapeFunc( Prj::vglcanvas, drvResize, 0L );
00444  vglSetRedrawFunc( Prj::vglcanvas, drvRedisplay, 0L );
00445  vglSetMouseButtonFunc( Prj::vglcanvas, drvMouseBt, Prj::vglcanvas );
00446  vglSetMouseMotionFunc( Prj::vglcanvas, drvMouseMv, Prj::vglcanvas );
00447 
00448 /* Initialize project.
00449  */
00450  Prj::Init( );
00451 
00452 /* Exhibit user interface dialog.
00453  */
00454  IupShowXY( Omain, IUP_CENTER, IUP_CENTER );
00455 
00456 /* Pass control to user-interface system.
00457  */
00458  IupMainLoop( );
00459 
00460  IupClose( );
00461 
00462  return( 0 );
00463 }
00464 

Generated on Mon Jun 21 12:45:12 2010 for Trab4 by  doxygen 1.4.2-20050421