git-svn-id: http://bladebattles.com/kurok/SVN@11 20cd92bb-ff49-0410-b73e-96a06e42c3b9
[kurok.git] / r_sky.c
blob83c93e15036e68c62c8f8fdee2667a1f4b102c61
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // r_sky.c
22 #include "quakedef.h"
23 #include "r_local.h"
24 #include "d_local.h"
27 int iskyspeed = 8;
28 int iskyspeed2 = 2;
29 float skyspeed, skyspeed2;
31 float skytime;
33 byte *r_skysource;
35 int r_skymade;
36 int r_skydirect; // not used?
39 // TODO: clean up these routines
41 byte bottomsky[128*131];
42 byte bottommask[128*131];
43 byte newsky[128*256]; // newsky and topsky both pack in here, 128 bytes
44 // of newsky on the left of each scan, 128 bytes
45 // of topsky on the right, because the low-level
46 // drawers need 256-byte scan widths
50 =============
51 R_InitSky
53 A sky texture is 256*128, with the right side being a masked overlay
54 ==============
56 void R_InitSky (texture_t *mt)
58 int i, j;
59 byte *src;
61 src = (byte *)mt + mt->offsets[0];
63 for (i=0 ; i<128 ; i++)
65 for (j=0 ; j<128 ; j++)
67 newsky[(i*256) + j + 128] = src[i*256 + j + 128];
71 for (i=0 ; i<128 ; i++)
73 for (j=0 ; j<131 ; j++)
75 if (src[i*256 + (j & 0x7F)])
77 bottomsky[(i*131) + j] = src[i*256 + (j & 0x7F)];
78 bottommask[(i*131) + j] = 0;
80 else
82 bottomsky[(i*131) + j] = 0;
83 bottommask[(i*131) + j] = 0xff;
88 r_skysource = newsky;
93 =================
94 R_MakeSky
95 =================
97 void R_MakeSky (void)
99 int x, y;
100 int ofs, baseofs;
101 int xshift, yshift;
102 unsigned *pnewsky;
103 static int xlast = -1, ylast = -1;
105 xshift = skytime*skyspeed;
106 yshift = skytime*skyspeed;
108 if ((xshift == xlast) && (yshift == ylast))
109 return;
111 xlast = xshift;
112 ylast = yshift;
114 pnewsky = (unsigned *)&newsky[0];
116 for (y=0 ; y<SKYSIZE ; y++)
118 baseofs = ((y+yshift) & SKYMASK) * 131;
120 // FIXME: clean this up
121 #if UNALIGNED_OK
122 for (x=0 ; x<SKYSIZE ; x += 4)
124 ofs = baseofs + ((x+xshift) & SKYMASK);
126 // PORT: unaligned dword access to bottommask and bottomsky
127 *pnewsky = (*(pnewsky + (128 / sizeof (unsigned))) &
128 *(unsigned *)&bottommask[ofs]) |
129 *(unsigned *)&bottomsky[ofs];
130 pnewsky++;
132 #else
133 for (x=0 ; x<SKYSIZE ; x++)
135 ofs = baseofs + ((x+xshift) & SKYMASK);
137 *(byte *)pnewsky = (*((byte *)pnewsky + 128) &
138 *(byte *)&bottommask[ofs]) |
139 *(byte *)&bottomsky[ofs];
140 pnewsky = (unsigned *)((byte *)pnewsky + 1);
142 #endif
144 pnewsky += 128 / sizeof (unsigned);
147 r_skymade = 1;
152 =================
153 R_GenSkyTile
154 =================
156 void R_GenSkyTile (void *pdest)
158 int x, y;
159 int ofs, baseofs;
160 int xshift, yshift;
161 unsigned *pnewsky;
162 unsigned *pd;
164 xshift = skytime*skyspeed;
165 yshift = skytime*skyspeed;
167 pnewsky = (unsigned *)&newsky[0];
168 pd = (unsigned *)pdest;
170 for (y=0 ; y<SKYSIZE ; y++)
172 baseofs = ((y+yshift) & SKYMASK) * 131;
174 // FIXME: clean this up
175 #if UNALIGNED_OK
177 for (x=0 ; x<SKYSIZE ; x += 4)
179 ofs = baseofs + ((x+xshift) & SKYMASK);
181 // PORT: unaligned dword access to bottommask and bottomsky
183 *pd = (*(pnewsky + (128 / sizeof (unsigned))) &
184 *(unsigned *)&bottommask[ofs]) |
185 *(unsigned *)&bottomsky[ofs];
186 pnewsky++;
187 pd++;
190 #else
192 for (x=0 ; x<SKYSIZE ; x++)
194 ofs = baseofs + ((x+xshift) & SKYMASK);
196 *(byte *)pd = (*((byte *)pnewsky + 128) &
197 *(byte *)&bottommask[ofs]) |
198 *(byte *)&bottomsky[ofs];
199 pnewsky = (unsigned *)((byte *)pnewsky + 1);
200 pd = (unsigned *)((byte *)pd + 1);
203 #endif
205 pnewsky += 128 / sizeof (unsigned);
211 =================
212 R_GenSkyTile16
213 =================
215 void R_GenSkyTile16 (void *pdest)
217 int x, y;
218 int ofs, baseofs;
219 int xshift, yshift;
220 byte *pnewsky;
221 unsigned short *pd;
223 xshift = skytime * skyspeed;
224 yshift = skytime * skyspeed;
226 pnewsky = (byte *)&newsky[0];
227 pd = (unsigned short *)pdest;
229 for (y=0 ; y<SKYSIZE ; y++)
231 baseofs = ((y+yshift) & SKYMASK) * 131;
233 // FIXME: clean this up
234 // FIXME: do faster unaligned version?
235 for (x=0 ; x<SKYSIZE ; x++)
237 ofs = baseofs + ((x+xshift) & SKYMASK);
239 *pd = d_8to16table[(*(pnewsky + 128) &
240 *(byte *)&bottommask[ofs]) |
241 *(byte *)&bottomsky[ofs]];
242 pnewsky++;
243 pd++;
246 pnewsky += TILE_SIZE;
252 =============
253 R_SetSkyFrame
254 ==============
256 void R_SetSkyFrame (void)
258 int g, s1, s2;
259 float temp;
261 skyspeed = iskyspeed;
262 skyspeed2 = iskyspeed2;
264 g = GreatestCommonDivisor (iskyspeed, iskyspeed2);
265 s1 = iskyspeed / g;
266 s2 = iskyspeed2 / g;
267 temp = SKYSIZE * s1 * s2;
269 skytime = cl.time - ((int)(cl.time / temp) * temp);
272 r_skymade = 0;