home *** CD-ROM | disk | FTP | other *** search
/ DOS/V Power Report 1997 March / VPR9703A.ISO / VPR_DATA / DOGA / SOURCES / REND.LZH / REND / STAR.C < prev    next >
C/C++ Source or Header  |  1996-05-28  |  6KB  |  331 lines

  1. #ifdef STAR
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <assert.h>
  6. #include "reader.h"
  7. #include "glib.h"
  8. #include "rend.h"
  9.  
  10. #define ESC                0x1B
  11. #define MAXRNDARY        127
  12.  
  13. static unsigned int *starbuf_x;
  14. static unsigned int *starbuf_y;
  15. static unsigned int *starbuf_len;
  16. static ColorCode *starbuf_col;
  17. static unsigned int nowstars;
  18. static int        starbuf_size;
  19.  
  20. static int        nowline = 0;
  21. static int        nowp = 0;
  22.  
  23. static int        stars = 0 ;
  24. static int        stardir = 0;
  25. static int        stardirstep = 0;
  26. static int        seed ;
  27. static int        rndary[MAXRNDARY] ;
  28. #if 0
  29. static Point     *posbuf, *tempbuf ;
  30. static ColorCode    *col ;
  31. #endif
  32.  
  33. static Matrix starview, starstep;
  34. #if 0
  35. static    void    getrandpos( Point* );
  36. #endif
  37. static    void    rndinit( void );
  38. static    int        rnd( void );
  39. static    void    starpset( int x, int y, ColorCode col, int len);
  40. static    void    getstar( Point *pos, ColorCode *col);
  41.  
  42.  
  43. void StarInit(int s, int dir, int step)
  44. {
  45.     stars = s;
  46.     if (step > 0) {
  47.         Matrix tmp;
  48.         stardir = dir;
  49.         stardirstep = step;
  50.         m_unit(starview);
  51.         m_unit(starstep);
  52.         m_rot(tmp, 0, (Float)dir);
  53.         m_mult(starstep, tmp, starstep);
  54.         m_rot(tmp, 1, (Float)step);
  55.         m_mult(starstep, tmp, starstep);
  56.         m_rot(tmp, 0, -(Float)dir);
  57.         m_mult(starstep, tmp, starstep);
  58.     }
  59. }
  60.  
  61. void    StarStep(void)
  62. {
  63.     if (stardirstep > 0) {
  64.         m_mult( starview, starstep, starview);
  65.     }
  66. }
  67.  
  68. /*    表示    */
  69. void    Star(int s, Matrix view, Float dist)
  70. {
  71.     int        i;
  72.     int        x, y ;
  73.  
  74.     Matrix mat;
  75.  
  76. /*
  77.     getrandpos( posbuf );
  78. */
  79.     if (s == 0 && stars == 0) {
  80.         return;
  81.     }
  82.     if (s > 0) {
  83.         stars = s;
  84.     }
  85.  
  86.     /*    座標計算  */
  87.  
  88.     if (stardirstep > 0) {
  89.         Matrix tmp, m;
  90.         m_inv( tmp, starview);
  91.  
  92.         /*    奥行き座標軸の変更    */
  93.         m_unit( m );
  94.         m[0][0] = m[1][1] = m[2][2] = 0.0 ;
  95.         m[0][2] = m[1][0] = m[2][1] = 1.0 ;
  96.         m_mult( tmp, tmp, m );
  97.  
  98.         /*    スクリーン座標変換    */
  99.         m_unit( m );
  100.         m[2][0] = (Float)XPixel / 2.0 ;
  101.         m[2][1] = (Float)YPixel / 2.0 ;
  102.         m[0][0] = - dist * (Float)XPixel ;
  103.         m[1][1] = - dist * (Float)XPixel / PixelRatio;
  104.         m_mult( tmp, tmp, m );
  105.         m_copy( mat, tmp );
  106.         m_mult( starview, starstep, starview);
  107.     } else {
  108.         m_copy( mat, view);
  109.     }
  110.  
  111.     nowstars = 0;
  112.     starbuf_size = stars * antiareas / 2;
  113.     starbuf_x = dataalloc(sizeof(*starbuf_x) * starbuf_size);
  114.     starbuf_y = dataalloc(sizeof(*starbuf_y) * starbuf_size);
  115.     starbuf_len = dataalloc(sizeof(*starbuf_len) * starbuf_size);
  116.     starbuf_col = dataalloc(sizeof(*starbuf_col) * starbuf_size);
  117.     rndinit();
  118.     for( i = 0 ; i < stars ; ++i )
  119.     {
  120.         Point pos, temp;
  121.         ColorCode col;
  122.         getstar(&pos, &col);
  123.  
  124.         vec_mult_mat( &temp, &pos, mat, 1, 0 );
  125.  
  126.         if (temp[2] != 0.0) {
  127.             int loop;
  128.             x = temp[0] / temp[2] ;
  129.             y = temp[1] / temp[2] ;
  130.             for    (loop = -antiareas+1; loop < antiareas; ++loop) {
  131.                 starpset( x-antiareas+1, y+loop,   col, antiareas*2-1 );
  132.             }
  133. #if 0
  134.             } else {
  135.                 int j;
  136.                 Float r = (Float)((XPixel / 320) + 1) * 1.41421356 / 2.0;
  137.                 starpset( x-(int)r, y, col, 2*(int)r );
  138.                 for (j = r-1; j > 0; --j) {
  139.                     int k;
  140.                     k = (int)sqrt(r * r - (Float)(j * j));
  141.                     starpset( x-k, y-j, col, 2*k );
  142.                     starpset( x-k, y+j, col, 2*k );
  143.                 }
  144.             }
  145. #endif
  146.         }
  147.     }
  148. #if 0
  149. #define ERR(sss) {FILE *fp;fp=fopen("tmp.log","a");sss;fclose(fp);}
  150. ERR(fprintf(fp, "starbuf_size=%08d, nowstars=%08d\n", starbuf_size, nowstars));
  151. #endif
  152.     starbuf_y[nowstars] = YPixel;
  153.     nowp = 0;
  154.     nowline = 0;
  155. }
  156.  
  157.  
  158. #if 0
  159. /*    乱数の発生    */
  160. static    void    getrandpos( pos )
  161. Point    *pos ;
  162. {
  163.     int        n, i, c ;
  164.     Point     *p ;
  165.  
  166.     rndinit();
  167.     for( n = 0 ; n < stars ; ++n )
  168.     {
  169.         p = pos++ ;
  170.         do
  171.         {
  172.             for( i = 0 ; i < 3 ; ++i )
  173.                 p[0][i] = (Float)( rnd() - 1024 );
  174.         }
  175.         while( p[0][0] == 0.0 && p[0][1] == 0.0 && p[0][2] == 0.0 );
  176.  
  177.         c = rnd() % 7 ;
  178.         if ( c == 0 )
  179.         {
  180.             col[n] = 0xffffff00;
  181.         }
  182.         else if ( c <= 2 )
  183.         {
  184.             col[n] = 0xc0c0c000;
  185.         }
  186. #if 1
  187.         else
  188.         {
  189.             col[n] = 0x80808000;
  190.         }
  191. #else
  192.         else if (c <= 5)
  193.         {
  194.             col[n] = 0x80808000;
  195.         }
  196.         else
  197.         {
  198.             int c = rnd() % 768;
  199.             if (c < 256) {
  200.                 col[n] = (c << 24) | 0x00ff0000;
  201.             } else if (c < 512) {
  202.                 col[n] = 0xffff0000 | ((c-256)<<8);
  203.             } else {
  204.                 col[n] = 0xff00ff00 | ((c-512)<<16);
  205.             }
  206.         }
  207. #endif
  208.     }
  209. }
  210. #endif
  211.  
  212. static    void    getstar( Point *pos, ColorCode *col)
  213. {
  214.     int    i, c ;
  215.  
  216.     do
  217.     {
  218.         for( i = 0 ; i < 3 ; ++i )
  219.             (*pos)[i] = (Float)( rnd() - 1024 );
  220.     }
  221.     while( (*pos)[0] == 0.0 && (*pos)[1] == 0.0 && (*pos)[2] == 0.0 );
  222.  
  223.     c = rnd() % 7 ;
  224.     if ( c == 0 )
  225.     {
  226.         *col = 0xffffff00L;
  227.     }
  228.     else if ( c <= 2 )
  229.     {
  230.         *col = 0xc0c0c000L;
  231.     }
  232.     else
  233.     {
  234.         *col = 0x80808000L;
  235.     }
  236. }
  237.  
  238. /*    乱数の初期化    */
  239. static    void    rndinit(void)
  240. {
  241.     int        i ;
  242.  
  243.     seed = 12345 ;
  244.  
  245.     for( i = 0 ; i < MAXRNDARY ; ++i )
  246.     {
  247.         seed = (int)(( (long)seed * 899L + 48243L ) % 32768L) ;
  248.         rndary[i] = seed ;
  249.     }
  250. }
  251.  
  252. /*    乱数の発生    */
  253. static    int        rnd(void)
  254. {
  255.     int        n, r ;
  256.  
  257.     seed = (int)(( (long)seed * 899L + 48243L ) % 32768L) ;
  258.     n = seed % MAXRNDARY ;
  259.     r = rndary[n] ;
  260.     rndary[n] = seed ;
  261.  
  262.     return( r >> 4 );
  263. }
  264.  
  265.  
  266. static void    starpset( int x, int y, ColorCode col, int len)
  267. {
  268.     int i, j;
  269.     if (0 <= x && x+len < XPixel && 0 <= y && y <= YPixel && nowstars < starbuf_size-2) {
  270. #if 0
  271.         for (i = 0; i < nowstars; ++i) {
  272.             if (y < starbuf_y[i]
  273.              || (y == starbuf_y[i] && x < starbuf_x[i]))    {
  274.                 for ( j = nowstars; j > i; --j) {
  275.                     starbuf_x[j] = starbuf_x[j-1];
  276.                     starbuf_y[j] = starbuf_y[j-1];
  277.                     starbuf_col[j] = starbuf_col[j-1];
  278.                     starbuf_len[j] = starbuf_len[j-1];
  279.                 }
  280.                 break;
  281.             }
  282.         }
  283. #else
  284.         int i1, i2;
  285.         i1 = -1;
  286.         i2 = nowstars;
  287.         while (i1 < i2-1) {
  288.             i = (i1+i2)/2;
  289.             if (y < starbuf_y[i]
  290.              || (y == starbuf_y[i] && x < starbuf_x[i]))    {
  291.                 i2 = i;
  292.             } else {
  293.                 i1 = i;
  294.             }
  295.         }
  296.         if (i2 < nowstars) {
  297.             for (j = nowstars; j > i2; --j) {
  298.                 starbuf_x[j] = starbuf_x[j-1];
  299.                 starbuf_y[j] = starbuf_y[j-1];
  300.                 starbuf_col[j] = starbuf_col[j-1];
  301.                 starbuf_len[j] = starbuf_len[j-1];
  302.             }
  303.         }
  304.         i = i2;
  305. #endif
  306.         starbuf_x[i] = x;
  307.         starbuf_y[i] = y;
  308.         starbuf_col[i] = col;
  309.         starbuf_len[i] = len;
  310.         nowstars++;
  311.     }
  312. }
  313.  
  314.  
  315. void    StarCopy(ColorCode *buf)
  316. {
  317.     ColorCode *p;
  318.     int i;
  319.     if (stars == 0) {
  320.         return;
  321.     }
  322.     for (;nowline == starbuf_y[nowp];nowp++) {
  323.         p = buf + starbuf_x[nowp];
  324.         for (i = starbuf_len[nowp]; i > 0; --i) {
  325.             *p++ = starbuf_col[nowp];
  326.         }
  327.     }
  328.     nowline++;
  329. }
  330. #endif
  331.