2 * SDLEMU library - Free sdl related functions library
3 * Copyrigh(c) 1999-2002 sdlemu development crew
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * 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 #include "sdlemu_opengl.h"
22 static SDL_Surface *texture = 0;
23 static GLuint texid = 0;
24 static GLfloat texcoord[4];
25 static unsigned int glFilter;
27 static inline int power_of_two(int input)
37 void sdlemu_init_opengl(SDL_Surface * src, int texturetype, float size, int filter)
41 printf("\nOpenGL driver information :\n");
43 printf("Vendor: %s\n", glGetString(GL_VENDOR));
44 printf("Renderer: %s\n", glGetString(GL_RENDERER));
45 printf("Version: %s\n", glGetString(GL_VERSION));
46 printf("OpenGL drawmethod: ");
51 printf("GL_QUAD rendering\n\n");
54 printf("GL_TRIANGLE rendering\n\n");
60 // Texture width/height should be power of 2
61 // So, find the largest power of two that will contain both the width and height
62 w = power_of_two(src->w);
63 h = power_of_two(src->h);
65 // create texture surface
66 texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
67 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
68 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
70 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
73 // setup 2D gl environment
74 glPushAttrib(GL_ENABLE_BIT);
75 glDisable(GL_DEPTH_TEST);
76 glDisable(GL_CULL_FACE);
77 glEnable(GL_TEXTURE_2D);
79 glViewport(0, 0, src->w * size, src->h * size);
81 glMatrixMode(GL_PROJECTION);
85 glOrtho(0.0, (GLdouble)(src->w * size), (GLdouble)(src->h * size), 0.0, 0.0, 1.0);
86 glMatrixMode(GL_MODELVIEW);
90 // texture coordinates
93 texcoord[2] = (GLfloat)(src->w) / texture->w;
94 texcoord[3] = (GLfloat)(src->h) / texture->h;
96 // create an RGBA texture for the texture surface
97 glGenTextures(1, &texid);
98 glBindTexture(GL_TEXTURE_2D, texid);
102 printf("OpenGL filters: enabled\n");
103 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
104 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
108 printf("OpenGL filters: disabled\n");
109 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
110 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
113 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
116 void sdlemu_draw_texture(SDL_Surface * dst, SDL_Surface * src, int texturetype)
119 // convert color-indexed surface to RGB texture
120 SDL_BlitSurface(src, NULL, texture, NULL);
122 // Texturemap complete texture to surface so we have free scaling
124 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->w, texture->h,
125 GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
131 glTexCoord2f(texcoord[0], texcoord[1]);
133 glTexCoord2f(texcoord[2], texcoord[1]);
134 glVertex2f(dst->w , 0);
135 glTexCoord2f(texcoord[2], texcoord[3]);
136 glVertex2f(dst->w , dst->h );
137 glTexCoord2f(texcoord[0], texcoord[3]);
138 glVertex2f(0, dst->h );
142 glBegin(GL_TRIANGLE_STRIP);
143 glTexCoord2f(texcoord[0], texcoord[1]); glVertex3i(0, 0, 0);
144 glTexCoord2f(texcoord[2], texcoord[1]); glVertex3i(dst->w, 0, 0);
145 glTexCoord2f(texcoord[0], texcoord[3]); glVertex3i(0, dst->h, 0);
146 glTexCoord2f(texcoord[2], texcoord[3]); glVertex3i(dst->w, dst->h, 0);
150 SDL_GL_SwapBuffers();
153 void sdlemu_close_opengl(void)
156 SDL_FreeSurface(texture);
161 // Resize the texture
162 // This should honor the glFilter flag that is passed in to the initialization code,
163 // but, at the moment, it doesn't...
166 void sdlemu_resize_texture(SDL_Surface * src, SDL_Surface * dst, int filter)
168 // Texture width/height should be power of 2
169 // So, find the largest power of two that will contain both the width and height
170 int w = power_of_two(src->w), h = power_of_two(src->h);
172 // Delete old texture (if allocated)
174 SDL_FreeSurface(texture);
176 // create texture surface
177 //NOTE: Seems the byte order here *is* important! (Perhaps only for 32 BPP?)
178 texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
179 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
180 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
182 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
185 // setup 2D gl environment
186 glPushAttrib(GL_ENABLE_BIT);
187 glDisable(GL_DEPTH_TEST);
188 glDisable(GL_CULL_FACE);
189 glEnable(GL_TEXTURE_2D);
191 glViewport(0, 0, dst->w, dst->h);
193 glMatrixMode(GL_PROJECTION);
197 glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0);
198 glMatrixMode(GL_MODELVIEW);
202 // texture coordinates
203 texcoord[0] = 0.0f, texcoord[1] = 0.0f,
204 texcoord[2] = (GLfloat)(src->w) / texture->w,
205 texcoord[3] = (GLfloat)(src->h) / texture->h;
207 // create an RGBA texture for the texture surface
208 glGenTextures(1, &texid);
209 glBindTexture(GL_TEXTURE_2D, texid);
214 printf("OpenGL filters: enabled\n");
215 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
220 printf("OpenGL filters: disabled\n");
221 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
222 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
225 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);