home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / utilities / utilsf / gol / !Gol / c / Gol < prev    next >
Text File  |  1993-12-19  |  39KB  |  1,108 lines

  1. /*
  2.  *  Copyright © 1993 Michael R Brown
  3.  *  Release version: 0.01 (Oct 24 1993)
  4.  *  Permission is granted to use and alter this software for private use.
  5.  *  Permission is also granted to copy this software in its un-altered form.
  6.  */
  7.  
  8. /* Wimp interface to !GOL */
  9.  
  10. #include "wimp.h"        /*  access to WIMP SWIs                      */
  11. #include "wimpt.h"       /*  wimp task facilities                     */
  12. #include "win.h"         /*  registering window handlers              */
  13. #include "event.h"       /*  poll loops, etc                          */
  14. #include "baricon.h"     /*  putting icon on icon bar                 */
  15. #include "sprite.h"      /*  sprite operations                        */
  16. #include "werr.h"        /*  error reporting                          */
  17. #include "res.h"         /*  access to resources                      */
  18. #include "resspr.h"      /*  sprite resources                         */
  19. #include "flex.h"        /*  dynamic mem alloc from WIMP              */
  20. #include "template.h"    /*  reading in template file                 */
  21. #include "bbc.h"         /*  olde-style graphics routines             */
  22. #include "colourtran.h"  /*  interface to colour translation module   */
  23. #include "os.h"          /*  low-level RISCOS access                  */
  24. #include "dbox.h"        /*  dialogue box handling                    */
  25. #include "saveas.h"      /*  data export from dbox by icon dragging   */
  26. #include "visdelay.h"    /*  show the hourglass for delay             */
  27. #include "xferrecv.h"
  28.  
  29. #include <stdarg.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <time.h>
  33. #include "life.h"
  34. #include "fcntl.h"
  35.  
  36. #define REG     register
  37.  
  38. struct lVARS_str
  39.        {int       width;
  40.         int      height;
  41.         int       flags;
  42.         int        init;
  43.         int        mask;
  44.         int        bits;
  45.         int       Rules[10];
  46.        } lVARS={256, 256, 3, 53, LIFE_STANDARD, 8, lCLEAR, lCLEAR,
  47.                 lLEAVE, lSET, lCLEAR, lCLEAR, lCLEAR, lCLEAR, lCLEAR, lCLEAR};
  48.  
  49. #define GOL_FAST_LIFE   0x1
  50. #define GOL_LIFE_WRAP   0x2
  51. #define GOL_PAUSED      0x4
  52. #define GOL_FAST_SPRITE 0x8
  53. typedef struct
  54.         {menu                 wmenu;
  55.          int                  whand;
  56.          int                     wh; /* width in bytes | height<<16 */
  57.          int                  flags;
  58.          char                 table[0x40000];
  59.          struct lVARS_str lVARS_bak;
  60.          char         save_filename[128];
  61. /* */
  62.          int    scanline, scanlines; 
  63.          int       spr_mag, spr_div;
  64.          sprite_factors     factors;
  65.          sprite_pixtrans   pixtrans[256];
  66.          int               *spr_ptr[2];
  67.          sprite_id               id[2];
  68.          sprite_area           area;
  69.         } GOL_struct;
  70.  
  71. typedef enum {AREA_HEADER, SPRITE_HEADER
  72.              ,SPRITE_DATA, SKIP_BYTES} IO_State_str;
  73.  
  74. static int         GOL_count=16;
  75. static char        GOL_titles[16][32];
  76. static GOL_struct *GOL_vector[16]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  77. static os_regset   GOL_registers;
  78.  
  79. static char            CLEAR_TEXT[]="Clear";
  80. static char            LEAVE_TEXT[]="Leave";
  81. static char              SET_TEXT[]="Set";
  82. static char      LIFE_WINDOW_NAME[]="LifeField";
  83. static char     SETUP_WINDOW_NAME[]="Setup";
  84. static char      INFO_WINDOW_NAME[]="Info";
  85. static char          SPRITE1_NAME[]="s1";
  86. static char          SPRITE2_NAME[]="s2";
  87. static char              APP_NAME[]="Gol";
  88. static char           SPRITE_NAME[]="!Gol";
  89. static char          WINDOW_TITLE[]="Life";
  90. static char COPYWRITE[]="This software is copywrite Michael R Brown (Oct 24 1993)";
  91. static char MAGNIFIER_WINDOW_NAME[]="Magnifier";
  92. static char         SAVE_FILENAME[]="LifeField";
  93. static char          VALID_STRING[]="31091973";
  94.  
  95. #define Sprite_FileType 0x0ff9
  96. #define Sprite_Mode     18
  97. #define xshift(x)       ((x)<<1)
  98. #define yshift(y)       ((y)<<1)
  99. #define shiftx(x)       ((x)>>1)
  100. #define shifty(y)       ((y)>>1)
  101. #define REDRAW_WIMP     0
  102. #define REDRAW_ALL      1
  103. #define REDRAW_AREA     2
  104. #define REDRAW_LATER    3
  105.  
  106.  
  107. #define Sprite_FileSize(w,h) (((w)*(h))+sizeof(sprite_area) \
  108.                              +sizeof(sprite_header)-sizeof(int))
  109. #define Sprite_Size ((lVARS.height*(lVARS.width>>2))+sizeof(sprite_area) \
  110.                     +(sizeof(sprite_header)<<1))
  111. #define Alloc_Size  (Sprite_Size+sizeof(GOL_struct))
  112. #define Task_Threshold (32*8192)
  113.  
  114. #define Icon_Rules       4
  115. #define Icon_Pixels      25
  116. #define Icon_Fast_Life   36
  117. #define Icon_Life_Wrap   37
  118. #define Icon_Width_Down  39
  119. #define Icon_Width_Up    40
  120. #define Icon_Width       38
  121. #define Icon_Height_Down 42
  122. #define Icon_Height_Up   43
  123. #define Icon_Height      41
  124. #define Icon_Go          47
  125. #define Icon_Paused      50
  126. #define Icon_Clear_All   51
  127. #define Icon_Set_All     52
  128. #define Icon_Random      53
  129.  
  130. #define Icon_Mag_Down    0
  131. #define Icon_Mag_Up      1
  132. #define Icon_Mag         2
  133. #define Icon_Div         3
  134. #define Icon_Div_Up      6
  135. #define Icon_Div_Down    7
  136.  
  137. void GOL_info(void);
  138. void GOL_open_setup(wimp_i i);
  139. void GOL_close(wimp_w whand, int slot);
  140. void GOL_redraw(int slot, int all);
  141. static int GOL_redraw_ignor=0;
  142.  
  143. void GOL_err(const char *format,...)
  144.      {va_list  args;
  145.       static char line[256];
  146.  
  147.       va_start(args,format);
  148.       vsprintf(line,format,args);
  149.       werr(FALSE, line);
  150.       va_end(args);
  151.      }
  152.  
  153.  
  154. int WIMP_MAG;
  155. int WIMP_XMAG;
  156. int WIMP_YMAG;
  157. int WIMP_BPP;
  158.  
  159. void GOL_SpeedCheck(slot)
  160.      int slot;
  161.  
  162.      {GOL_struct *handle=GOL_vector[slot];
  163.       int mag=handle->spr_mag;
  164.       int div=handle->spr_div;
  165.  
  166.       if (WIMP_MAG && !(mag%div) && xshift(mag/div)==WIMP_MAG)
  167.            handle->flags|= GOL_FAST_SPRITE;
  168.       else handle->flags&=~GOL_FAST_SPRITE;
  169.      }
  170.  
  171. void GOL_ModeChange()
  172.      {int    slot;
  173.       GOL_struct *handle;
  174.  
  175.       GOL_registers.r[0]=-1;
  176.       GOL_registers.r[1]=4;
  177.       WIMP_XMAG=1<<(os_swi(0x35, &GOL_registers), GOL_registers.r[2]);
  178.       GOL_registers.r[1]=5;
  179.       WIMP_YMAG=1<<(os_swi(0x35, &GOL_registers), GOL_registers.r[2]);
  180.       GOL_registers.r[1]=9;
  181.       WIMP_BPP =1<<(os_swi(0x35, &GOL_registers), GOL_registers.r[2]);
  182.  
  183.       if (WIMP_BPP==1 && WIMP_XMAG==WIMP_YMAG)
  184.            WIMP_MAG=WIMP_XMAG;
  185.       else WIMP_MAG=0;
  186.  
  187.       for (slot=0; slot<16; slot++) if (GOL_vector[slot])
  188.           {handle=GOL_vector[slot];
  189.            wimp_readpixtrans(&(handle->area), &(handle->id[0]),
  190.                              &(handle->factors), handle->pixtrans);
  191.            GOL_SpeedCheck(slot);
  192.           }
  193.      }
  194.  
  195. static wimp_redrawstr GOL_redraw_box;
  196. void GOL_redraw(slot, mode)
  197.      int     slot;
  198.      int     mode;
  199.  
  200.      {wimp_redrawstr          r;
  201.       sprite_factors    factors;
  202.       GOL_struct        *handle=GOL_vector[slot];
  203.       int    mag=handle->spr_mag;
  204.       int    div=handle->spr_div;
  205.       int  width=(handle->wh&0xffff)<<3;
  206.       int height=(handle->wh>>16);
  207.  
  208.       r.w=handle->whand;
  209.       switch (mode)
  210.              {case REDRAW_WIMP:
  211.                    wimpt_noerr(wimp_redraw_wind(&r, &slot));
  212.                    break;
  213.               case REDRAW_ALL:
  214.                    r.box.x0=r.box.y1=0;
  215.                    r.box.x1=  xshift( width)*mag/div ;
  216.                    r.box.y0=-(yshift(height)*mag/div);
  217.                    wimp_update_wind(&r, &slot);
  218.                    break;
  219.               case REDRAW_AREA:
  220.                    r.box.x0=xshift(shiftx(GOL_redraw_box.box.x0*div/mag)*mag/div);
  221.                    r.box.x1=xshift(shiftx(GOL_redraw_box.box.x1*div/mag)*mag/div);
  222.                    r.box.y0=-yshift(shifty((-GOL_redraw_box.box.y0)*div/mag)*mag/div);
  223.                    r.box.y1=-yshift(shifty((-GOL_redraw_box.box.y1)*div/mag)*mag/div);
  224.                    wimpt_noerr(wimp_update_wind(&r, &slot));
  225.                    break;
  226.               case REDRAW_LATER:
  227.                    r.box.x0=r.box.y1=0;
  228.                    r.box.x1=  xshift( width)*mag/div ;
  229.                    r.box.y0=-(yshift(height)*mag/div);
  230.                    wimp_force_redraw(&r);
  231.                    return; 
  232.              }
  233.  
  234.       if (slot)
  235.          {factors.xmag=handle->factors.xmag*mag;
  236.           factors.ymag=handle->factors.ymag*mag;
  237.           factors.xdiv=handle->factors.xdiv*div;
  238.           factors.ydiv=handle->factors.ydiv*div;
  239.  
  240.