home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Madness / VRMAD96_ONE.ISO / virtek / examples / example.c next >
C/C++ Source or Header  |  1995-08-24  |  9KB  |  298 lines

  1.  
  2. // ┌─────────────────────────────────────────────────────────────┐
  3. // │                                                             │
  4. // │ Filename:       explore.c                                   │
  5. // │ Author:         C.K. Donnison                               │
  6. // │                               <charles@virtek.com>          │
  7. // │ Position:       Senior programmer                           │
  8. // │                                                             │
  9. // │ Creation date:  3rd June 1994                               │
  10. // │                                                             │
  11. // │ Compiled using: Borland C++ V3.1                            │
  12. // │                                                             │
  13. // │ Tested by:      CKD, PBA, NAJ                               │
  14. // │                                                             │
  15. // │                                                             │
  16. // │ Purpose:        A small program to load a 3D-Ware           │
  17. // │                 world file and use a mouse to control       │
  18. // │                 flight throughout the world.                │
  19. // │                                                             │
  20. // └─────────────────────────────────────────────────────────────┘
  21.  
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <dos.h>
  25. #include <dir.h>
  26.  
  27. #include "..\3d-ware\dddware.h"                                  // The important bit :-)
  28.  
  29. struct ffblk fileinfo;
  30. char shape_name[14];
  31. char currpal[14];
  32. FILE *file_ptr;
  33. char tmp[128];
  34. char horitoggle=0;
  35. float version=1.0;
  36. float fileversion=0.0;
  37. int no_of_shapes=0;
  38. long int distance=16384;
  39. long  heading = 0;
  40. short response = 19;
  41. int flyspeed=0;
  42. long int pos[3];
  43. int ang[3];
  44. int ith=0;
  45.  
  46. void  FlyingControls(void);
  47. void  FlyAround(void);
  48. void  Control(void);
  49. short LoadWorld(char *path);
  50.  
  51. typedef struct worldshape worldshape;
  52. struct worldshape
  53.    {
  54.    long x,y,z;
  55.    short ax,ay,az;
  56.    unsigned short handle;
  57.    };
  58. worldshape far wshape[2500];
  59.  
  60.  
  61. int main(int argc,char *argv[])
  62. {
  63. if(argc<2)
  64.    {
  65.    printf("(C) 1994 VIRTEK INTERNATIONAL CORP.\n");              // Say hello.
  66.    printf("USAGE: EXAMPLE <filename.WLD>\n");                    // Display usage.
  67.    }
  68. else
  69.    {
  70.    dddInitAll();                                                 // Initialise 3D-Ware.
  71.    if(LoadWorld(argv[1])==-1)                                    // Try to load the world.
  72.       {
  73.       dddRestoreAll();                                           // Hmm. Can't load so exit.
  74.       return(-1);                                                // Bye.
  75.       }
  76.    FlyAround();                                                  // Explore the world.
  77.    dddRestoreAll();                                              // Clean up.
  78.    }
  79. return(0);                                                       // Bye.
  80. }
  81.  
  82.  
  83. // ┌─────────────────────────────────────────────────────┐
  84. // │ LoadWorld                                           │
  85. // │          This loads in an existing world file.      │
  86. // │                                                     │
  87. // └─────────────────────────────────────────────────────┘
  88.  
  89. short LoadWorld(char *path)
  90. {
  91.    if(findfirst(path,&fileinfo,0)!=0)
  92.    {
  93.       return(-1);
  94.    }
  95.    if((file_ptr=fopen(path,"rt"))==NULL)
  96.       {
  97.       return(-1);
  98.       }
  99.    fseek(file_ptr,0,SEEK_SET);
  100.    fscanf(file_ptr,"%s%s%s%f",&tmp,&tmp,&tmp,&fileversion);
  101.    if(fileversion!=version)
  102.       {
  103.          return(-1);
  104.       }
  105.    fscanf(file_ptr,"%s",&tmp);
  106.    fscanf(file_ptr,"%s",&tmp);
  107.    fscanf(file_ptr,"%s",&tmp);
  108.    fscanf(file_ptr,"%s",&tmp);
  109.    fscanf(file_ptr,"%s",&tmp);
  110.    fscanf(file_ptr,"%s",&tmp);
  111.    fscanf(file_ptr,"%s",&tmp);
  112.    fscanf(file_ptr,"%s%d",&tmp,&horitoggle);
  113.    fscanf(file_ptr,"%s%s",&tmp,&tmp);
  114.    fscanf(file_ptr,"%s",&tmp);
  115.    if((stricmp(tmp,"DEFAULT")!=0)&&(tmp))
  116.    {
  117.       strcpy(currpal,tmp);
  118.       dddLoadColors(currpal);                                    // Load in the requested palette.
  119.    }
  120.    else
  121.    {
  122.       strset(currpal,NULL);                                      // Clear palette to black.
  123.    }
  124.    dddSetPal(dddstandardpal);                                    // Initialise the palette registers.
  125.  
  126.    fscanf(file_ptr,"%s",&tmp);
  127.    fscanf(file_ptr,"%s",&tmp);
  128.    fscanf(file_ptr,"%s",&tmp);
  129.    fscanf(file_ptr,"%s",&tmp);
  130.    fscanf(file_ptr,"%s",&tmp);
  131.    fscanf(file_ptr,"%s",&tmp);
  132.    fscanf(file_ptr,"%s",&tmp);
  133.    fscanf(file_ptr,"%s",&tmp);
  134.    fscanf(file_ptr,"%s",shape_name);
  135.    while((!(feof(file_ptr))>0)&&(ith<2500))
  136.    {
  137.       fscanf(file_ptr,"%Fld",&wshape[ith].x);
  138.       fscanf(file_ptr,"%Fld",&wshape[ith].y);
  139.       fscanf(file_ptr,"%Fld",&wshape[ith].z);
  140.       fscanf(file_ptr,"%Fld",&wshape[ith].ax);
  141.       fscanf(file_ptr,"%Fld",&wshape[ith].ay);
  142.       fscanf(file_ptr,"%Fld",&wshape[ith].az);
  143.       wshape[ith].handle = dddShapeLoad(shape_name);             // Load the geometry file.
  144.       ith++;
  145.       fscanf(file_ptr,"%s",shape_name);
  146.       if(stricmp(shape_name,"FLAT")==0)
  147.          fscanf(file_ptr,"%s",shape_name);
  148.    }
  149.    no_of_shapes=ith;
  150.    fclose(file_ptr);
  151.    return(0);
  152. }
  153.  
  154.  
  155.  
  156. void FlyAround(void)
  157. {
  158. short i=0;
  159.  
  160.    dddceiling =- 32000;                                          // Really only needs to be done once.
  161.    dddfloor = -100;                                              // Really only needs to be done once.
  162.    dddmouse_x = 160;
  163.    dddmouse_y = 100;
  164.  
  165.    dddHideGraphicsMouse();                                       // Do not display the mouse pointer.
  166.    while(dddkeycode!=_ESC)
  167.    {
  168.       dddClearObjects();                                         // Clear the internal object list.
  169.       dddReadMousePosition();                                    // Does what it says.
  170.       if(horitoggle==0)
  171.       {
  172.          dddBox(0,0,0,319,199);                                  // Hmmm  could use dddCls();
  173.       }
  174.       else if(horitoggle==1)
  175.       {
  176.          dddHorizon(ang[0],ang[2]);                              // Standard Horizon.
  177.       }
  178.       else 
  179.       {
  180.          dddHoriFade(ang[0],ang[2]);                             // Banded horizon
  181.       }
  182.       i=0;
  183.       while((i<no_of_shapes))
  184.       {
  185.          if(((pos[0]-wshape[i].x)<distance)&&((pos[0]-wshape[i].x)>-distance)&&
  186.          ((pos[1]-wshape[i].y)<distance)&&((pos[1]-wshape[i].y)>-distance)&&
  187.          ((pos[2]-wshape[i].z)<distance)&&((pos[2]-wshape[i].z)>-distance))
  188.          {
  189.             dddInsertObject16(DRW_MOBILE,wshape[i].handle,255,wshape[i].x,wshape[i].y,wshape[i].z,wshape[i].ax,wshape[i].ay,wshape[i].az);
  190.          }
  191.          i++;
  192.       }
  193.       dddDrawObjects();                                          // Draw all the inserted shapes.
  194.       FlyingControls();                                          // Handle the mouse input.
  195.       dddScreenSwap();                                           // Make the rendered screen visible.
  196.    }
  197. }
  198.  
  199.  
  200. void FlyingControls(void)
  201. {
  202.    if((dddlbutton)||(dddkeypressed[_SPACE]))
  203.    {
  204.       if(flyspeed<400)
  205.       {
  206.          flyspeed+=10;
  207.       }
  208.       else if(flyspeed<800)
  209.       {
  210.          flyspeed+=30;
  211.       }
  212.    }
  213.    else if(flyspeed>400)
  214.    {
  215.       flyspeed-=10;
  216.    }
  217.    else if(flyspeed>0)
  218.    {
  219.       flyspeed-=3;
  220.    }
  221.    else if(flyspeed<0)
  222.    {
  223.       flyspeed=0;
  224.    }
  225.    Control();
  226.    dddProjectForward(pos,ang,flyspeed);
  227.    dddSetViewAngles(ang[0],ang[1],ang[2]);
  228.    dddSetViewPos16(pos[0],pos[1],pos[2]);                        // 16 bit co-ordinates.
  229. }
  230.  
  231.  
  232. void Control(void)
  233. {
  234. #define KEYBOARD_RESPONSE   16
  235. long dx;
  236. int long anx,any,anz;
  237. int key_response;
  238.  
  239.    key_response = KEYBOARD_RESPONSE;
  240.    if(dddkeypressed[_L_SHIFT])
  241.    {
  242.       key_response += key_response;
  243.    }
  244.    if(dddkeypressed[_LF_ARROW])
  245.    {
  246.       if( dddmouse_x >0)
  247.       {
  248.          dddmouse_x -= key_response;
  249.       }
  250.       if( dddmouse_x < 0)
  251.       {
  252.          dddmouse_x = 0;
  253.       }
  254.    }
  255.    if(dddkeypressed[_RT_ARROW])
  256.    {
  257.       if( dddmouse_x < 319)
  258.       {
  259.          dddmouse_x += key_response;
  260.       }
  261.       if( dddmouse_x > 319)
  262.       {
  263.          dddmouse_x = 319;
  264.       }
  265.    }
  266.    if(dddkeypressed[_UP_ARROW])
  267.    {
  268.       if( dddmouse_y >0)
  269.       {
  270.          dddmouse_y -= key_response;
  271.       }
  272.       if( dddmouse_y < 0)
  273.       {
  274.          dddmouse_y = 0;
  275.       }
  276.    }
  277.    if(dddkeypressed[_DN_ARROW])
  278.    {
  279.       if( dddmouse_y < 199)
  280.       {
  281.          dddmouse_y += key_response;
  282.       }
  283.       if( dddmouse_y > 199)
  284.       {
  285.          dddmouse_y = 199;
  286.       }
  287.    }
  288.    anz = ((dddmouse_x-160)*3) << 4;
  289.    anx = ((dddmouse_y-100)*4)<<4;
  290.    dx = ((((long)dddmouse_x - 160L) ) << response);
  291.    heading = heading - dx;
  292.    any = (heading >> 16L);
  293.  
  294.    ang[0]=-(int)anx;
  295.    ang[1]=-(int)any;
  296.    ang[2]=(int)anz;
  297. }
  298.