]> Shamusworld >> Repos - virtualjaguar/blob - src/sdlemu_opengl.c
1.0.7 update
[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 static unsigned int glFilter;
26
27 static inline int power_of_two(int input)
28 {
29         int value = 1;
30
31         while (value < input)
32                 value <<= 1;
33
34         return value;
35 }
36
37 void sdlemu_init_opengl(SDL_Surface * src, int texturetype, float size, int filter)
38 {
39         int w, h;
40
41         printf("\nOpenGL driver information :\n");
42         printf("\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: ");
47
48         switch (texturetype)
49         {
50         case 1:
51                 printf("GL_QUAD rendering\n\n");
52                 break;
53         default:
54                 printf("GL_TRIANGLE rendering\n\n");
55                 break;
56         }
57
58         glFilter = filter;
59
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);
64
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);
69         #else
70                 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
71         #endif
72
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);
78
79         glViewport(0, 0, src->w * size, src->h * size);
80
81         glMatrixMode(GL_PROJECTION);
82         glPushMatrix();
83         glLoadIdentity();
84
85         glOrtho(0.0, (GLdouble)(src->w * size), (GLdouble)(src->h * size), 0.0, 0.0, 1.0);
86         glMatrixMode(GL_MODELVIEW);
87         glPushMatrix();
88         glLoadIdentity();
89
90         // texture coordinates
91         texcoord[0] = 0.0f;
92         texcoord[1] = 0.0f;
93         texcoord[2] = (GLfloat)(src->w) / texture->w;
94         texcoord[3] = (GLfloat)(src->h) / texture->h;
95
96         // create an RGBA texture for the texture surface
97         glGenTextures(1, &texid);
98         glBindTexture(GL_TEXTURE_2D, texid);
99         
100         if (glFilter)
101         {
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);
105         }
106         else
107         {
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);
111     }
112
113         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
114 }
115
116 void sdlemu_draw_texture(SDL_Surface * dst, SDL_Surface * src, int texturetype)
117 {
118
119         // convert color-indexed surface to RGB texture
120         SDL_BlitSurface(src, NULL, texture, NULL);
121
122         // Texturemap complete texture to surface so we have free scaling 
123         // and antialiasing 
124         glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture->w, texture->h,
125                 GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
126         
127         switch (texturetype)
128         {
129         case 1:
130         glBegin(GL_QUADS);
131                 glTexCoord2f(texcoord[0], texcoord[1]);
132                 glVertex2f(0, 0);
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 );
139         glEnd();      
140
141         default:
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);
147                 glEnd();
148         }
149
150         SDL_GL_SwapBuffers();    
151 }
152
153 void sdlemu_close_opengl(void)
154 {
155         if (texture)
156                 SDL_FreeSurface(texture);
157 }
158
159
160 //
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...
164 // Now it does...!
165 //
166 void sdlemu_resize_texture(SDL_Surface * src, SDL_Surface * dst, int filter)
167 {
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);
171
172         // Delete old texture (if allocated)
173         if (texture)
174                 SDL_FreeSurface(texture);
175
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);
181         #else
182                 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);
183         #endif
184
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);
190
191         glViewport(0, 0, dst->w, dst->h);
192
193         glMatrixMode(GL_PROJECTION);
194         glPushMatrix();
195         glLoadIdentity();
196
197         glOrtho(0.0, (GLdouble)dst->w, (GLdouble)dst->h, 0.0, 0.0, 1.0);
198         glMatrixMode(GL_MODELVIEW);
199         glPushMatrix();
200         glLoadIdentity();
201
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;
206
207         // create an RGBA texture for the texture surface
208         glGenTextures(1, &texid);
209         glBindTexture(GL_TEXTURE_2D, texid);
210         
211 //      if (glFilter)
212         if (filter)
213         {
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);
217         }
218         else
219         {
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);
223     }
224
225         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture->w, texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels);
226 }