home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser 2002 January / STC_CD_01_2002.iso / GAMES / WINLABY / SOURCE / WL_MAIN.C < prev   
C/C++ Source or Header  |  1997-08-04  |  8KB  |  277 lines

  1. /********************************************************************/
  2. /*                          Das WindowLabyrinth                            */
  3. /*                    written by Thomas Huth, 1995-1997                 */
  4. /*------------------------------------------------------------------*/
  5. /* See Readmes for more information.                                */
  6. /* Set tabulator size to 4 !                                        */
  7. /********************************************************************/
  8.  
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <aes.h>
  12. #include <vdi.h>
  13. #include "winlaby.h"
  14.  
  15. int work_in[11], work_out[57];    /* Für Openvwk */
  16. int ap_id;                        /* Applikationsnummer */
  17. int vhandle;                    /* VDI - Handle */
  18. int msgbuff[8];                 /* Message-Buffer */
  19. int xy[10];                     /* Feld für VDI-Malfunktionen */
  20.  
  21. int wi_handle;                    /* Window-Handle */
  22. GRECT wnd;                        /* Fensterkoordinaten */
  23. void *tree_adr;                 /* Adressvariable für's Pulldownmenü */
  24. int deskx, desky, deskw, deskh; /* Desktopgrö₧e */
  25.  
  26. int richtung=2;                 /* Himmelsrichtung, in die man schaut */
  27. int xpos,ypos;                    /* Koordinaten des Spielers */
  28. int zielx,ziely;                /* Koordinaten des Ziels */
  29. int pmode=1;                    /* 1=Leer zeichnen, 2=gefüllt */
  30. unsigned char orttyp[21][21];    /* Feld für's Labyrinth  */
  31.  
  32. char inf_richtung[31]=" Richtung: X    Distanz:      "; /* String für Infozeile */
  33.  
  34. int redrawflag=FALSE;            /* Flag, ob neu gezeichnet werden soll */
  35. int exitflag=FALSE;                /* If TRUE then exit */
  36. int winnerflag=FALSE;            /* TRUE if you won the game */
  37.  
  38. /* Prototypen: */
  39. int GEMinit(void);
  40. void GEMexit(void);
  41. int init_window(void);
  42. void draw_laby(void);
  43.  
  44.  
  45. /* ****Fehlermeldungausgabe**** */
  46. void fehler(unsigned char *text)
  47. {
  48.  form_alert(1,text);
  49.  v_clsvwk(vhandle);         /* Virtuelle abmelden    */
  50.  appl_exit();
  51. }
  52.  
  53.  
  54. /* ****Menüwahl auswerten**** */
  55. void menue(int auswahl)
  56. {
  57.  switch(auswahl)
  58.   {
  59.    case LABINFO:
  60.      form_alert(1,"[1][Das WindowLabyrinth V1.1!|Geschrieben von|Thomas Huth, 1995-97][ OK ]");
  61.      break;
  62.    case NEU:
  63.      form_alert(1,"[3][Wie du willst:|Zurück zum Start!][Auf ein Neues]");
  64.      redrawflag=TRUE;
  65.      break;
  66.    case ENDE:
  67.      form_alert(1,"[0][   Bis|  bald!   | ][ OK ]");
  68.      exitflag=TRUE;
  69.      break;
  70.    case LABMODUS:
  71.      pmode=form_alert(pmode, "[2][Welche Darstellungsart?][leer|gefüllt]");
  72.      break;
  73.   }
  74. }
  75.  
  76.  
  77. /* ****Grobe Wurzelfunktion**** */
  78. int wurzel(int zahl)
  79. {
  80.  int i=0;
  81.  if(zahl<0)  return(-1);
  82.  for(i=0;i*i<zahl;i++)
  83.   ;
  84.  return(i);
  85. }
  86.  
  87.  
  88. /* ****Auf Tastendruck reagieren bzw. Spieler bewegen**** */
  89. void walk(unsigned int keycode)
  90. {
  91.  unsigned char scancode, asciicode;
  92.  
  93.  scancode=(char)( (keycode>>8) & 0xFF );
  94.  asciicode=(char)( keycode & 0xFF );
  95.  
  96.  if(asciicode=='8' || scancode==0x48)                    /* Vorwärts gehen */
  97.   {
  98.     if(richtung==1 && orttyp[xpos][ypos-1]!=1)    --ypos;
  99.     if(richtung==2 && orttyp[xpos+1][ypos]!=1)    ++xpos;
  100.     if(richtung==3 && orttyp[xpos][ypos+1]!=1)    ++ypos;
  101.     if(richtung==4 && orttyp[xpos-1][ypos]!=1)    --xpos;
  102.   }
  103.  
  104.  if(asciicode=='2' || scancode==0x50)
  105.   {
  106.     if(richtung==1 && orttyp[xpos][ypos+1]!=1)    ++ypos;
  107.     if(richtung==2 && orttyp[xpos-1][ypos]!=1)    --xpos;
  108.     if(richtung==3 && orttyp[xpos][ypos-1]!=1)    --ypos;
  109.     if(richtung==4 && orttyp[xpos+1][ypos]!=1)    ++xpos;
  110.   }
  111.  
  112.  if(asciicode=='4' || scancode==0x4B)    /* Drehung nach links, Taste "4"  */
  113.     richtung=richtung-1;
  114.  if(richtung==0)    richtung=4;
  115.  
  116.  if(asciicode=='6' || scancode==0x4D)    /* Drehung nach rechts, Taste "6" */
  117.     richtung=richtung+1;
  118.  if(richtung==5)    richtung=1;
  119. }
  120.  
  121.  
  122. /* ****Irrgarten laden**** */
  123. int labload(void)
  124. {
  125.  FILE *file_ptr;              /* Datei-Handle  */
  126.  unsigned char readtext;      /* Dummy für's Dateilesen */
  127.  int x,y;
  128.  static char labname[]="laby00.lab";
  129.  
  130.  if( ++labname[5] > '9' )
  131.   {
  132.    labname[5]='0';
  133.    if( ++labname[4] > '9' )
  134.     return(-1);
  135.   }
  136.  
  137.  file_ptr=fopen(labname,"r");
  138.  if(file_ptr==NULL)  return(49);
  139.  for(y=1; y<21; y++)
  140.   {
  141.     for(x=1; x<21; x++)
  142.     {
  143.      readtext=(unsigned char) fgetc(file_ptr);
  144.      if(readtext=='#') orttyp[x][y]=1; else orttyp[x][y]=0;
  145.      if(readtext=='Z') { orttyp[x][y]=0; zielx=x; ziely=y; }
  146.      if(readtext=='S') { orttyp[x][y]=0; xpos=x; ypos=y; }
  147.     }
  148.     do readtext=(unsigned char) fgetc(file_ptr);  while(readtext!='\n');
  149.   }
  150.  fclose(file_ptr);
  151.  return(0);
  152. }
  153.  
  154.  
  155. /* ***** UND LOS GEHT'S ! ***** */
  156. int main(void)
  157. {
  158.  int dummy;                 /* Dummyvariable */
  159.  int *d=&dummy;             /* Dummyzeiger     */
  160.  int lab_nr=1;                /* Nummer des Labyrinths */
  161.  int taste;                 /* Für Eingaben  */
  162.  GRECT clp;                    /* Für Werte aus der Rechtecksliste */
  163.  int whichevnt;             /* Welches Ereignis eintritt */
  164.  
  165.  if( GEMinit() ) return(-1);    /* GEM anmelden */
  166.  
  167.  if( labload() )                /* 1. Laby laden */
  168.   {
  169.    form_alert(1, "[3][Konnte 1. Laby|nicht laden.][Abbruch]");
  170.    GEMexit();
  171.    return(-1);
  172.   }
  173.  
  174.  if(init_window()!=0)
  175.     return(-1);
  176.  
  177.  /*--- Und ab ins Labyrinth ---*/
  178.  do
  179.  {
  180.  
  181.   /* Infozeile erstellen */
  182.   switch(richtung)
  183.   {
  184.     case 1: inf_richtung[11]='N'; break;
  185.     case 2: inf_richtung[11]='O'; break;
  186.     case 3: inf_richtung[11]='S'; break;
  187.     case 4: inf_richtung[11]='W'; break;
  188.   }
  189.   dummy=(zielx-xpos)*(zielx-xpos)+(ziely-ypos)*(ziely-ypos);
  190.   itoa(wurzel(dummy), &inf_richtung[25], 10);
  191.   wind_set(wi_handle, WF_INFO, inf_richtung, 0L);  /* Richtung anzeigen */
  192.  
  193.   /* Auf Eingaben warten */
  194.   whichevnt=evnt_multi(MU_MESAG|MU_KEYBD, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  195.                         msgbuff, 0, 0, d, d, d, d, &taste, d);
  196.  
  197.   /* Wenn Taste gedrückt, dann bewegen */
  198.   if(whichevnt & MU_KEYBD)
  199.    {
  200.     walk(taste);
  201.     redrawflag=1;
  202.    }
  203.  
  204.   /* Auf Ereignisse reagieren */
  205.   if(whichevnt & MU_MESAG)
  206.    switch(msgbuff[0])
  207.     {
  208.      case WM_SIZED:                         /* Fenstergrö₧e ändern */
  209.          redrawflag=TRUE;
  210.      case WM_MOVED:
  211.          wind_set(msgbuff[3], WF_CURRXYWH, msgbuff[4], msgbuff[5],
  212.                   msgbuff[6], msgbuff[7]);
  213.          break;
  214.      case WM_FULLED:                        /* Fenster auf Maximalgrö₧e */
  215.          wind_set(msgbuff[3], WF_CURRXYWH, deskx, desky, deskw, deskh);
  216.          break;
  217.      case WM_TOPPED:                        /* Fenster toppen */
  218.          wind_set(msgbuff[3], WF_TOP, 0L, 0L);
  219.          break;
  220.      case WM_REDRAW:
  221.          redrawflag=TRUE;
  222.          break;
  223.      case WM_CLOSED:
  224.          exitflag=TRUE;
  225.          break;
  226.      case MN_SELECTED:                        /* Menü */
  227.          menu_tnormal(tree_adr, msgbuff[3], 1);
  228.          menue(msgbuff[4]);
  229.          if(msgbuff[4]==NEU)  { xpos=2; ypos=2; }
  230.          if(msgbuff[4]==LABMODUS)    redrawflag=1;
  231.          break;
  232.     }
  233.  
  234.   /*--- Neues Labyrinth laden ---*/
  235.   if(xpos==zielx && ypos==ziely)
  236.    {
  237.     form_alert(1,"[1][Dieses Labyrinth wäre|überstanden.|Auf ins nächste!][ OK ]");
  238.     dummy=labload();
  239.     if(dummy<0)        { GEMexit(); return(-1); }
  240.     if(dummy==49)    { exitflag=TRUE; winnerflag=TRUE; }
  241.     ++lab_nr;
  242.    }
  243.  
  244.   /* --- Labyrinth zeichnen --- */
  245.   wind_get(wi_handle, WF_WORKXYWH, &wnd.g_x, &wnd.g_y, &wnd.g_w, &wnd.g_h); /* Fensterparameter */
  246.   if(redrawflag)                    /* Neu zeichnen ? */
  247.    {
  248.     redrawflag=0;
  249.     wind_update(BEG_UPDATE);        /* Bildaufbau Anfang */
  250.     graf_mouse(M_OFF, 0L);            /* Maus aus */
  251.     wind_get(wi_handle, WF_FIRSTXYWH, &clp.g_x, &clp.g_y, &clp.g_w, &clp.g_h);
  252.     while(clp.g_w && clp.g_h)        /* Solange noch Rechtecke in der Liste sind */
  253.      if( rc_intersect(&wnd, &clp) )
  254.       {
  255.        clp.g_w+=clp.g_x-1;    clp.g_h+=clp.g_y-1;
  256.        vs_clip(vhandle, 1, &clp);    /* Clipping setzen */
  257.        draw_laby();                    /* Teil des Labys zeichnen */
  258.        wind_get(wi_handle, WF_NEXTXYWH, &clp.g_x, &clp.g_y, &clp.g_w, &clp.g_h);
  259.       }
  260.     graf_mouse(M_ON, 0L);            /* Maus an */
  261.     wind_update(END_UPDATE);        /* Bildaufbau Ende */
  262.    }
  263.  
  264.  }
  265.  while(!exitflag);
  266.  
  267.  /*--- Programm verlassen ---*/
  268.  
  269.  wind_close(wi_handle);                 /* Window schlie₧en */
  270.  wind_delete(wi_handle);                /* Window abmelden    */
  271.  
  272.  if(winnerflag)
  273.   form_alert(1,"[1][Gratuliere, du hast|alle Irrgärten|durchwandert!][ YEAH! ]");
  274.  
  275.  GEMexit();
  276. }
  277.