File: gltexture.c

package info (click to toggle)
egoboo-data 2.22-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 44,296 kB
  • ctags: 2,618
  • sloc: ansic: 23,985; makefile: 94
file content (207 lines) | stat: -rw-r--r-- 7,183 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// GLTexture.c

// Egoboo, Copyright (C) 2000 Aaron Bishop

#include "egoboo.h" // GAC - Needed for Win32 stuff
#include "gltexture.h"


/********************> GLTexture_Load() <*****/
void		GLTexture_Load( GLTexture *texture, const char *filename )
{
	
	SDL_Surface	*tempSurface, *imageSurface;
	
	/* Load the bitmap into an SDL_Surface */
	imageSurface = SDL_LoadBMP( FILENAME(filename) );
	
	/* Make sure a valid SDL_Surface was returned */
	if ( imageSurface != NULL )
	{
		/* Generate an OpenGL texture */
		glGenTextures( 1, &texture->textureID );
		
		/* Set up some parameters for the format of the OpenGL texture */
		glBindTexture( GL_TEXTURE_2D, texture->textureID );					/* Bind Our Texture */
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );	/* Linear Filtered */
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );	/* Linear Filtered */
		
		/* Set the original image's size (incase it's not an exact square of a power of two) */
		texture->imgHeight = imageSurface->h;
		texture->imgWidth = imageSurface->w;
		
		/* Determine the correct power of two greater than or equal to the original image's size */
		texture->txDimensions = 2;
		while ( ( texture->txDimensions < texture->imgHeight ) && ( texture->txDimensions < texture->imgWidth ) )
			texture->txDimensions *= 2;
		
		/* Set the texture's alpha */
		texture->alpha = 1;
		
		/* Create a blank SDL_Surface (of the smallest size to fit the image) & copy imageSurface into it*/
		if(imageSurface->format->Gmask)
		{
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, imageSurface->format->Rmask, imageSurface->format->Gmask, imageSurface->format->Bmask, imageSurface->format->Amask );
		}
		else
		{
#ifdef _LITTLE_ENDIAN
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
#else
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 24, 0xFF0000, 0x00FF00, 0x0000FF, 0x000000);
#endif
		}
		SDL_BlitSurface( imageSurface, &imageSurface->clip_rect, tempSurface, &imageSurface->clip_rect );
		
		/* actually create the OpenGL textures */
		glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, tempSurface->w, tempSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tempSurface->pixels );
		
		/* get rid of our SDL_Surfaces now that we're done with them */
		SDL_FreeSurface( tempSurface );
		SDL_FreeSurface( imageSurface );
	}
	
}

/********************> GLTexture_LoadA() <*****/
void		GLTexture_LoadA( GLTexture *texture, const char *filename, Uint32 key )
{
	
	/* The key param indicates which color to set alpha to 0.  All other values are 0xFF. */
	SDL_Surface	*tempSurface, *imageSurface;
	Sint16  x,y;
	Uint32  *p;

	/* Load the bitmap into an SDL_Surface */
	imageSurface = SDL_LoadBMP( FILENAME(filename) );
	
	/* Make sure a valid SDL_Surface was returned */
	if ( imageSurface != NULL )
	{
		/* Generate an OpenGL texture */
		glGenTextures( 1, &texture->textureID );
		
		/* Set up some parameters for the format of the OpenGL texture */
		glBindTexture( GL_TEXTURE_2D, texture->textureID );					/* Bind Our Texture */
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );	/* Linear Filtered */
		glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );	/* Linear Filtered */
		
		/* Set the original image's size (incase it's not an exact square of a power of two) */
		texture->imgHeight = imageSurface->h;
		texture->imgWidth = imageSurface->w;
		
		/* Determine the correct power of two greater than or equal to the original image's size */
		texture->txDimensions = 2;
		while ( ( texture->txDimensions < texture->imgHeight ) && ( texture->txDimensions < texture->imgWidth ) )
			texture->txDimensions *= 2;
		
		/* Set the texture's alpha */
		texture->alpha = 1;
		
		/* Create a blank SDL_Surface (of the smallest size to fit the image) & copy imageSurface into it*/
              //SDL_SetColorKey(imageSurface, SDL_SRCCOLORKEY,0);
              //cvtSurface = SDL_ConvertSurface(imageSurface, &fmt, SDL_SWSURFACE);
		
		if(imageSurface->format->Gmask)
		{
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, imageSurface->format->Rmask, imageSurface->format->Gmask, imageSurface->format->Bmask, imageSurface->format->Amask );
		}
		else
		{
			#ifdef _LITTLE_ENDIAN
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
			#else
			tempSurface = SDL_CreateRGBSurface( SDL_SWSURFACE, texture->txDimensions, texture->txDimensions, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x00000000);
			#endif
		}
		//SDL_BlitSurface( cvtSurface, &cvtSurface->clip_rect, tempSurface, &cvtSurface->clip_rect );
		SDL_BlitSurface( imageSurface, &imageSurface->clip_rect, tempSurface, &imageSurface->clip_rect );
              
		/* Fix the alpha values */
		SDL_LockSurface(tempSurface);
		p = tempSurface->pixels;
		for( y = (texture->txDimensions - 1) ;y >= 0; y-- )
		{
			for( x = (texture->txDimensions - 1); x >= 0; x-- )
			{
				if( p[x+y*texture->txDimensions] != key )
				{
					#ifdef _LITTLE_ENDIAN
					p[x+y*texture->txDimensions] = p[x+y*texture->txDimensions] | 0xFF000000;
					#else
					p[x+y*texture->txDimensions] = p[x+y*texture->txDimensions] | 0x000000FF;
					#endif
				}
			}
		}
		SDL_UnlockSurface(tempSurface);
		
		/* actually create the OpenGL textures */
		glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, tempSurface->w, tempSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempSurface->pixels );
		
		/* get rid of our SDL_Surfaces now that we're done with them */
		SDL_FreeSurface( tempSurface );
		SDL_FreeSurface( imageSurface );
	}

}

/********************> GLTexture_GetTextureID() <*****/
GLuint		GLTexture_GetTextureID( GLTexture *texture )
{
	
	return texture->textureID;
	
}

/********************> GLTexture_GetImageHeight() <*****/
GLsizei		GLTexture_GetImageHeight( GLTexture *texture )
{
	
	return texture->imgHeight;
	
}

/********************> GLTexture_GetImageWidth() <*****/
GLsizei		GLTexture_GetImageWidth( GLTexture *texture )
{
	
	return texture->imgWidth;
	
}

/********************> GLTexture_GetDimensions() <*****/
GLsizei		GLTexture_GetDimensions( GLTexture *texture )
{
	
	return texture->txDimensions;
	
}

/********************> GLTexture_SetAlpha() <*****/
void		GLTexture_SetAlpha( GLTexture *texture, GLfloat alpha )
{
	
	texture->alpha = alpha;
	
}

/********************> GLTexture_GetAlpha() <*****/
GLfloat		GLTexture_GetAlpha( GLTexture *texture )
{
	
	return texture->alpha;
	
}

/********************> GLTexture_Release() <*****/
void		GLTexture_Release( GLTexture *texture )
{
	
	/* Delete the OpenGL texture */
	glDeleteTextures( 1, &texture->textureID );
	
	/* Reset the other data */
	texture->imgHeight = texture->imgWidth = texture->txDimensions = 0;
	
}