home *** CD-ROM | disk | FTP | other *** search
/ The CDPD Public Domain Collection for CDTV 3 / CDPDIII.bin / pd / commodities / superdark / blankers / flyingtoaster.dark.c < prev    next >
C/C++ Source or Header  |  1993-03-11  |  10KB  |  445 lines

  1. /*--------------------------------------------------------------------------*/
  2. /*                                                                            */
  3. /*                    F l y i n g   T o a s t e r s                            */
  4. /*                                                                            */
  5. /*                                                                            */
  6. /*                                                                            */
  7. /*--------------------------------------------------------------------------*/
  8. /*    MAIN.c                                                                    */
  9. /*--------------------------------------------------------------------------*/
  10. /* Note: A big part of this code was taken of the Flying Toaster    */
  11. /* programm by                                */
  12.  
  13. #include    <exec/types.h>
  14. #include    <dos/dos.h>
  15. #include    <intuition/intuition.h>
  16. #include    <intuition/gadgetclass.h>
  17. #include    <graphics/gfx.h>
  18. #include    <graphics/gfxbase.h>
  19. #include    <graphics/gfxmacros.h>
  20. #include    <graphics/rastport.h>
  21. #include    <graphics/displayinfo.h>
  22. #include    <hardware/custom.h>
  23. #include    <clib/exec_protos.h>
  24. #include    <clib/intuition_protos.h>
  25. #include    "/includes/struct.h"
  26. #include    "/includes/tom_gadget.h"
  27.  
  28. char    p_ver[]="$VER: FlyingToaster v1.0 (30/01/93)";
  29.  
  30. #define LOCAL
  31. /*
  32. #include    "ft.h"
  33. */
  34. #include    "FlyingToaster.dark.h"
  35. #ifndef custom
  36. #define custom (*(struct Custom *)0xDFF000)
  37. #endif    /* custom */
  38.  
  39. #define WIDTH    640
  40. #define HEIGHT    256
  41. #define    BOOLEAN    int
  42. #define    INT    int
  43.  
  44. #define    IM_WIDTH    64
  45. #define    IM_HEIGHT    64
  46. #define    IM_RADIUS    120
  47.  
  48. #define    LOOPRATE    10
  49. #define    FACERATE    10
  50.  
  51. #define    RATE_DEC    5
  52.  
  53. #define    FT_SPEED    (urand(4)+1)
  54. #define    FT_RATE(x)    (2*(4-ft[x].speed))
  55.  
  56. int    delay_rate=2;
  57.  
  58. struct    RastPort    *rp;
  59. struct    Screen    *s,*scr;
  60. struct    Window    *win;
  61. extern    struct    appel_proc    *p_data_proc;
  62. extern    struct    GfxBase    *GfxBase;
  63.  
  64. #define    MAX_FT    40
  65. typedef    struct    object{
  66.     int    delay;
  67.     int    x,y;
  68.     int    old_x,old_y;
  69.     int    speed;
  70.     int    rate;
  71.     int    phase;
  72.     int    rate_count;
  73.     struct    sequence    *seq;
  74. };
  75. struct    object    ft[MAX_FT];
  76.  
  77. char    *p_text_info="    'Flying Toaster'\n\nA lot of code have been take\nfrom the FlyingToaster blanker\nby Gernot Reisinger (himself\ninspired form the AfterDark's\nFlyingToaster!  :-)\n";
  78.  
  79. struct tom_gadget my_gadg[]={
  80.     {"_Number of objects:",SLIDER,  185,14,60,9,20, 1,MAX_FT,0,0},
  81.     {"_Speed"          ,SLIDER,  185,35,60,9,2, 1,3,0,0},
  82.     {0,        END_LISTE,  0,  0,   0, 0, 0,0,0,0,0}
  83. };
  84.  
  85. USHORT    nr_objects=20;
  86.  
  87.  
  88. /*--------------------------------------------------------------------------
  89.                             V A R I A B L E S
  90.   --------------------------------------------------------------------------*/
  91. LOCAL struct TagItem    win_tags[] = {
  92.     WA_Left,          0,                                    // 0
  93.     WA_Top,           0,                                    // 1
  94.     WA_Width,         640,                                    // 2
  95.     WA_Height,        512,                                    // 3
  96.     WA_IDCMP,         0,                                    // 4
  97.     WA_Flags,         WFLG_SIMPLE_REFRESH|WFLG_BORDERLESS|
  98.                       WFLG_ACTIVATE,                        // 5
  99.     WA_ScreenTitle,   0L,                                    // 6
  100.     WA_CustomScreen,  0L,                                    // 7
  101.     TAG_DONE };
  102.  
  103.  
  104. LOCAL struct ColorSpec  scr_cols[] = {
  105.      0, 0x00, 0x00, 0x00,
  106.      1, 0x0F, 0x0F, 0x0F,
  107.     ~0, 0x00, 0x00, 0x00 };
  108.  
  109. LOCAL UWORD    dri_pens[] = {
  110.     ~0 };
  111.  
  112.  
  113. LOCAL struct TagItem    scr_tags[] = {
  114.     SA_Left,        0,                                        // 0
  115.     SA_Top,            0,                                        // 1
  116.     SA_Depth,        1,                                        // 2
  117.     SA_Behind,        TRUE,                                    // 3
  118.     SA_Quiet,        TRUE,                                    // 4
  119.     SA_Overscan,    OSCAN_MAX,                                // 5
  120.     SA_Colors,        (LONG) scr_cols,                        // 6
  121.     SA_Type,        CUSTOMSCREEN,                            // 7
  122.     SA_DisplayID,    HIRESLACE_KEY,                            // 8
  123.     SA_Pens,        (LONG) dri_pens,                        // 9
  124.     TAG_DONE
  125.     };
  126.  
  127.  
  128. /*--------------------------------------------------------------------------
  129.                                 G L O B A L S
  130.   --------------------------------------------------------------------------*/
  131. char    *ver = "Ver 1.0 (18.12.66)";
  132.  
  133. struct    sequence *seq_list[] = {
  134.     NULL,                                            // free flight
  135.     &default_seq,
  136.     &loop_seq,
  137.     &face_seq
  138.     };
  139.  
  140.                                                     // detach code
  141. LONG    _priority = 0L;
  142. LONG    _BackGroundIO = 1L;
  143. GLOBAL    BPTR    _Backstdout;
  144.  
  145.  
  146.  
  147. /*--------------------------------------------------------------------------*/
  148. /*    open_display                                                            */
  149. /*--------------------------------------------------------------------------*/
  150. BOOLEAN open_display(void)
  151.     {
  152.  
  153.     if ((scr = OpenScreenTagList(NULL,scr_tags)) == NULL)
  154.         return(FALSE);
  155.  
  156.     win_tags[7].ti_Data = (LONG) scr;
  157.     win_tags[2].ti_Data = (LONG) scr->Width;
  158.     win_tags[3].ti_Data = (LONG) scr->Height;
  159.  
  160.     if ((win = OpenWindowTagList(NULL,win_tags)) == NULL)
  161.         return(FALSE);
  162.  
  163.     FreeSprite (0);
  164.     GfxBase->SpriteReserved|=1;
  165.     ScreenToFront(scr);
  166.  
  167.     SetAPen(win->RPort,0);
  168.  
  169.     WaitTOF();
  170.  
  171.     FreeSprite (0);
  172.     GfxBase->SpriteReserved|=1;
  173. /*    SetPointer(win,pointer_data,POINTER_HEIGHT,POINTER_WIDTH,1,1);*/
  174.  
  175.     return(TRUE);
  176.  
  177.     }    /* open_display */
  178.  
  179.  
  180.  
  181. /*--------------------------------------------------------------------------*/
  182. /*    close_display                                                            */
  183. /*--------------------------------------------------------------------------*/
  184. void close_display(void)
  185.     {
  186.  
  187.     if (win != NULL) {
  188.         CloseWindow(win);
  189.         win = NULL;
  190.         }
  191.  
  192.     if (scr != NULL) {
  193.         CloseScreen(scr);
  194.         scr = NULL;
  195.         }
  196.     }    /* close_display */
  197.  
  198.  
  199. /*--------------------------------------------------------------------------*/
  200. /*    urand                                                                    */
  201. /*--------------------------------------------------------------------------*/
  202. int urand(int limit)
  203.     {
  204.     static LONG random_seed = 0xDEAD0123;
  205.  
  206.     random_seed = random_seed * custom.vhposr + 0xe153766f;
  207.  
  208.     return ((int) ((unsigned int) random_seed % limit));
  209.  
  210.     }    /* urand */
  211.  
  212.  
  213.  
  214.  
  215. /*--------------------------------------------------------------------------*/
  216. /*    check_collision                                                            */
  217. /*--------------------------------------------------------------------------*/
  218. INT check_collision(INT i,INT x, INT y)
  219.     {
  220.     INT        j,
  221.             jx,jy;
  222.  
  223.  
  224.     x += IM_WIDTH / 2;
  225.     y += IM_HEIGHT / 2;
  226.  
  227.     for (j = 0; j < nr_objects; j++) {
  228.         if (j != i && ft[j].delay == 0) {
  229.             jx = ft[j].x + IM_WIDTH / 2;
  230.             jy = ft[j].y + IM_HEIGHT / 2;
  231.  
  232.             if (((jx - x) * (jx - x) + (jy - y) * (jy - y)) <= (4 * IM_RADIUS * IM_RADIUS))
  233.                 return(j);
  234.             }
  235.         }
  236.     return(-1);
  237.     }    /* check_collision */
  238.  
  239.  
  240.  
  241. /*--------------------------------------------------------------------------*/
  242. /*    calc_new_pos                                                            */
  243. /*--------------------------------------------------------------------------*/
  244. void calc_new_pos(INT i)
  245.     {
  246.     INT        x,y,
  247.             c;
  248.  
  249.  
  250.     x = ft[i].x - ft[i].speed;
  251.     y = ft[i].y + 2;
  252.  
  253.     ft[i].old_x = ft[i].x;
  254.     ft[i].old_y = ft[i].y;
  255.  
  256.     if ((c = check_collision(i,x,y)) != -1) {
  257.         x = ft[i].x;
  258.         y += 2;
  259.         if ((c = check_collision(i,x,y)) != -1) {
  260.             y = ft[i].y - 4;
  261.             if ((c = check_collision(i,x,y)) != -1)
  262.                 return;
  263.             }
  264.         }
  265.  
  266.     ft[i].x = x;
  267.     ft[i].y = y;
  268.  
  269.     }    /* calc_new_pos */
  270.  
  271.  
  272.  
  273. /*--------------------------------------------------------------------------*/
  274. /*    find_launch_pos                                                            */
  275. /*--------------------------------------------------------------------------*/
  276. BOOLEAN find_launch_pos(INT i)
  277.     {
  278.     BOOLEAN    colides;
  279.     INT        j,
  280.             x,y,
  281.             ix,iy,
  282.             jx,jy;
  283.  
  284.  
  285.     x = win->Width;
  286. /*    y = (urand(7) - 1) * IM_HEIGHT;*/
  287.     y = (urand(win->Height));
  288.  
  289.     FOREVER {
  290.         if (x <= (2 * IM_WIDTH) && y <= - IM_HEIGHT)
  291.             return(FALSE);
  292.  
  293.         ix = x + IM_WIDTH / 2;
  294.         iy = y + IM_HEIGHT / 2;
  295.  
  296.         colides = FALSE;
  297.         for (j = 0; j < nr_objects && ! colides; j++) {
  298.             if (i != j) {
  299.                 jx = ft[j].x + IM_WIDTH / 2;
  300.                 jy = ft[j].y + IM_HEIGHT / 2;
  301.  
  302.                 if (((jx - ix) * (jx - ix) + (jy - iy) * (jy - iy)) <=
  303.                         (4 * IM_RADIUS * IM_RADIUS)) {
  304.                     colides = TRUE;
  305.                     }
  306.                 }
  307.             }
  308.  
  309.         if (! colides) {
  310.             ft[i].x = ft[i].old_x = x;
  311.             ft[i].y = ft[i].old_y = y;
  312.             return(TRUE);
  313.             }
  314.  
  315.         if (y > - IM_HEIGHT)
  316.             y -= IM_HEIGHT;
  317.         else
  318.             x -= IM_WIDTH;
  319.  
  320.         }
  321.     }    /* find_launch_pos */
  322.  
  323.  
  324.  
  325.  
  326. /*--------------------------------------------------------------------------*/
  327. /*    goto_next_img                                                            */
  328. /*--------------------------------------------------------------------------*/
  329. void goto_next_img(struct object *obj, struct sequence *next_seq)
  330.     {
  331.     obj->phase++;
  332.     if (obj->phase >= obj->seq->img_count) {
  333.         obj->phase = 0;
  334.         if (next_seq == NULL) {
  335.             obj->seq = obj->seq->next_sequence;
  336.             if (urand(LOOPRATE) == 0) {
  337.                 obj->rate = RATE_DEC - 1;
  338.                 obj->seq = &loop_seq;
  339.                 }
  340.             else if (urand(FACERATE) == 0) {
  341.                 obj->rate = RATE_DEC - 1;
  342.                 obj->seq = &face_seq;
  343.                 }
  344.             }
  345.         else
  346.             obj->seq = next_seq;
  347.         }
  348.     }    /* goto_next_img */
  349.  
  350.  
  351.  
  352. /*--------------------------------------------------------------------------*/
  353. /*    run_ft                                                                    */
  354. /*--------------------------------------------------------------------------*/
  355. void    dark(void)
  356.     {
  357.     INT        i;
  358.  
  359.     if (! open_display()) {
  360.         close_display();
  361.         return;
  362.     }
  363.  
  364.     nr_objects=my_gadg[0].value;
  365.     delay_rate=my_gadg[1].value;
  366.  
  367.     for (i = 0; i < nr_objects; i++) {
  368.         memset(&ft[i],'\0',sizeof(ft[0]));
  369.  
  370.         if (! find_launch_pos(i)){
  371.             ft[i].delay = 30;
  372.         }else{
  373.             ft[i].delay = urand(50);
  374.         }
  375.         ft[i].phase = urand(IMAGEMAX);
  376.         ft[i].seq = &default_seq;
  377.         ft[i].speed = FT_SPEED;
  378.         ft[i].rate = ft[i].rate_count = FT_RATE(i);
  379.     }
  380.  
  381.     while(tst_end()==FALSE){
  382.         for (i = 0; i < nr_objects; i++) {
  383.             if (ft[i].delay == 0) {
  384.                 calc_new_pos(i);
  385.  
  386.                 ft[i].rate_count -= RATE_DEC;
  387.                 if (ft[i].rate_count < 0) {
  388.                     ft[i].rate_count = ft[i].rate;
  389.                     goto_next_img(&ft[i], NULL);
  390.                     }
  391.  
  392.                 if ((ft[i].old_x != ft[i].x || ft[i].old_y != ft[i].y)) {
  393.  
  394.                     if (ft[i].y > ft[i].old_y) {
  395.                         EraseRect(win->RPort,ft[i].old_x,ft[i].old_y,
  396.                                 ft[i].old_x + IM_WIDTH,ft[i].y + 1);
  397.                         }
  398.                     if (ft[i].x < ft[i].old_x) {
  399.                         EraseRect(win->RPort,ft[i].x + IM_WIDTH - 1,ft[i].y,
  400.                                 ft[i].old_x + IM_WIDTH,ft[i].old_y + IM_HEIGHT);
  401.                         }
  402.                     }
  403.  
  404.                 DrawImage(win->RPort,ft[i].seq->img[ft[i].phase],ft[i].x,ft[i].y);
  405.  
  406.                 if (ft[i].x < - (IM_WIDTH + 1) || ft[i].y > win->Height)
  407.                     ft[i].delay = urand(50);
  408.                 }
  409.             else {
  410.                 ft[i].delay--;
  411.                 if (ft[i].delay == 0) {
  412.                     if (find_launch_pos(i)) {
  413.                         ft[i].speed = FT_SPEED;
  414.                         ft[i].rate = ft[i].rate_count = FT_RATE(i);
  415.                         }
  416.                     else
  417.                         ft[i].delay = 30;
  418.                     }
  419.                 }
  420.             }
  421.  
  422.         Delay(delay_rate);
  423.  
  424.     }
  425.  
  426.     close_display();
  427.  
  428.  
  429. }    /* run_ft */
  430.  
  431.  
  432. void    proc_init()
  433. {
  434.     p_data_proc->type_screen=SCR_OWN;
  435.     p_data_proc->code_ret=DARK_WB_20;
  436. }
  437. void    proc_save()
  438. {
  439. }
  440.  
  441. void    proc_end()
  442. {
  443. }
  444.  
  445.