1 /*
2 	r_sky.c
3 
4 	(description)
5 
6 	Copyright (C) 1996-1997  Id Software, Inc.
7 
8 	This program is free software; you can redistribute it and/or
9 	modify it under the terms of the GNU General Public License
10 	as published by the Free Software Foundation; either version 2
11 	of the License, or (at your option) any later version.
12 
13 	This program is distributed in the hope that it will be useful,
14 	but WITHOUT ANY WARRANTY; without even the implied warranty of
15 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 	See the GNU General Public License for more details.
18 
19 	You should have received a copy of the GNU General Public License
20 	along with this program; if not, write to:
21 
22 		Free Software Foundation, Inc.
23 		59 Temple Place - Suite 330
24 		Boston, MA  02111-1307, USA
25 
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30 
31 #define NH_DEFINE
32 #include "namehack.h"
33 
34 #ifdef HAVE_STRING_H
35 # include "string.h"
36 #endif
37 #ifdef HAVE_STRINGS_H
38 # include "strings.h"
39 #endif
40 
41 #include "QF/sys.h"
42 #include "QF/render.h"
43 
44 #include "r_internal.h"
45 #include "vid_internal.h"
46 
47 
48 static int         iskyspeed = 8;
49 static int         iskyspeed2 = 2;
50 float       sw32_r_skyspeed;
51 
52 float       sw32_r_skytime;
53 
54 byte       *sw32_r_skysource;
55 
56 int         sw32_r_skymade;
57 
58 // TODO: clean up these routines
59 
60 /*
61 byte        bottomsky[128 * 131];
62 byte        bottommask[128 * 131];
63 byte        newsky[128 * 256];			// newsky and topsky both pack in here,
64 										// 128 bytes of newsky on the left of
65 										// each scan, 128 bytes of topsky on
66 										// the right, because the low-level
67 										// drawers need 256-byte scan widths
68 */
69 static byte skydata[128*256]; // sky layers for making skytex
70 static byte skytex[128*256*4]; // current sky texture
71 
72 
73 /*
74 	R_InitSky
75 
76 	A sky texture is 256*128, with the right side being a masked overlay
77 */
78 void
sw32_R_InitSky(texture_t * mt)79 sw32_R_InitSky (texture_t *mt)
80 {
81 	/*
82 	int         i, j;
83 	byte       *src;
84 
85 	src = (byte *) mt + mt->offsets[0];
86 
87 	for (i = 0; i < 128; i++) {
88 		for (j = 0; j < 128; j++) {
89 			newsky[(i * 256) + j + 128] = src[i * 256 + j + 128];
90 		}
91 	}
92 
93 	for (i = 0; i < 128; i++) {
94 		for (j = 0; j < 131; j++) {
95 			if (src[i * 256 + (j & 0x7F)]) {
96 				bottomsky[(i * 131) + j] = src[i * 256 + (j & 0x7F)];
97 				bottommask[(i * 131) + j] = 0;
98 			} else {
99 				bottomsky[(i * 131) + j] = 0;
100 				bottommask[(i * 131) + j] = 0xff;
101 			}
102 		}
103 	}
104 
105 	sw32_r_skysource = newsky;
106 	*/
107 
108 	// LordHavoc: save sky for use
109 	memcpy(skydata, (byte *) mt + mt->offsets[0], 128*256);
110 	sw32_r_skysource = skytex;
111 }
112 
113 void
sw32_R_MakeSky(void)114 sw32_R_MakeSky (void)
115 {
116 	int x, y, xshift1, yshift1, xshift2, yshift2;
117 	byte *base1, *base2;
118 	static int  xlast = -1, ylast = -1;
119 
120 	xshift2 = sw32_r_skytime * sw32_r_skyspeed * 2.0f;
121 	yshift2 = sw32_r_skytime * sw32_r_skyspeed * 2.0f;
122 
123 	if ((xshift2 == xlast) && (yshift2 == ylast))
124 		return;
125 
126 	xlast = xshift2;
127 	ylast = yshift2;
128 	xshift1 = xshift2 >> 1;
129 	yshift1 = yshift2 >> 1;
130 
131 	switch(sw32_r_pixbytes)
132 	{
133 	case 1:
134 		{
135 			byte *out = (byte *) skytex;
136 			for (y = 0;y < 128;y++)
137 			{
138 				base1 = &skydata[((y + yshift1) & 127) * 256];
139 				base2 = &skydata[((y + yshift2) & 127) * 256 + 128];
140 				for (x = 0;x < 128;x++)
141 				{
142 					if (base1[(x + xshift1) & 127])
143 						*out = base1[(x + xshift1) & 127];
144 					else
145 						*out = base2[(x + xshift2) & 127];
146 					out++;
147 				}
148 				out += 128;
149 			}
150 		}
151 		break;
152 	case 2:
153 		{
154 			unsigned short *out = (unsigned short *) skytex;
155 			for (y = 0;y < 128;y++)
156 			{
157 				base1 = &skydata[((y + yshift1) & 127) * 256];
158 				base2 = &skydata[((y + yshift2) & 127) * 256 + 128];
159 				for (x = 0;x < 128;x++)
160 				{
161 					if (base1[(x + xshift1) & 127])
162 						*out = sw32_8to16table[base1[(x + xshift1) & 127]];
163 					else
164 						*out = sw32_8to16table[base2[(x + xshift2) & 127]];
165 					out++;
166 				}
167 				out += 128;
168 			}
169 		}
170 		break;
171 	case 4:
172 		{
173 			unsigned int *out = (unsigned int *) skytex;
174 			for (y = 0;y < 128;y++)
175 			{
176 				base1 = &skydata[((y + yshift1) & 127) * 256];
177 				base2 = &skydata[((y + yshift2) & 127) * 256 + 128];
178 				for (x = 0;x < 128;x++)
179 				{
180 					if (base1[(x + xshift1) & 127])
181 						*out = d_8to24table[base1[(x + xshift1) & 127]];
182 					else
183 						*out = d_8to24table[base2[(x + xshift2) & 127]];
184 					out++;
185 				}
186 				out += 128;
187 			}
188 		}
189 		break;
190 	default:
191 		Sys_Error("R_MakeSky: unsupported r_pixbytes %i", sw32_r_pixbytes);
192 	}
193 	sw32_r_skymade = 1;
194 }
195 
196 
197 void
sw32_R_SetSkyFrame(void)198 sw32_R_SetSkyFrame (void)
199 {
200 	int         g, s1, s2;
201 	float       temp;
202 
203 	sw32_r_skyspeed = iskyspeed;
204 
205 	g = GreatestCommonDivisor (iskyspeed, iskyspeed2);
206 	s1 = iskyspeed / g;
207 	s2 = iskyspeed2 / g;
208 	temp = SKYSIZE * s1 * s2;
209 
210 	sw32_r_skytime = vr_data.realtime - ((int) (vr_data.realtime / temp) * temp);
211 
212 	sw32_r_skymade = 0;
213 }
214 
215 
216 /*
217    Stub function for loading a skybox.  Currently we have support for
218    skyboxes only in GL targets, so we just do nothing here.  --KB
219 */
220 void
sw32_R_LoadSkys(const char * name)221 sw32_R_LoadSkys (const char *name)
222 {
223 }
224