home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Amiga / Jeux / demos / crystalPPC.lha / main.cpp < prev    next >
C/C++ Source or Header  |  1998-05-05  |  13KB  |  633 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #ifdef OS_DOS
  4. #  include <conio.h>
  5. #endif
  6. #include <time.h>
  7.  
  8. #ifndef DEF_H
  9. #include "def.h"
  10. #endif
  11.  
  12. #ifndef SYSTEM_H
  13. #include "system.h"
  14. #endif
  15.  
  16. #ifndef TEXTURE_H
  17. #include "texture.h"
  18. #endif
  19.  
  20. #ifndef CAMERA_H
  21. #include "camera.h"
  22. #endif
  23.  
  24. #ifndef POLYGON_H
  25. #include "polygon.h"
  26. #endif
  27.  
  28. #ifndef SECTOR_H
  29. #include "sector.h"
  30. #endif
  31.  
  32. #ifndef VIEWPOLY_H
  33. #include "viewpoly.h"
  34. #endif
  35.  
  36. #ifndef WORLD_H
  37. #include "world.h"
  38. #endif
  39.  
  40. #ifndef LIGHT_H
  41. #include "light.h"
  42. #endif
  43.  
  44. #ifndef CONFIG_H
  45. #include "config.h"
  46. #endif
  47.  
  48. #ifndef LIGHTMAP_H
  49. #include "lightmap.h"
  50. #endif
  51.  
  52. #include "global.h"
  53.  
  54. CrystConfig config ("cryst.cfg");
  55.  
  56. int fps=0;
  57. int num=0;
  58.  
  59. Camera c;
  60. ViewPolygon view (40);
  61. World world;
  62. Textures* tex;
  63. Sector* room;
  64. System *Sys;
  65.  
  66. double tb_scale_hi;
  67. double tb_scale_lo;
  68. int show_fps=0;
  69. int thetimer=0;
  70.  
  71. extern "C" int ppctimer(unsigned int clock[2]);
  72.  
  73. long I_GetTime (void)
  74. {
  75.   unsigned int clock[2];
  76.   double currtics;
  77.   static double basetics=0.0;
  78.   if (!thetimer) return time(0);
  79.   else
  80.   {
  81.   ppctimer (clock);
  82.   if (basetics == 0.0)
  83.     basetics = ((double) clock[0])*tb_scale_hi + ((double) clock[1])/tb_scale_lo;
  84.  
  85.   currtics = ((double) clock[0])*tb_scale_hi + ((double) clock[1])/tb_scale_lo;
  86.   return (int) ((currtics-basetics)/70);
  87.   }
  88. }
  89.  
  90. //vonmir
  91. //extern "C" CaptureScreen (char *, byte *);
  92. extern CaptureScreen (char *, byte *);
  93. //
  94.  
  95. void do_expose ()
  96. {
  97.   if (!Sys->Graph->Memory) return;
  98.   //Sys->Graph.Clear (0); // @@@ ONLY TO SEE IF WE DRAW EVERY PIXEL!
  99.   world.draw (Sys->Graph, &c, &view);
  100.   Sys->Graph->Print();
  101. }
  102.  
  103. void do_buttonpress (int x, int y, int shift, int alt, int ctrl)
  104. {
  105.   (void)alt;
  106.   (void)ctrl;
  107.  
  108.   if (c.edit_mode == MODE_POLYGON)
  109.     c.sel_polygon = world.select_polygon (&c, &view, x, y);
  110.   else if (c.edit_mode == MODE_VERTEX)
  111.   {
  112.     Vertex* v = world.select_vertex (&c, &view, x, y);
  113.     if (v)
  114.     {
  115.       if (shift) c.sel_vertex[c.num_sel_verts++] = v;
  116.       else
  117.       {
  118.         c.sel_vertex[0] = v;
  119.         c.num_sel_verts = 1;
  120.       }
  121.     }
  122.   }
  123. }
  124.  
  125. void do_buttonrelease (int x, int y)
  126. {
  127.   (void)x; (void)y;
  128. }
  129.  
  130. void do_mousemotion (int x, int y)
  131. {
  132.   (void)x; (void)y;
  133. }
  134.  
  135. int cnt = 1;
  136. long time0 = -1;
  137.  
  138. #include <clib/rtgmaster_protos.h>
  139. #include <rtgmaster/rtgmaster.h>
  140. #include <rtgmaster/rtgsublibs.h>
  141.  
  142. extern struct RtgScreen *MyScreen;
  143. int mhz;
  144.  
  145. void video_do_fps()
  146. {
  147.   int x;
  148.   static unsigned int start_time[2] = {0, 0};
  149.   unsigned int end_time[2];
  150.   char msg[4];
  151.  
  152.   ppctimer (end_time);
  153.   if (end_time[1] >= start_time[1])
  154.     x = (((end_time[1] - start_time[1]) << 2) / mhz);
  155.   else
  156.     x = (((end_time[1] - start_time[1]) << 2) / mhz + 1000000);
  157.   if (x != 0) {
  158.     x = (1000000 + (x >> 1)) / x;   /* round to nearest */
  159.     msg[0] = (x % 1000) / 100 + '0';
  160.     msg[1] = (x % 100) / 10 + '0';
  161.     msg[2] = (x % 10) + '0';
  162.     msg[3] = '\0';
  163.     PPCRtgText(MyScreen,PPCGetBufAdr(MyScreen,0),msg,3,FRAME_WIDTH-24,6);
  164.   }
  165.   start_time[1] = end_time[1];
  166. }
  167.  
  168. void do_stuff ()
  169. {
  170.   if (show_fps)
  171.   {
  172.    video_do_fps();
  173.   }
  174.   {
  175.    if (cnt <= 0)
  176.   {
  177.     long time1 = I_GetTime();
  178.     if (time0 != -1)
  179.     {
  180.       fps=fps+100./(float)(time1-time0);
  181.       num=num+1;
  182.       //dprintf ("Fps = %f\n", 100./(float)(time1-time0));
  183.     }
  184.     cnt = 100;
  185.     time0 = I_GetTime();
  186.   }
  187.   cnt--;
  188.   }
  189.  
  190.   world.step_scripts ();
  191.   do_expose ();
  192. }
  193.  
  194. void perf_test ()
  195. {
  196.   long t1, t2;
  197.   dprintf ("Start performance test at current position!\n");
  198.   t1 = I_GetTime();
  199.   int i;
  200.   for (i = 0 ; i < 100 ; i++)
  201.   {
  202.     world.step_scripts ();
  203.     do_expose ();
  204.   }
  205.   t2 = I_GetTime();
  206.   dprintf ("It took %ld seconds to render 100 frames: %f fps\n",
  207.         t2-t1, 100./(float)(t2-t1));
  208.   cnt = 1;
  209.   time0 = -1;
  210. }
  211.  
  212. //vonmir
  213. //extern "C" void WritePCX (char *name, char *data, byte *pal, int width,int height);
  214. extern void WritePCX (char *name, char *data, byte *pal, int width,int height);
  215. //
  216.  
  217. void CaptureScreen (void)
  218. {
  219.         int i = 0;
  220.         char name[255];
  221.         byte pall[768], *pal = pall;
  222.  
  223. //vonmir
  224. //        do {
  225. //                sprintf (name, "cryst%02d.pcx", i++);
  226. //        } while (i < 100
  227. //#ifndef COMP_WCC
  228. //        && (access (name, 0) == 0)
  229. //#endif
  230. //                        );
  231.  
  232. //ersatzlos gestrichen
  233. //
  234.  
  235.         if ( i>= 100) return;
  236.         for (i=0; i<256; i++)
  237.         {
  238.                 *pal++ = graphicsPalette[i].red;
  239.                 *pal++ = graphicsPalette[i].green;
  240.                 *pal++ = graphicsPalette[i].blue;
  241.         }
  242.         WritePCX (name, graphicsData, pall, FRAME_WIDTH, FRAME_HEIGHT);
  243.         dprintf ("Screenshot: %s", name);
  244. }
  245.  
  246. char* edit_modes[] = { "NONE", "EDIT", "VERTEX", "POLYGON", "MOVE_VERTEX" };
  247.  
  248. void do_keypress (int key, int shift, int alt, int ctrl)
  249. {
  250.   Vector3* v;
  251.  
  252.   switch (key)
  253.   {
  254.     case 'b':
  255.       c.turn_around ();
  256.       break;
  257.     case KEY_UP:
  258.       if (alt)
  259.       {
  260.         if (ctrl) c.up (.01);
  261.         else if (shift) c.up (.4);
  262.         else c.up (.2);
  263.       }
  264.       else if (ctrl) c.forward (.01);
  265.       else if (shift) c.forward (.4);
  266.       else c.forward (.2);
  267.       break;
  268.     case KEY_DOWN:
  269.       if (alt)
  270.       {
  271.         if (ctrl) c.down (.01);
  272.         else if (shift) c.down (.4);
  273.         else c.down (.2);
  274.       }
  275.       else if (ctrl) c.backward (.01);
  276.       else if (shift) c.backward (.4);
  277.       else c.backward (.2);
  278.       break;
  279.     case KEY_LEFT:
  280.       if (alt)
  281.       {
  282.         if (ctrl) c.left (.01);
  283.         else if (shift) c.left (.4);
  284.         else c.left (.2);
  285.       }
  286.       else if (ctrl) c.rot_left (.005);
  287.       else if (shift) c.rot_left (.2);
  288.       else c.rot_left (.1);
  289.       break;
  290.     case KEY_RIGHT:
  291.       if (alt)
  292.       {
  293.         if (ctrl) c.right (.01);
  294.         else if (shift) c.right (.4);
  295.         else c.right (.2);
  296.       }
  297.       else if (ctrl) c.rot_right (.005);
  298.       else if (shift) c.rot_right (.2);
  299.       else c.rot_right (.1);
  300.       break;
  301.     case KEY_PGUP:
  302.       if (alt)
  303.       {
  304.         if (ctrl) c.rot_z_left (.005);
  305.         else c.rot_z_left (.1);
  306.       }
  307.       else if (ctrl) c.rot_up (.005);
  308.       else if (shift) c.rot_up (.2);
  309.       else c.rot_up (.1);
  310.       break;
  311.     case KEY_PGDN:
  312.       if (alt)
  313.       {
  314.         if (ctrl) c.rot_z_right (.005);
  315.         else c.rot_z_right (.1);
  316.       }
  317.       else if (ctrl) c.rot_down (.005);
  318.       else if (shift) c.rot_down (.2);
  319.       else c.rot_down (.1);
  320.       break;
  321.     case ' ':
  322.       world.trigger_activate (c);
  323.       break;
  324.     case 'l':
  325.       tex->do_lighting = !tex->do_lighting;
  326.       dprintf ("Lighting '%s'.\n", tex->do_lighting ? "on" : "off");
  327. #if 0
  328.       {
  329.         Vector3 pos;
  330.         c.get_forward_position (6, pos);
  331.         c.sector->add_dyn_light (pos.x, pos.y, pos.z, 7, 1, 0, 0);
  332.       }
  333. #endif
  334.       break;
  335.     case 'a':
  336.         CaptureScreen ();
  337.         break;
  338.     case 'e':
  339.       if (c.edit_mode == MODE_NONE) c.edit_mode = MODE_EDIT;
  340.       else c.edit_mode = MODE_NONE;
  341.       dprintf ("Current mode: %s\n", edit_modes[c.edit_mode]);
  342.       break;
  343.     case 'm':
  344.       if (c.edit_mode == MODE_NONE) break;
  345.       if (c.edit_mode == MODE_VERTEX) c.edit_mode = MODE_POLYGON;
  346.       else if (c.edit_mode == MODE_POLYGON) c.edit_mode = MODE_EDIT;
  347.       else c.edit_mode = MODE_VERTEX;
  348.       dprintf ("Current mode: %s\n", edit_modes[c.edit_mode]);
  349.       break;
  350.     case 'v':
  351.       if (c.edit_mode == MODE_NONE) break;
  352.       if (c.edit_mode == MODE_MOVE_VERTEX) c.edit_mode = MODE_EDIT;
  353.       else c.edit_mode = MODE_MOVE_VERTEX;
  354.       dprintf ("Current mode: %s\n", edit_modes[c.edit_mode]);
  355.       break;
  356.     case 'x':
  357.       if (c.edit_mode != MODE_MOVE_VERTEX && !c.sel_vertex[0]) break;
  358.       v = &c.sel_vertex[0]->get_ov ();
  359.       if (shift) v->x -= .05;
  360.       else v->x += .05;
  361.       c.sel_vertex[0]->set (*v);
  362.       break;
  363.     case 'y':
  364.       if (c.edit_mode != MODE_MOVE_VERTEX && !c.sel_vertex[0]) break;
  365.       v = &c.sel_vertex[0]->get_ov ();
  366.       if (shift) v->y -= .05;
  367.       else v->y += .05;
  368.       c.sel_vertex[0]->set (*v);
  369.       break;
  370.     case 'z':
  371.       if (c.edit_mode != MODE_MOVE_VERTEX && !c.sel_vertex[0]) break;
  372.       v = &c.sel_vertex[0]->get_ov ();
  373.       if (shift) v->z -= .05;
  374.       else v->z += .05;
  375.       c.sel_vertex[0]->set (*v);
  376.       break;
  377.     case 'c':
  378.       dprintf ("==============================================================\n");
  379.       if (shift)
  380.       {
  381.         printf ("SAVE COORDS\n");
  382.         c.save_file ("coord");
  383.       }
  384.       else
  385.       {
  386.         dprintf ("LOAD COORDS\n");
  387.         c.load_file (&world, "coord");
  388.       }
  389.       dprintf ("==============================================================\n");
  390.       break;
  391.     case 's':
  392.       if (alt)
  393.       {
  394.         dprintf ("SAVE LEVEL\n");
  395.         world.save ("world");
  396.         dprintf ("DONE!\n");
  397.       }
  398.       else if (shift)
  399.       {
  400.         dprintf ("LOAD LEVEL\n");
  401.         world.load ("world");
  402.         dprintf ("DONE!\n");
  403.         tex = world.get_textures ();
  404.         tex->alloc_palette (Sys->Graph);
  405.         room = world.get_sector ("room");
  406.  
  407.         c.m_world2cam.identity ();
  408.         c.v_world2cam.x = 0;
  409.         c.v_world2cam.y = 0;
  410.         c.v_world2cam.z = 0;
  411.         c.sector = room;
  412.       }
  413.       else
  414.       {
  415.         if (c.edit_mode == MODE_NONE) break;
  416.         dprintf ("Split poly\n");
  417.         world.edit_split_poly (&c);
  418.       }
  419.       break;
  420.     case 'q':
  421.         Sys->Shutdown = 1;
  422.         break;
  423.     case 't':
  424.       if (shift)
  425.       {
  426.         switch (tex->mipmapped)
  427.         {
  428.           case 0:
  429.             tex->mipmapped = 1;
  430.             dprintf ("Mipmapping 'on'\n");
  431.             break;
  432.           case 1:
  433.             tex->mipmapped = 2;
  434.             dprintf ("Mipmapping 'always'\n");
  435.             break;
  436.           case 2:
  437.             tex->mipmapped = 0;
  438.             dprintf ("Mipmapping 'off'\n");
  439.             break;
  440.         }
  441.       }
  442.       else if (alt)
  443.       {
  444.         switch (LightMap::setting)
  445.         {
  446.           case MIPMAP_SHADOW_ACCURATE:
  447.             LightMap::setting = MIPMAP_SHADOW_REASONABLE;
  448.             dprintf ("Shadow-mipmapping 'reasonable'\n");
  449.             break;
  450.           case MIPMAP_SHADOW_REASONABLE:
  451.             LightMap::setting = MIPMAP_SHADOW_INACCURATE;
  452.             dprintf ("Shadow-mipmapping 'inaccurate'\n");
  453.             break;
  454.           case MIPMAP_SHADOW_INACCURATE:
  455.             LightMap::setting = MIPMAP_SHADOW_ACCURATE;
  456.             dprintf ("Shadow-mipmapping 'accurate'\n");
  457.             break;
  458.         }
  459.         world.mipmap_settings (LightMap::setting);
  460.       }
  461.       else
  462.       {
  463.         tex->textured = !tex->textured;
  464.         dprintf ("Texture mapping '%s'.\n", tex->textured ? "on" : "off");
  465.       }
  466.       break;
  467.     case 'i':
  468.       cache.dump ();
  469.       break;
  470.     case 'r':
  471.       dprintf ("Refresh (clear) the texture cache.\n");
  472.       cache.clear ();
  473.       break;
  474.     case 'p':
  475.       perf_test ();
  476.       break;
  477.     default:
  478.       break;
  479.   }
  480. }
  481.  
  482.  
  483. /*
  484.  * Our main event loop.
  485.  */
  486. void main_loop ()
  487. {
  488.   //render_time = time (NULL);
  489.   Sys->Loop();
  490.   if (num>0) printf("fps = %i\n",fps/num);
  491. }
  492.  
  493. int func1 (float x)
  494. {
  495.   return QRound (x);
  496. }
  497.  
  498. int func2 (float x)
  499. {
  500.   return (int)(x+.5);
  501. }
  502.  
  503. int test (float par)
  504. {
  505.   time_t s1, s2;
  506.   int i;
  507.   int rc1 = 1, rc2 = 1;
  508.  
  509.   printf ("START (1)\n");
  510.   s1 = I_GetTime();
  511.   for (i = 0 ; i < 10000000 ; i++)
  512.   {
  513.     rc1 = func1 (par);
  514.   }
  515.   s2 = I_GetTime();
  516.   printf ("STOP: %ld\n", s2-s1);
  517.  
  518.   printf ("START (2)\n");
  519.   s1 = I_GetTime();
  520.   for (i = 0 ; i < 10000000 ; i++)
  521.   {
  522.     rc2 = func2 (par);
  523.   }
  524.   s2 = I_GetTime();
  525.   printf ("STOP: %ld\n", s2-s1);
  526.  
  527.   printf ("rc1 = %d, rc2 = %d\n", rc1, rc2);
  528.  
  529.   return 0;
  530. }
  531.  
  532. /*
  533.  * Main function
  534.  */
  535.  
  536. int mmu=0;
  537.  
  538.  
  539. int main (int argc, char* argv[])
  540. {
  541. #if 0
  542.   // This construction enables short-precision mode in the pentium
  543.   // processor. I read somewhere that this should be faster. As
  544.   // this doesn't seem to give a speed increase in Linux I don't enable it
  545.   // yet. Maybe later when I test it in DOS?
  546.   int OldFPUCW, FPUCW;
  547.   asm volatile ("\
  548.         fstcw (%0)              \n\
  549.         movl (%0),%%eax         \n\
  550.         andl $0xfcff,%%eax      \n\
  551.         movl %%eax,(%1)         \n\
  552.         fldcw (%1)              "
  553.         :
  554.         : "g" (&OldFPUCW), "g" (&FPUCW)
  555.         : "eax");
  556. #endif
  557.   int f;
  558.   show_fps=0;
  559.   mhz=40;
  560.   if (argc>1)
  561.   {
  562.    for (f=1;f<=argc-1;f++)
  563.    {
  564.     if (strstr(argv[f],"-fps"))
  565.     {
  566.      printf("fps-Counter enabled.\n");
  567.      show_fps=1;
  568.     }
  569.     if (strstr(argv[f],"-bus"))
  570.     {
  571.      if (argc-1>f)
  572.      {
  573.       mhz=atoi(argv[f+1]);
  574.       printf("Bus Clock: %i\n",mhz);
  575.      }
  576.     }
  577.     else if (strstr(argv[f],"-mmu"))
  578.     {
  579.      printf("MMU used.\n");
  580.      mmu=1;
  581.     }
  582.     else if (strstr(argv[f],"-thetimer"))
  583.     {
  584.      printf("PPC Timer used.\n");
  585.      thetimer=1;
  586.     }
  587.    }
  588.   }
  589.   tb_scale_lo = ((double)(mhz*1000000 >> 2)) / 35.0;
  590.   tb_scale_hi = (4.294967296E9 / (double)(mhz*1000000 >> 2)) * 35.0;
  591.  
  592.   extern int test (float);
  593.   //test (3.1415926);
  594.  
  595.   Matrix3::init ();
  596.  
  597.   Sys = new System (argc, argv);
  598.  
  599.   world.load ("world");
  600.   tex = world.get_textures ();
  601.   room = world.get_sector ("room");
  602.  
  603.   c.m_world2cam.identity ();
  604.   c.v_world2cam.x = 0;
  605.   c.v_world2cam.y = 0;
  606.   c.v_world2cam.z = 0;
  607.   c.sector = room;
  608.  
  609.   view.add_vertex (3, FRAME_HEIGHT-3);
  610.   view.add_vertex (FRAME_WIDTH-3, FRAME_HEIGHT-3);
  611.   view.add_vertex (FRAME_WIDTH-3, 3);
  612.   view.add_vertex (3, 3);
  613.  
  614.   printf ("------------------------------------------------------\n");
  615.  
  616.   Sys->Open();
  617.  
  618.   tex->alloc_palette (Sys->Graph);
  619.   main_loop ();
  620.  
  621.   Sys->Close();
  622.   return (1);
  623. }
  624.  
  625. #include <wbstartup.h>
  626.  
  627. void wbmain(struct WBStartup * argmsg)
  628. {
  629.  char argv[1][]={"CrystalPPC"};
  630.  int argc=1;
  631.  main(argc,(char **)argv);
  632. }
  633.