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 >
Wrap
C/C++ Source or Header
|
1996-05-28
|
6KB
|
331 lines
#ifdef STAR
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "reader.h"
#include "glib.h"
#include "rend.h"
#define ESC 0x1B
#define MAXRNDARY 127
static unsigned int *starbuf_x;
static unsigned int *starbuf_y;
static unsigned int *starbuf_len;
static ColorCode *starbuf_col;
static unsigned int nowstars;
static int starbuf_size;
static int nowline = 0;
static int nowp = 0;
static int stars = 0 ;
static int stardir = 0;
static int stardirstep = 0;
static int seed ;
static int rndary[MAXRNDARY] ;
#if 0
static Point *posbuf, *tempbuf ;
static ColorCode *col ;
#endif
static Matrix starview, starstep;
#if 0
static void getrandpos( Point* );
#endif
static void rndinit( void );
static int rnd( void );
static void starpset( int x, int y, ColorCode col, int len);
static void getstar( Point *pos, ColorCode *col);
void StarInit(int s, int dir, int step)
{
stars = s;
if (step > 0) {
Matrix tmp;
stardir = dir;
stardirstep = step;
m_unit(starview);
m_unit(starstep);
m_rot(tmp, 0, (Float)dir);
m_mult(starstep, tmp, starstep);
m_rot(tmp, 1, (Float)step);
m_mult(starstep, tmp, starstep);
m_rot(tmp, 0, -(Float)dir);
m_mult(starstep, tmp, starstep);
}
}
void StarStep(void)
{
if (stardirstep > 0) {
m_mult( starview, starstep, starview);
}
}
/* 表示 */
void Star(int s, Matrix view, Float dist)
{
int i;
int x, y ;
Matrix mat;
/*
getrandpos( posbuf );
*/
if (s == 0 && stars == 0) {
return;
}
if (s > 0) {
stars = s;
}
/* 座標計算 */
if (stardirstep > 0) {
Matrix tmp, m;
m_inv( tmp, starview);
/* 奥行き座標軸の変更 */
m_unit( m );
m[0][0] = m[1][1] = m[2][2] = 0.0 ;
m[0][2] = m[1][0] = m[2][1] = 1.0 ;
m_mult( tmp, tmp, m );
/* スクリーン座標変換 */
m_unit( m );
m[2][0] = (Float)XPixel / 2.0 ;
m[2][1] = (Float)YPixel / 2.0 ;
m[0][0] = - dist * (Float)XPixel ;
m[1][1] = - dist * (Float)XPixel / PixelRatio;
m_mult( tmp, tmp, m );
m_copy( mat, tmp );
m_mult( starview, starstep, starview);
} else {
m_copy( mat, view);
}
nowstars = 0;
starbuf_size = stars * antiareas / 2;
starbuf_x = dataalloc(sizeof(*starbuf_x) * starbuf_size);
starbuf_y = dataalloc(sizeof(*starbuf_y) * starbuf_size);
starbuf_len = dataalloc(sizeof(*starbuf_len) * starbuf_size);
starbuf_col = dataalloc(sizeof(*starbuf_col) * starbuf_size);
rndinit();
for( i = 0 ; i < stars ; ++i )
{
Point pos, temp;
ColorCode col;
getstar(&pos, &col);
vec_mult_mat( &temp, &pos, mat, 1, 0 );
if (temp[2] != 0.0) {
int loop;
x = temp[0] / temp[2] ;
y = temp[1] / temp[2] ;
for (loop = -antiareas+1; loop < antiareas; ++loop) {
starpset( x-antiareas+1, y+loop, col, antiareas*2-1 );
}
#if 0
} else {
int j;
Float r = (Float)((XPixel / 320) + 1) * 1.41421356 / 2.0;
starpset( x-(int)r, y, col, 2*(int)r );
for (j = r-1; j > 0; --j) {
int k;
k = (int)sqrt(r * r - (Float)(j * j));
starpset( x-k, y-j, col, 2*k );
starpset( x-k, y+j, col, 2*k );
}
}
#endif
}
}
#if 0
#define ERR(sss) {FILE *fp;fp=fopen("tmp.log","a");sss;fclose(fp);}
ERR(fprintf(fp, "starbuf_size=%08d, nowstars=%08d\n", starbuf_size, nowstars));
#endif
starbuf_y[nowstars] = YPixel;
nowp = 0;
nowline = 0;
}
#if 0
/* 乱数の発生 */
static void getrandpos( pos )
Point *pos ;
{
int n, i, c ;
Point *p ;
rndinit();
for( n = 0 ; n < stars ; ++n )
{
p = pos++ ;
do
{
for( i = 0 ; i < 3 ; ++i )
p[0][i] = (Float)( rnd() - 1024 );
}
while( p[0][0] == 0.0 && p[0][1] == 0.0 && p[0][2] == 0.0 );
c = rnd() % 7 ;
if ( c == 0 )
{
col[n] = 0xffffff00;
}
else if ( c <= 2 )
{
col[n] = 0xc0c0c000;
}
#if 1
else
{
col[n] = 0x80808000;
}
#else
else if (c <= 5)
{
col[n] = 0x80808000;
}
else
{
int c = rnd() % 768;
if (c < 256) {
col[n] = (c << 24) | 0x00ff0000;
} else if (c < 512) {
col[n] = 0xffff0000 | ((c-256)<<8);
} else {
col[n] = 0xff00ff00 | ((c-512)<<16);
}
}
#endif
}
}
#endif
static void getstar( Point *pos, ColorCode *col)
{
int i, c ;
do
{
for( i = 0 ; i < 3 ; ++i )
(*pos)[i] = (Float)( rnd() - 1024 );
}
while( (*pos)[0] == 0.0 && (*pos)[1] == 0.0 && (*pos)[2] == 0.0 );
c = rnd() % 7 ;
if ( c == 0 )
{
*col = 0xffffff00L;
}
else if ( c <= 2 )
{
*col = 0xc0c0c000L;
}
else
{
*col = 0x80808000L;
}
}
/* 乱数の初期化 */
static void rndinit(void)
{
int i ;
seed = 12345 ;
for( i = 0 ; i < MAXRNDARY ; ++i )
{
seed = (int)(( (long)seed * 899L + 48243L ) % 32768L) ;
rndary[i] = seed ;
}
}
/* 乱数の発生 */
static int rnd(void)
{
int n, r ;
seed = (int)(( (long)seed * 899L + 48243L ) % 32768L) ;
n = seed % MAXRNDARY ;
r = rndary[n] ;
rndary[n] = seed ;
return( r >> 4 );
}
static void starpset( int x, int y, ColorCode col, int len)
{
int i, j;
if (0 <= x && x+len < XPixel && 0 <= y && y <= YPixel && nowstars < starbuf_size-2) {
#if 0
for (i = 0; i < nowstars; ++i) {
if (y < starbuf_y[i]
|| (y == starbuf_y[i] && x < starbuf_x[i])) {
for ( j = nowstars; j > i; --j) {
starbuf_x[j] = starbuf_x[j-1];
starbuf_y[j] = starbuf_y[j-1];
starbuf_col[j] = starbuf_col[j-1];
starbuf_len[j] = starbuf_len[j-1];
}
break;
}
}
#else
int i1, i2;
i1 = -1;
i2 = nowstars;
while (i1 < i2-1) {
i = (i1+i2)/2;
if (y < starbuf_y[i]
|| (y == starbuf_y[i] && x < starbuf_x[i])) {
i2 = i;
} else {
i1 = i;
}
}
if (i2 < nowstars) {
for (j = nowstars; j > i2; --j) {
starbuf_x[j] = starbuf_x[j-1];
starbuf_y[j] = starbuf_y[j-1];
starbuf_col[j] = starbuf_col[j-1];
starbuf_len[j] = starbuf_len[j-1];
}
}
i = i2;
#endif
starbuf_x[i] = x;
starbuf_y[i] = y;
starbuf_col[i] = col;
starbuf_len[i] = len;
nowstars++;
}
}
void StarCopy(ColorCode *buf)
{
ColorCode *p;
int i;
if (stars == 0) {
return;
}
for (;nowline == starbuf_y[nowp];nowp++) {
p = buf + starbuf_x[nowp];
for (i = starbuf_len[nowp]; i > 0; --i) {
*p++ = starbuf_col[nowp];
}
}
nowline++;
}
#endif