Adding initialization checking to all GLUT interface functions and removing asserts...
[freeglut] / src / 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 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include <GL/freeglut.h>
33 #include "freeglut_internal.h"
34
35 /*
36  * TODO BEFORE THE STABLE RELEASE:
37  *
38  *  glutSetColor()     --
39  *  glutGetColor()     --
40  *  glutCopyColormap() --
41  *  glutSetKeyRepeat() -- this is evil and should be removed from API
42  */
43
44 /* -- INTERFACE FUNCTIONS -------------------------------------------------- */
45
46 /*
47  * This functions checks if an OpenGL extension is supported or not
48  *
49  * XXX Wouldn't this be simpler and clearer if we used strtok()?
50  */
51 int FGAPIENTRY glutExtensionSupported( const char* extension )
52 {
53   const char *extensions, *start;
54   const int len = strlen( extension );
55
56   /* Make sure there is a current window, and thus a current context available */
57   FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutExtensionSupported" );
58   freeglut_return_val_if_fail( fgStructure.Window != NULL, 0 );
59
60   if (strchr(extension, ' '))
61     return 0;
62   start = extensions = (const char *) glGetString(GL_EXTENSIONS);
63
64   /* XXX consider printing a warning to stderr that there's no current
65    * rendering context.
66    */
67   freeglut_return_val_if_fail( extensions != NULL, 0 );
68
69   while (1) {
70      const char *p = strstr(extensions, extension);
71      if (!p)
72         return 0;  /* not found */
73      /* check that the match isn't a super string */
74      if ((p == start || p[-1] == ' ') && (p[len] == ' ' || p[len] == 0))
75         return 1;
76      /* skip the false match and continue */
77      extensions = p + len;
78   }
79
80   return 0 ;
81 }
82
83 /*
84  * This function reports all the OpenGL errors that happened till now
85  */
86 void FGAPIENTRY glutReportErrors( void )
87 {
88     GLenum error;
89     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutReportErrors" );
90     while( ( error = glGetError() ) != GL_NO_ERROR )
91         fgWarning( "GL error: %s", gluErrorString( error ) );
92 }
93
94 /*
95  * Control the auto-repeat of keystrokes to the current window
96  */
97 void FGAPIENTRY glutIgnoreKeyRepeat( int ignore )
98 {
99     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutIgnoreKeyRepeat" );
100     FREEGLUT_EXIT_IF_NO_WINDOW ( "glutIgnoreKeyRepeat" );
101
102     fgStructure.Window->State.IgnoreKeyRepeat = ignore ? GL_TRUE : GL_FALSE;
103 }
104
105 /*
106  * Set global auto-repeat of keystrokes
107  *
108  * RepeatMode should be either:
109  *    GLUT_KEY_REPEAT_OFF
110  *    GLUT_KEY_REPEAT_ON
111  *    GLUT_KEY_REPEAT_DEFAULT
112  */
113 void FGAPIENTRY glutSetKeyRepeat( int repeatMode )
114 {
115     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetKeyRepeat" );
116
117     switch( repeatMode )
118     {
119     case GLUT_KEY_REPEAT_OFF:
120     case GLUT_KEY_REPEAT_ON:
121      fgState.KeyRepeat = repeatMode;
122      break;
123
124     case GLUT_KEY_REPEAT_DEFAULT:
125      fgState.KeyRepeat = GLUT_KEY_REPEAT_ON;
126      break;
127
128     default:
129         fgError ("Invalid glutSetKeyRepeat mode: %d", repeatMode);
130         break;
131     }
132 }
133
134 /*
135  * Forces the joystick callback to be executed
136  */
137 void FGAPIENTRY glutForceJoystickFunc( void )
138 {
139     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutForceJoystickFunc" );
140 #if !TARGET_HOST_WINCE
141     freeglut_return_if_fail( fgStructure.Window != NULL );
142     freeglut_return_if_fail( FETCH_WCB( *( fgStructure.Window ), Joystick ) );
143     fgJoystickPollWindow( fgStructure.Window );
144 #endif /* !TARGET_HOST_WINCE */
145 }
146
147 /*
148  *
149  */
150 void FGAPIENTRY glutSetColor( int nColor, GLfloat red, GLfloat green, GLfloat blue )
151 {
152     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutSetColor" );
153     /* We really need to do something here. */
154 }
155
156 /*
157  *
158  */
159 GLfloat FGAPIENTRY glutGetColor( int color, int component )
160 {
161     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutGetColor" );
162     /* We really need to do something here. */
163     return( 0.0f );
164 }
165
166 /*
167  *
168  */
169 void FGAPIENTRY glutCopyColormap( int window )
170 {
171     FREEGLUT_EXIT_IF_NOT_INITIALISED ( "glutCopyColormap" );
172     /* We really need to do something here. */
173 }
174
175 /*** END OF FILE ***/