1 #ifdef GL_HERETIC
2 
3 //#define __DOOMTYPE__
4 
5 #include <stdio.h>
6 #include <string.h>
7 #include <math.h>
8 
9 #include "gl_syms.h"
10 
11 #include "gl_struct.h"
12 #include "gl_mem.h"
13 #ifndef GL_HEXEN
14 #include "doomdef.h"
15 #else
16 #include "h2def.h"
17 #endif
18 #include "r_local.h"
19 #include "p_spec.h"
20 
21 extern int iNbGLTextures;
22 extern GLuint *texobjs;
23 extern GLuint *sprobjs;
24 extern int		numpatches;
25 //extern GLTexture *p_stGLSprites;
26 //extern int	iNbGLSprites;
27 //extern GLTexArray *p_stGLSprArray;
28 extern int iLightLevelMin,iLightLevelMax;
29 extern byte *p_bPalette;
30 
31 extern player_t *player;
32 
33 extern char* mlump_filename;
34 extern char* wlump_filename;
35 
36 int fn_iGetTexHeightGL(int iHeight);
37 void fn_vRegisterSpriteOnTheFly(int iSpriteLump);
38 extern void (*GL_vDrawMD2)(mobj_t *pSpr,float fLightLevel);
39 
40 // sprites
41 int SPRITE_WIDTH=128;
42 int SPRITE_HEIGHT=128;
43 GLTexture *p_stGLSprites;
44 int	iNbGLSprites;
45 GLTexArray *p_stGLSprArray;
46 
47 GLMapSprite *p_stMapSprites;
48 
49 GLSprite a_SpriteRendered[MAXVISSPRITES];
50 int iNbSpriteRendered;
51 
52 GLboolean g_bDynamicAlloc;
53 
54 // Sprite compression parameters...
55 GLboolean g_bSpriteOpti=FALSE,g_bSpriteBilinear=FALSE;
56 float fScaleSprite=0.5f;
57 int iMaxSizeBeforeReduction=65536;	//4096;	64x64
58 #ifndef DOOM_GL
59 float fSpriteFilterDistanceCoeff=0.00312f;	// DOOM_GL
60 #else
61 float fSpriteFilterDistanceCoeff=0.012f;
62 //float fSpriteFilterDistanceCoeff=0.00312f;
63 #endif
64 
__max(int a,int b)65 int __max(int a, int b) {
66   if (a>=b)
67     return a;
68   else
69     return b;
70 }
71 
72 // Coronas.
73 long usecoronas;
74 void GL_AddCorona(int doomednum,float x,float y,float z,float fLightLevel);
75 
fn_vSaveRAW(char * szFilename,unsigned char * buffer,int width,int height)76 void fn_vSaveRAW(char *szFilename,unsigned char *buffer,int width,int height)
77 { FILE *fp;
78   int x,y;
79 
80 	fp=fopen(szFilename,"wb");
81 	for (x=0;x<width;x++)
82 		for (y=0;y<height;y++)
83 		{	fwrite(&buffer[4*(x*height+y)],1,1,fp);
84 			fwrite(&buffer[4*(x*height+y)+1],1,1,fp);
85 			fwrite(&buffer[4*(x*height+y)+2],1,1,fp);
86 		}
87 	fclose(fp);
88 }
89 
fn_vSaveSprites()90 void fn_vSaveSprites()
91 { char szFilename[255];
92   int iGLIndex;
93 
94 	for (iGLIndex=0;iGLIndex<iNbGLSprites;iGLIndex++)
95 	{
96 		sprintf(szFilename,"pix%03d.raw",iGLIndex);
97 		fn_vSaveRAW(szFilename,p_stGLSprites[iGLIndex].p_bRGBBuffer,p_stGLSprites[iGLIndex].iWidth,p_stGLSprites[iGLIndex].iHeight);
98 	}
99 }
100 
ScalePicture(unsigned char * src,int ws,int hs,unsigned char * dst,int wd,int hd)101 void ScalePicture(unsigned char *src,int ws,int hs,unsigned char *dst,int wd,int hd)
102 {
103   float dx,dy,ddx,ddy;
104 
105   float fA,fB,fC,fD;
106   int x,y;
107   float r,g,b,a;
108   int w,h;
109   unsigned char *ptr;
110   unsigned char t;
111 
112   float fw=(float) ws/ (float) wd;
113   float fh=(float) hs/ (float) hd;
114 
115   for (h=0;h<hd;h++)
116   {
117     dy=(float) (h*fh);
118     y=(int) dy;
119     dy-=(float) y;
120     ddy=1.0f-dy;
121 
122     for(w=0;w<wd;w++)
123     {
124       dx=(float) (w*fw);
125       x=(int) dx;
126       dx-=(float) x;
127       ddx=1.0f-dx;
128 
129       fA=ddx*ddy;
130       fB=dx*ddy;
131       fC=dx*dy;
132       fD=ddx*dy;
133 
134       // A
135       ptr=src+(y*ws+x)*4;
136 	  if (!ptr[3])
137 	  	fA=0;
138 	  r=(float) (ptr[0]*fA);
139 	  g=(float) (ptr[1]*fA);
140 	  b=(float) (ptr[2]*fA);
141 	  a=(float) ptr[3]*fA;
142 
143       // B
144       //ptr=src+(y*ws+x+1)*3;
145       ptr+=4;
146 	  if (!ptr[3])
147 		fB=0;
148 		  r+=(float) (ptr[0]*fB);
149 		  g+=(float) (ptr[1]*fB);
150 		  b+=(float) (ptr[2]*fB);
151 		  a+=(float) ptr[3]*fB;
152 
153       // C
154       //ptr=src+((y+1)*ws+x)*3;
155       ptr+=4*(ws-1);
156 	  if (!ptr[3])
157 		fC=0;
158 		  r+=(float) (ptr[0]*fC);
159 		  g+=(float) (ptr[1]*fC);
160 		  b+=(float) (ptr[2]*fC);
161 		  a+=(float) ptr[3]*fC;
162 
163       // D
164       //ptr=src+((y+1)*ws+x+1)*3;
165       ptr+=4;
166 	  if (!ptr[3])
167 		fD=0;
168 		  r+=(float) (ptr[0]*fD);
169 		  g+=(float) (ptr[1]*fD);
170 		  b+=(float) (ptr[2]*fD);
171 		  a+=(float) ptr[3]*fD;
172 
173       if (fA+fB+fC+fD)
174 	  {
175 		  r/=(fA+fB+fC+fD);
176 		  b/=(fA+fB+fC+fD);
177 		  g/=(fA+fB+fC+fD);
178 	  }
179 
180 	  // clipping to be sure
181       if (r<0.0f) t=0; else if (r>255.0f) t=255; else t=(unsigned char) r; *dst++=t;
182       if (g<0.0f) t=0; else if (g>255.0f) t=255; else t=(unsigned char) g; *dst++=t;
183       if (b<0.0f) t=0; else if (b>255.0f) t=255; else t=(unsigned char) b; *dst++=t;
184 	  if (a==0)
185 		*dst++=0;
186 	  else
187 		*dst++=255;
188 	}
189   }
190 }
191 
192 // MR2606
193 /*void GL_vSetSpriteVisible(vissprite_t *vis)
194 { int i;
195 	for (i=0;i<iNbSpriteRendered;i++)
196 		if (a_SpriteRendered[i].vis==vis)
197 	{	a_SpriteRendered[i].bIsVisible=true;
198 		return;
199 	}
200 }*/
201 
fn_vAddSprite(mobj_t * pSpr,int lump,H_boolean flip,fixed_t dx,fixed_t dy,fixed_t dz)202 void fn_vAddSprite(mobj_t *pSpr,int lump, H_boolean flip,fixed_t dx,fixed_t dy,fixed_t dz/*,vissprite_t*	vis*/)	// MR2606(vissp)
203 { int i,j,iInsert;
204   float fDist1,fdx1,fdy1,fdz;
205   /* float fdx2, fdy2, fDist2; */
206 
207 	if (iNbSpriteRendered>=MAXVISSPRITES)	// MR1805
208 		return;
209 	// MR2806
210 	{	fdx1=(float)dx/MAP_SCALE;
211 		fdy1=(float)dy/MAP_SCALE;
212 		fdz=(float)dz/MAP_SCALE;
213 		fDist1=sqrt(fdx1*fdx1+fdy1*fdy1+fdz*fdz);
214 	}
215 	iInsert=-1;
216 	for (i=0;i<iNbSpriteRendered;i++)
217 	{	/*fdx2=(float)a_SpriteRendered[i].dx/MAP_SCALE;
218 		fdy2=(float)a_SpriteRendered[i].dy/MAP_SCALE;
219 		fDist2=sqrt(fdx2*fdx2+fdy2*fdy2);
220 		if (fDist1>fDist2)*/
221 		if (pSpr==a_SpriteRendered[i].p_Obj)
222 			return;		// Consequence de l'anti-bug lookdir (rappel du BSPNode dans r_main.c)
223 		if ((iInsert==-1)&&(fDist1>a_SpriteRendered[i].fDist))
224 		{	iInsert=i;
225 		}
226 	}
227 	if (iInsert==-1)
228 		// MR1707
229 		//iInsert=0;
230 		iInsert=iNbSpriteRendered;
231 	for (j=iNbSpriteRendered;j>iInsert;j--)
232 	{	/*a_SpriteRendered[j+1].dx=a_SpriteRendered[j].dx;
233 		a_SpriteRendered[j+1].dy=a_SpriteRendered[j].dy;*/
234 		a_SpriteRendered[j].fDist=a_SpriteRendered[j-1].fDist;
235 		a_SpriteRendered[j].p_Obj=a_SpriteRendered[j-1].p_Obj;
236 		a_SpriteRendered[j].iLump=a_SpriteRendered[j-1].iLump;
237 		//a_SpriteRendered[j].vis=a_SpriteRendered[j-1].vis;
238 		//a_SpriteRendered[j].bIsVisible=a_SpriteRendered[j-1].bIsVisible;
239 		a_SpriteRendered[j].bFlip=a_SpriteRendered[j-1].bFlip;
240 		a_SpriteRendered[j].fLightLevel=a_SpriteRendered[j-1].fLightLevel;
241 	}
242 
243 	// MR2806
244 	/*a_SpriteRendered[i].dx=dx;
245 	a_SpriteRendered[i].dy=dy;*/
246 	a_SpriteRendered[iInsert].fDist=fDist1;
247 	a_SpriteRendered[iInsert].p_Obj=pSpr;
248 	a_SpriteRendered[iInsert].iLump=lump;
249 	//a_SpriteRendered[iInsert].vis=vis;	// MR2606
250 	//a_SpriteRendered[iInsert].bIsVisible=false;	// MR2606
251 
252 	/*if (fixedcolormap)
253 		a_SpriteRendered[iNbSpriteRendered].fLightLevel = fixedcolormap;	// fixed map
254 	else*/ if (pSpr->frame & FF_FULLBRIGHT)
255 		a_SpriteRendered[iInsert].fLightLevel = 1.0f;				// full bright
256 	else
257 	{	// diminished light
258 		// MR0207
259 		//a_SpriteRendered[iInsert].fLightLevel =(float)(pSpr->subsector->sector->lightlevel+(extralight<<LIGHTSEGSHIFT)-iLightLevelMin)/(float)(iLightLevelMax-iLightLevelMin);
260 		//a_SpriteRendered[iInsert].fLightLevel =(float)(pSpr->subsector->sector->lightlevel+(extralight<<LIGHTSEGSHIFT))/255.0f;
261 		//a_SpriteRendered[iInsert].fLightLevel =(float)(pSpr->subsector->sector->lightlevel+(extralight<<LIGHTSEGSHIFT)-iLightLevelMin/2)/255.0f;
262 		a_SpriteRendered[iInsert].fLightLevel=(1.0f+0.25f*(float)usegamma)*((float)(pSpr->subsector->sector->lightlevel+(extralight<<LIGHTSEGSHIFT))-0.5f)/512.0f;		// MR0706
263 	}
264 
265 	a_SpriteRendered[iInsert].bFlip=flip;
266 	iNbSpriteRendered++;
267 
268 // Test with select or feedback buffers...
269 #ifdef DOOM_GL
270 /*	{ float xi,yi,zi;
271 
272 		xi=-(float)pSpr->x/MAP_SCALE;
273 		yi=((float)pSpr->z+(float)spritetopoffset[lump])/MAP_SCALE+(float)3/(MAP_SCALE>>FRACBITS);	// Why +2 ???
274 		zi=(float)pSpr->y/MAP_SCALE;
275 
276 		if (pSpr->info->doomednum==2028)
277 		{	//if (P_CheckSight(player->mo,pSpr))
278 			//if (GL_ChechCoronaInSight(player->mo,pSpr))
279 				GL_AddCorona(xi,yi,zi);
280 		}
281 	}*/
282 #endif
283 }
284 
fn_bRotateSprite(int doomednum)285 H_boolean fn_bRotateSprite(int doomednum)
286 {
287 #ifdef DOOM_GL
288 	if (((doomednum>0)&&(doomednum<5))||
289 		((doomednum>24)&&(doomednum<38))||
290 		((doomednum>43)&&(doomednum<58))||
291 		((doomednum>58)&&(doomednum<64))||
292 		((doomednum>72)&&(doomednum<79))||
293 		(doomednum==-1)||
294 		(doomednum==11)||
295 		(doomednum==14)||
296 		(doomednum==85)||
297 		(doomednum==86)||
298 		(doomednum==2025)||
299 		(doomednum==2028))
300 #elif GL_HEXEN
301 	if (
302 		((doomednum>0)&&(doomednum<6))||
303 		(doomednum==11)||
304 		(doomednum==14)||
305 		(doomednum==17)||
306 		((doomednum>23)&&(doomednum<28))||
307 		((doomednum>47)&&(doomednum<53))||
308 		((doomednum>53)&&(doomednum<58))||
309 		(doomednum==60)||
310 		(doomednum==61)||
311 		((doomednum>62)&&(doomednum<70))||
312 		((doomednum>70)&&(doomednum<81))||
313 		((doomednum>86)&&(doomednum<100))||
314 		(doomednum==103)||
315 		(doomednum==108)||
316 		(doomednum==109)||
317 		(doomednum==116)||
318 		(doomednum==117)||
319 		(doomednum==119)||
320 		(doomednum==140)||
321 		(doomednum==1090)||
322 		(doomednum==1091)||
323 		((doomednum>8041)&&(doomednum<8053))||
324 		((doomednum>8059)&&(doomednum<8069))||
325 		((doomednum>8070)&&(doomednum<8078))||
326 		(doomednum==8103)||
327 		(doomednum==9011)||
328 		(doomednum==9012))
329 #else
330 	if (
331 		((doomednum>0)&&(doomednum<5))||
332 		(doomednum==17)||
333 		((doomednum>23)&&(doomednum<30))||
334 		((doomednum>36)&&(doomednum<41))||
335 		((doomednum>46)&&(doomednum<52))||
336 		(doomednum==76)||
337 		((doomednum>93)&&(doomednum<97))
338 		)
339 #endif
340 		return false;
341 	return true;
342 }
343 
344 // MD2 (� virer)
345 extern long g_bMD2;
346 
347 extern boolean P_CheckSight(mobj_t *t1, mobj_t *t2);
348 
349 #ifdef OPTI_SPRITE
fn_vDrawSpritesCompressed(player_t * player)350 void fn_vDrawSpritesCompressed(player_t *player)
351 { int i;
352   float xi,yi,zi,/*fdx,fdy,*/fDistance,/*x1,*/y1,/*x2,*/y2,z1,z2;
353   float fWidthL,fWidthR/*,fWidth,fTang,fTangL,fTangR*/;
354   float fU1,fU2,fV1,fV2,fLightLevel;
355   mobj_t *pSpr;
356   int lump;
357   int iCoronaIdx;
358 
359 #if 0
360 	for (i=0;i<iNbSpriteRendered;i++)
361 	{
362 		pSpr=a_SpriteRendered[i].p_Obj;
363 		fLightLevel=a_SpriteRendered[i].fLightLevel;
364 		lump=a_SpriteRendered[i].iLump;
365 
366 		if (p_stGLSprArray[lump].iTexID==-1)
367 			fn_vRegisterSpriteOnTheFly(lump);
368 
369 		xi=-(float)pSpr->x/MAP_SCALE;
370 		zi=(float)pSpr->y/MAP_SCALE;
371 		fdx=(float)a_SpriteRendered[i].dx/MAP_SCALE;
372 		fdy=(float)a_SpriteRendered[i].dy/MAP_SCALE;
373 		fDistance=(float)sqrt(fdx*fdx+fdy*fdy);
374 		/*fWidth=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(2*(MAP_SCALE>>FRACBITS));
375 		fTang=fWidth/fDistance;
376 		x1=xi+fTang*fdy;
377 		y1=(float)pSpr->z/MAP_SCALE;
378 		z1=zi+fTang*fdx;
379 		x2=xi-fTang*fdy;
380 		y2=(float)pSpr->z/MAP_SCALE+(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight/(MAP_SCALE>>FRACBITS);
381 		z2=zi-fTang*fdx;*/
382 		fWidthL=(float)spriteoffset[lump]/MAP_SCALE;
383 		if (p_stGLSprArray[lump].iFlatID==-2)
384 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth*2.0f/(MAP_SCALE>>FRACBITS)-fWidthL;
385 		else
386 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(MAP_SCALE>>FRACBITS)-fWidthL;
387 		fTangL=fWidthL/fDistance;
388 		fTangR=fWidthR/fDistance;
389 		x1=xi-fTangL*fdy;
390 		y1=(float)pSpr->z/MAP_SCALE+(float)spritetopoffset[lump]/MAP_SCALE+(float)3/(MAP_SCALE>>FRACBITS);		// Why +2 ???
391 		z1=zi-fTangL*fdx;
392 		x2=xi+fTangR*fdy;
393 		if (p_stGLSprArray[lump].iFlatID==-2)
394 			y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight*2.0f/(MAP_SCALE>>FRACBITS);
395 		else
396 			y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight/(MAP_SCALE>>FRACBITS);
397 		z2=zi+fTangR*fdx;
398 
399 		if (a_SpriteRendered[i].bFlip)
400 		{
401 			fU1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
402 			fU2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
403 		}
404 		else
405 		{
406 			fU2=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
407 			fU1=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
408 		}
409 		fV1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
410 		fV2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
411 
412 		//(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[p_stGLSprArray[lump].iGLTexIndex+iNbGLTextures]);
413 		(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[p_stGLSprArray[lump].iGLTexIndex]);
414 
415 		//(*glEnable_s) (GL_ALPHA_TEST);
416 
417 		//if(pSpr->flags&MF_SHADOW)
418 		{	//(*glAlphaFunc_s) (GL_EQUAL,0.5f);
419 			(*glAlphaFunc_s) (GL_NOTEQUAL,0.0f);
420 			(*glEnable_s) (GL_BLEND);
421 			(*glBlendFunc_s) (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
422 		}
423 		/*else
424 			(*glAlphaFunc_s) (GL_EQUAL,1.0f);*/
425 
426 		if (!g_bSpriteBilinear||(fDistance>MAP_COEFF*fSpriteFilterDistanceCoeff))
427 		{	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
428 			(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
429 		}
430 
431 /*		if (pSpr->type==42)		// Wall Torch
432 			//(*glDepthFunc_s) (GL_ALWAYS);
433 			(*glDepthFunc_s) (GL_LEQUAL);*/
434 
435 		(*glBegin_s) (GL_QUADS);
436 			if(pSpr->flags&MF_SHADOW)
437 				GL_StaticLight4f(fLightLevel,fLightLevel,fLightLevel,0.5f);
438 			else
439 				GL_StaticLight3f(fLightLevel,fLightLevel,fLightLevel);
440 			(*glTexCoord2f_s) (fU1, fV1); (*glVertex3f_s) ( x1, y1, z1);
441 			(*glTexCoord2f_s) (fU1, fV2); (*glVertex3f_s) ( x1, y2, z1);
442 			(*glTexCoord2f_s) (fU2, fV2); (*glVertex3f_s) ( x2, y2, z2);
443 			(*glTexCoord2f_s) (fU2, fV1); (*glVertex3f_s) ( x2, y1, z2);
444 		(*glEnd_s) ();
445 
446 		//if(pSpr->flags&MF_SHADOW)
447 			(*glDisable_s) (GL_BLEND);
448 
449 		(*glDisable_s) (GL_ALPHA_TEST);
450 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
451 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
452 
453 /*		if (pSpr->type==42)
454 			(*glDepthFunc_s) (GL_LESS);*/
455 	}
456 #else
457 	// MR2606
458 /*    R_SortVisSprites ();
459 
460     if (vissprite_p > vissprites)
461     { vissprite_t *spr;
462 		// draw all vissprites back to front
463 		for (spr = vsprsortedhead.next ;
464 			 spr != &vsprsortedhead ;
465 			 spr=spr->next)
466 			R_DrawSprite (spr);
467     }*/
468 	// End MR2606
469 	for (i=0;i<iNbSpriteRendered;i++)
470 	{
471 		/*if (!a_SpriteRendered[i].bIsVisible)	// MR2806
472 			continue;*/
473 		pSpr=a_SpriteRendered[i].p_Obj;
474 		fLightLevel=a_SpriteRendered[i].fLightLevel;
475 
476 #ifndef GL_HEXEN
477 		if (pSpr->type==MT_PLAYER)
478 #else
479 		if ((pSpr->type==MT_PLAYER_FIGHTER)||
480 			(pSpr->type==MT_PLAYER_SPEED)||
481 			(pSpr->type==MT_PLAYER_CLERIC)||
482 			(pSpr->type==MT_PLAYER_MAGE)||
483 			(pSpr->type==MT_PIGPLAYER))
484 #endif
485 			continue;
486 		lump=a_SpriteRendered[i].iLump;
487 
488 		if (p_stGLSprArray[lump].iTexID==-1)
489 			fn_vRegisterSpriteOnTheFly(lump);
490 
491 		xi=-(float)pSpr->x/MAP_SCALE;
492 		yi=((float)pSpr->z+(float)spritetopoffset[lump])/MAP_SCALE+(float)3/(MAP_SCALE>>FRACBITS);	// Why +2 ???
493 		zi=(float)pSpr->y/MAP_SCALE;
494 		// MR2806
495 		/*fdx=(float)a_SpriteRendered[i].dx/MAP_SCALE;
496 		fdy=(float)a_SpriteRendered[i].dy/MAP_SCALE;
497 		fDistance=(float)sqrt(fdx*fdx+fdy*fdy);*/
498 		fDistance=a_SpriteRendered[i].fDist;
499 
500 		fWidthL=(float)spriteoffset[lump]/MAP_SCALE;
501 		// MR1206
502 //#ifdef GL_HEXEN
503 #if 0
504 		if (p_stGLSprArray[lump].iFlatID==-2)
505 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth*2.0f/(MAP_SCALE>>FRACBITS)-fWidthL;
506 		else
507 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(MAP_SCALE>>FRACBITS)-fWidthL;
508 #endif
509 //		x1=xi-fWidthL;
510 		y1=0;
511 		z1=zi;
512 //		x2=xi+fWidthR;
513 		if (p_stGLSprArray[lump].iFlatID==-2)
514 			y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight*2.0f/(MAP_SCALE>>FRACBITS);
515 		else
516 			y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight/(MAP_SCALE>>FRACBITS);
517 		z2=zi;
518 
519 		if (a_SpriteRendered[i].bFlip)
520 		{
521 			fU1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
522 			fU2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
523 		}
524 		else
525 		{
526 			fU2=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
527 			fU1=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
528 		}
529 		fV1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
530 		fV2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
531 
532 		//(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[p_stGLSprArray[lump].iGLTexIndex+iNbGLTextures]);
533 		(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[p_stGLSprArray[lump].iGLTexIndex]);
534 
535 		(*glEnable_s) (GL_ALPHA_TEST);	// MR2806
536 
537 		// MR2806
538 		/*if(pSpr->flags&MF_SHADOW)
539 		{	(*glAlphaFunc_s) (GL_EQUAL,0.5f);*/
540 			(*glAlphaFunc_s) (GL_NOTEQUAL,0.0f);
541 			(*glEnable_s) (GL_BLEND);
542 			(*glBlendFunc_s) (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
543 		/*}
544 		else
545 			(*glAlphaFunc_s) (GL_EQUAL,1.0f);*/
546 
547 		if (!g_bSpriteBilinear/*||(fDistance>MAP_COEFF*fSpriteFilterDistanceCoeff)*/)
548 		{	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
549 			(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
550 		}
551 
552 		(*glMatrixMode_s) (GL_MODELVIEW);
553 		(*glPushMatrix_s) ();
554 		//glLoadIdentity();
555 		//(*glTranslatef_s) (xi,(float)pSpr->z/MAP_SCALE,zi);
556 		(*glTranslatef_s) (xi,yi,zi);
557 		(*glRotatef_s) (-90.0f+(float)(player->mo->angle>>ANGLETOFINESHIFT)*360.0f/FINEANGLES,   0.0f, 1.0f, 0.0f);
558 		//if (pSpr->info->speed)
559 		if (fn_bRotateSprite(pSpr->info->doomednum))
560 			(*glRotatef_s) (-(float)player->lookdir/2 /* /4 */,   1.0f, 0.0f, 0.0f);
561 		// MR1206
562 //#ifndef GL_HEXEN	// A voir...
563 #if 1
564 		if (p_stGLSprArray[lump].iFlatID==-2)
565 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth*2.0f/(MAP_SCALE>>FRACBITS);
566 		else
567 			fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(MAP_SCALE>>FRACBITS);
568 		(*glTranslatef_s) (fWidthL,0.0f,0.0f);
569 #endif
570 		(*glBegin_s) (GL_QUADS);
571 			if(pSpr->flags&MF_SHADOW)
572 				//GL_StaticLight4f(fLightLevel,fLightLevel,fLightLevel,0.5f);
573 				GL_DynamicLight4f(xi, yi, zi,fLightLevel,0.5f);
574 			else
575 				//GL_StaticLight3f(fLightLevel,fLightLevel,fLightLevel);
576 				GL_DynamicLight4f(xi, yi, zi,fLightLevel,1.0f);
577 // Back-face culling.
578 /*			(*glTexCoord2f_s) (fU1, fV1); (*glVertex3f_s) ( -fWidthL, y1, 0);
579 			(*glTexCoord2f_s) (fU1, fV2); (*glVertex3f_s) ( -fWidthL, y2, 0);
580 			(*glTexCoord2f_s) (fU2, fV2); (*glVertex3f_s) ( fWidthR, y2, 0);
581 			(*glTexCoord2f_s) (fU2, fV1); (*glVertex3f_s) ( fWidthR, y1, 0);*/
582 // MR1206
583 //#ifdef GL_HEXEN		// A voir...
584 #if 0
585 			(*glTexCoord2f_s) (fU1, fV1); (*glVertex3f_s) ( -fWidthL, y1, 0);
586 			(*glTexCoord2f_s) (fU2, fV1); (*glVertex3f_s) ( fWidthR, y1, 0);
587 			(*glTexCoord2f_s) (fU2, fV2); (*glVertex3f_s) ( fWidthR, y2, 0);
588 			(*glTexCoord2f_s) (fU1, fV2); (*glVertex3f_s) ( -fWidthL, y2, 0);
589 #else
590 			(*glTexCoord2f_s) (fU1, fV1); (*glVertex3f_s) ( -fWidthR, y1, 0);
591 			(*glTexCoord2f_s) (fU2, fV1); (*glVertex3f_s) ( 0, y1, 0);
592 			(*glTexCoord2f_s) (fU2, fV2); (*glVertex3f_s) ( 0, y2, 0);
593 			(*glTexCoord2f_s) (fU1, fV2); (*glVertex3f_s) ( -fWidthR, y2, 0);
594 #endif
595 		(*glEnd_s) ();
596 
597 		(*glPopMatrix_s) ();
598 
599 		//if(pSpr->flags&MF_SHADOW)	// MR2806
600 			(*glDisable_s) (GL_BLEND);
601 
602 		(*glDisable_s) (GL_ALPHA_TEST);	// MR2806
603 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
604 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
605 
606 		// Corona Test...
607 		//if (usecoronas&&((iCoronaIdx=GL_GenerateCorona(pSpr->info->doomednum))!=-1))
608 		if (usecoronas&&((iCoronaIdx=GL_GenerateCorona(pSpr->type))!=-1))	// MR2007
609 		{
610 			if (P_CheckSight(player->mo,pSpr))
611 			//while (GL_iGetDoomednum(iCoronaIdx)==pSpr->info->doomednum)
612 			while (GL_iGetType(iCoronaIdx)==pSpr->type)
613 			{
614 				if (GL_ChechCoronaInSight(iCoronaIdx,player->mo,pSpr))
615 					GL_AddCorona(iCoronaIdx,xi,(float)pSpr->z/MAP_SCALE,zi,fLightLevel);
616 				iCoronaIdx++;
617 			}
618 		}
619 //#endif
620 	}
621 #endif
622 }
623 #endif		// OPTI_SPRITE
fn_vDrawSpritesNonCompressed()624 void fn_vDrawSpritesNonCompressed()
625 { int i;
626   float xi, zi, fdx=0.0f, fdy=0.0f, fDistance, x1, y1, x2, y2, z1, z2;
627   float fWidthL,fTangL,fWidthR,fTangR/*,fWidth,fTang*/;
628   float fU1,fU2,fV1,fV2,fLightLevel;
629   mobj_t *pSpr;
630   int lump;
631 
632 	for (i=0;i<iNbSpriteRendered;i++)
633 	{
634 		pSpr=a_SpriteRendered[i].p_Obj;
635 		//fLightLevel=(float)(pSpr->subsector->sector->lightlevel-iLightLevelMin)/(float)(iLightLevelMax-iLightLevelMin);
636 		fLightLevel=a_SpriteRendered[i].fLightLevel;
637 
638 		lump=a_SpriteRendered[i].iLump;
639 
640 		if (p_stGLSprArray[lump].iTexID==-1)
641 			fn_vRegisterSpriteOnTheFly(lump);
642 
643 		xi=-(float)pSpr->x/MAP_SCALE;
644 		zi=(float)pSpr->y/MAP_SCALE;
645 		// MR2806
646 /*		fdx=(float)a_SpriteRendered[i].dx/MAP_SCALE;
647 		fdy=(float)a_SpriteRendered[i].dy/MAP_SCALE;
648 		fDistance=(float)sqrt(fdx*fdx+fdy*fdy);*/
649 		fDistance=a_SpriteRendered[i].fDist;
650 		/*fWidth=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(2*(MAP_SCALE>>FRACBITS));
651 		fTang=fWidth/fDistance;
652 		x1=xi+fTang*fdy;
653 		//y1=(float)pSpr->z/MAP_SCALE+(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].topoffset/(MAP_SCALE>>FRACBITS);
654 		y1=(float)pSpr->z/MAP_SCALE+(float)spritetopoffset[lump]/MAP_SCALE+(float)2/(MAP_SCALE>>FRACBITS);		// Why +2 ???
655 		z1=zi+fTang*fdx;
656 		x2=xi-fTang*fdy;
657 		y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight/(MAP_SCALE>>FRACBITS);
658 		z2=zi-fTang*fdx;*/
659 		fWidthL=(float)spriteoffset[lump]/MAP_SCALE;
660 		fWidthR=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth/(MAP_SCALE>>FRACBITS)-fWidthL;
661 		fTangL=fWidthL/fDistance;
662 		fTangR=fWidthR/fDistance;
663 		x1=xi-fTangL*fdy;
664 		//y1=(float)pSpr->z/MAP_SCALE+(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].topoffset/(MAP_SCALE>>FRACBITS);
665 		y1=(float)pSpr->z/MAP_SCALE+(float)spritetopoffset[lump]/MAP_SCALE+(float)3/(MAP_SCALE>>FRACBITS);		// Why +2 ???
666 		z1=zi-fTangL*fdx;
667 		x2=xi+fTangR*fdy;
668 		y2=y1-(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight/(MAP_SCALE>>FRACBITS);
669 		z2=zi+fTangR*fdx;
670 
671 		if (a_SpriteRendered[i].bFlip)
672 		{
673 			fU1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
674 			fU2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
675 		}
676 		else
677 		{
678 			fU2=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
679 			fU1=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iU+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iWidth)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iWidth;
680 		}
681 		fV1=(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
682 		fV2=(float)(p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iV+p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].a_stSubTex[p_stGLSprArray[lump].iUVIndex].iHeight)/(float)p_stGLSprites[p_stGLSprArray[lump].iGLTexIndex].iHeight;
683 
684 		//(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[p_stGLSprArray[lump].iGLTexIndex+iNbGLTextures]);
685 		(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[p_stGLSprArray[lump].iGLTexIndex]);
686 
687 		(*glEnable_s) (GL_ALPHA_TEST);
688 		if(pSpr->flags&MF_SHADOW)
689 		{	(*glAlphaFunc_s) (GL_EQUAL,0.5f);
690 			(*glEnable_s) (GL_BLEND);
691 			(*glBlendFunc_s) (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
692 		}
693 		else
694 			(*glAlphaFunc_s) (GL_EQUAL,1.0f);
695 		//if (fDistance>0.78f)
696 		if (!g_bSpriteBilinear/*||fDistance>MAP_COEFF*fSpriteFilterDistanceCoeff*/)
697 		{	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
698 			(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
699 		}
700 
701 		if (pSpr->type==42)		// Wall Torch
702 			//(*glDepthFunc_s) (GL_ALWAYS);
703 			(*glDepthFunc_s) (GL_LEQUAL);
704 
705 		(*glBegin_s) (GL_QUADS);
706 			if(pSpr->flags&MF_SHADOW)
707 				GL_StaticLight4f(fLightLevel,fLightLevel,fLightLevel,0.5f);
708 			else
709 				GL_StaticLight3f(fLightLevel,fLightLevel,fLightLevel);
710 			(*glTexCoord2f_s) (fU1, fV1); (*glVertex3f_s) ( x1, y1, z1);
711 			(*glTexCoord2f_s) (fU1, fV2); (*glVertex3f_s) ( x1, y2, z1);
712 			(*glTexCoord2f_s) (fU2, fV2); (*glVertex3f_s) ( x2, y2, z2);
713 			(*glTexCoord2f_s) (fU2, fV1); (*glVertex3f_s) ( x2, y1, z2);
714 		(*glEnd_s) ();
715 
716 		if(pSpr->flags&MF_SHADOW)
717 			(*glDisable_s) (GL_BLEND);
718 		(*glDisable_s) (GL_ALPHA_TEST);
719 
720 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
721 		(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
722 
723 		if (pSpr->type==42)
724 			(*glDepthFunc_s) (GL_LESS);
725 	}
726 }
727 //#endif		// OPTI_SPRITE
728 
fn_vDrawSprites(player_t * player)729 void fn_vDrawSprites(player_t *player)
730 {
731 #ifdef OPTI_SPRITE
732 	if (g_bSpriteOpti)
733 		fn_vDrawSpritesCompressed(player);
734 	else
735 #endif
736 		fn_vDrawSpritesNonCompressed();
737 }
738 
739 // -----------------------------------------------------------------------
740 // Sprites
741 // -----------------------------------------------------------------------
fn_vAllocSpriteMemory()742 void fn_vAllocSpriteMemory()
743 { int i;
744 
745 	//p_stGLSprites=(GLTexture *)Malloc(numspritelumps*sizeof(GLTexture));
746 	//p_stMapSprites=(GLMapSprite *)Malloc(numspritelumps*sizeof(GLMapSprite));
747 	p_stGLSprites=(GLTexture *)Malloc(sizeof(GLTexture));
748 	p_stMapSprites=(GLMapSprite *)Malloc(sizeof(GLMapSprite));
749 	p_stGLSprArray=(GLTexArray *)Malloc(numspritelumps*sizeof(GLTexArray));
750 	for (i=0;i<numspritelumps;i++)
751 		p_stGLSprArray[i].iTexID=-1;
752 	p_bPalette=W_CacheLumpName("PLAYPAL", PU_STATIC);
753 	iNbGLSprites=0;
754 }
755 
756 #ifdef OPTI_SPRITE
757 
758 #define Map(i,x,y) p_stMapSprites[i].p_cTex[x+(y)*p_stGLSprites[i].iWidth]
759 
fn_vUpdateTextureFreeSpace(GLTexture * p_stGLSprites,GLMapSprite * p_stMapSprites,int iGLIndex,int iSubIndex)760 void fn_vUpdateTextureFreeSpace(GLTexture *p_stGLSprites,GLMapSprite *p_stMapSprites,int iGLIndex,int iSubIndex)
761 { int x,y,yNext,u,v,iHeight,iWidth,iFreeSpace;
762 
763 	for (y=0;y<p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iHeight;y++)
764 	for (x=0;x<p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iWidth;x++)
765 		Map(iGLIndex,x+p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iU,y+p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iV)=iSubIndex+1;
766 
767 	//yNext=0x7fffffff;
768 	if (g_bDynamicAlloc)
769 		p_stGLSprites[iGLIndex].a_stFreeSpace=(GLSubTexture *)Malloc(300*sizeof(GLSubTexture));
770 
771 	iFreeSpace=0;
772 	for (y=0;y<p_stGLSprites[iGLIndex].iHeight;y++)
773 	{	yNext=p_stGLSprites[iGLIndex].iHeight;
774 		for (x=0;x<p_stGLSprites[iGLIndex].iWidth;x++)
775 		{	if (!Map(iGLIndex,x,y))
776 			{ GLboolean bAdd;
777 			  int k;
778 
779 				u=x; v=y;
780 				while ((x<p_stGLSprites[iGLIndex].iWidth)&&(!Map(iGLIndex,x,y))) x++;
781 				iWidth=x-u;
782 				x=u;
783 				while ((y<p_stGLSprites[iGLIndex].iHeight)&&(!Map(iGLIndex,x,y))) y++;
784 				iHeight=y-v;
785 				//y=yNext;
786 				y=v;
787 				bAdd=TRUE;
788 				for (k=0;k<iFreeSpace;k++)
789 				  {
790 				    if ((p_stGLSprites[iGLIndex].a_stFreeSpace[k].iU<=u)&&
791 					(p_stGLSprites[iGLIndex].a_stFreeSpace[k].iV<=v)&&
792 					(p_stGLSprites[iGLIndex].a_stFreeSpace[k].iU+p_stGLSprites[iGLIndex].a_stFreeSpace[k].iWidth>u)&&
793 					(p_stGLSprites[iGLIndex].a_stFreeSpace[k].iV+p_stGLSprites[iGLIndex].a_stFreeSpace[k].iHeight>v)&&
794 					(p_stGLSprites[iGLIndex].a_stFreeSpace[k].iU+p_stGLSprites[iGLIndex].a_stFreeSpace[k].iWidth>=u+iWidth)&&
795 					(p_stGLSprites[iGLIndex].a_stFreeSpace[k].iV+p_stGLSprites[iGLIndex].a_stFreeSpace[k].iHeight>=v+iHeight))
796 				      bAdd=FALSE;
797 				  }
798 
799 				if (bAdd)
800 				{	p_stGLSprites[iGLIndex].a_stFreeSpace[iFreeSpace].iU=u;
801 					p_stGLSprites[iGLIndex].a_stFreeSpace[iFreeSpace].iV=v;
802 					p_stGLSprites[iGLIndex].a_stFreeSpace[iFreeSpace].iHeight=iHeight;
803 					p_stGLSprites[iGLIndex].a_stFreeSpace[iFreeSpace++].iWidth=iWidth;
804 					if (iFreeSpace>299)
805 					{	printf("[SDLGLdrv/sprites] Too much free space in texture\n");
806 						exit(0);
807 					}
808 				}
809 				break;
810 			}
811 			/*else
812 				yNext=__min(yNext,p_stGLSprites[iGLIndex].a_stSubTex[p_stMapSprites[iGLIndex].p_cTex[x][y]-1].iV+
813 								p_stGLSprites[iGLIndex].a_stSubTex[p_stMapSprites[iGLIndex].p_cTex[x][y]-1].iHeight);*/
814 		}
815 	}
816 	p_stGLSprites[iGLIndex].iNbFreeSpace=iFreeSpace;
817 
818 	if (g_bDynamicAlloc)
819 		p_stGLSprites[iGLIndex].a_stFreeSpace=(GLSubTexture *)Realloc(p_stGLSprites[iGLIndex].a_stFreeSpace,sizeof(GLSubTexture)*iFreeSpace);
820 }
821 
fn_iGetBestGLTex(GLTexture * p_stGLSprites,int width,int height,int * iU,int * iV)822 int fn_iGetBestGLTex(GLTexture *p_stGLSprites,int width,int height,int *iU,int *iV)
823 { int k,j,iGLIndex,surfaceP,surface=0,diffSurface;
824 
825 	iGLIndex=-1;
826 	surfaceP=width*height;
827 	diffSurface=0x7fffffff;
828 	for (j=0;j<iNbGLSprites;j++)
829 	{
830 	  for (k=0;k<p_stGLSprites[j].iNbFreeSpace;k++)
831 		{
832 		  surface=p_stGLSprites[j].a_stFreeSpace[k].iWidth*p_stGLSprites[j].a_stFreeSpace[k].iHeight;
833 			if ((p_stGLSprites[j].a_stFreeSpace[k].iWidth>=width)&&(p_stGLSprites[j].a_stFreeSpace[k].iHeight>=height)&&
834 				(abs(surfaceP-surface)<diffSurface))
835 			{	*iU=p_stGLSprites[j].a_stFreeSpace[k].iU;
836 				*iV=p_stGLSprites[j].a_stFreeSpace[k].iV;
837 				iGLIndex=j;
838 				diffSurface=abs(surfaceP-surface);
839 				//break;	// a virer pour surface opti
840 			}
841 		}
842 		//if (iGLIndex!=-1)	// a virer pour surface opti
843 		//	break;
844 	}
845 	return iGLIndex;
846 }
847 
848 extern GLboolean g_bInitFonts;
fn_vRegisterSmallSprite(GLTexture ** p2_stGLSprites,GLMapSprite ** p2_stMapSprites,GLTexArray ** p2_stGLSprArray,int iSpriteLump,patch_t * patch)849 int fn_vRegisterSmallSprite(GLTexture **p2_stGLSprites,GLMapSprite **p2_stMapSprites,GLTexArray **p2_stGLSprArray,int iSpriteLump,patch_t *patch)		// RGBA SPRITE ONLY !!
850 { int k,iGLIndex,iSubIndex,iFreeSpace,j,iU,iV,iSpriteWidth,iSpriteHeight;
851   unsigned char *p_bRGBBuffer1,*p_bRGBBuffer2;
852   int iGLTexW,iGLTexH;
853   GLTexture *p_stGLSprites=*p2_stGLSprites;
854   GLMapSprite *p_stMapSprites=*p2_stMapSprites;
855   GLTexArray *p_stGLSprArray=*p2_stGLSprArray;
856   int start;
857 
858 	if (p_stGLSprArray[iSpriteLump].iTexID!=-1)
859 		return -1;
860 
861 	// MR3006
862 	if (g_bInitFonts)
863 	{	iSpriteWidth=(int)((float)SHORT(patch->width)*fScaleSprite);
864 		iSpriteHeight=(int)((float)SHORT(patch->height)*fScaleSprite);
865 		start=0;
866 	}
867 	else
868 	{
869 		iSpriteWidth=(int)((float)SHORT(patch->width)*fScaleSprite)+2;
870 		iSpriteHeight=(int)((float)SHORT(patch->height)*fScaleSprite)+2;
871 		start=1;
872 	}
873 
874 	printf("[SDLGLdrv/sprites] [%x, %x]\n",(unsigned int)SHORT(patch->width),(unsigned int)SHORT(patch->height));
875 	iGLIndex=fn_iGetBestGLTex(p_stGLSprites,iSpriteWidth,iSpriteHeight,&iU,&iV);
876 	p_bRGBBuffer1=(unsigned char *)Malloc(4*SHORT(patch->width)*SHORT(patch->height));
877 	p_bRGBBuffer2=(unsigned char *)Malloc(4*(iSpriteWidth-2*start)*(iSpriteHeight-2*start));
878 	for (j=0;j<SHORT(patch->width);j++)
879 	for (k=0;k<SHORT(patch->height);k++)
880 	{	// MR2806
881 		/*p_bRGBBuffer1[4*(j+k*SHORT(patch->width))]=255;
882 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))+1]=255;
883 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))+2]=255;*/
884 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))]=0;
885 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))+1]=0;
886 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))+2]=0;
887 		p_bRGBBuffer1[4*(j+k*SHORT(patch->width))+3]=0;
888 	}
889 
890 	if (iGLIndex==-1)
891 	{
892 		iU=0;
893 		iV=0;
894 
895 		iGLTexH=__max(fn_iGetTexHeightGL(iSpriteHeight),SPRITE_HEIGHT);
896 		iGLTexW=__max(fn_iGetTexHeightGL(iSpriteWidth),SPRITE_WIDTH);
897 
898 		p_stGLSprites=(GLTexture *)Realloc(p_stGLSprites,sizeof(GLTexture)*(iNbGLSprites+1));
899 		p_stMapSprites=(GLMapSprite *)Realloc(p_stMapSprites,sizeof(GLMapSprite)*(iNbGLSprites+1));
900 		p_stMapSprites[iNbGLSprites].p_cTex=(char *)Malloc(iGLTexW*iGLTexH);
901 		memset(p_stMapSprites[iNbGLSprites].p_cTex,0,iGLTexH*iGLTexW);
902 
903 		p_stGLSprites[iNbGLSprites].iWidth=iGLTexW;
904 		p_stGLSprites[iNbGLSprites].iHeight=iGLTexH;
905 		p_stGLSprites[iNbGLSprites].p_bRGBBuffer=(unsigned char *)Malloc(iGLTexH*iGLTexW*4);
906 
907 		// MR2806
908 		memset(p_stGLSprites[iNbGLSprites].p_bRGBBuffer,0,iGLTexW*iGLTexH*4);
909 
910 		if (!g_bDynamicAlloc)
911 		{	p_stGLSprites[iNbGLSprites].a_stSubTex=(GLSubTexture *)Malloc(300*sizeof(GLSubTexture));
912 			p_stGLSprites[iNbGLSprites].a_stFreeSpace=(GLSubTexture *)Malloc(300*sizeof(GLSubTexture));
913 		}
914 		else
915 		{	p_stGLSprites[iNbGLSprites].a_stSubTex=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
916 			p_stGLSprites[iNbGLSprites].a_stFreeSpace=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
917 		}
918 		p_stGLSprites[iNbGLSprites].iNbSubTextures=0;
919 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iU=0;
920 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iV=0;
921 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iWidth=0;
922 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iHeight=0;
923 		p_stGLSprites[iNbGLSprites].iNbFreeSpace=1;
924 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iU=0;
925 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iV=0;
926 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iWidth=iGLTexW;
927 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iHeight=iGLTexH;
928 
929 		iGLIndex=iNbGLSprites++;
930 		iFreeSpace=0;
931 	}
932 	{ int x;
933 
934 		iGLTexW=p_stGLSprites[iGLIndex].iWidth;
935 		iGLTexH=p_stGLSprites[iGLIndex].iHeight;
936 		iSubIndex=p_stGLSprites[iGLIndex].iNbSubTextures;
937 		if (!g_bDynamicAlloc)
938 		{	if (iSubIndex>299)
939 			{	printf("[SDLGLdrv/sprites] Too much sub textures\n");
940 				exit(0);
941 			}
942 		}
943 		else
944 			p_stGLSprites[iGLIndex].a_stSubTex=(GLSubTexture *)Realloc(p_stGLSprites[iGLIndex].a_stSubTex,(iSubIndex+1)*sizeof(GLSubTexture));
945 
946 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iU=iU;
947 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iV=iV;
948 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iWidth=iSpriteWidth;
949 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iHeight=iSpriteHeight;
950 
951 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=0;
952 
953 		fn_vUpdateTextureFreeSpace(p_stGLSprites,p_stMapSprites,iGLIndex,iSubIndex);
954 
955 		for (x=0;x<SHORT(patch->width);x++)
956 		{ column_t *p_bColumn_t;
957 		  byte *p_bColumn;
958 
959 			p_bColumn_t=(column_t *)((byte *)patch+LONG(patch->columnofs[x]));
960 			for ( ; p_bColumn_t->topdelta != 0xff ; )
961 			{
962 				p_bColumn=(byte *)p_bColumn_t + 3;
963 
964 #undef Y
965 #define Y (j+p_bColumn_t->topdelta)
966 
967 				for (j=0;j<p_bColumn_t->length;j++)
968 				{
969 					//if (p_bColumn[j]!=205)
970 					if (1)		// DOOM_GL
971 					{	p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))]=p_bPalette[p_bColumn[j]*3];
972 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+1]=p_bPalette[p_bColumn[j]*3+1];
973 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+2]=p_bPalette[p_bColumn[j]*3+2];
974 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+3]=255;
975 					}
976 					else
977 					{	p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))]=255;
978 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+1]=255;
979 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+2]=255;
980 						p_bRGBBuffer1[4*(x+Y*SHORT(patch->width))+3]=0;
981 					}
982 				}
983 				p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
984 			}
985 		}
986 
987 		ScalePicture(p_bRGBBuffer1,SHORT(patch->width),SHORT(patch->height),p_bRGBBuffer2,iSpriteWidth-2*start,iSpriteHeight-2*start);
988 
989 		for (x=start;x<iSpriteWidth-1;x++)
990 		{ 	for (j=start;j<iSpriteHeight-1;j++)
991 			{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*iGLTexW)]=p_bRGBBuffer2[4*(x-start+(j-start)*(iSpriteWidth-2*start))];
992 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*iGLTexW)+1]=p_bRGBBuffer2[4*(x-start+(j-start)*(iSpriteWidth-2*start))+1];
993 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*iGLTexW)+2]=p_bRGBBuffer2[4*(x-start+(j-start)*(iSpriteWidth-2*start))+2];
994 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*iGLTexW)+3]=p_bRGBBuffer2[4*(x-start+(j-start)*(iSpriteWidth-2*start))+3];
995 			}
996 			/*{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*SPRITE_WIDTH)]=p_bRGBBuffer1[4*(x+j*iSpriteWidth)];
997 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*SPRITE_WIDTH)+1]=p_bRGBBuffer1[4*(x+j*iSpriteWidth)+1];
998 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*SPRITE_WIDTH)+2]=p_bRGBBuffer1[4*(x+j*iSpriteWidth)+2];
999 				p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(j+iV)*SPRITE_WIDTH)+3]=p_bRGBBuffer1[4*(x+j*iSpriteWidth)+3];
1000 			}*/
1001 		}
1002 
1003 		Free(p_bRGBBuffer1);
1004 		Free(p_bRGBBuffer2);
1005 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
1006 
1007 		p_stGLSprites[iGLIndex].iNbSubTextures++;
1008 
1009 		if (g_bDynamicAlloc)
1010 			p_stGLSprites[iGLIndex].a_stSubTex=(GLSubTexture *)Realloc(p_stGLSprites[iGLIndex].a_stSubTex,sizeof(GLSubTexture)*p_stGLSprites[iGLIndex].iNbSubTextures);
1011 
1012 		p_stGLSprArray[iSpriteLump].iTexID=iSpriteLump;
1013 		p_stGLSprArray[iSpriteLump].iFlatID=-2;	// -2 => small sprite
1014 		p_stGLSprArray[iSpriteLump].iGLTexIndex=iGLIndex;
1015 		p_stGLSprArray[iSpriteLump].iUVIndex=iSubIndex;
1016 	}
1017 
1018 	*p2_stGLSprites=p_stGLSprites;
1019 	*p2_stMapSprites=p_stMapSprites;
1020 	*p2_stGLSprArray=p_stGLSprArray;
1021 
1022 	return iGLIndex;
1023 }
1024 
1025 typedef struct _tdstSpriteList
1026 {	int lump;
1027 	int surface;
1028 	struct _tdstSpriteList *p_stNext;
1029 } tdstSpriteList;
1030 
1031 tdstSpriteList *g_stSprLst=NULL;
1032 
fn_vAddSpriteToList(int iSpriteLump,patch_t * patch)1033 void fn_vAddSpriteToList(int iSpriteLump,patch_t *patch)
1034 { int surface;
1035   tdstSpriteList *p_stTmp,*p_stTmp1,*p_stSprLst;
1036 
1037 	surface=SHORT(patch->width)*SHORT(patch->height);
1038 	if (surface>iMaxSizeBeforeReduction)
1039 		surface=(int)((float)surface*fScaleSprite*fScaleSprite);
1040 
1041 	if (!g_stSprLst)
1042 	{	g_stSprLst=(tdstSpriteList *)Malloc(sizeof(tdstSpriteList));
1043 		g_stSprLst->lump=iSpriteLump;
1044 		g_stSprLst->surface=surface;
1045 		g_stSprLst->p_stNext=NULL;
1046 	}
1047 	else
1048 	{
1049 		p_stSprLst=(tdstSpriteList *)Malloc(sizeof(tdstSpriteList));
1050 		p_stSprLst->lump=iSpriteLump;
1051 		p_stSprLst->surface=surface;
1052 		p_stSprLst->p_stNext=NULL;
1053 		p_stTmp=g_stSprLst;
1054 		p_stTmp1=NULL;
1055 		while (p_stTmp!=NULL)
1056 		{	if (iSpriteLump==p_stTmp->lump)
1057 			{	Free(p_stSprLst);
1058 				return;
1059 			}
1060 			if (surface>p_stTmp->surface)
1061 			{	if (p_stTmp1==NULL)
1062 				{	p_stSprLst->p_stNext=g_stSprLst;
1063 					g_stSprLst=p_stSprLst;
1064 				}
1065 				else
1066 				{	p_stTmp1->p_stNext=p_stSprLst;
1067 					p_stSprLst->p_stNext=p_stTmp;
1068 				}
1069 				break;
1070 			}
1071 			p_stTmp1=p_stTmp;
1072 			p_stTmp=p_stTmp->p_stNext;
1073 		}
1074 		if (p_stTmp==NULL)
1075 			p_stTmp1->p_stNext=p_stSprLst;
1076 	}
1077 }
1078 
fn_vRegisterSpriteCompressed(GLTexture ** p2_stGLSprites,GLMapSprite ** p2_stMapSprites,GLTexArray ** p2_stGLSprArray,int iSpriteLump,patch_t * patch)1079 int fn_vRegisterSpriteCompressed(GLTexture **p2_stGLSprites,GLMapSprite **p2_stMapSprites,GLTexArray **p2_stGLSprArray,int iSpriteLump,patch_t *patch)
1080 { int iGLIndex,iSubIndex,iFreeSpace,j,iU,iV;
1081   int iGLTexW,iGLTexH;
1082   GLTexture *p_stGLSprites=*p2_stGLSprites;
1083   GLMapSprite *p_stMapSprites=*p2_stMapSprites;
1084   GLTexArray *p_stGLSprArray=*p2_stGLSprArray;
1085   int iWidth,iHeight;	// MR3006
1086   int start;
1087 
1088 	if (g_bInitFonts)
1089 	{	iWidth=SHORT(patch->width);
1090 		iHeight=SHORT(patch->height);
1091 	}
1092 	else
1093 	{	iWidth=SHORT(patch->width)+2;
1094 		iHeight=SHORT(patch->height)+2;
1095 	}
1096 	if (p_stGLSprArray[iSpriteLump].iTexID!=-1)
1097 		return -1;
1098 
1099 	if (SHORT(patch->width)*SHORT(patch->height)>iMaxSizeBeforeReduction)
1100 		return fn_vRegisterSmallSprite(p2_stGLSprites,p2_stMapSprites,p2_stGLSprArray,iSpriteLump,patch);
1101 
1102 	//iGLIndex=fn_iGetBestGLTex(p_stGLSprites,SHORT(patch->width),SHORT(patch->height),&iU,&iV);
1103 	iGLIndex=fn_iGetBestGLTex(p_stGLSprites,iWidth,iHeight,&iU,&iV);
1104 
1105 	if (iGLIndex==-1)
1106 	{	iU=0;
1107 		iV=0;
1108 
1109 /*		iGLTexH=__max(fn_iGetTexHeightGL(SHORT(patch->height)),SPRITE_HEIGHT);
1110 		iGLTexW=__max(fn_iGetTexHeightGL(SHORT(patch->width)),SPRITE_WIDTH);*/
1111 		iGLTexH=__max(fn_iGetTexHeightGL(iHeight),SPRITE_HEIGHT);
1112 		iGLTexW=__max(fn_iGetTexHeightGL(iWidth),SPRITE_WIDTH);
1113 
1114 		p_stGLSprites=(GLTexture *)Realloc(p_stGLSprites,sizeof(GLTexture)*(iNbGLSprites+1));
1115 		p_stMapSprites=(GLMapSprite *)Realloc(p_stMapSprites,sizeof(GLMapSprite)*(iNbGLSprites+1));
1116 		p_stMapSprites[iNbGLSprites].p_cTex=(char *)Malloc(iGLTexW*iGLTexH);
1117 		memset(p_stMapSprites[iNbGLSprites].p_cTex,0,iGLTexW*iGLTexH);
1118 
1119 		p_stGLSprites[iNbGLSprites].iWidth=iGLTexW;
1120 		p_stGLSprites[iNbGLSprites].iHeight=iGLTexH;
1121 		p_stGLSprites[iNbGLSprites].p_bRGBBuffer=(unsigned char *)Malloc(iGLTexW*iGLTexH*4);
1122 #ifdef PALETTED_SPRITE
1123 		memset(p_stGLSprites[iNbGLSprites].p_bRGBBuffer,205,iGLTexW*iGLTexH);
1124 #else	// MR2806
1125 		memset(p_stGLSprites[iNbGLSprites].p_bRGBBuffer,0,iGLTexW*iGLTexH*4);
1126 #endif
1127 
1128 		if (!g_bDynamicAlloc)
1129 		{	p_stGLSprites[iNbGLSprites].a_stSubTex=(GLSubTexture *)Malloc(300*sizeof(GLSubTexture));
1130 			p_stGLSprites[iNbGLSprites].a_stFreeSpace=(GLSubTexture *)Malloc(300*sizeof(GLSubTexture));
1131 		}
1132 		else
1133 		{	p_stGLSprites[iNbGLSprites].a_stSubTex=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
1134 			p_stGLSprites[iNbGLSprites].a_stFreeSpace=(GLSubTexture *)Malloc(sizeof(GLSubTexture));
1135 		}
1136 		p_stGLSprites[iNbGLSprites].iNbSubTextures=0;
1137 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iU=0;
1138 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iV=0;
1139 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iWidth=0;
1140 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iHeight=0;
1141 		p_stGLSprites[iNbGLSprites].iNbFreeSpace=1;
1142 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iU=0;
1143 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iV=0;
1144 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iWidth=iGLTexW;
1145 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iHeight=iGLTexH;
1146 
1147 		iGLIndex=iNbGLSprites++;
1148 		iFreeSpace=0;
1149 	}
1150 	{ int x;
1151 
1152 		iGLTexW=p_stGLSprites[iGLIndex].iWidth;
1153 		iGLTexH=p_stGLSprites[iGLIndex].iHeight;
1154 
1155 		iSubIndex=p_stGLSprites[iGLIndex].iNbSubTextures;
1156 		if (!g_bDynamicAlloc)
1157 		{	if (iSubIndex>299)
1158 			{	printf("[SDLGLdrv/sprites] Too much sub textures\n");
1159 				exit(0);
1160 			}
1161 		}
1162 		else
1163 			p_stGLSprites[iGLIndex].a_stSubTex=(GLSubTexture *)Realloc(p_stGLSprites[iGLIndex].a_stSubTex,(iSubIndex+1)*sizeof(GLSubTexture));
1164 
1165 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iU=iU;
1166 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iV=iV;
1167 		/*p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iWidth=SHORT(patch->width);
1168 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iHeight=SHORT(patch->height);*/
1169 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iWidth=iWidth;
1170 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].iHeight=iHeight;
1171 
1172 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=0;
1173 
1174 		fn_vUpdateTextureFreeSpace(p_stGLSprites,p_stMapSprites,iGLIndex,iSubIndex);
1175 
1176 		// MR3006
1177 		if (g_bInitFonts)
1178 			start=0;
1179 		else
1180 			start=1;
1181 		//for (x=0;x<SHORT(patch->width);x++)
1182 		for (x=start;x<SHORT(patch->width);x++)
1183 		{ column_t *p_bColumn_t;
1184 		  byte *p_bColumn;
1185 
1186 			// MR3006
1187 			//p_bColumn_t=(column_t *)((byte *)patch+patch->columnofs[x]);
1188 			p_bColumn_t=(column_t *)((byte *)patch+LONG(patch->columnofs[x-start]));
1189 			for ( ; p_bColumn_t->topdelta != 0xff ; )
1190 			{
1191 				p_bColumn=(byte *)p_bColumn_t + 3;
1192 
1193 #undef Y
1194 // MR3006
1195 //#define Y j+p_bColumn_t->topdelta
1196 #define Y j+p_bColumn_t->topdelta+start
1197 
1198 				for (j=0;j<p_bColumn_t->length;j++)
1199 				{
1200 #ifndef PALETTED_SPRITE
1201 					//if (p_bColumn[j]!=205)
1202 					if (1)	// DOOM_GL
1203 					{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)]=p_bPalette[p_bColumn[j]*3];
1204 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+1]=p_bPalette[p_bColumn[j]*3+1];
1205 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+2]=p_bPalette[p_bColumn[j]*3+2];
1206 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+3]=255;
1207 					}
1208 					else
1209 					{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)]=0;
1210 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+1]=0;
1211 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+2]=0;
1212 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexW)+3]=0;
1213 						p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
1214 					}
1215 #else
1216 					p_stGLSprites[iGLIndex].p_bRGBBuffer[(x+iU)+(Y+iV)*iGLTexW]=p_bColumn[j];
1217 #endif
1218 				}
1219 				p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
1220 			}
1221 		}
1222 		p_stGLSprites[iGLIndex].iNbSubTextures++;
1223 
1224 		if (g_bDynamicAlloc)
1225 			p_stGLSprites[iGLIndex].a_stSubTex=(GLSubTexture *)Realloc(p_stGLSprites[iGLIndex].a_stSubTex,sizeof(GLSubTexture)*p_stGLSprites[iGLIndex].iNbSubTextures);
1226 
1227 		p_stGLSprArray[iSpriteLump].iTexID=iSpriteLump;
1228 		p_stGLSprArray[iSpriteLump].iFlatID=-1;
1229 		p_stGLSprArray[iSpriteLump].iGLTexIndex=iGLIndex;
1230 		p_stGLSprArray[iSpriteLump].iUVIndex=iSubIndex;
1231 	}
1232 
1233 	*p2_stGLSprites=p_stGLSprites;
1234 	*p2_stMapSprites=p_stMapSprites;
1235 	*p2_stGLSprArray=p_stGLSprArray;
1236 
1237 	return iGLIndex;
1238 }
1239 #endif	// OPTI_SPRITE
1240 
fn_vRegisterSpriteNonCompressed(int iSpriteLump,patch_t * patch)1241 int fn_vRegisterSpriteNonCompressed(int iSpriteLump,patch_t *patch)
1242 { int iGLIndex,iSubIndex,iFreeSpace,j,iU,iV,iGLTexWidth,iGLTexHeight;
1243 
1244 	if (p_stGLSprArray[iSpriteLump].iTexID!=-1)
1245 		return -1;
1246 	iGLIndex=-1;
1247 /*	for (j=0;j<iNbGLSprites;j++)
1248 	{
1249 		for (k=0;k<p_stGLSprites[j].iNbFreeSpace;k++)
1250 		if ((p_stGLSprites[j].a_stFreeSpace[k].iWidth>=SHORT(patch->width))&&(p_stGLSprites[j].a_stFreeSpace[k].iHeight>=SHORT(patch->height)))
1251 		{	iU=p_stGLSprites[j].a_stFreeSpace[k].iU;
1252 			iV=p_stGLSprites[j].a_stFreeSpace[k].iV;
1253 			iGLIndex=j;
1254 			iFreeSpace=k;
1255 			break;
1256 		}
1257 		if (iGLIndex!=-1)
1258 			break;
1259 	}*/
1260 
1261 	if (iGLIndex==-1)
1262 	{	iU=0;
1263 		iV=0;
1264 
1265 		p_stGLSprites=(GLTexture *)Realloc(p_stGLSprites,sizeof(GLTexture)*(iNbGLSprites+1));
1266 
1267 		iGLTexWidth=fn_iGetTexHeightGL(SHORT(patch->width));
1268 		iGLTexHeight=fn_iGetTexHeightGL(SHORT(patch->height));
1269 		p_stGLSprites[iNbGLSprites].iWidth=iGLTexWidth;
1270 		p_stGLSprites[iNbGLSprites].iHeight=iGLTexHeight;
1271 		p_stGLSprites[iNbGLSprites].p_bRGBBuffer=(unsigned char *)Malloc(iGLTexWidth*iGLTexHeight*4);
1272 		memset(p_stGLSprites[iNbGLSprites].p_bRGBBuffer,205,iGLTexWidth*iGLTexHeight*4);
1273 
1274 		p_stGLSprites[iNbGLSprites].a_stSubTex=(GLSubTexture *)Malloc(1*sizeof(GLSubTexture));
1275 		p_stGLSprites[iNbGLSprites].a_stFreeSpace=(GLSubTexture *)Malloc(1*sizeof(GLSubTexture));
1276 		p_stGLSprites[iNbGLSprites].iNbSubTextures=1;
1277 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iU=0;
1278 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iV=0;
1279 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iWidth=SHORT(patch->width);
1280 		p_stGLSprites[iNbGLSprites].a_stSubTex[0].iHeight=SHORT(patch->height);
1281 		//p_stGLSprites[iNbGLSprites].a_stSubTex[0].leftoffset=SHORT(patch->leftoffset);
1282 		//p_stGLSprites[iNbGLSprites].a_stSubTex[0].topoffset=SHORT(patch->topoffset);
1283 		p_stGLSprites[iNbGLSprites].iNbFreeSpace=0;
1284 		/*p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iU=0;
1285 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iV=0;
1286 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iWidth=0;
1287 		p_stGLSprites[iNbGLSprites].a_stFreeSpace[0].iHeight=0;*/
1288 
1289 		iGLIndex=iNbGLSprites++;
1290 		iFreeSpace=0;
1291 	}
1292 	{ int x;
1293 
1294 		iSubIndex=0;
1295 
1296 		p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=0;
1297 		//fn_vStretchTexture(SHORT(patch->width),SHORT(patch->height),iGLTexWidth,iGLTexHeight);
1298 
1299 		//for (y=0;y<iGLTexHeight;y++)
1300 		for (x=0;x<SHORT(patch->width);x++)
1301 		{ column_t *p_bColumn_t;
1302 		  byte *p_bColumn;
1303 
1304 			p_bColumn_t=(column_t *)((byte *)patch+LONG(patch->columnofs[x]));
1305 //			y=0;
1306 			for ( ; p_bColumn_t->topdelta != 0xff ; )
1307 			//for (x=0;x<iGLTexWidth;x++)
1308 			{
1309 				//p_bColumn=R_GetColumn(iSpriteLump,x);
1310 /*				p_bColumn_t=(column_t *)patch+patch->columnofs[xS[x]];
1311 				p_bColumn=(byte *)patch+patch->columnofs[xS[x]]+3;
1312 				if ((p_bColumn[yS[y]]!=205)||(yS[y]<p_bColumn_t->length))
1313 				{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=p_bPalette[p_bColumn[yS[y]]*3];
1314 					p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=p_bPalette[p_bColumn[yS[y]]*3+1];
1315 					p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=p_bPalette[p_bColumn[yS[y]]*3+2];
1316 					p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=255;
1317 				}*/
1318 				p_bColumn=(byte *)p_bColumn_t + 3;
1319 
1320 #undef Y
1321 #define Y j+p_bColumn_t->topdelta
1322 
1323 				for (j=0;j<p_bColumn_t->length;j++)
1324 				{	//if (p_bColumn[j]!=205)
1325 					if (1)	// DOOM_GL
1326 					{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=p_bPalette[p_bColumn[j]*3];
1327 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=p_bPalette[p_bColumn[j]*3+1];
1328 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=p_bPalette[p_bColumn[j]*3+2];
1329 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=255;
1330 					}
1331 					else
1332 					{	p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)]=255;
1333 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+1]=0;
1334 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+2]=0;
1335 						p_stGLSprites[iGLIndex].p_bRGBBuffer[4*((x+iU)+(Y+iV)*iGLTexWidth)+3]=0;
1336 						p_stGLSprites[iGLIndex].a_stSubTex[iSubIndex].bIsNZ=1;
1337 					}
1338 				}
1339 				p_bColumn_t = (column_t *)(  (byte *)p_bColumn_t + p_bColumn_t->length + 4);
1340 			}
1341 		}
1342 
1343 /*		if (!iSpriteLump)
1344 			p_stGLSprArray=(GLTexArray *)Malloc((iSpriteLump+1)*sizeof(GLTexArray));
1345 		else
1346 			p_stGLSprArray=(GLTexArray *)Realloc(p_stGLSprArray,(iSpriteLump+1)*sizeof(GLTexArray));*/
1347 
1348 		p_stGLSprArray[iSpriteLump].iTexID=iSpriteLump;
1349 		p_stGLSprArray[iSpriteLump].iFlatID=-1;
1350 		p_stGLSprArray[iSpriteLump].iGLTexIndex=iGLIndex;
1351 		p_stGLSprArray[iSpriteLump].iUVIndex=iSubIndex;
1352 	}
1353 
1354 	return iGLIndex;
1355 }
1356 
fn_vRegisterSprite(int iSpriteLump,patch_t * patch)1357 int fn_vRegisterSprite(int iSpriteLump,patch_t *patch)
1358 {
1359 #ifdef OPTI_SPRITE
1360 	if (g_bSpriteOpti)
1361 		return fn_vRegisterSpriteCompressed(&p_stGLSprites,&p_stMapSprites,&p_stGLSprArray,iSpriteLump,patch);
1362 	else
1363 #endif	// OPTI_SPRITE
1364 		return fn_vRegisterSpriteNonCompressed(iSpriteLump,patch);
1365 }
1366 
1367 extern GLboolean g_b3Dfx;
fn_vOptimizeSpriteMemory()1368 void fn_vOptimizeSpriteMemory()
1369 { int i;
1370   tdstSpriteList *p_stSprLst,*p_stTmp;
1371   patch_t *patch;
1372   FILE *hFile;
1373   int iPrevMax;
1374   float fPrevScale;
1375 
1376 	// Load "missings" lumps
1377 	/* printf("DEBUG-M: %s\n", mlump_filename); */
1378 	hFile=fopen(mlump_filename,"r");
1379 	if (hFile)
1380 	{ int iSpriteLump;
1381 
1382 		while (!feof(hFile))
1383 		{	fscanf(hFile,"%d",&iSpriteLump);
1384 			patch = W_CacheLumpNum (firstspritelump+iSpriteLump, PU_CACHE);
1385 #ifdef OPTI_SPRITE
1386 			fn_vAddSpriteToList(iSpriteLump,patch);
1387 #else
1388 			fn_vRegisterSprite(p_stSprLst->lump,patch);
1389 #endif
1390 		}
1391 		fclose(hFile);
1392 	}
1393 #ifdef OPTI_SPRITE
1394 	p_stSprLst=g_stSprLst;
1395 	while (p_stSprLst!=NULL)
1396 	{	patch = W_CacheLumpNum (firstspritelump+p_stSprLst->lump, PU_CACHE);
1397 		fn_vRegisterSprite(p_stSprLst->lump,patch);
1398 		p_stTmp=p_stSprLst;
1399 		p_stSprLst=p_stSprLst->p_stNext;
1400 		Free(p_stTmp);
1401 	}
1402 	g_stSprLst=NULL;
1403 #endif
1404 
1405 	// Load Weapons lumps
1406 	iPrevMax=iMaxSizeBeforeReduction;
1407 	iMaxSizeBeforeReduction=65536;
1408 	fPrevScale=fScaleSprite;
1409 
1410 	/* printf("DEBUG-W: %s\n", wlump_filename); */
1411 	hFile=fopen(wlump_filename,"r");
1412 	if (hFile)
1413 	{ int iSpriteLump;
1414 
1415 		while (!feof(hFile))
1416 		{	fscanf(hFile,"%d",&iSpriteLump);
1417 			patch = W_CacheLumpNum (firstspritelump+iSpriteLump, PU_CACHE);
1418 //#ifdef OPTI_SPRITE
1419 //			fn_vAddSpriteToList(iSpriteLump,patch);
1420 //#else
1421 			if ((SHORT(patch->width)>256)&&g_b3Dfx)
1422 			{	iMaxSizeBeforeReduction=2;
1423 				//fScaleSprite=256.0f/SHORT(patch->width);
1424 				fScaleSprite=256.0f/(SHORT(patch->width)+2);	// MR1307
1425 			}
1426 			else
1427 			{	iMaxSizeBeforeReduction=65536;
1428 				fScaleSprite=fPrevScale;
1429 			}
1430 			fn_vRegisterSprite(iSpriteLump,patch);
1431 //#endif
1432 		}
1433 		fclose(hFile);
1434 	}
1435 	iMaxSizeBeforeReduction=iPrevMax;
1436 	fScaleSprite=fPrevScale;
1437 
1438 	for (i=0;i<iNbGLSprites;i++)
1439 	{	p_stGLSprites[i].a_stFreeSpace=(GLSubTexture *)Realloc(p_stGLSprites[i].a_stFreeSpace,sizeof(GLSubTexture)*p_stGLSprites[i].iNbFreeSpace);
1440 		p_stGLSprites[i].a_stSubTex=(GLSubTexture *)Realloc(p_stGLSprites[i].a_stSubTex,sizeof(GLSubTexture)*p_stGLSprites[i].iNbSubTextures);
1441 	}
1442 }
1443 
1444 extern void **lumpcache;
1445 
1446 #ifdef DOOM_GL
1447 extern int		numflats;
1448 #endif
1449 
fn_vRegisterSpriteOnTheFly(int iSpriteLump)1450 void fn_vRegisterSpriteOnTheFly(int iSpriteLump)
1451 { patch_t *patch;
1452   int iOldNbGLSprites,iGLTex;
1453   FILE *hFile;
1454 
1455 	//lumpcache[firstspritelump+iSpriteLump]=0;
1456 	g_bDynamicAlloc=TRUE;
1457 
1458 	/* printf("DEBUG-M: %s\n", mlump_filename); */
1459 	hFile=fopen(mlump_filename,"a+");
1460 	fprintf(hFile,"%d\n",iSpriteLump); fflush(hFile);
1461 	fclose(hFile);
1462 	patch = W_CacheLumpNum (firstspritelump+iSpriteLump, PU_CACHE);
1463 	iOldNbGLSprites=iNbGLSprites;
1464 	iGLTex=fn_vRegisterSprite(iSpriteLump,patch);
1465 	if (iNbGLSprites==iOldNbGLSprites)
1466 	{	//(*glBindTexture_s) (GL_TEXTURE_2D, texobjs[iGLTex+iNbGLTextures]);
1467 		(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[iGLTex]);
1468 		(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLSprites[iGLTex].iWidth, p_stGLSprites[iGLTex].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLSprites[iGLTex].p_bRGBBuffer);
1469 		return;
1470 	}
1471 	//texobjs=(GLuint *)Realloc(texobjs,(iNbGLTextures+iNbGLSprites)*sizeof(GLuint));
1472 	//texobjs[iNbGLSprites-1+iNbGLTextures] = iNbGLSprites+iNbGLTextures;
1473 	sprobjs=(GLuint *)Realloc(sprobjs,iNbGLSprites*sizeof(GLuint));
1474 	sprobjs[iNbGLSprites-1] = iNbGLSprites+numflats+numpatches;
1475 
1476 	(*glBindTexture_s) (GL_TEXTURE_2D, sprobjs[iNbGLSprites-1]);
1477 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
1478 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
1479 #ifdef BILINEAR
1480 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
1481 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
1482 #else
1483 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
1484 	(*glTexParameterf_s) (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
1485 #endif
1486 	(*glTexEnvf_s) (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE /*GL_DECAL*/);
1487 
1488 	(*glTexImage2D_s) (GL_TEXTURE_2D, 0, 4, p_stGLSprites[iNbGLSprites-1].iWidth, p_stGLSprites[iNbGLSprites-1].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, p_stGLSprites[iNbGLSprites-1].p_bRGBBuffer);
1489 
1490 	g_bDynamicAlloc=FALSE;
1491 }
1492 
1493 
1494 #endif GL_HERETIC
1495