home *** CD-ROM | disk | FTP | other *** search
/ Collection of Hack-Phreak Scene Programs / cleanhpvac.zip / cleanhpvac / ROADS15.ARJ / ROADS.C < prev    next >
C/C++ Source or Header  |  1993-12-27  |  9KB  |  331 lines

  1. #define ROADS_C
  2.  
  3. #include <fastgraf.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <time.h> /* FOR RANDOM */
  7. #include "roads.h"
  8. #include "tiles.h"    /* DUE TO R AND C CHEATS        */
  9. #include "fx.h"       /* FOR FADING STUFF             */
  10. #include "version.h"  /* INFO ON THIS VERSION         */
  11. #include "keys.h"     /* KEY AND SCANCODE DEFINITIONS */
  12.  
  13. extern int far *topography;      /* BACKGROUND TILE LIST (ARRAY) */
  14. extern int far *terrain;      /* FOREGROUND TILE LIST (ARRAY) */
  15. extern int view_x, view_y;     /* VIEW AREA (UPPER LEFT CORNER) */
  16. extern int viewpage;           /* CURRENTLY VIEWED PAGE */
  17. extern int world_type;         /* TENDENCY TO GRASS */
  18.  
  19. extern int edgemode;           /* BLOCKY GRASS/DIRT OR EDGED? */
  20. extern int animatemode;
  21. extern int frogmode;
  22. extern int frogwatchmode;
  23.  
  24. int keyboardmode=0;
  25.  
  26. /* PROTOTYPES FOR INTERNAL FUNCTIONS */
  27. void time_test (void);
  28. void cheat (int type);
  29. void toggle_mode (int type);
  30. void make_world (int type);
  31. void view_tile_page (void);
  32. void move_view (void);
  33. int keycheck (void);
  34. void init_all (void);
  35. void gogofrog (void);
  36.  
  37. #pragma argsused
  38. void main (int argc, char *argv[])
  39. {
  40.     char quitting_time=0;       /* QUIT PROGRAM LOOP   */
  41.  
  42.     printf (HEADER);
  43.  
  44.     if (argc>1)
  45.     {
  46.         printf (KEY_HELP);
  47.         exit (2);
  48.     }
  49.  
  50.     printf ("Loading ... [Escape quits] ... [Type ROADS /? for more keys!]\n");
  51.     init_all(); /* INITIALIZE ALL SYSTEMS */
  52.  
  53.     while (!quitting_time) /* LOOP FOREVER */
  54.     {
  55.         quitting_time=keycheck(); /* CHECK FOR REGULAR KEYS */
  56.         if (animatemode) animate(); /* PERFORM ALL ANIMATIONS */
  57.     }
  58.  
  59.     program_shutdown("Thank you for running ROADS!", 0);
  60. }
  61.  
  62. #define TIMETEST_LENGTH 10 /* TIME TEST LENGTH IN SECONDS */
  63.  
  64. /*
  65.  *
  66.  * Performs time testing to try and guess a FPS.
  67.  *
  68.  */
  69. void time_test (void)
  70. {
  71.     int x, dir;
  72.     long end_time;
  73.     int frames_shown[2];
  74.  
  75.     for (x=0; x<2; x++) /* TEST TWICE, ONCE WITH ANIMATION */
  76.     {
  77.         while (redraw(SCROLL_UL)); /* SCROLL UPPER LEFT TO START */
  78.         fg_music ("L64FAC.AE.B$");
  79.         frames_shown[x]=0; dir=0;
  80.         end_time=TIMETEST_LENGTH*182/10;
  81.         end_time+=fg_getclock();
  82.  
  83.         while (fg_getclock()<end_time)
  84.         {
  85.             frames_shown[x]++;
  86.  
  87.             switch (dir)
  88.             {
  89.                 case 0: if (!redraw (SCROLL_DR   )) dir++; break;
  90.                 case 1: if (!redraw (SCROLL_UP   )) dir++; break;
  91.                 case 2: if (!redraw (SCROLL_DL   )) dir++; break;
  92.                 case 3: if (!redraw (SCROLL_RIGHT)) dir++; break;
  93.                 case 4: if (!redraw (SCROLL_UL   )) dir++; break;
  94.                 case 5: if (!redraw (SCROLL_DOWN )) dir++; break;
  95.                 case 6: if (!redraw (SCROLL_UR   )) dir++; break;
  96.                 case 7: if (!redraw (SCROLL_LEFT )) dir=0; break;
  97.             }
  98.  
  99.             if (x==0) animate(); /* ANIMATION ON FIRST TEST ONLY */
  100.         }
  101.     }
  102.  
  103.     program_shutdown ("",-1); /* DON'T EXIT YET */
  104.     printf ("%d Frames in %i seconds (%i FPS) with animation\n",
  105.         frames_shown[0], TIMETEST_LENGTH, frames_shown[0]/TIMETEST_LENGTH);
  106.     printf ("%d Frames in %i seconds (%i FPS) without animation\n",
  107.         frames_shown[1], TIMETEST_LENGTH, frames_shown[1]/TIMETEST_LENGTH);
  108.     exit (0);
  109. }
  110.  
  111. /*
  112.  *
  113.  * Turns on cheats (fills screen with anims for testing)
  114.  *
  115.  */
  116. void cheat (int type)
  117. {
  118.     register int x;
  119.  
  120.     if (type==0)
  121.     {
  122.         fade_out_all();
  123.         for (x=0; x<WORLD_TILES_TOTAL; x++)
  124.             terrain[x]=ANM_FIRE;
  125.         redraw(REFRESH);
  126.         animatewhilefading=0;
  127.         fade_in_all();
  128.         animatewhilefading=1;
  129.     }
  130.  
  131.     else if (type==1)
  132.     {
  133.         fade_out_all();
  134.         for (x=0; x<WORLD_TILES_TOTAL; x++)
  135.             if (!isroad(terrain[x])) terrain[x]=ANM_FIRE;
  136.         redraw(REFRESH);
  137.         animatewhilefading=0; /* DON'T ANIMATE IN FADES -- TOO MANY ANIMS! */
  138.         fade_in_all();
  139.         animatewhilefading=1;
  140.     }
  141. }
  142.  
  143. /*
  144.  *
  145.  * Toggles modes on and off (animation, edging, etc.)
  146.  *
  147.  */
  148. void toggle_mode (int type)
  149. {
  150.     if (type==0)
  151.     {
  152.         animatemode=!animatemode;
  153.         fg_music ("L64G.AG.A$");
  154.     }
  155.  
  156.     else if (type==1)
  157.     {
  158.         edgemode=!edgemode;
  159.         fg_music ("S1L20B..G..F..$");
  160.         if (edgemode)
  161.         {
  162.             add_dirt_edges();
  163.             redraw(REFRESH);
  164.         }
  165.     }
  166.     else if (type==2)
  167.     {
  168.         keyboardmode=!keyboardmode;
  169.         fg_music ("L40BABAGFG.$");
  170.     }
  171.     else if (type==3)
  172.     {
  173.         frogwatchmode=!frogwatchmode;
  174.         fg_music ("O1L30D.ED.A$");
  175.     }
  176. }
  177.  
  178. /*
  179.  *
  180.  * Initializes the world, foreground and/or background.
  181.  *
  182.  */
  183. void make_world (int type)
  184. {
  185.     if (type==0)
  186.     {
  187.         fade_out_all();
  188.         init_world();
  189.         redraw(REFRESH);
  190.         fade_in_all();
  191.     }
  192.  
  193.     else if (type==1)
  194.     {
  195.         init_background();
  196.         redraw(REFRESH);
  197.     }
  198.  
  199.     else if (type==2)
  200.     {
  201.         init_foreground();
  202.         redraw(REFRESH);
  203.     }
  204. }
  205.  
  206. /*
  207.  *
  208.  * Shows the contents of the tile page.  Hit any key to fade back.
  209.  *
  210.  */
  211. void view_tile_page (void)
  212. {
  213.     char key1, key2;
  214.  
  215.     fade_out_all();
  216.     fg_setvpage (TILEPAGE);
  217.     fade_in_all();
  218.  
  219.     fg_kbinit(0);
  220.  
  221.     do {
  222.       fg_intkey (&key1, &key2);
  223.       animate();
  224.     } while (!(key1+key2)); /* DO LOOP WHILE KEYS ARE NOT HIT */
  225.  
  226.     fg_kbinit(1);
  227.  
  228.     fade_out_all();
  229.     fg_setvpage (viewpage);
  230.     fade_in_all();
  231. }
  232.  
  233. /*
  234.  *
  235.  * Scans for arrow keys and scrolls the view area in reaction to them.
  236.  *
  237.  */
  238. void move_view (void)
  239. {
  240.     static char left=0, right=0, up=0, down=0; /* KEYBOARD VARS       */
  241.  
  242.         /* CHECK FOR ARROWS BEING PRESSED */
  243.     if (fg_kbtest(SCAN_RIGHT))      right++; else right=0;
  244.     if (fg_kbtest(SCAN_LEFT))       left++;  else left=0;
  245.     if (fg_kbtest(SCAN_UP))         up++;    else up=0;
  246.     if (fg_kbtest(SCAN_DOWN))       down++;  else down=0;
  247.  
  248.         /* MAKE SURE COUNTERS DON'T GO TOO HIGH */
  249.     if (right>100)  right=100;
  250.     if (left>100)   left=100;
  251.     if (up>100)     up=100;
  252.     if (down>100)   down=100;
  253.  
  254.         /* IF "TAP" KEYBOARD MODE IS ON, DON'T MOVE UNTIL KEYS RELEASED */
  255.     if (keyboardmode && (right>1 || left>1 || up>1 || down>1)) return;
  256.  
  257.         /* MOVE, CHECKING FOR DIAGONAL MOVEMENT FIRST */
  258.     if (up && right)        redraw (SCROLL_UR);
  259.     else if (down && left)  redraw (SCROLL_DL);
  260.     else if (up && left)    redraw (SCROLL_UL);
  261.     else if (down && right) redraw (SCROLL_DR);
  262.     else if (right)         redraw (SCROLL_RIGHT);
  263.     else if (left)          redraw (SCROLL_LEFT);
  264.     else if (up)            redraw (SCROLL_UP);
  265.     else if (down)          redraw (SCROLL_DOWN);
  266. }
  267.  
  268. /*
  269.  *
  270.  * Initializes all systems and brings display up.
  271.  *
  272.  */
  273. void init_all (void)
  274. {
  275.     fg_kbinit(1);       /* LATCH LOW-LEVEL KEYBOARD HANDLER */
  276.     randomize();        /* ALLOW RANDOMIZATIONS             */
  277.  
  278.     init_anim();        /* CALL BEFORE WORLD CREATION       */
  279.     init_data();        /* CALL BEFORE WORLD CREATION       */
  280.     init_world();       /* RANDOMIZE THE WORLD              */
  281.     init_video();       /* SET OUR VIDEO MODE ETC.          */
  282.  
  283.     fade_init();        /* ALLOW FADING                     */
  284.     fade_blackout();    /* SET ALL COLORS TO BLACK          */
  285.     redraw(REFRESH);    /* DRAW THE SCREEN (UNSEEN)         */
  286.     fade_in_all();      /* FADE IN SCREEN                   */
  287. }
  288.  
  289. /*
  290.  *
  291.  * Keycheck checks all keys and reacts upon them.
  292.  * Returns 1 if a key has indicated the user has requested to quit.
  293.  *
  294.  */
  295. int keycheck (void)
  296. {
  297.     if (fg_kbtest(SCAN_T)) time_test();
  298.     if (fg_kbtest(SCAN_C)) cheat (0);
  299.     if (fg_kbtest(SCAN_R)) cheat (1);
  300.     if (fg_kbtest(SCAN_A)) toggle_mode(0);
  301.     if (fg_kbtest(SCAN_E)) toggle_mode(1);
  302.     if (fg_kbtest(SCAN_K)) toggle_mode(2);
  303.     if (fg_kbtest(SCAN_W)) toggle_mode(3);
  304.     if (fg_kbtest(SCAN_F))
  305.     {
  306.         fg_music ("L50O4BAFDEF.$");
  307.         switch (frogmode)
  308.         {
  309.             case 1: frogmode=2; break;
  310.             case 3: frogmode=0; break;
  311.         }
  312.     }
  313.     if (fg_kbtest(SCAN_G)) /* RERANDOMIZE GRASS/DIRT TENDENCY */
  314.     {
  315.         world_type=random(100);
  316.         fg_music ("S1L20C..B..A..$");
  317.     }
  318.     if (fg_kbtest(SCAN_SPACE))  make_world (0);
  319.     if (fg_kbtest(SCAN_B))      make_world (1);
  320.     if (fg_kbtest(SCAN_ENTER))  make_world (2);
  321.     if (fg_kbtest(SCAN_S))      view_tile_page();
  322.  
  323.     move_view(); /* RESPOND TO ARROW KEYS MOVING VIEW */
  324.  
  325.     if (fg_kbtest(SCAN_ESC) || fg_kbtest(SCAN_Q)) /* ESCAPE TO QUIT */
  326.         return 1;
  327.  
  328.     return 0;
  329. }
  330.  
  331.