home *** CD-ROM | disk | FTP | other *** search
/ Trixter's Scene Collection / trixter.zip / trixter / Demos / GN_SKP.ZIP / SRC / GN2.C < prev    next >
C/C++ Source or Header  |  1994-06-26  |  54KB  |  2,476 lines

  1. #include <alloc.h>
  2. #include <conio.h>
  3. #include <ctype.h>
  4. #include <dos.h>
  5. #include <math.h>
  6. #include <mem.h>
  7. #include <sound.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <time.h>
  11. #include <wgt4.h>
  12. #include <wgt3d.h>
  13.  
  14. extern color pal1[256];
  15. extern color pal2[256];
  16. extern color blackpal[256];
  17. extern int i,x,y,j;
  18. extern block virt, virt2, virt3;
  19. extern int sortlist[50];
  20. extern block data;
  21. extern int ctr;
  22. extern int music_on;
  23.  
  24. typedef struct {     /* Used to keep track what portion of the */
  25.   int x1,y1,x2,y2;     /* screen has been changed, so we can update */
  26.   } rect;         /* the least amount of video memory */
  27. extern rect lastrect,thisrect;
  28.  
  29.  
  30. void draw_land_plasma(void);
  31.  
  32. /* Landscape Engine - written by Chris Egerter */
  33.  
  34. #define SCREEN_WIDTH 256
  35. #define HSCREEN_WIDTH 128
  36. #define SCREEN_HEIGHT 30
  37.  
  38. block gum;
  39. block landscape;
  40.  
  41. int posx,posy;  /* Player's location */
  42.  
  43. char c;
  44. float hgt;
  45.  
  46. block texture;
  47.  
  48. int ox[320];
  49. unsigned char oc[320];
  50.  
  51.  
  52. int pspy[50];
  53. long ff[50];
  54. int dxy[50];
  55. int dxx[320];
  56.  
  57. void gen_plasma(void);
  58. void draw_plasma(void);
  59. extern float teller;
  60. extern int plasmatype;
  61.  
  62. extern long memleft;
  63.  
  64. void showmem(void)
  65. {
  66.  wsetmode(3);
  67.  printf("%lu",farcoreleft());
  68.  getch();
  69.  vga256();
  70. }
  71.  
  72. void calc_tables(void)
  73. {
  74. int i;
  75. long x,y,z;
  76.  
  77. for (i=1; i<SCREEN_HEIGHT; i++)
  78.  {
  79.   pspy[i] = (long)5900/(SCREEN_HEIGHT-i+15)-20;
  80.   ff[i] = (long)20024/(SCREEN_HEIGHT-i+15);
  81.   dxy[i] = (int)(i+30);
  82.  }
  83.  
  84. for (i=0; i<SCREEN_WIDTH; i++)
  85.    dxx[i] = (long)(i-160) * 1510 / 100;
  86. }
  87.  
  88.  
  89. void wvline(int x, int y, int y2, unsigned char col)
  90. {
  91. block temp;
  92. int length;
  93.  
  94. if (y < ty) y = ty;
  95. else if (y > by) y = by;
  96. if (y2 < ty) y2 = ty;
  97. else if (y2 > by) y2 = by;
  98. temp = abuf + y*320 + x;
  99. length = y2 - y + 1;
  100.  
  101. asm {
  102.   push ds
  103.   mov cx,length
  104.   lds si, temp
  105.   mov al, col
  106.   }
  107. vlineloop:
  108. ;
  109. asm {
  110.   mov [ds:si],al
  111.   add si,320
  112.   loop vlineloop
  113.   pop ds
  114.   }
  115. }
  116.  
  117.  
  118. void generate_table(void)
  119. {
  120. unsigned int x,y;
  121. unsigned int dsx;
  122. unsigned int dx;
  123. int *gumptr;
  124. int *gumptr2;
  125.  
  126. int ofs;
  127.  
  128. /*
  129. ofs = 0;
  130.  
  131. for (y = 1; y < SCREEN_HEIGHT; y++)
  132.  {
  133.   for (x = 0; x < SCREEN_WIDTH; x++)
  134.   {
  135.    dx = dxx[x] / dxy[y] - 55;
  136.  
  137.    dsx = y*320+dx;
  138.    memcpy(&gum[ofs], &dsx, 2);
  139.    ofs += 2;
  140.   }
  141.  }
  142.  
  143. gumptr = &gum[0];
  144. gumptr2= &gum[2];
  145. *gumptr = *gumptr2;
  146. gumptr++;
  147. gumptr2++;
  148.  
  149. for (y = 1; y < SCREEN_HEIGHT; y++)
  150.   for (x = 0; x < SCREEN_WIDTH; x++)
  151.   {
  152.    *gumptr = *gumptr2 - *gumptr;
  153.    gumptr++;
  154.    gumptr2++;
  155.   }
  156.  
  157.  /* Writes the land data to disk */
  158.  landdata = fopen ("gn.004", "wb");
  159.  fwrite (gum, SCREEN_WIDTH * SCREEN_HEIGHT * 2, 1, landdata);
  160.  fclose (landdata);
  161.  */
  162.  
  163.  /* Reads the land data into memory. */
  164.  gum = lib2buf("gn.004");
  165. }
  166.  
  167.  
  168.  
  169.  
  170. void draw_rays (void)
  171. {
  172. int startx, starty;
  173. int x, y;
  174. long perspy;
  175. long f;
  176. int colr;
  177. int dx;
  178. int nx;
  179. int *gumptr;
  180. block mapptr;
  181.  
  182. wsetscreen(virt);
  183. for (y = 0; y < 200; y++)
  184.  {
  185.   wsetcolor(y+128);
  186.   wline(0,y,SCREEN_WIDTH,y);
  187.  }
  188.  
  189. for (x = 0; x < SCREEN_WIDTH; x++)
  190.   ox[x] = 199;
  191.  
  192. gumptr = gum;
  193.  
  194. mapptr = landscape + 4 + posy*320 + posx;
  195.  
  196. for (y = 1; y < SCREEN_HEIGHT; y++)
  197.  {
  198.  perspy = pspy[y] - y * hgt;
  199.  f = ff[y];
  200.  for (x = 0; x < SCREEN_WIDTH; x++)
  201.   {
  202.    mapptr+= *gumptr++;
  203.    colr = *mapptr;
  204.  
  205.    nx = perspy - ((long)colr * f>>10);
  206.    if (nx > ox[x])
  207.      wvline (x, ox[x], nx, colr);
  208.    ox[x] = nx;
  209.   }
  210.  }
  211.  
  212. wcopyscreen (0, 0, SCREEN_WIDTH-2, 198, virt, 32, 0, NULL);
  213. }
  214.  
  215. extern MS *m;            /* Music Structure */
  216.  
  217.  
  218. void draw_volumebars (void)
  219. {
  220. int curvol;
  221.  
  222.     wsetscreen (landscape);
  223.     for(i = 0; i < 8; i++)    /* For every channel in song */
  224.     {
  225.       m = mGetMS (i);            /* Get music structure */
  226.       curvol = m->DecVolume+5;
  227.       wsetcolor (curvol);
  228.       wbar (120 + i * 10, 100, 120 + i * 10 + 10, 105);
  229.     }
  230. }
  231.  
  232.  
  233.  
  234. void start_landscape(void)
  235. {
  236. int xdir, ydir;
  237. int fadectr;
  238. int ctr;
  239.  
  240. wnormscreen();
  241. wcls(0);
  242. wloadpalette("lands.pal",pal1);
  243. wsetrgb(0,0,0,0,pal1);
  244. landscape = wloadpak("lands.pak");
  245. calc_tables();
  246. virt = wnewblock (0,0, 319,199);
  247. wtextcolor(1);
  248.  
  249. posx = 225;
  250. posy = 94;
  251. xdir = -1;
  252. ydir = -1;
  253. fadectr = 0;
  254. ctr = 0;
  255.  
  256. //gum = farmalloc(SCREEN_WIDTH * SCREEN_HEIGHT * 2);
  257. generate_table();
  258.  
  259. fadectr = 64;
  260. if (!music_on)
  261.   ctr = 200;
  262.  
  263. do
  264.  {
  265.   if (music_on)
  266.     draw_volumebars ();
  267.  
  268.   if (ctr > 200)
  269.   {
  270.   posx += xdir;
  271.   if (posx > 320)
  272.     xdir = -1;
  273.   if (posx < 0)
  274.     xdir = 1;
  275.  
  276.   posy += ydir;
  277.   if (posy > 170)
  278.     ydir = -1;
  279.   if (posy < 60)
  280.     ydir = 1;
  281.   }
  282.  
  283.   draw_rays ();
  284.  
  285.  if ((kbhit()) || (ctr == 432))
  286.    {
  287.     while (kbhit()) getch();
  288.     fadectr = -64;
  289.     for (i=0; i<256; i++)
  290.      wsetrgb(i,0,0,0,blackpal);
  291.     wreadpalette(0,255,pal1);
  292.    }
  293.  if (fadectr > 0)
  294.   {
  295.    wfade_between_once (0, 255, blackpal, pal1);
  296.    wsetpalette(0,255,blackpal);
  297.    fadectr--;
  298.   }
  299.  
  300.  if (fadectr < 0)
  301.   {
  302.    wfade_between_once (0, 255, pal1, blackpal);
  303.    wsetpalette(0,255,pal1);
  304.    fadectr++;
  305.    if (fadectr == 0)
  306.      ctr = 500;
  307.   }
  308.  ctr++;
  309.  } while (ctr < 500);
  310.  
  311.  
  312.   posx = 140;
  313.   posy = 0;
  314.   ctr = 0;
  315.   hgt = -2.5;
  316.  
  317.   fadectr = 64;
  318.   wloadpalette("ocean.pal",pal1);
  319.   wfreeblock (landscape);
  320.   landscape = wnewblock(0,0,319,30);
  321.   gen_plasma();
  322.  
  323.  if (farcoreleft () < memleft)
  324.    memleft = farcoreleft ();
  325.   
  326. do
  327.  {
  328.   draw_land_plasma();
  329.   draw_rays ();
  330. //  wnormscreen();
  331. //  wputblock(0,0,landscape,0);
  332.  
  333.  if ((kbhit()) || (ctr == 232))
  334.   {
  335.    while (kbhit()) getch();
  336.    fadectr = -64;
  337.    for (i=0; i<256; i++)
  338.     wsetrgb(i,0,0,0,blackpal);
  339.    wreadpalette(0,255,pal1);
  340.   }
  341.  
  342.  if (fadectr > 0)
  343.   {
  344.    wfade_between_once (0, 255, blackpal, pal1);
  345.    wsetpalette(0,255,blackpal);
  346.    fadectr--;
  347.   }
  348.  
  349.  if (fadectr < 0)
  350.   {
  351.    wfade_between_once (0, 255, pal1, blackpal);
  352.    wsetpalette(0,255,pal1);
  353.    fadectr++;
  354.    if (fadectr == 0)
  355.      ctr = 300;
  356.   }
  357.  ctr++;
  358.  
  359.  } while (ctr < 300);
  360. while (kbhit()) getch();
  361.  
  362. wfreeblock (gum);
  363. wfreeblock (virt);
  364. wfreeblock (landscape);
  365. farfree(data);
  366. }
  367.  
  368. void gen_plasma(void)
  369. {
  370.  FILE *plasmadata;
  371.  
  372.  randomize();
  373. /* data = (unsigned char*)farmalloc(65535l);
  374.  if (!data)
  375.   {
  376.     printf("Not enough free memory.\n\n");
  377.     exit(1);
  378.   }*/
  379.     wtextcolor(15);
  380.  
  381.  /*
  382.  for (x=1; x<256; x++)
  383.   for (y=0; y<256; y++)
  384.    data[x+256*y]=(unsigned char)(16*(3+(cos(0.32*sqrt((x-128)*(x-128)+(y-128)*(y-128))))+
  385.        cos(x/11.0)+cos(y/26.0)));
  386.  
  387.  /* Writes the plasma data to disk */
  388.  plasmadata = fopen ("gn.003", "wb");
  389.  fwrite (data, 65535L, 1, plasmadata);
  390.  fclose (plasmadata);
  391.  */
  392.  
  393.  /* Reads the plasma data into memory. */
  394.  data = lib2buf("gn.003");
  395. }
  396.  
  397. void draw_land_plasma(void)
  398. {
  399. int pl1, pl2;
  400.  
  401. teller++;
  402.  
  403. if (teller > 30000) teller = 0;
  404.  
  405. pl1=48+cos(teller/37)*47.0+256*(int)(48+47*(sin(teller/31)));
  406. pl2=48+sin(teller/84)*47.0+256*(int)(48+47*(cos(teller/19)))-pl1;
  407.                 asm .386
  408.                 asm les di,landscape
  409.                 asm add di,4
  410.         asm push ds
  411.         asm lds si,data
  412.         asm add si,pl1
  413.         asm mov bx,pl2
  414.         asm mov cl,30
  415. l1: asm mov dx,160
  416. l2: asm lodsb
  417.         asm add al,[si+bx]
  418.                 asm mov ah, al
  419.         asm stosw
  420.         asm dec dx
  421.         asm jnz l2
  422.         asm add si,256-160
  423.         asm dec cl
  424.         asm jnz l1
  425.         asm pop ds
  426.  
  427. }
  428.  
  429. int px[4] = {0, 319, 319, 0};
  430. int py[4] = {0,   0, 199, 199};
  431. /* Original source points */
  432.  
  433. int rx[4], ry[4];
  434. /* Source points after rotation and sin wave warping */
  435. long lsin[360], lcos[360];
  436.  
  437. void lcalc_sin(void)
  438. {
  439. int i;
  440.  for (i = 0; i < 360; i++)
  441.  {
  442.   lsin[i] = sin(3.1415 * ((double)i / 180.0)) * 1024;
  443.   lcos[i] = cos(3.1415 * ((double)i / 180.0)) * 1024;
  444.  }
  445.  
  446.  
  447. void rotatepoly(int centx, int centy, int rot)
  448. /* Rotates the 4 corners of the texture source points, given the center of
  449.    rotation and the rotational amount. */
  450. {
  451.  int i;
  452.  int x, y;
  453.  int x2, y2;
  454.  
  455.  for (i = 0; i <= 3; i++)
  456.   {
  457.    x = px[i] - centx;    /* Subtract the centers */
  458.    y = py[i] - centy;  /* so coordinates are based around (0,0) */
  459.                                          
  460.    x2 = ((long)x * lcos[rot] - (long)y * lsin[rot]) >> 10;
  461.    y2 = ((lo