[PATCH] Use glGenTextures for texture handles

Rather than just assuming that the texture handles that we generate are okay
to use, use the proper glGenTextures function. Note that we're still leaking
textures all over the place (every map load, etc.) - proper texture management
is still needed.

diff -urN a/common/gl_draw.c head/common/gl_draw.c
--- a/common/gl_draw.c	2006-05-13 20:17:10.000000000 +0930
+++ head/common/gl_draw.c	2006-05-24 10:34:32.000000000 +0930
@@ -54,9 +54,9 @@
 qpic_t *draw_disc;
 static qpic_t *draw_backtile;
 
-static int translate_texture;
-static int char_texture;
-static int cs_texture;		// crosshair texture
+static GLuint translate_texture;
+static GLuint char_texture;
+static GLuint cs_texture;		// crosshair texture
 
 static byte cs_data[64] = {
     0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff,
@@ -87,7 +87,7 @@
 static int texels;
 
 typedef struct {
-    int texnum;
+    GLuint texnum;
     char identifier[64];
     int width, height;
     qboolean mipmap;
@@ -135,7 +135,7 @@
 static int scrap_allocated[MAX_SCRAPS][BLOCK_WIDTH];
 static byte scrap_texels[MAX_SCRAPS][BLOCK_WIDTH * BLOCK_HEIGHT * 4];
 static qboolean scrap_dirty;
-static int scrap_texnum;
+static GLuint scrap_textures[MAX_SCRAPS];
 
 // returns a texture number and the position inside it
 static int
@@ -185,7 +185,7 @@
 
     scrap_uploads++;
     for (texnum = 0; texnum < MAX_SCRAPS; ++texnum) {
-	GL_Bind(scrap_texnum + texnum);
+	GL_Bind(scrap_textures[texnum]);
 	GL_Upload8(scrap_texels[texnum], BLOCK_WIDTH, BLOCK_HEIGHT, false,
 		   true);
     }
@@ -232,8 +232,7 @@
 	    for (j = 0; j < p->width; j++, k++)
 		scrap_texels[texnum][(y + i) * BLOCK_WIDTH + x + j] =
 		    p->data[k];
-	texnum += scrap_texnum;
-	gl->texnum = texnum;
+	gl->texnum = scrap_textures[texnum];
 	gl->sl = (x + 0.01) / (float)BLOCK_WIDTH;
 	gl->sh = (x + p->width - 0.01) / (float)BLOCK_WIDTH;
 	gl->tl = (y + 0.01) / (float)BLOCK_WIDTH;
@@ -512,11 +511,10 @@
     Hunk_FreeToLowMark(start);
 
     // save a texture slot for translated picture
-    translate_texture = texture_extension_number++;
+    glGenTextures(1, &translate_texture);
 
     // save slots for scraps
-    scrap_texnum = texture_extension_number;
-    texture_extension_number += MAX_SCRAPS;
+    glGenTextures(MAX_SCRAPS, scrap_textures);
 
     //
     // get the other pics we need
@@ -1364,8 +1362,7 @@
     strncpy(glt->identifier, identifier, sizeof(glt->identifier) - 1);
     glt->identifier[sizeof(glt->identifier) - 1] = '\0';
 
-    glt->texnum = texture_extension_number;
-    texture_extension_number++;
+    glGenTextures(1, &glt->texnum);
 
   GL_LoadTexture_setup:
     glt->crc = crc;
diff -urN a/common/gl_rsurf.c head/common/gl_rsurf.c
--- a/common/gl_rsurf.c	2006-05-20 17:10:37.000000000 +0930
+++ head/common/gl_rsurf.c	2006-05-24 10:35:47.000000000 +0930
@@ -52,15 +52,17 @@
 
 int skytexturenum;
 
+#define	MAX_LIGHTMAPS	80
+
 static int lightmap_bytes;		// 1, 2, or 4
-static int lightmap_textures;
+static int lightmap_textures_initialised = 0;
+static GLuint lightmap_textures[MAX_LIGHTMAPS];
 
 static unsigned blocklights[18 * 18];
 
 #define	BLOCK_WIDTH	128
 #define	BLOCK_HEIGHT	128
 
-#define	MAX_LIGHTMAPS	80
 
 typedef struct glRect_s {
     unsigned char l, t, w, h;
@@ -541,7 +543,7 @@
 	p = lightmap_polys[i];
 	if (!p)
 	    continue;
-	GL_Bind(lightmap_textures + i);
+	GL_Bind(lightmap_textures[i]);
 	if (lightmap_modified[i]) {
 	    R_UploadLightmapUpdate(i);
 	    lightmap_modified[i] = false;
@@ -657,7 +659,7 @@
 	/* All lightmaps are up to date... */
 	i = fa->lightmaptexturenum;
 	GL_EnableMultitexture();
-	GL_Bind(lightmap_textures + i);
+	GL_Bind(lightmap_textures[i]);
 	if (lightmap_modified[i]) {
 	    R_UploadLightmapUpdate(i);
 	    lightmap_modified[i] = false;
@@ -1308,9 +1310,10 @@
 
     r_framecount = 1;		// no dlightcache
 
-    if (!lightmap_textures) {
-	lightmap_textures = texture_extension_number;
-	texture_extension_number += MAX_LIGHTMAPS;
+    // FIXME - move this to one of the init functions...
+    if (!lightmap_textures_initialised) {
+	glGenTextures(MAX_LIGHTMAPS, lightmap_textures);
+	lightmap_textures_initialised = 1;
     }
 
     gl_lightmap_format = GL_LUMINANCE;
@@ -1431,7 +1434,7 @@
 	lightmap_rectchange[i].t = BLOCK_HEIGHT;
 	lightmap_rectchange[i].w = 0;
 	lightmap_rectchange[i].h = 0;
-	GL_Bind(lightmap_textures + i);
+	GL_Bind(lightmap_textures[i]);
 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
 	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 	glTexImage2D(GL_TEXTURE_2D, 0, lightmap_bytes, BLOCK_WIDTH,
diff -urN a/common/gl_vidlinuxglx.c head/common/gl_vidlinuxglx.c
--- a/common/gl_vidlinuxglx.c	2006-02-19 08:49:32.000000000 +1030
+++ head/common/gl_vidlinuxglx.c	2006-05-24 10:38:26.000000000 +0930
@@ -93,8 +93,6 @@
 //int           texture_mode = GL_LINEAR_MIPMAP_NEAREST;
 //int           texture_mode = GL_LINEAR_MIPMAP_LINEAR;
 
-int texture_extension_number = 1;
-
 float gldepthmin, gldepthmax;
 
 cvar_t gl_ztrick = { "gl_ztrick", "1" };
diff -urN a/common/gl_vidnt.c head/common/gl_vidnt.c
--- a/common/gl_vidnt.c	2006-03-12 06:18:04.000000000 +1030
+++ head/common/gl_vidnt.c	2006-05-24 10:38:35.000000000 +0930
@@ -571,8 +571,6 @@
 //int           texture_mode = GL_LINEAR_MIPMAP_NEAREST;
 //int           texture_mode = GL_LINEAR_MIPMAP_LINEAR;
 
-int texture_extension_number = 1;
-
 static void
 CheckMultiTextureExtensions(void)
 {
diff -urN a/common/gl_warp.c head/common/gl_warp.c
--- a/common/gl_warp.c	2006-05-20 17:10:37.000000000 +0930
+++ head/common/gl_warp.c	2006-05-24 10:33:22.000000000 +0930
@@ -31,10 +31,13 @@
 
 int skytexturenum;
 
-int solidskytexture;
-int alphaskytexture;
-float speedscale;		// for top sky and bottom sky
-float speedscale2;		// for sky alpha layer using multitexture
+static GLuint solidskytexture;
+static GLuint alphaskytexture;
+static int solidskytexture_initialised = 0;
+static int alphaskytexture_initialised = 0;
+
+static float speedscale;	// for top sky and bottom sky
+static float speedscale2;	// for sky alpha layer using multitexture
 
 msurface_t *warpface;
 
@@ -425,8 +428,11 @@
     ((byte *)&transpix)[2] = b / (128 * 128);
     ((byte *)&transpix)[3] = 0;
 
-    if (!solidskytexture)
-	solidskytexture = texture_extension_number++;
+    // FIXME - move this to some init function
+    if (!solidskytexture_initialised) {
+	glGenTextures(1, &solidskytexture);
+	solidskytexture_initialised = 1;
+    }
     GL_Bind(solidskytexture);
     glTexImage2D(GL_TEXTURE_2D, 0, gl_solid_format, 128, 128, 0, GL_RGBA,
 		 GL_UNSIGNED_BYTE, trans);
@@ -442,8 +448,12 @@
 		trans[(i * 128) + j] = d_8to24table[p];
 	}
 
-    if (!alphaskytexture)
-	alphaskytexture = texture_extension_number++;
+    // FIXME - move this to an init function...
+    if (!alphaskytexture_initialised) {
+	glGenTextures(1, &alphaskytexture);
+	alphaskytexture_initialised = 1;
+    }
+
     GL_Bind(alphaskytexture);
     glTexImage2D(GL_TEXTURE_2D, 0, gl_alpha_format, 128, 128, 0, GL_RGBA,
 		 GL_UNSIGNED_BYTE, trans);
diff -urN a/include/glquake.h head/include/glquake.h
--- a/include/glquake.h	2005-12-30 22:26:52.000000000 +1030
+++ head/include/glquake.h	2006-05-24 10:38:00.000000000 +0930
@@ -31,8 +31,9 @@
 #include <GL/glext.h>
 #endif
 
-#include "gl_model.h"
 #include "client.h"
+#include "gl_model.h"
+#include "protocol.h"
 
 #ifndef APIENTRY
 #define APIENTRY
@@ -72,7 +73,6 @@
 #define GL_MAX_TEXTURE_UNITS GL_MAX_TEXTURE_UNITS_ARB
 #endif
 
-extern int texture_extension_number;
 extern int texture_mode;
 
 extern float gldepthmin, gldepthmax;
@@ -182,9 +182,9 @@
 extern int d_lightstylevalue[256];	// 8.8 fraction of base light value
 
 extern qboolean envmap;
-extern int currenttexture;
-extern int particletexture;
-extern int playertextures;
+extern GLuint currenttexture;
+extern GLuint particletexture;
+extern GLuint playertextures[MAX_CLIENTS];
 
 extern int skytexturenum;	// index in cl.loadmodel, not gl texture object
 
@@ -225,7 +225,7 @@
 #endif
 
 #ifdef QW_HACK
-extern int netgraphtexture;	// netgraph texture
+extern GLuint netgraphtexture;	// netgraph texture
 extern cvar_t r_netgraph;
 void R_NetGraph(void);
 #endif
@@ -270,10 +270,6 @@
 void EmitSkyPolys(msurface_t *fa);
 void R_DrawSkyChain(msurface_t *s);
 
-extern int solidskytexture;
-extern int alphaskytexture;
-extern float speedscale;    // for top sky and bottom sky
-
 //
 // gl_draw.c
 //
diff -urN a/NQ/gl_rmain.c head/NQ/gl_rmain.c
--- a/NQ/gl_rmain.c	2006-05-20 17:10:37.000000000 +0930
+++ head/NQ/gl_rmain.c	2006-05-24 09:59:46.000000000 +0930
@@ -45,8 +45,8 @@
 
 qboolean envmap;		// true during envmap command capture
 
-int currenttexture = -1;	// to avoid unnecessary texture sets
-int playertextures;		// up to 16 color translated skins
+GLuint currenttexture = -1;	// to avoid unnecessary texture sets
+GLuint playertextures[MAX_CLIENTS];// up to 16 color translated skins
 
 int mirrortexturenum;		// quake texturenum, not gltexturenum
 qboolean mirror;
@@ -552,7 +552,7 @@
 	if (i >= 1 && i <= cl.maxclients
 	    /* && !strcmp (currententity->model->name, "progs/player.mdl") */
 	    )
-	    GL_Bind(playertextures - 1 + i);
+	    GL_Bind(playertextures[i - 1]);
     }
 
     if (gl_smoothmodels.value)
diff -urN a/NQ/gl_rmisc.c head/NQ/gl_rmisc.c
--- a/NQ/gl_rmisc.c	2006-05-20 17:10:37.000000000 +0930
+++ head/NQ/gl_rmisc.c	2006-05-24 10:07:06.000000000 +0930
@@ -27,7 +27,7 @@
 #include "sys.h"
 
 // FIXME - should only be needed in r_part.c or here, not both.
-int particletexture;
+GLuint particletexture;
 
 /*
 ==================
@@ -84,7 +84,7 @@
     //
     // particle texture
     //
-    particletexture = texture_extension_number++;
+    glGenTextures(1, &particletexture);
     GL_Bind(particletexture);
 
     for (x = 0; x < 8; x++) {
@@ -233,8 +233,7 @@
     R_InitParticles();
     R_InitParticleTexture();
 
-    playertextures = texture_extension_number;
-    texture_extension_number += MAX_CLIENTS;
+    glGenTextures(MAX_CLIENTS, playertextures);
 }
 
 /*
@@ -308,7 +307,7 @@
 
     // because this happens during gameplay, do it fast
     // instead of sending it through gl_upload 8
-    GL_Bind(playertextures + playernum);
+    GL_Bind(playertextures[playernum]);
 
 #if 0
     byte translated[320 * 200];
diff -urN a/QW/client/gl_ngraph.c head/QW/client/gl_ngraph.c
--- a/QW/client/gl_ngraph.c	2004-07-25 15:59:56.000000000 +0930
+++ head/QW/client/gl_ngraph.c	2006-05-24 10:13:49.000000000 +0930
@@ -29,7 +29,7 @@
 // FIXME - header hacks
 extern byte *draw_chars;
 
-int netgraphtexture;		// netgraph texture
+GLuint netgraphtexture;		// netgraph texture
 
 #define NET_GRAPHHEIGHT 32
 
diff -urN a/QW/client/gl_rmain.c head/QW/client/gl_rmain.c
--- a/QW/client/gl_rmain.c	2006-05-20 17:10:37.000000000 +0930
+++ head/QW/client/gl_rmain.c	2006-05-24 10:02:57.000000000 +0930
@@ -46,8 +46,8 @@
 
 qboolean envmap;		// true during envmap command capture
 
-int currenttexture = -1;	// to avoid unnecessary texture sets
-int playertextures;		// up to 16 color translated skins
+GLuint currenttexture = -1;	// to avoid unnecessary texture sets
+GLuint playertextures[MAX_CLIENTS];// up to 16 color translated skins
 
 int mirrortexturenum;		// quake texturenum, not gltexturenum
 qboolean mirror;
@@ -556,7 +556,7 @@
 	    R_TranslatePlayerSkin(i);
 	}
 	if (i >= 0 && i < MAX_CLIENTS)
-	    GL_Bind(playertextures + i);
+	    GL_Bind(playertextures[i]);
     }
 
     if (gl_smoothmodels.value)
diff -urN a/QW/client/gl_rmisc.c head/QW/client/gl_rmisc.c
--- a/QW/client/gl_rmisc.c	2006-05-20 17:10:37.000000000 +0930
+++ head/QW/client/gl_rmisc.c	2006-05-24 10:13:28.000000000 +0930
@@ -27,7 +27,7 @@
 #include "sys.h"
 
 // FIXME - should only be needed in r_part.c or here, not both.
-int particletexture;
+GLuint particletexture;
 
 /*
 ==================
@@ -84,7 +84,7 @@
     //
     // particle texture
     //
-    particletexture = texture_extension_number++;
+    glGenTextures(1, &particletexture);
     GL_Bind(particletexture);
 
     for (x = 0; x < 8; x++) {
@@ -233,11 +233,8 @@
     R_InitParticles();
     R_InitParticleTexture();
 
-    netgraphtexture = texture_extension_number;
-    texture_extension_number++;
-
-    playertextures = texture_extension_number;
-    texture_extension_number += MAX_CLIENTS;
+    glGenTextures(1, &netgraphtexture);
+    glGenTextures(MAX_CLIENTS, playertextures);
 }
 
 /*
@@ -323,7 +320,7 @@
 
 	// because this happens during gameplay, do it fast
 	// instead of sending it through gl_upload 8
-	GL_Bind(playertextures + playernum);
+	GL_Bind(playertextures[playernum]);
 
 #if 0
 	s = 320 * 200;
