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];
26 static inline int power_of_two(int input)
36 void sdlemu_init_opengl(SDL_Surface * src, int texturetype, int size, int glfilter)
40 printf("\nOpenGL driver information :\n");
42 printf("Vendor : %s\n", glGetString(GL_VENDOR));
43 printf("Renderer : %s\n", glGetString(GL_RENDERER));
44 printf("Version : %s\n", glGetString(GL_VERSION));
45 printf("OpenGL drawmethod : ");
50 printf("GL_QUAD rendering\n\n");
53 printf("GL_TRIANGLE rendering\n\n");
57 // Texture width/height should be power of 2
58 // So, find the largest power of two that will contain both the width and height
59 w = power_of_two(src->w);
60 h = power_of_two(src->h);
62 // create texture surface
63 texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
64 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
65 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
67 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
70 // setup 2D gl environment
71 glPushAttrib(GL_ENABLE_BIT);
72 glDisable(GL_DEPTH_TEST);
73 glDisable(GL_CULL_FACE);
74 glEnable(GL_TEXTURE_2D);
76 glViewport(0, 0, src->w * size, src->h * size);
78 glMatrixMode(GL_PROJECTION);
82 glOrtho(0.0, (GLdouble)(src->w * size), (GLdouble)(src->h * size), 0.0, 0.0, 1.0);
83 glMatrixMode(GL_MODELVIEW);
87 // texture coordinates
90 texcoord[2] = (GLfloat)(src->w) / texture->w;
91 texcoord[3] = (GLfloat)(src->h) / texture->h;
93 // create an RGBA texture for the texture surface
94 glGenTextures(1, &texid);
95 glBindTexture(GL_TEXTURE_2D, texid);
99 printf("OpenGL filters : enabled\n");
100 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
105 printf("OpenGL filters : disabled\n");
106 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
110 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
113 void sdlemu_draw_texture(SDL_Surface * dst, SDL_Surface * src, int texturetype)
116 // convert color-indexed surface to RGB texture
117 SDL_BlitSurface(src, NULL, texture, NULL);
119 // Texturemap complete texture to surface so we have free scaling
121 glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->w, texture->h,
122 GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
128 glTexCoord2f(texcoord[0], texcoord[1]);
130 glTexCoord2f(texcoord[2], texcoord[1]);
131 glVertex2f(dst->w , 0);
132 glTexCoord2f(texcoord[2], texcoord[3]);
133 glVertex2f(dst->w , dst->h );
134 glTexCoord2f(texcoord[0], texcoord[3]);
135 glVertex2f(0, dst->h );
139 glBegin(GL_TRIANGLE_STRIP);
140 glTexCoord2f(texcoord[0], texcoord[1]); glVertex3i(0, 0, 0);
141 glTexCoord2f(texcoord[2], texcoord[1]); glVertex3i(dst->w, 0, 0);
142 glTexCoord2f(texcoord[0], texcoord[3]); glVertex3i(0, dst->h, 0);
143 glTexCoord2f(texcoord[2], texcoord[3]); glVertex3i(dst->w, dst->h, 0);
147 SDL_GL_SwapBuffers();
150 void sdlemu_close_opengl(void)
153 SDL_FreeSurface(texture);
158 // Resize the texture
159 // This should honor the glFilter flag that is passed in to the initialization code,
160 // but, at the moment, it doesn't...
162 void sdlemu_resize_texture(SDL_Surface * src, SDL_Surface * dst)
164 // Texture width/height should be power of 2
165 // So, find the largest power of two that will contain both the width and height
166 int w = power_of_two(src->w), h = power_of_two(src->h);
168 // Delete old texture (if allocated)
170 SDL_FreeSurface(texture);
172 // create texture surface
173 //NOTE: Seems the byte order here *is* important! (Perhaps only for 32 BPP?)
174 texture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32,
175 #if SDL_BYTEORDER == SDL_LIL_ENDIAN
176 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
178 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
181 // setup 2D gl environment
182 glPushAttrib(GL_ENABLE_BIT);
183 glDisable(GL_DEPTH_TEST);
184 glDisable(GL_CULL_FACE);
185 glEnable(GL_TEXTURE_2D);
187 glViewport(0, 0, dst->w, dst->h);
189 glMatrixMode(GL_PROJECTION);
193 glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0);
194 glMatrixMode(GL_MODELVIEW);
198 // texture coordinates
199 texcoord[0] = 0.0f, texcoord[1] = 0.0f,
200 texcoord[2] = (GLfloat)(src->w) / texture->w,
201 texcoord[3] = (GLfloat)(src->h) / texture->h;
203 // create an RGBA texture for the texture surface
204 glGenTextures(1, &texid);
205 glBindTexture(GL_TEXTURE_2D, texid);
209 printf("OpenGL filters : enabled\n");
210 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
211 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
215 printf("OpenGL filters : disabled\n");
216 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
217 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
220 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);