r_sky.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. Copyright (C) 1996-1997 Id Software, Inc.
  3. This program is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU General Public License
  5. as published by the Free Software Foundation; either version 2
  6. of the License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. See the GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. // r_sky.c
  16. #include "quakedef.h"
  17. #include "r_local.h"
  18. #include "d_local.h"
  19. int iskyspeed = 8;
  20. int iskyspeed2 = 2;
  21. float skyspeed, skyspeed2;
  22. float skytime;
  23. byte *r_skysource;
  24. int r_skymade;
  25. int r_skydirect; // not used?
  26. // TODO: clean up these routines
  27. byte bottomsky[128*131];
  28. byte bottommask[128*131];
  29. byte newsky[128*256]; // newsky and topsky both pack in here, 128 bytes
  30. // of newsky on the left of each scan, 128 bytes
  31. // of topsky on the right, because the low-level
  32. // drawers need 256-byte scan widths
  33. /*
  34. =============
  35. R_InitSky
  36. A sky texture is 256*128, with the right side being a masked overlay
  37. ==============
  38. */
  39. void R_InitSky (texture_t *mt)
  40. {
  41. int i, j;
  42. byte *src;
  43. src = (byte *)mt + mt->offsets[0];
  44. for (i=0 ; i<128 ; i++)
  45. {
  46. for (j=0 ; j<128 ; j++)
  47. {
  48. newsky[(i*256) + j + 128] = src[i*256 + j + 128];
  49. }
  50. }
  51. for (i=0 ; i<128 ; i++)
  52. {
  53. for (j=0 ; j<131 ; j++)
  54. {
  55. if (src[i*256 + (j & 0x7F)])
  56. {
  57. bottomsky[(i*131) + j] = src[i*256 + (j & 0x7F)];
  58. bottommask[(i*131) + j] = 0;
  59. }
  60. else
  61. {
  62. bottomsky[(i*131) + j] = 0;
  63. bottommask[(i*131) + j] = 0xff;
  64. }
  65. }
  66. }
  67. r_skysource = newsky;
  68. }
  69. /*
  70. =================
  71. R_MakeSky
  72. =================
  73. */
  74. void R_MakeSky (void)
  75. {
  76. int x, y;
  77. int ofs, baseofs;
  78. int xshift, yshift;
  79. unsigned *pnewsky;
  80. static int xlast = -1, ylast = -1;
  81. xshift = skytime*skyspeed;
  82. yshift = skytime*skyspeed;
  83. if ((xshift == xlast) && (yshift == ylast))
  84. return;
  85. xlast = xshift;
  86. ylast = yshift;
  87. pnewsky = (unsigned *)&newsky[0];
  88. for (y=0 ; y<SKYSIZE ; y++)
  89. {
  90. baseofs = ((y+yshift) & SKYMASK) * 131;
  91. // FIXME: clean this up
  92. #if UNALIGNED_OK
  93. for (x=0 ; x<SKYSIZE ; x += 4)
  94. {
  95. ofs = baseofs + ((x+xshift) & SKYMASK);
  96. // PORT: unaligned dword access to bottommask and bottomsky
  97. *pnewsky = (*(pnewsky + (128 / sizeof (unsigned))) &
  98. *(unsigned *)&bottommask[ofs]) |
  99. *(unsigned *)&bottomsky[ofs];
  100. pnewsky++;
  101. }
  102. #else
  103. for (x=0 ; x<SKYSIZE ; x++)
  104. {
  105. ofs = baseofs + ((x+xshift) & SKYMASK);
  106. *(byte *)pnewsky = (*((byte *)pnewsky + 128) &
  107. *(byte *)&bottommask[ofs]) |
  108. *(byte *)&bottomsky[ofs];
  109. pnewsky = (unsigned *)((byte *)pnewsky + 1);
  110. }
  111. #endif
  112. pnewsky += 128 / sizeof (unsigned);
  113. }
  114. r_skymade = 1;
  115. }
  116. /*
  117. =================
  118. R_GenSkyTile
  119. =================
  120. */
  121. void R_GenSkyTile (void *pdest)
  122. {
  123. int x, y;
  124. int ofs, baseofs;
  125. int xshift, yshift;
  126. unsigned *pnewsky;
  127. unsigned *pd;
  128. xshift = skytime*skyspeed;
  129. yshift = skytime*skyspeed;
  130. pnewsky = (unsigned *)&newsky[0];
  131. pd = (unsigned *)pdest;
  132. for (y=0 ; y<SKYSIZE ; y++)
  133. {
  134. baseofs = ((y+yshift) & SKYMASK) * 131;
  135. // FIXME: clean this up
  136. #if UNALIGNED_OK
  137. for (x=0 ; x<SKYSIZE ; x += 4)
  138. {
  139. ofs = baseofs + ((x+xshift) & SKYMASK);
  140. // PORT: unaligned dword access to bottommask and bottomsky
  141. *pd = (*(pnewsky + (128 / sizeof (unsigned))) &
  142. *(unsigned *)&bottommask[ofs]) |
  143. *(unsigned *)&bottomsky[ofs];
  144. pnewsky++;
  145. pd++;
  146. }
  147. #else
  148. for (x=0 ; x<SKYSIZE ; x++)
  149. {
  150. ofs = baseofs + ((x+xshift) & SKYMASK);
  151. *(byte *)pd = (*((byte *)pnewsky + 128) &
  152. *(byte *)&bottommask[ofs]) |
  153. *(byte *)&bottomsky[ofs];
  154. pnewsky = (unsigned *)((byte *)pnewsky + 1);
  155. pd = (unsigned *)((byte *)pd + 1);
  156. }
  157. #endif
  158. pnewsky += 128 / sizeof (unsigned);
  159. }
  160. }
  161. /*
  162. =================
  163. R_GenSkyTile16
  164. =================
  165. */
  166. void R_GenSkyTile16 (void *pdest)
  167. {
  168. int x, y;
  169. int ofs, baseofs;
  170. int xshift, yshift;
  171. byte *pnewsky;
  172. unsigned short *pd;
  173. xshift = skytime * skyspeed;
  174. yshift = skytime * skyspeed;
  175. pnewsky = (byte *)&newsky[0];
  176. pd = (unsigned short *)pdest;
  177. for (y=0 ; y<SKYSIZE ; y++)
  178. {
  179. baseofs = ((y+yshift) & SKYMASK) * 131;
  180. // FIXME: clean this up
  181. // FIXME: do faster unaligned version?
  182. for (x=0 ; x<SKYSIZE ; x++)
  183. {
  184. ofs = baseofs + ((x+xshift) & SKYMASK);
  185. *pd = d_8to16table[(*(pnewsky + 128) &
  186. *(byte *)&bottommask[ofs]) |
  187. *(byte *)&bottomsky[ofs]];
  188. pnewsky++;
  189. pd++;
  190. }
  191. pnewsky += TILE_SIZE;
  192. }
  193. }
  194. /*
  195. =============
  196. R_SetSkyFrame
  197. ==============
  198. */
  199. void R_SetSkyFrame (void)
  200. {
  201. int g, s1, s2;
  202. float temp;
  203. skyspeed = iskyspeed;
  204. skyspeed2 = iskyspeed2;
  205. g = GreatestCommonDivisor (iskyspeed, iskyspeed2);
  206. s1 = iskyspeed / g;
  207. s2 = iskyspeed2 / g;
  208. temp = SKYSIZE * s1 * s2;
  209. skytime = cl.time - ((int)(cl.time / temp) * temp);
  210. r_skymade = 0;
  211. }