home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / falcon / demo / f030line / f030line.c next >
C/C++ Source or Header  |  1993-10-16  |  10KB  |  473 lines

  1. #include <tos.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. /******************************************************************/
  5. /* Some of the available video modes supported by the falcon 030  */
  6. /* Vxxx_xxx_xxx = VGA Monitor.                                    */
  7. /* For use with the xbios(0x58, int mode) aka Vsetmode(int mode). */
  8. /******************************************************************/
  9.  
  10. #define  V320_240_4   273
  11. #define  V320_240_16  274
  12. #define  V320_240_256 275
  13. #define  V320_240_32k 276
  14. #define  V320_200_16  402
  15. #define  V640_200_4   409
  16. #define  V640_400_2   152
  17. #define  V640_480_2   24
  18. #define  V640_480_4   25
  19. #define  V640_480_16  26
  20. #define  V640_480_256 27
  21. #define  V320_480_32k 20
  22.  
  23. typedef struct
  24. {
  25.    unsigned int red:5;
  26.    unsigned int green:5;
  27.    unsigned int overlay:1;
  28.    unsigned int blue:5;
  29. }HIGHCOLOR;
  30.  
  31. unsigned int old_vm;
  32. unsigned int line_color;
  33.  
  34. unsigned int *y_table[500];
  35. unsigned long screen_loc;
  36. unsigned int *old_screen;
  37. void line(int x1, int y1, int x2, int y2, unsigned int color);
  38. void plot(int x, int y, unsigned int color);
  39. void xorline(int x1, int y1, int x2, int y2, unsigned int color);
  40. void set_colors(void);
  41.  
  42.  
  43. #define LINECOLOR 576
  44.  
  45. unsigned int color_table[LINECOLOR+10];
  46. int dx1, dx2, dy1, dy2;
  47. int x1, x2, y1, y2;
  48. int mask = 0x1;
  49. void main()
  50. {
  51.    unsigned int *screen_loc;
  52.    unsigned int *old_screen;
  53.    unsigned int *t;
  54.    register long int index;
  55.    
  56.    screen_loc = (unsigned int *) Malloc(-1L);
  57.    if(screen_loc < (unsigned int *)307400L)
  58.    {
  59.       printf("Unable to allocate screen memeory, sorry!");
  60.       exit(0);
  61.    }
  62.  
  63.    set_colors();
  64.    
  65.    old_screen = (unsigned int *) Physbase();
  66.    t = screen_loc = (unsigned int *) Malloc(307400L);
  67.    
  68.    for(index = 0; index < 500; index++)
  69.       y_table[index] = (unsigned int *)(screen_loc+((long)index * 320L));
  70.  
  71.    old_vm = (unsigned int)Vsetmode(V320_480_32k);
  72.    xbios(5,screen_loc, screen_loc, 3, V320_480_32k);
  73.    for(index = 0; index < (307200L/2L); index++)
  74.        *(t++) = 0;
  75.  
  76.    while(Bconstat(2)) Bconin(2);
  77.    line_color = 0;
  78.    
  79.    x1 = (int)(Random() & 0xff);
  80.    y1 = (int)(Random() & 0xff);
  81.    x2 = (int)(Random() & 0xff);
  82.    y2 = (int)(Random() & 0xff);
  83.  
  84.    dx1 = -1;
  85.    dx2 = 1;
  86.    dy1 = -1;
  87.    dy2 = 1;
  88.    
  89.    while(Bconstat(2)) Bconin(2);
  90.    
  91.    while(1)
  92.    {
  93.       if(x1 > 318) 
  94.       {
  95.          x1 = 318;
  96.          dx1 = -((int)(Random() & mask)+1);
  97.       }
  98.       
  99.       if(x1 < 1) 
  100.       {
  101.          x1 = 1;
  102.          dx1 = ((int)(Random() & mask)+1);
  103.       }
  104.  
  105.       if(x2 > 318) 
  106.       {
  107.          x2 = 318;
  108.          dx2 = -((int)(Random() & mask)+1);
  109.       }
  110.       
  111.       if(x2 < 1) 
  112.       {
  113.          x2 = 1;
  114.          dx2 = ((int)(Random() & mask)+1);
  115.       }
  116.       
  117.       if(y1 > 478) 
  118.       {
  119.          y1 = 478;
  120.          dy1 = -((int)(Random() & mask)+1);
  121.       }
  122.       
  123.       if(y1 < 1) 
  124.       {
  125.          y1 = 1;
  126.          dy1 = ((int)(Random() & mask)+1);
  127.       }
  128.       
  129.       if(y2 > 478) 
  130.       {
  131.          y2 = 478;
  132.          dy2 = -((int)(Random() & mask)+1);
  133.       }
  134.       
  135.       if(y2 < 1) 
  136.       {
  137.          y2 = 1;
  138.          dy2 = ((int)(Random() & mask)+1);
  139.       }
  140.       
  141.       x1 += dx1;
  142.       y1 += dy1;
  143.       x2 += dx2;
  144.       y2 += dy2;
  145.            
  146.       line_color++;
  147.       line(x1, y1, x2, y2, color_table[line_color % LINECOLOR]);
  148.  
  149.       if(Bconstat(2)) break;
  150.  
  151.    }  
  152.    
  153.    Bconin(2);
  154.    xbios(5,old_screen, old_screen, 3, old_vm); 
  155.    Mfree(screen_loc);
  156.  
  157. }
  158.  
  159. /****************************************************************/
  160. /* Draw a line x1,y1,x2,y2,color                                */
  161. /****************************************************************/
  162. void line(int x1, int y1, int x2, int y2, unsigned int color)
  163. {
  164.  
  165.    #define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
  166.  
  167.    
  168.    register int x, y, py, px;
  169.    register int dx,dy,dxabs,dyabs,i,sdx,sdy;
  170.  
  171.    if(x1 < 1) x1 = 0;
  172.    if(x1 > 318) x1 = 319;
  173.    if(x2 < 1) x2 = 0;
  174.    if(x2 > 318) x2 = 319;
  175.    if(y1 > 478) y1 = 479;
  176.    if(y1 < 1) y1 = 0;
  177.    if(y2 > 478) y2 = 479;
  178.    if(y2 < 1) y2 = 0;
  179.       
  180.    dx = x2 - x1;
  181.    dy = y2 - y1;
  182.  
  183.    sdx = sign(dx);
  184.    sdy = sign(dy);
  185.    dxabs = abs(dx);
  186.    dyabs = abs(dy);
  187.    x = 0;
  188.    y = 0;
  189.    px = x1;
  190.    py = y1;
  191.    
  192.    if(dxabs >= dyabs)
  193.    {
  194.       for(i = 0; i <= dxabs; i++)
  195.       {
  196.          y += dyabs;
  197.          
  198.          if(y >= dxabs)
  199.          {
  200.             y -= dxabs;
  201.             py += sdy;
  202.          }
  203.          
  204.          *((unsigned int *)((y_table[py]) + (px))) = color;
  205.  
  206.          px += sdx;
  207.       }
  208.    }
  209.    else
  210.    {
  211.       for(i = 0; i < dyabs; i++)
  212.       {
  213.          x += dxabs;
  214.          if(x >= dyabs)
  215.          {
  216.             x -= dyabs;
  217.             px += sdx;
  218.          }
  219.  
  220.          *((unsigned int *)((y_table[py]) + (px))) = color;
  221.          py += sdy;
  222.       }
  223.    }
  224. }
  225. /****************************************************************/
  226. /* Draw a line x1,y1,x2,y2,color                                */
  227. /****************************************************************/
  228. void xorline(int x1, int y1, int x2, int y2, unsigned int color)
  229. {
  230.  
  231.    #define sign(x) ((x) > 0 ? 1: ((x) == 0 ? 0: (-1)))
  232.  
  233.    
  234.    register int x, y, py, px;
  235.    register int dx,dy,dxabs,dyabs,i,sdx,sdy;
  236.  
  237.    if(x1 < 1) x1 = 0;
  238.    if(x1 > 318) x1 = 319;
  239.    if(x2 < 1) x2 = 0;
  240.    if(x2 > 318) x2 = 319;
  241.    if(y1 > 478) y1 = 479;
  242.    if(y1 < 1) y1 = 0;
  243.    if(y2 > 478) y2 = 479;
  244.    if(y2 < 1) y2 = 0;
  245.    
  246.    dx = x2 - x1;
  247.    dy = y2 - y1;
  248.  
  249.    sdx = sign(dx);
  250.    sdy = sign(dy);
  251.    dxabs = abs(dx);
  252.    dyabs = abs(dy);
  253.    x = 0;
  254.    y = 0;
  255.    px = x1;
  256.    py = y1;
  257.    
  258.    if(dxabs >= dyabs)
  259.    {
  260.       for(i = 0; i <= dxabs; i++)
  261.       {
  262.          y += dyabs;
  263.          
  264.          if(y >= dxabs)
  265.          {
  266.             y -= dxabs;
  267.             py += sdy;
  268.          }
  269.          
  270.          *((unsigned int *)((y_table[py]) + (px))) ^= color;
  271.          px += sdx;
  272.       }
  273.    }
  274.    else
  275.    {
  276.       for(i = 0; i < dyabs; i++)
  277.       {
  278.          x += dxabs;
  279.          if(x >= dyabs)
  280.          {
  281.             x -= dyabs;
  282.             px += sdx;
  283.          }
  284.  
  285.          *((unsigned int *)((y_table[py]) + (px))) ^= color;
  286.          py += sdy;
  287.       }
  288.    }
  289. }
  290.  
  291. /************************************************************/
  292. /* Plot color at x,y.                                       */
  293. /************************************************************/
  294. void plot(int x, int y, unsigned int color)
  295. {
  296.    register unsigned int *temp;
  297.    temp = (unsigned int *) ((y_table[y]) + (x));
  298.    *temp = color;
  299. }
  300. /*************************************************************/
  301. /* Set the color table.                                      */
  302. /*************************************************************/
  303. void set_colors(void)
  304. {
  305.    int a, b;
  306.    HIGHCOLOR tc;
  307.    unsigned int *ti;
  308.    b = 0;
  309.    
  310.    ti = (unsigned int *)&tc;
  311.    
  312.    for(a = 0; a < 32; a++)
  313.    {
  314.       tc.red = a;
  315.       tc.green = 0;
  316.       tc.blue = 0;
  317.       color_table[b] = *ti;
  318.       b++;
  319.    }
  320.    
  321.    for(a = 31; a > 0; a--)
  322.    {
  323.       tc.red = a;
  324.       tc.green = 0;
  325.       tc.blue = 0;
  326.       color_table[b] = *ti;
  327.       b++;
  328.    }
  329.    
  330.    for(a = 0; a < 32; a++)
  331.    {
  332.       tc.red = a;
  333.       tc.green = a;
  334.       tc.blue = 0;
  335.       color_table[b] = *ti;
  336.       b++;
  337.    }
  338.    
  339.    for(a = 31; a > 0; a--)
  340.    {
  341.       tc.red = a;
  342.       tc.green = a;
  343.       tc.blue = 0;
  344.       color_table[b] = *ti;
  345.       b++;
  346.    }
  347.  
  348.    for(a = 0; a < 32; a++)
  349.    {
  350.       tc.red = a;
  351.       tc.green = a;
  352.       tc.blue = a;
  353.       color_table[b] = *ti;
  354.       b++;
  355.    }
  356.    
  357.    for(a = 31; a > 0; a--)
  358.    {
  359.       tc.red = a;
  360.       tc.green = a;
  361.       tc.blue = a;
  362.       color_table[b] = *ti;
  363.       b++;
  364.    }
  365.    for(a = 0; a < 32; a++)
  366.    {
  367.       tc.red = 0;
  368.       tc.green = a;
  369.       tc.blue = a;
  370.       color_table[b] = *ti;
  371.       b++;
  372.    }
  373.    
  374.    for(a = 31; a > 0; a--)
  375.    {
  376.       tc.red = 0;
  377.       tc.green = a;
  378.       tc.blue = a;
  379.       color_table[b] = *ti;
  380.       b++;
  381.    }
  382.    for(a = 0; a < 32; a++)
  383.    {
  384.       tc.red = 0;
  385.       tc.green = 0;
  386.       tc.blue = a;
  387.       color_table[b] = *ti;
  388.       b++;
  389.    }
  390.    
  391.    for(a = 31; a > 0; a--)
  392.    {
  393.       tc.red = 0;
  394.       tc.green = 0;
  395.       tc.blue = a;
  396.       color_table[b] = *ti;
  397.       b++;
  398.    }
  399.    
  400.    /*************************************/
  401.    
  402.    for(a = 0; a < 32; a++)
  403.    {
  404.       tc.red = a;
  405.       tc.green = 0;
  406.       tc.blue = 15;
  407.       color_table[b] = *ti;
  408.       b++;
  409.    }
  410.    
  411.    for(a = 31; a > 0; a--)
  412.    {
  413.       tc.red = a;
  414.       tc.green = 0;
  415.       tc.blue = 15;
  416.       color_table[b] = *ti;
  417.       b++;
  418.    }
  419.    
  420.    for(a = 0; a < 32; a++)
  421.    {
  422.       tc.red = a;
  423.       tc.green = a;
  424.       tc.blue = 15;
  425.       color_table[b] = *ti;
  426.       b++;
  427.    }
  428.    
  429.    for(a = 31; a > 0; a--)
  430.    {
  431.       tc.red = a;
  432.       tc.green = a;
  433.       tc.blue = 15;
  434.       color_table[b] = *ti;
  435.       b++;
  436.    }
  437.  
  438.    for(a = 0; a < 32; a++)
  439.    {
  440.       tc.red = 15;
  441.       tc.green = a;
  442.       tc.blue = a;
  443.       color_table[b] = *ti;
  444.       b++;
  445.    }
  446.    
  447.    for(a = 31; a > 0; a--)
  448.    {
  449.       tc.red = 15;
  450.       tc.green = a;
  451.       tc.blue = a;
  452.       color_table[b] = *ti;
  453.       b++;
  454.    }
  455.    for(a = 0; a < 32; a++)
  456.    {
  457.       tc.red = 15;
  458.       tc.green = 0;
  459.       tc.blue = a;
  460.       color_table[b] = *ti;
  461.       b++;
  462.    }
  463.    
  464.    for(a = 31; a > 0; a--)
  465.    {
  466.       tc.red = 15;
  467.       tc.green = 0;
  468.       tc.blue = a;
  469.       color_table[b] = *ti;
  470.       b++;
  471.    }
  472.    
  473. }