]> Shamusworld >> Repos - virtualjaguar/blob - src/sdlemu_opengl.c
New SDLEmu stuff (slightly modified) ;-)
[virtualjaguar] / src / sdlemu_opengl.c
1 /*
2  * SDLEMU library - Free sdl related functions library
3  * Copyrigh(c) 1999-2002 sdlemu development crew
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "sdlemu_opengl.h"
21
22 static SDL_Surface *texture      = 0;
23 static GLuint       texid        = 0;
24 static GLfloat      texcoord[4];
25
26 static inline int power_of_two(int input)
27 {
28         int value = 1;
29
30         while (value < input)
31                 value <<= 1;
32
33         return value;
34 }
35
36 void sdlemu_init_opengl(SDL_Surface * src, int texturetype, int size, int glfilter)
37 {
38         int w, h;
39
40         printf("\nOpenGL driver information :\n");
41         printf("\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 : ");
46
47         switch (texturetype)
48         {
49         case 1:
50                 printf("GL_QUAD rendering\n\n");
51                 break;
52         default:
53                 printf("GL_TRIANGLE rendering\n\n");
54                 break;
55         }
56
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);
61
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);
66         #else
67                 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
68         #endif
69
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);
75
76         glViewport(0, 0, src->w * size, src->h * size);
77
78         glMatrixMode(GL_PROJECTION);
79         glPushMatrix();
80         glLoadIdentity();
81
82         glOrtho(0.0, (GLdouble)(src->w * size), (GLdouble)(src->h * size), 0.0, 0.0, 1.0);
83         glMatrixMode(GL_MODELVIEW);
84         glPushMatrix();
85         glLoadIdentity();
86
87         // texture coordinates
88         texcoord[0] = 0.0f;
89         texcoord[1] = 0.0f;
90         texcoord[2] = (GLfloat)(src->w) / texture->w;
91         texcoord[3] = (GLfloat)(src->h) / texture->h;
92
93         // create an RGBA texture for the texture surface
94         glGenTextures(1, &texid);
95         glBindTexture(GL_TEXTURE_2D, texid);
96         
97         if (glfilter)
98         {
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);
102         }
103         else
104         {
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);
108     }
109
110         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
111 }
112
113 void sdlemu_draw_texture(SDL_Surface * dst, SDL_Surface * src, int texturetype)
114 {
115
116         // convert color-indexed surface to RGB texture
117         SDL_BlitSurface(src, NULL, texture, NULL);
118
119         // Texturemap complete texture to surface so we have free scaling 
120         // and antialiasing 
121         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->w, texture->h,
122                 GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
123         
124         switch (texturetype)
125         {
126         case 1:
127         glBegin(GL_QUADS);
128                 glTexCoord2f(texcoord[0], texcoord[1]);
129                 glVertex2f(0, 0);
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 );
136         glEnd();      
137
138         default:
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);
144                 glEnd();
145         }
146
147         SDL_GL_SwapBuffers();    
148 }
149
150 void sdlemu_close_opengl(void)
151 {
152         if (texture)
153                 SDL_FreeSurface(texture);
154 }
155
156
157 //
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...
161 //
162 void sdlemu_resize_texture(SDL_Surface * src, SDL_Surface * dst)
163 {
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);
167
168         // Delete old texture (if allocated)
169         if (texture)
170                 SDL_FreeSurface(texture);
171
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);
177         #else
178                 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
179         #endif
180
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);
186
187         glViewport(0, 0, dst->w, dst->h);
188
189         glMatrixMode(GL_PROJECTION);
190         glPushMatrix();
191         glLoadIdentity();
192
193         glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0);
194         glMatrixMode(GL_MODELVIEW);
195         glPushMatrix();
196         glLoadIdentity();
197
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;
202
203         // create an RGBA texture for the texture surface
204         glGenTextures(1, &texid);
205         glBindTexture(GL_TEXTURE_2D, texid);
206         
207 /*      if (glfilter)
208         {
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);
212         }
213         else*/
214         {
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);
218     }
219
220         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
221 }