home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / falcon / program / dcjh_src / dontclik.c next >
C/C++ Source or Header  |  1994-02-12  |  10KB  |  452 lines

  1. /*
  2.  * Don't Click Jim's Head
  3.  *  v. 1.0
  4.  */
  5.  
  6. #include <osbind.h>
  7. #include <vdi.h>
  8. #include <dos.h>
  9. #include <aes.h>
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <avr.h>
  13.  
  14. #include "jimshead.h"
  15. #include "jimshead.c"
  16.  
  17. int falcon;  
  18. int width;
  19. short id;
  20. short aes_handle;
  21. short vdi_handle;
  22.  
  23. short junk;
  24. short work_in[11] = {1,1,1,1,1,1,1,1,1,1,2};
  25. short work_out[57];
  26. short xyarray[4] = {0,0,2,30};
  27. short max_xyarray[4] = {0,32,639,199};
  28. short status;
  29. short mousex;
  30. short mousey;
  31.  
  32. short cliparray[] = {0,1,1,1};
  33. short intout[128];
  34. int wh=-1;
  35. short aes_version;
  36. short antishift_variable = 0;
  37.  
  38. int state;
  39. short key, button, clicks;
  40. short evnt_x, evnt_y;
  41.  
  42. char dontclik_menu[] = "  Don't Click Jim  ";
  43. char dontclik_title[] = "From that Bastard Dan";
  44. OBJECT *WHICH;
  45.  
  46. short *sound_buf=NULL;
  47. avr_t avr_h;                    // global AVR header for sample loading/saving
  48.  
  49. long sound_length;
  50. short sound_rate;
  51.  
  52. /*
  53.  * This is the sample list definition. It consists of a doubly linked
  54.  * list of entries containing the samples which are available in the
  55.  * current sample directory.
  56.  */
  57. struct samp_list
  58. {
  59.     struct samp_list *next;        // next samp_list entry (or NULL)
  60.     struct samp_list *prev;        // previous samp_list entry (or NULL)
  61.     char name[12];            // filename of sample in sample directory
  62.     struct
  63.     {
  64.         long length;            // length of sample
  65.         short rate;                // sample replay 'rate'
  66.         char name[28];            // 28 character name - 0 padded
  67.     } h;                        // extracted from file header
  68.     char zero;                    // a zero since h.name is not guaranteed
  69.                                 // null terminated.
  70. };
  71.  
  72. struct samp_list simple_beep;    // dummy entry for standard bell
  73.  
  74. struct samp_list *root;            // root of list
  75.  
  76. struct samp_list *current_sound;    // the current sound
  77. struct samp_list *first;            // pointer to element at top of window
  78.  
  79. long mxalloc = 0;                // does this GEMDOS know about Mxalloc?
  80.  
  81. #define    sndbase    ((volatile short *)0xffff8900)
  82.  
  83. #define    sndbasehi    ((volatile short *)0xffff8902)
  84. #define    sndbasemid    ((volatile short *)0xffff8904)
  85. #define    sndbaselo    ((volatile short *)0xffff8906)
  86.  
  87. #define    sndendhi    ((volatile short *)0xffff890e)
  88. #define    sndendmid    ((volatile short *)0xffff8910)
  89. #define    sndendlo    ((volatile short *)0xffff8912)
  90.  
  91. #define    sndmode    ((volatile short *)0xffff8920)
  92.  
  93.  
  94. #define W_TYPE (NAME | MOVER | CLOSER)
  95.  
  96. void
  97. which_resource(void)
  98. {
  99.     if (aes_version < 0x0401)
  100.     {
  101.         if (work_out[1] < 391)
  102.         {
  103.             WHICH = JIMS_HEAD_R_L;
  104.         }
  105.         if (work_out[1] > 390)
  106.         {
  107.             WHICH = JIMS_HEAD_R_H;
  108.         }
  109.     }
  110.     if (aes_version >= 0x0401)
  111.     {
  112.         if (work_out[1] < 391)
  113.         {
  114.             WHICH = JIMS_HEAD_M_L;
  115.         }
  116.         if (work_out[1] > 390)
  117.         {
  118.             WHICH = JIMS_HEAD_M_H;
  119.         }
  120.     }
  121. }
  122.  
  123. int    
  124. new_window(OBJECT *tree, const char *title)
  125. {
  126.     GRECT p;
  127.  
  128.     // compute required size for window given object tree
  129.     wind_calc(WC_BORDER, W_TYPE, PTRS((GRECT *)&WHICH[ROOT].ob_x),
  130.       &p.g_x, &p.g_y, &p.g_w, &p.g_h);
  131.       
  132.     wh = wind_create(W_TYPE, ELTS(p));
  133.     if (wh >= 0) {
  134.         wind_title(wh, title);
  135.         wind_open(wh, ELTS(p));
  136.     }
  137.     return wh;
  138. }
  139.  
  140. int
  141. redraw(int wh,GRECT *p)
  142. {
  143.     objc_draw(WHICH,ROOT,MAX_DEPTH,PTRS(p));
  144.     return 1;
  145. }
  146.  
  147. void
  148. Get_tos_version(void)
  149. {
  150.     falcon = 1;
  151.     aes_version = _AESglobal[0];
  152.  
  153.     if(aes_version <  0x0340)
  154.     {
  155.     falcon = 0;
  156.     }
  157. }
  158.  
  159. void snd_wait(void)
  160. {
  161.     while ((*sndbase)&0xff);
  162. }
  163.  
  164. void snd_kill(void)
  165. {
  166.     *sndbase = 0;
  167. }
  168.  
  169. void snd_play(short *buf,long length,short rate)
  170. {
  171.         snd_kill();
  172.         *sndbasehi=(short)((unsigned long)buf>>16);
  173.         *sndbasemid=(short)((unsigned long)buf>>8);
  174.         *sndbaselo=(short)buf;
  175.  
  176.         buf=(short *)((unsigned long)buf+length);
  177.         *sndendhi=(short)((unsigned long)buf>>16);
  178.         *sndendmid=(short)((unsigned long)buf>>8);
  179.         *sndendlo=(short)buf;
  180.     
  181.         *sndmode=rate;
  182.         *sndbase=1;        /* start single play mode */
  183. }
  184.  
  185.  
  186. /*
  187.  * do_file - process a sample file, extracting the length and sample
  188.  * rate information into p.
  189.  *
  190.  * This routine is woefully short on error checking and should be extended.
  191.  */
  192. void
  193. do_file(struct samp_list *p)
  194. {
  195.     int fd=Fopen(p->name,FO_READ);
  196.  
  197.     if (fd>=0)
  198.     {
  199.         static unsigned short freqs[]={0,12517,25033,50066};
  200.         int i;
  201.  
  202.         Fread(fd,sizeof(avr_h),&avr_h);
  203.         p->h.length=avr_h.avr_length;
  204.         p->h.name[sizeof(avr_h.avr_name)-1]='\0';        // place sentinel in buffer
  205.         strncpy(p->h.name,avr_h.avr_name,sizeof(avr_h.avr_name));
  206.         if (p->h.name[sizeof(avr_h.avr_name)-1]!='\0')    // did they hit the sentinel ?
  207.             strncpy(p->h.name+sizeof(avr_h.avr_name),avr_h.avr_xname,
  208.               sizeof(avr_h.avr_xname));
  209.  
  210.         /*
  211.          * Here we approximate the frequency stored in the header
  212.          */
  213.         for (i=sizeof(freqs)/sizeof(freqs[0]); i--; )
  214.             if (freqs[i]>=(avr_h.avr_frequency&0xffffff))
  215.                 p->h.rate=0x80|i;
  216.  
  217.         Fclose(fd);
  218.         p->zero=0;
  219.     }
  220. //    else
  221. //        process_err(fd);
  222. }
  223.  
  224. /*
  225.  * play_sound - play the current sample. This routine exists solely as
  226.  * a target for a Supexec() call.
  227.  */
  228. long
  229. play_sound(void)
  230. {
  231.     snd_play(sound_buf,current_sound->h.length,current_sound->h.rate);
  232.     return 0;                // to stop warnings appearing
  233. }
  234.  
  235. /*
  236.  * wait_sample - wait for a sample to stop playing. We only really want
  237.  * this to happen when clicking between items to we don't try and overlap
  238.  * them. Again this must be called in Supervisor mode.
  239.  */
  240. long
  241. wait_sample(void)
  242. {
  243.     snd_wait();
  244.     return 0;                // to stop warnings appearing
  245. }
  246.  
  247. /*
  248.  * release_buffer - check if a buffer is allocated and free it.
  249.  */
  250. void
  251. release_buffer(void)
  252. {
  253.     if (sound_buf)
  254.     {
  255.         Mfree(sound_buf);
  256.         sound_buf=NULL;
  257.     }
  258. }
  259.  
  260. /*
  261.  * load_sample - load a sample into memory. If sound_buf is not set up
  262.  * we load it into the cookie's buffer.
  263.  */
  264. void
  265. load_sample(const char *s,long length)
  266. {
  267.     int fd=Fopen(s,FO_READ);
  268.  
  269.     if (fd>=0)
  270.     {
  271.         Fseek(sizeof(avr_t),fd,FSEEK_SET);
  272.         Fread(fd,length,sound_buf); 
  273.         Fclose(fd);
  274.     }
  275. }
  276.  
  277. /*  sound chunk, this is a chunk of doo out of bell.cpx */
  278. void
  279. sound_works(struct samp_list *p)
  280. {
  281.     //graf_mouse(BUSY_BEE, NULL);
  282.  
  283.     /*
  284.      * If the name is empty then we're looking at the standard bell and
  285.      * don't need to load anything up.
  286.      */
  287.     if (p->name[0])
  288.     {
  289.         /*
  290.          * CPXs should not allocate memory, sadly we need it here so we
  291.          * can preserve the contents of the existing buffer for the
  292.          * purpose of cancelling. Since we free it immediately
  293.          * afterwards with no intervening AES calls we should be safe.
  294.          */
  295.         //printf("                   %d \n ",p->h.length);
  296.         sound_buf =  Malloc(p->h.length);
  297.         if ((long)sound_buf <= 0)
  298.         {
  299.             //need to do something here but I don't know what
  300.             //goto nomem;                    // was that a goto !!!
  301.         }
  302.         else
  303.         {
  304.             load_sample(p->name,p->h.length);
  305.         }
  306.     }
  307.     
  308.     /*
  309.      * Play the sample, then wait for it to finish. The routines which
  310.      * do this must be called at the Supervisor level as they play with
  311.      * the hardware.
  312.      */
  313.     current_sound=p;
  314.  
  315.     Supexec(play_sound);
  316.     Supexec(wait_sample);
  317.  
  318.     /*
  319.      * We must release the memory as quickly as possible so that there
  320.      * is no danger of it becoming lost as a result of a resolution
  321.      * change etc.
  322.      */
  323.     release_buffer();
  324. //nomem:
  325. }
  326.  
  327. int
  328. main(void)
  329. {
  330.     GRECT full;
  331.     struct samp_list *p;
  332.     
  333.     id = appl_init();
  334.     aes_handle = graf_handle(&junk,&junk,&junk,&junk);
  335.     vdi_handle = aes_handle;
  336.  
  337.     Get_tos_version();
  338.     
  339.     if (aes_version >= 0x0400)        /* check for MultiTOS*/
  340.     {
  341.         menu_register(id, dontclik_menu); /* if it is make the name pretty*/
  342.     }
  343.  
  344.     v_opnvwk(work_in,&vdi_handle,work_out);
  345.  
  346.     rsrc_init();
  347.  
  348.     //vq_extnd(vdi_handle, 1, intout);
  349.     
  350.     wind_get(DESK, WF_WXYWH, &full.g_x, &full.g_y, &full.g_w, &full.g_h);
  351.  
  352.     which_resource();
  353.  
  354.     rc_constrain(&full, (GRECT *)&WHICH[ROOT].ob_x);
  355.  
  356.     //form_alert(1,"[2][Good so far][ok]");
  357.  
  358.     wh = new_window(WHICH, dontclik_title);
  359.  
  360.     for (;;) {
  361.         short msg[8], junk;
  362.         int whichone,which_obj;
  363.         
  364.         whichone = evnt_multi(MU_TIMER | MU_MESAG | MU_BUTTON,
  365.           0, 0, 0,            // mouses
  366.           0, 0, 0, 0, 0,    // rectangle 1
  367.           0, 0, 0, 0, 0,    // rectangle 2
  368.           msg,                // message buffer
  369.           2000, 0,            // respond every 2s
  370.           &junk, &junk, &button, &junk, &junk, &junk);
  371.  
  372.         if (whichone & MU_BUTTON)
  373.         {
  374.  
  375.             if ( button == 1 )
  376.                 {
  377.                     vq_mouse(vdi_handle,&status,&mousex,&mousey);
  378.  
  379.                     which_obj = objc_find(WHICH,0,2,mousex,mousey);
  380.                         
  381.                     if ( which_obj == 1)
  382.                     {
  383.                         strcpy(p->name,"QUIT_IT.AVR");    // fill in entry
  384.                         //root->prev=p;        // and link into list
  385.                         p->prev=NULL;
  386.                         p->next=root;
  387.                         root=p;
  388.                         
  389.                         do_file(p);
  390.     
  391.                         sound_works(p);
  392.                         
  393.                     }    
  394.  
  395.                 }
  396.         }
  397.         
  398.         if (whichone & MU_MESAG)
  399.             switch (msg[0]) {
  400.  
  401.                 case WM_TOPPED:
  402.                     wind_set(msg[3], WF_TOP, msg[3]);
  403.                     break;
  404.  
  405.                 case WM_CLOSED:
  406.                     wind_close(wh);
  407.                     wind_delete(wh);
  408.                     wh = -1;
  409.                     break;
  410.  
  411.                 case WM_REDRAW:
  412.                     wind_redraw(msg[3], (GRECT *)&msg[4], redraw);
  413.                     break;
  414.  
  415.                 case WM_MOVED:
  416.                     wind_set(wh, WF_CXYWH, PTRS((GRECT *)&msg[4]));
  417.                     wind_calc(WC_WORK, W_TYPE, PTRS((GRECT *)&msg[4]),
  418.                       &WHICH[ROOT].ob_x,
  419.                       &WHICH[ROOT].ob_y,
  420.                       &WHICH[ROOT].ob_width,
  421.                       &WHICH[ROOT].ob_height);
  422.                     break;
  423.                 }
  424.         
  425.             if (wh < 0)
  426.             break;
  427.         }
  428.     appl_exit();
  429.     return 0;
  430.  
  431. }
  432.  
  433. /*
  434. void
  435. Wait_a_sec(void)
  436. {
  437.     
  438.     //  Need to wait for a second here
  439.     // Set a variable to the time
  440.     // while variable = time  ' wait
  441.  
  442.     int mytime;
  443.                 
  444.     mytime = clock()+20;
  445.     
  446.     do
  447.     {
  448.      //  just sitting here burning the milliseconds  ;)
  449.     } while (clock() < mytime);
  450.  
  451. }
  452. */