home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 543a.lha / Nebula / source.LZH / source / intro.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-06-10  |  5.3 KB  |  313 lines

  1. /*
  2.  
  3. ------------------------------------------------------------------
  4.  
  5. Black Nebula
  6.  
  7. File :                intro.c
  8. Programmer:        Colin Adams
  9. Date:                15/5/91
  10. Last Modified :    10/6/91
  11.  
  12. Description:
  13.  
  14. Does all the stuff for the introduction screens at the start.
  15.  
  16. ------------------------------------------------------------------
  17.  
  18. */
  19.  
  20.  
  21. /*    ------------------------------------------------------------------
  22.         Includes and Defines
  23.         ------------------------------------------------------------------
  24. */
  25.  
  26. #define AMIGA_INCLUDES
  27. #include "3d.h"
  28.  
  29. #include "intro.h"
  30.  
  31. /*    ------------------------------------------------------------------
  32.         Data/Variables
  33.         ------------------------------------------------------------------
  34. */
  35.  
  36. struct IntuiMessage *message;
  37. struct Window *window1;
  38. struct Screen *screen;
  39. struct RastPort *intro_rast;
  40.  
  41. USHORT Code;
  42. ULONG Class;
  43. APTR IAddress;
  44.  
  45. static int x;
  46. static int y;
  47.  
  48. extern char *EnterName(void);
  49. extern int player_score;
  50.  
  51. /*    ------------------------------------------------------------------
  52.         Routines
  53.         ------------------------------------------------------------------
  54. */
  55.  
  56. void cls(void)
  57. {
  58.     x = 15;
  59.     y = 135;
  60.     
  61.     SetAPen(intro_rast, 0);
  62.     RectFill(intro_rast, x, y-10, 635, 250);
  63.     SetAPen(intro_rast, 2);
  64.     Move(intro_rast, x, y);
  65. }
  66.  
  67. void printit(char *string, char flag)
  68. {
  69.     Move(intro_rast, x, y);
  70.     Text(intro_rast, string, strlen(string));
  71.   
  72.     x += TextLength(intro_rast, string, strlen(string));
  73.   
  74.     if(flag)
  75.     {
  76.         y+= 10;
  77.         x = 15;
  78.     }
  79. }
  80.  
  81. void print(char *string)
  82. {
  83.   printit(string, 0);
  84. }
  85.  
  86. void println(char *string)
  87. {
  88.     printit(string, 1);
  89. }
  90.  
  91. void ViewFame(void)
  92. {
  93.     FILE *fp;
  94.     int i, score, c, counter, number;
  95.     char name[50];
  96.     char printbuff[20];
  97.     
  98.     if(!(fp=fopen("fame.dat","r")))
  99.     {
  100.         DisplayBeep(NULL);
  101.         return;
  102.     }
  103.     
  104.     cls();
  105.     
  106.     fscanf(fp, "%d",&number);
  107.         
  108.     SetAPen(intro_rast, 3);
  109.     print("Name:");
  110.     x = 480;
  111.     println("Score:");
  112.         
  113.     for(i=0; i<number; i++)
  114.     {
  115.         SetAPen(intro_rast, 2);
  116.         counter = 0;
  117.         while((c=getc(fp))!='+')
  118.         {
  119.             if(c!='\n')
  120.                 name[counter++] = c;
  121.         }
  122.         
  123.         name[counter] = 0;
  124.  
  125.         fscanf(fp,"%d",&score);
  126.         
  127.         print(name);
  128.         x = 480;
  129.         sprintf(printbuff, "%6d",score);
  130.         SetAPen(intro_rast, 1);
  131.         println(printbuff);
  132.     }
  133.     fclose(fp);
  134. }
  135.  
  136. void Instructions(void)
  137. {
  138.     cls();
  139.     SetAPen(intro_rast, 3);
  140.     println("Objective:");
  141.     SetAPen(intro_rast, 2);
  142.     println("  Blow everything to pieces.");
  143.     SetAPen(intro_rast, 3);
  144.     println("Controls:");
  145.     SetAPen(intro_rast, 2);
  146.     println("  Joystick up - increase altitude");
  147.     println("  Joystick down - decrease altitude");
  148.     println("  Joystick right - rotate ship clockwise");
  149.     println("  Joystick left - rotate ship anti-clockwise");
  150.     println("  Space Bar - Increase velocity");
  151.     println("  Backspace - Decrease velocity");
  152.     println("  Escape - Quit");
  153.     println("  P - Pause");
  154.     println("  U - Unpause");
  155. }
  156.   
  157. void OutputScore(void)
  158. {
  159.     char pscore[10], flag = 0;
  160.     FILE *fp;
  161.     int number, c, i, counter, score[10];
  162.     char name[10][50];
  163.     
  164.     cls();
  165.     SetAPen(intro_rast, 2);
  166.     print("You scored ");
  167.     sprintf(pscore, "%d",player_score);
  168.     SetAPen(intro_rast, 3);
  169.     print(pscore);
  170.     SetAPen(intro_rast, 2);
  171.     println(" points.");
  172.  
  173.  
  174.     /* read in high score table */
  175.     
  176.     if(!(fp=fopen("fame.dat","r")))
  177.     {
  178.         DisplayBeep(NULL);
  179.         return;
  180.     }
  181.     
  182.     fscanf(fp, "%d",&number);
  183.         
  184.     for(i=0; i<number; i++)
  185.     {
  186.         counter = 0;
  187.         while((c=getc(fp))!='+')
  188.         {
  189.             if(c!='\n')
  190.                 name[i][counter++] = c;
  191.         }
  192.         
  193.         name[i][counter] = 0;
  194.  
  195.         fscanf(fp,"%d",&score[i]);
  196.     }
  197.     
  198.     fclose(fp);
  199.  
  200.     /* check if player score higher than someone in the hall of fame */
  201.     
  202.     for(i=0; i<number; i++)
  203.     {
  204.         if(player_score > score[i])
  205.         {
  206.             int j;
  207.             
  208.             println("");
  209.             println("Congratulations!!! You have a new high score.");
  210.             
  211.             for(j=number-1; j>i; j--)
  212.             {
  213.                 strcpy(&name[j][0], &name[j-1][0]);
  214.                 score[j] = score[j-1];
  215.             }
  216.             
  217.             strcpy(&name[i][0], EnterName());
  218.             score[i] = player_score;
  219.             flag = 1;
  220.             break;
  221.         }
  222.     }
  223.  
  224.     if(!flag)
  225.     {
  226.         println("");
  227.         println("You didn't score high enough to qualify for the hall");
  228.         println("of fame.");
  229.         SetAPen(intro_rast, 3);
  230.         println("");
  231.         println("More practise is recommended!!");
  232.         return;
  233.     }
  234.  
  235.     /* write it out again */
  236.     
  237.     
  238.     if(!(fp=fopen("fame.dat","w")))
  239.     {
  240.         DisplayBeep(NULL);
  241.         return;
  242.     }
  243.     
  244.     fprintf(fp,"%d\n",number);
  245.     
  246.     for(i=0; i<number; i++)
  247.         fprintf(fp,"%s+ %d\n",&name[i][0],score[i]);
  248.         
  249.     fclose(fp);
  250.         
  251. }
  252.  
  253. void EventLoop(void)
  254. {
  255.     while(1)
  256.     {
  257.         if((message=(struct IntuiMessage *)
  258.             GetMsg(window1->UserPort))==NULL)
  259.         {
  260.             Wait(1 << window1->UserPort->mp_SigBit);
  261.             continue;
  262.         }
  263.         
  264.         /* store fields needed */
  265.         
  266.         Class = message->Class; 
  267.         Code = message->Code;
  268.         IAddress = message->IAddress;
  269.         
  270.         ReplyMsg((struct Message *) message);    /* reply to intuition */
  271.  
  272.         if(Class == GADGETUP)
  273.         {
  274.             if(IAddress==(APTR) &Gadget1)
  275.             {
  276.                 Do3d();
  277.                 OutputScore();
  278.             }
  279.             else if(IAddress==(APTR) &Gadget2)
  280.                 ViewFame();
  281.             else if(IAddress==(APTR) &Gadget3)
  282.                 Instructions();
  283.             else if(IAddress==(APTR) &Gadget4)
  284.                 return;
  285.         }
  286.  
  287.     }
  288. }
  289.  
  290. void DoIntro(void)
  291. {
  292.     if(!(screen=(struct Screen *) OpenScreen(&NewScreenStructure)))
  293.     {
  294.         printf("Open Screen failed.\n");
  295.         CleanUpandExit();
  296.     }
  297.     
  298.     NewWindowStructure1.Screen = screen;
  299.  
  300.   if(!(window1=(struct Window *) OpenWindow(&NewWindowStructure1)))
  301.     {
  302.         printf("Open Window failed.\n");
  303.         CleanUpandExit();
  304.     }
  305.     
  306.     intro_rast = window1->RPort;
  307.     ActivateWindow(window1); /* make window the active one */
  308.  
  309.     EventLoop();
  310. }
  311.  
  312. /* end of module intro.c */
  313.