comparison test/testgl.c @ 233:5b42a7f5fab3

SDL_OPENGLBLIT is deprecated, show the "right way" of doing things
author Sam Lantinga <slouken@libsdl.org>
date Tue, 06 Nov 2001 00:15:24 +0000
parents 50620ec9c86a
children 1af4be6a73cd
comparison
equal deleted inserted replaced
232:678e3ed01019 233:5b42a7f5fab3
8 #ifdef HAVE_OPENGL 8 #ifdef HAVE_OPENGL
9 #include "SDL_opengl.h" 9 #include "SDL_opengl.h"
10 10
11 #define SHADED_CUBE 11 #define SHADED_CUBE
12 12
13 static SDL_bool USE_DEPRECATED_OPENGLBLIT = SDL_FALSE;
13 14
14 void HotKey_ToggleFullScreen(void) 15 void HotKey_ToggleFullScreen(void)
15 { 16 {
16 SDL_Surface *screen; 17 SDL_Surface *screen;
17 18
93 break; 94 break;
94 } 95 }
95 return(done); 96 return(done);
96 } 97 }
97 98
99 void SDL_GL_Enter2DMode()
100 {
101 SDL_Surface *screen = SDL_GetVideoSurface();
102
103 /* Note, there may be other things you need to change,
104 depending on how you have your OpenGL state set up.
105 */
106 glPushAttrib(GL_ENABLE_BIT);
107 glDisable(GL_DEPTH_TEST);
108 glDisable(GL_CULL_FACE);
109 glEnable(GL_TEXTURE_2D);
110
111 glViewport(0, 0, screen->w, screen->h);
112
113 glMatrixMode(GL_PROJECTION);
114 glPushMatrix();
115 glLoadIdentity();
116
117 glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0);
118
119 glMatrixMode(GL_MODELVIEW);
120 glPushMatrix();
121 glLoadIdentity();
122
123 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
124 }
125
126 void SDL_GL_Leave2DMode()
127 {
128 glMatrixMode(GL_MODELVIEW);
129 glPopMatrix();
130
131 glMatrixMode(GL_PROJECTION);
132 glPopMatrix();
133
134 glPopAttrib();
135 }
136
98 void DrawSDLLogo(void) 137 void DrawSDLLogo(void)
99 { 138 {
100 static SDL_Surface *image = NULL; 139 static SDL_Surface *image = NULL;
140 static GLuint texture;
101 static int x = 0; 141 static int x = 0;
102 static int y = 0; 142 static int y = 0;
143 static int w, h;
103 static int delta_x = 1; 144 static int delta_x = 1;
104 static int delta_y = 1; 145 static int delta_y = 1;
105 static Uint32 last_moved = 0; 146 static Uint32 last_moved = 0;
106 147
107 SDL_Rect dst; 148 SDL_Rect dst;
116 } 157 }
117 image = SDL_CreateRGBSurface( 158 image = SDL_CreateRGBSurface(
118 SDL_SWSURFACE, 159 SDL_SWSURFACE,
119 temp->w, temp->h, 160 temp->w, temp->h,
120 32, 161 32,
121 #if SDL_BYTEORDER == SDL_LIL_ENDIAN 162 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
122 0x000000FF, 163 0x000000FF,
123 0x0000FF00, 164 0x0000FF00,
124 0x00FF0000, 165 0x00FF0000,
125 0xFF000000 166 0xFF000000
126 #else 167 #else
127 0xFF000000, 168 0xFF000000,
128 0x00FF0000, 169 0x00FF0000,
129 0x0000FF00, 170 0x0000FF00,
130 0x000000FF 171 0x000000FF
131 #endif 172 #endif
132 ); 173 );
133 if ( image != NULL ) { 174 if ( image != NULL ) {
134 SDL_BlitSurface(temp, NULL, image, NULL); 175 SDL_BlitSurface(temp, NULL, image, NULL);
135 } 176 }
136 SDL_FreeSurface(temp); 177 SDL_FreeSurface(temp);
137 if ( image == NULL ) { 178 if ( image == NULL ) {
138 return; 179 return;
139 } 180 }
181 w = image->w;
182 h = image->h;
183
184 /* Create an OpenGL texture for the image */
185 if ( ! USE_DEPRECATED_OPENGLBLIT ) {
186 glGenTextures(1, &texture);
187 glBindTexture(GL_TEXTURE_2D, texture);
188 glTexParameteri(GL_TEXTURE_2D,
189 GL_TEXTURE_MAG_FILTER,
190 GL_NEAREST);
191 glTexParameteri(GL_TEXTURE_2D,
192 GL_TEXTURE_MIN_FILTER,
193 GL_NEAREST);
194 glTexImage2D(GL_TEXTURE_2D,
195 0,
196 GL_RGBA,
197 w, h,
198 0,
199 GL_RGBA,
200 GL_UNSIGNED_BYTE,
201 image->pixels);
202 SDL_FreeSurface(image); /* No longer needed */
203 }
140 } 204 }
141 205
142 screen = SDL_GetVideoSurface(); 206 screen = SDL_GetVideoSurface();
143 207
144 /* Show the image on the screen */ 208 /* Show the image on the screen */
145 dst.x = x; 209 dst.x = x;
146 dst.y = y; 210 dst.y = y;
147 dst.w = image->w; 211 dst.w = w;
148 dst.h = image->h; 212 dst.h = h;
149 213
150 /* Move it around 214 /* Move it around
151 Note that we do not clear the old position. This is because we 215 Note that we do not clear the old position. This is because we
152 perform a glClear() which clears the framebuffer and then only 216 perform a glClear() which clears the framebuffer and then only
153 update the new area. 217 update the new area.
158 x += delta_x; 222 x += delta_x;
159 if ( x < 0 ) { 223 if ( x < 0 ) {
160 x = 0; 224 x = 0;
161 delta_x = -delta_x; 225 delta_x = -delta_x;
162 } else 226 } else
163 if ( (x+image->w) > screen->w ) { 227 if ( (x+w) > screen->w ) {
164 x = screen->w-image->w; 228 x = screen->w-w;
165 delta_x = -delta_x; 229 delta_x = -delta_x;
166 } 230 }
167 y += delta_y; 231 y += delta_y;
168 if ( y < 0 ) { 232 if ( y < 0 ) {
169 y = 0; 233 y = 0;
170 delta_y = -delta_y; 234 delta_y = -delta_y;
171 } else 235 } else
172 if ( (y+image->h) > screen->h ) { 236 if ( (y+h) > screen->h ) {
173 y = screen->h-image->h; 237 y = screen->h-h;
174 delta_y = -delta_y; 238 delta_y = -delta_y;
175 } 239 }
176 SDL_BlitSurface(image, NULL, screen, &dst); 240 if ( USE_DEPRECATED_OPENGLBLIT ) {
177 } 241 SDL_BlitSurface(image, NULL, screen, &dst);
178 SDL_UpdateRects(screen, 1, &dst); 242 } else {
243 SDL_GL_Enter2DMode();
244 glBindTexture(GL_TEXTURE_2D, texture);
245 glBegin(GL_TRIANGLE_STRIP);
246 glTexCoord2f(0.0, 0.0); glVertex2i(x, y );
247 glTexCoord2f(1.0, 0.0); glVertex2i(x+w, y );
248 glTexCoord2f(0.0, 1.0); glVertex2i(x, y+h);
249 glTexCoord2f(1.0, 1.0); glVertex2i(x+w, y+h);
250 glEnd();
251 SDL_GL_Leave2DMode();
252 }
253 }
254 if ( USE_DEPRECATED_OPENGLBLIT ) {
255 SDL_UpdateRects(screen, 1, &dst);
256 }
179 } 257 }
180 258
181 int RunGLTest( int argc, char* argv[], 259 int RunGLTest( int argc, char* argv[],
182 int logo, int slowly, int bpp, float gamma ) 260 int logo, int slowly, int bpp, float gamma )
183 { 261 {
220 bpp = 16; /* More doesn't seem to work */ 298 bpp = 16; /* More doesn't seem to work */
221 } 299 }
222 } 300 }
223 301
224 /* Set the flags we want to use for setting the video mode */ 302 /* Set the flags we want to use for setting the video mode */
225 if ( logo ) { 303 if ( logo && USE_DEPRECATED_OPENGLBLIT ) {
226 video_flags = SDL_OPENGLBLIT; 304 video_flags = SDL_OPENGLBLIT;
227 } else { 305 } else {
228 video_flags = SDL_OPENGL; 306 video_flags = SDL_OPENGL;
229 } 307 }
230 for ( i=1; argv[i]; ++i ) { 308 for ( i=1; argv[i]; ++i ) {
476 if ( strcmp(argv[i], "-twice") == 0 ) { 554 if ( strcmp(argv[i], "-twice") == 0 ) {
477 ++numtests; 555 ++numtests;
478 } 556 }
479 if ( strcmp(argv[i], "-logo") == 0 ) { 557 if ( strcmp(argv[i], "-logo") == 0 ) {
480 logo = 1; 558 logo = 1;
559 USE_DEPRECATED_OPENGLBLIT = SDL_FALSE;
560 }
561 if ( strcmp(argv[i], "-logoblit") == 0 ) {
562 logo = 1;
563 USE_DEPRECATED_OPENGLBLIT = SDL_TRUE;
481 } 564 }
482 if ( strcmp(argv[i], "-slow") == 0 ) { 565 if ( strcmp(argv[i], "-slow") == 0 ) {
483 slowly = 1; 566 slowly = 1;
484 } 567 }
485 if ( strcmp(argv[i], "-bpp") == 0 ) { 568 if ( strcmp(argv[i], "-bpp") == 0 ) {