home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / audio / drive / Road.c++ < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.1 KB  |  133 lines

  1. /*
  2.  * Copyright 1992-1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. //////////////////////////////////////////////////////////////////////
  18. // Road.c++ - implementation of the road class
  19. //////////////////////////////////////////////////////////////////////
  20.  
  21. #include "Road.h"
  22. #include "Stretch.h"
  23. #include "Straight.h"
  24. #include "Curve.h"
  25. #include "Connect.h"
  26.  
  27.  
  28. Road::Road(SbString filename)
  29. {
  30.     // XXX determine from -metric
  31.     _distance_factor = 5280.0;
  32.  
  33.     // read the road file
  34.     
  35.     FILE *road_file = fopen(filename.getString(), "r");
  36.  
  37.     if (! road_file)
  38.     {
  39.         fprintf(stderr, "Road: Could not open road file %s\n",filename.getString());
  40.         exit(-1);
  41.     }
  42.  
  43.     int start_stretch_num;
  44.     
  45.     fscanf(road_file,"%d %d", &_num_stretches, &start_stretch_num);
  46.  
  47.     if ((_num_stretches + 1) > MAX_STRETCHES)
  48.     {
  49.         fprintf(stderr, "\nRoad: Too many stretches.\n");
  50.         exit(1);
  51.     }
  52.  
  53.     // the zeroth stretch is the null stretch
  54.     _stretch[0] = NULL;
  55.  
  56.     float width;
  57.     fscanf(road_file,"%f", &width);
  58.     
  59.     for (int i = 0; i < _num_stretches; i++)
  60.     {
  61.         int stretch_num, prev, next;
  62.         float length = 0.0, radius = 0.0, angle = 0.0, loop_offset = 0.0;
  63.         
  64.         char buf[24];
  65.         fscanf(road_file,"%d %s %d %d",&stretch_num,buf,&prev,&next);
  66.         SbString stretch_type(buf);
  67.  
  68.         if (stretch_type == "curve")
  69.         {
  70.             fscanf(road_file,"%f %f", &radius, &angle);
  71.             
  72.             _stretch[stretch_num] = 
  73.                 new Curve(_stretch[prev],_stretch[next],radius,width,angle);
  74.         }
  75.         else if (stretch_type == "straight")
  76.         {
  77.             fscanf(road_file,"%f %f", &length, &angle);
  78.             
  79.             _stretch[stretch_num] = 
  80.                 new Straight(_stretch[prev],_stretch[next],length,width,angle);
  81.         }
  82.         else if (stretch_type == "loop")
  83.         {
  84.             fscanf(road_file,"%f %f", &length, &loop_offset);
  85.             
  86.             _stretch[stretch_num] = new Straight(
  87.             _stretch[prev],_stretch[next],length,width,360.0,0.0,loop_offset);
  88.         }
  89.         else if (stretch_type == "connect")
  90.         {
  91.             fscanf(road_file,"%f %f", &length, &angle);
  92.             
  93.             _stretch[stretch_num] = 
  94.                 new Connect(_stretch[prev],_stretch[next]);
  95.         }
  96.         else
  97.         {
  98.             fprintf(stderr,
  99.                 "Road: unknown stretch type:    %s\n",stretch_type.getString());
  100.             
  101.             exit(-1);
  102.         }
  103.     }
  104.  
  105.     fclose(road_file);
  106.     
  107.     _start = _stretch[start_stretch_num];
  108. }
  109.  
  110.  
  111. Road::~Road()
  112. {
  113.     for (int i = 0; i <= _num_stretches; i++)
  114.         delete _stretch[i];
  115. }
  116.  
  117.  
  118. void Road::draw(
  119.     const Stretch *stretch, int marker, int display_mode,
  120.     Car *ignore_car)
  121. {
  122.     if (stretch)
  123.     {
  124.         stretch->init_draw(); // initialize culling
  125.         stretch->draw(marker, display_mode, ignore_car);
  126.     }
  127.     else
  128.     {
  129.         fprintf(stderr,"\nRoad: Null roadway\n");
  130.         exit(-1);
  131.     }
  132. }
  133.