06854c91695dc6e0d22470e2b85c7bccf6012863
[freeglut] / freeglut_misc.c
1 /*
2  * freeglut_misc.c
3  *
4  * Functions that didn't fit anywhere else...
5  *
6  * Copyright (c) 1999-2000 Pawel W. Olszta. All Rights Reserved.
7  * Written by Pawel W. Olszta, <olszta@sourceforge.net>
8  * Creation date: Thu Dec 9 1999
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
23  * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
24  * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  */
27
28 #include <GL/freeglut.h>
29 #include "freeglut_internal.h"
30
31 /*
32  * TODO BEFORE THE STABLE RELEASE:
33  *
34  *  glutSetColor()     --
35  *  glutGetColor()     --
36  *  glutCopyColormap() --
37  *  glutSetKeyRepeat() -- this is evil and should be removed from API
38  */
39
40 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
41
42 /*
43  * This functions checks if an OpenGL extension is supported or not
44  *
45  * XXX Wouldn't this be simpler and clearer if we used strtok()?
46  */
47 int FGAPIENTRY glutExtensionSupported( const char* extension )
48 {
49   const char *extensions, *start;
50   const size_t len = strlen( extension );
51
52   /* Make sure there is a current window, and thus a current context available */
53   FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutExtensionSupported" );
54   freeglut_return_val_if_fail( fgStructure.CurrentWindow != NULL, 0 );
55
56   if (strchr(extension, ' '))
57     return 0;
58   start = extensions = (const char *) glGetString(GL_EXTENSIONS);
59
60   /* XXX consider printing a warning to stderr that there's no current
61    * rendering context.
62    */
63   freeglut_return_val_if_fail( extensions != NULL, 0 );
64
65   while (1) {
66      const char *p = strstr(extensions, extension);
67      if (!p)
68         return 0;  /* not found */
69      /* check that the match isn't a super string */
70      if ((p == start || p[-1] == ' ') && (p[len] == ' ' || p[len] == 0))
71         return 1;
72      /* skip the false match and continue */
73      extensions = p + len;
74   }
75
76   return 0 ;
77 }
78
79 #ifndef GL_INVALID_FRAMEBUFFER_OPERATION
80 #ifdef GL_INVALID_FRAMEBUFFER_OPERATION_EXT
81 #define GL_INVALID_FRAMEBUFFER_OPERATION GL_INVALID_FRAMEBUFFER_OPERATION_EXT
82 #else
83 #define GL_INVALID_FRAMEBUFFER_OPERATION 0x0506
84 #endif
85 #endif
86
87 #ifndef GL_TEXTURE_TOO_LARGE
88 #ifdef GL_TEXTURE_TOO_LARGE_EXT
89 #define GL_TEXTURE_TOO_LARGE GL_TEXTURE_TOO_LARGE_EXT
90 #else
91 #define GL_TEXTURE_TOO_LARGE 0x8065
92 #endif
93 #endif
94
95 /*
96  * A cut-down local version of gluErrorString to avoid depending on GLU.
97  */
98 static const char* fghErrorString( GLenum error )
99 {
100   switch ( error ) {
101   case GL_INVALID_ENUM: return "invalid enumerant";
102   case GL_INVALID_VALUE: return "invalid value";
103   case GL_INVALID_OPERATION: return "invalid operation";
104   case GL_STACK_OVERFLOW: return "stack overflow";
105   case GL_STACK_UNDERFLOW: return "stack underflow";
106   case GL_OUT_OF_MEMORY: return "out of memory";
107   case GL_TABLE_TOO_LARGE: return "table too large";
108   case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffer operation";
109   case GL_TEXTURE_TOO_LARGE: return "texture too large";
110   default: return "unknown GL error";
111   }
112 }
113
114 /*
115  * This function reports all the OpenGL errors that happened till now
116  */
117 void FGAPIENTRY glutReportErrors( void )
118 {
119     GLenum error;
120     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReportErrors" );
121     while( ( error = glGetError() ) != GL_NO_ERROR )
122         fgWarning( "GL error: %s", fghErrorString( error ) );
123 }
124
125 /*
126  * Control the auto-repeat of keystrokes to the current window
127  */
128 void FGAPIENTRY glutIgnoreKeyRepeat( int ignore )
129 {
130     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIgnoreKeyRepeat" );
131     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIgnoreKeyRepeat" );
132
133     fgStructure.CurrentWindow->State.IgnoreKeyRepeat = ignore ? GL_TRUE : GL_FALSE;
134 }
135
136 /*
137  * Set global auto-repeat of keystrokes
138  *
139  * RepeatMode should be either:
140  *    GLUT_KEY_REPEAT_OFF
141  *    GLUT_KEY_REPEAT_ON
142  *    GLUT_KEY_REPEAT_DEFAULT
143  */
144 void FGAPIENTRY glutSetKeyRepeat( int repeatMode )
145 {
146     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetKeyRepeat" );
147
148     switch( repeatMode )
149     {
150     case GLUT_KEY_REPEAT_OFF:
151     case GLUT_KEY_REPEAT_ON:
152      fgState.KeyRepeat = repeatMode;
153      break;
154
155     case GLUT_KEY_REPEAT_DEFAULT:
156      fgState.KeyRepeat = GLUT_KEY_REPEAT_ON;
157      break;
158
159     default:
160         fgError ("Invalid glutSetKeyRepeat mode: %d", repeatMode);
161         break;
162     }
163 }
164
165 /*
166  * Forces the joystick callback to be executed
167  */
168 void FGAPIENTRY glutForceJoystickFunc( void )
169 {
170     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutForceJoystickFunc" );
171 #if !defined(_WIN32_WCE)
172     freeglut_return_if_fail( fgStructure.CurrentWindow != NULL );
173     freeglut_return_if_fail( FETCH_WCB( *( fgStructure.CurrentWindow ), Joystick ) );
174     fgJoystickPollWindow( fgStructure.CurrentWindow );
175 #endif /* !defined(_WIN32_WCE) */
176 }
177
178 /*
179  *
180  */
181 void FGAPIENTRY glutSetColor( int nColor, GLfloat red, GLfloat green, GLfloat blue )
182 {
183     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetColor" );
184     /* We really need to do something here. */
185 }
186
187 /*
188  *
189  */
190 GLfloat FGAPIENTRY glutGetColor( int color, int component )
191 {
192     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetColor" );
193     /* We really need to do something here. */
194     return( 0.0f );
195 }
196
197 /*
198  *
199  */
200 void FGAPIENTRY glutCopyColormap( int window )
201 {
202     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCopyColormap" );
203     /* We really need to do something here. */
204 }
205
206 /*** END OF FILE ***/