Use vsnprintf instead of the potentially dangerous sprintf to avoid

warnings. Using snprintf directly would be a little bit more tricky,
because once again Microsoft decided to avoid followind standards and
provide _snprintf instead. We could use this, too, but this would
require an additional autoconf check, which I'd like to avoid, if
possible.

Note: If VS *still* issues warnings, but this time about vsnprintf,
somebody should add some pragmas or whatever is needed to shut up that
warning, it would be silly.


git-svn-id: https://svn.code.sf.net/p/freeglut/code/trunk@783 7f0cb862-5218-0410-a997-914c9d46530a
This commit is contained in:
spanne 2009-02-28 12:56:20 +00:00
parent 15aa199c84
commit 41e68f8f5d
3 changed files with 40 additions and 37 deletions

View File

@ -7,6 +7,7 @@
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
static int sequence_number = 0 ; static int sequence_number = 0 ;
@ -28,10 +29,23 @@ int mouse_button = -1, mouse_updown = -1, mouse_x = -1, mouse_y = -1, mouse_seq
int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ; int mousewheel_number = -1, mousewheel_direction = -1, mousewheel_x = -1, mousewheel_y = -1, mousewheel_seq = -1 ;
int motion_x = -1, motion_y = -1, motion_seq = -1 ; int motion_x = -1, motion_y = -1, motion_seq = -1 ;
int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ; int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
static void
bitmapPrintf (const char *fmt, ...)
{
static char buf[256];
va_list args;
va_start(args, fmt);
(void) vsnprintf (buf, sizeof(buf), fmt, args);
va_end(args);
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
}
static void static void
Display(void) Display(void)
{ {
char line[180] ;
int window = glutGetWindow () ; int window = glutGetWindow () ;
glClear ( GL_COLOR_BUFFER_BIT ); glClear ( GL_COLOR_BUFFER_BIT );
@ -49,68 +63,57 @@ Display(void)
if ( reshape_called ) if ( reshape_called )
{ {
sprintf ( line, "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height ); bitmapPrintf ( "Reshape %d: %d %d\n", reshape_seq, reshape_width, reshape_height );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( key_called ) if ( key_called )
{ {
sprintf ( line, "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y ); bitmapPrintf ( "Key %d: %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( special_called ) if ( special_called )
{ {
sprintf ( line, "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y ); bitmapPrintf ( "Special %d: %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( visibility_called ) if ( visibility_called )
{ {
sprintf ( line, "Visibility %d: %d\n", visibility_seq, visibility_vis ); bitmapPrintf ( "Visibility %d: %d\n", visibility_seq, visibility_vis );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( keyup_called ) if ( keyup_called )
{ {
sprintf ( line, "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y ); bitmapPrintf ( "Key Up %d: %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( specialup_called ) if ( specialup_called )
{ {
sprintf ( line, "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y ); bitmapPrintf ( "Special Up %d: %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( joystick_called ) if ( joystick_called )
{ {
sprintf ( line, "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d ); bitmapPrintf ( "Joystick %d: %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( mouse_called ) if ( mouse_called )
{ {
sprintf ( line, "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y ); bitmapPrintf ( "Mouse %d: %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( mousewheel_called ) if ( mousewheel_called )
{ {
sprintf ( line, "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y ); bitmapPrintf ( "Mouse Wheel %d: %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( motion_called ) if ( motion_called )
{ {
sprintf ( line, "Motion %d: %d %d\n", motion_seq, motion_x, motion_y ); bitmapPrintf ( "Motion %d: %d %d\n", motion_seq, motion_x, motion_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
if ( passivemotion_called ) if ( passivemotion_called )
{ {
sprintf ( line, "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y ); bitmapPrintf ( "Passive Motion %d: %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
} }
glMatrixMode ( GL_PROJECTION ); glMatrixMode ( GL_PROJECTION );

View File

@ -29,6 +29,7 @@
/* Include Files */ /* Include Files */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdarg.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
#include <GL/freeglut.h> #include <GL/freeglut.h>
@ -242,10 +243,19 @@ void draw_curve ( int index, double position [ NUM_POINTS ][3] )
glEnd () ; glEnd () ;
} }
void bitmapPrintf (const char *fmt, ...)
{
static char buf[256];
va_list args;
va_start(args, fmt);
(void) vsnprintf (buf, sizeof(buf), fmt, args);
va_end(args);
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
}
void display_cb ( void ) void display_cb ( void )
{ {
char string [ 80 ] ;
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ; glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ) ;
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */ glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
@ -267,9 +277,8 @@ void display_cb ( void )
/* Print the distance between the two points */ /* Print the distance between the two points */
glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */ glColor3d ( 1.0, 1.0, 1.0 ) ; /* White */
sprintf ( string, "Distance: %10.6f", distance ) ;
glRasterPos2i ( 1, 1 ) ; glRasterPos2i ( 1, 1 ) ;
glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)string ) ; bitmapPrintf ( "Distance: %10.6f", distance ) ;
glutSwapBuffers(); glutSwapBuffers();
} }

View File

@ -180,15 +180,6 @@ static const entry table [] =
Limitation: Cannot address pixels. Limitation: Cannot address pixels.
Limitation: Renders in screen coords, not model coords. Limitation: Renders in screen coords, not model coords.
\note Uses a fixed, 256-byte array for holding strings.
The best way around this would be to use vasprintf(),
but that is not available on WIN32, I believe.
Another alternative would be to write our own formatter
from scratch and emit the characters one at a time to
the GLUT bitmap single-character drawing routine.
We could also use vsnprintf(), but I'm not sure if
that is standard...
*/ */
static void shapesPrintf (int row, int col, const char *fmt, ...) static void shapesPrintf (int row, int col, const char *fmt, ...)
{ {
@ -198,7 +189,7 @@ static void shapesPrintf (int row, int col, const char *fmt, ...)
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
(void) vsprintf (buf, fmt, args); (void) vsnprintf (buf, sizeof(buf), fmt, args);
va_end(args); va_end(args);
glGetIntegerv(GL_VIEWPORT,viewport); glGetIntegerv(GL_VIEWPORT,viewport);