nicely lining up the two windows for the callbackmaker demo, small other
[freeglut] / progs / demos / CallbackMaker / CallbackMaker.c
index 491c293..1b12e5f 100644 (file)
@@ -7,9 +7,12 @@
 #include <GL/freeglut.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdarg.h>
 
 static int sequence_number = 0 ;
 
+int windows[2] = {0};
+
 int reshape_called = 0, key_called = 0, special_called = 0, visibility_called = 0,
     keyup_called = 0, specialup_called = 0, joystick_called = 0, mouse_called = 0,
     mousewheel_called = 0, motion_called = 0, passivemotion_called = 0, entry_called = 0,
@@ -28,10 +31,29 @@ 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 motion_x = -1, motion_y = -1, motion_seq = -1 ;
 int passivemotion_x = -1, passivemotion_y = -1, passivemotion_seq = -1 ;
+
+#define STRING_LENGTH   10
+
+static void
+bitmapPrintf (const char *fmt, ...)
+{
+    static char buf[256];
+    va_list args;
+
+    va_start(args, fmt);
+#if defined(WIN32) && !defined(__CYGWIN__)
+    (void) _vsnprintf (buf, sizeof(buf), fmt, args);
+#else
+    (void) vsnprintf (buf, sizeof(buf), fmt, args);
+#endif
+    va_end(args);
+    glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)buf ) ;
+}
+
+
 static void 
 Display(void)
 {
-  char line[180] ;
   int window = glutGetWindow () ;
   glClear ( GL_COLOR_BUFFER_BIT );
 
@@ -45,72 +67,61 @@ Display(void)
   glPushMatrix ();
   glLoadIdentity ();
   glColor3ub ( 0, 0, 0 );
-  glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 10 );
+  glRasterPos2i ( 10, glutGet ( GLUT_WINDOW_HEIGHT ) - 20 );   /* 10pt margin above 10pt letters */
 
   if ( reshape_called )
   {
-    sprintf ( line, "Reshape %d:  %d %d\n", reshape_seq, reshape_width, reshape_height );
-    glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
+    bitmapPrintf ( "Reshape %d:  %d %d\n", reshape_seq, reshape_width, reshape_height );
   }
 
   if ( key_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Key %d:  %d(%c) %d %d\n", key_seq, key_key, key_key, key_x, key_y );
   }
 
   if ( special_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Special %d:  %d(%c) %d %d\n", special_seq, special_key, special_key, special_x, special_y );
   }
 
   if ( visibility_called )
   {
-    sprintf ( line, "Visibility %d:  %d\n", visibility_seq, visibility_vis );
-    glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
+    bitmapPrintf ( "Visibility %d:  %d\n", visibility_seq, visibility_vis );
   }
 
   if ( keyup_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Key Up %d:  %d(%c) %d %d\n", keyup_seq, keyup_key, keyup_key, keyup_x, keyup_y );
   }
 
   if ( specialup_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Special Up %d:  %d(%c) %d %d\n", specialup_seq, specialup_key, specialup_key, specialup_x, specialup_y );
   }
 
   if ( joystick_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Joystick %d:  %d %d %d %d\n", joystick_seq, joystick_a, joystick_b, joystick_c, joystick_d );
   }
 
   if ( mouse_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Mouse %d:  %d %d %d %d\n", mouse_seq, mouse_button, mouse_updown, mouse_x, mouse_y );
   }
 
   if ( mousewheel_called )
   {
-    sprintf ( line, "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 );
+    bitmapPrintf ( "Mouse Wheel %d:  %d %d %d %d\n", mousewheel_seq, mousewheel_number, mousewheel_direction, mousewheel_x, mousewheel_y );
   }
 
   if ( motion_called )
   {
-    sprintf ( line, "Motion %d:  %d %d\n", motion_seq, motion_x, motion_y );
-    glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
+    bitmapPrintf ( "Motion %d:  %d %d\n", motion_seq, motion_x, motion_y );
   }
 
   if ( passivemotion_called )
   {
-    sprintf ( line, "Passive Motion %d:  %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
-    glutBitmapString ( GLUT_BITMAP_HELVETICA_12, (unsigned char*)line );
+    bitmapPrintf ( "Passive Motion %d:  %d %d\n", passivemotion_seq, passivemotion_x, passivemotion_y );
   }
 
   glMatrixMode ( GL_PROJECTION );
@@ -124,6 +135,34 @@ Display(void)
   glutSwapBuffers();
 }
 
+static void
+Warning(const char *fmt, va_list ap)
+{
+    printf("%6d Warning callback:\n");
+
+    /* print warning message */
+    vprintf(fmt, ap);
+}
+
+static void
+Error(const char *fmt, va_list ap)
+{
+    char dummy_string[STRING_LENGTH];
+    printf("%6d Error callback:\n");
+
+    /* print warning message */
+    vprintf(fmt, ap);
+
+    /* terminate program, after pause for input so user can see */
+    printf ( "Please enter something to exit: " );
+    fgets ( dummy_string, STRING_LENGTH, stdin );
+
+    /* Call exit directly as freeglut is messed
+     * up internally when an error is called. 
+     */
+    exit(1);
+}
+
 static void 
 Reshape(int width, int height)
 {
@@ -134,6 +173,7 @@ Reshape(int width, int height)
   reshape_width = width ;
   reshape_height = height ;
   reshape_seq = sequence_number ;
+  glViewport(0,0,width,height);
   glutPostRedisplay () ;
 }
 
@@ -400,7 +440,9 @@ MenuDestroy( void )
   menudestroy_called = 1 ;
   printf ( "%6d Window %d MenuDestroy Callback\n",
             ++sequence_number, window ) ;
-  glutPostRedisplay () ;
+
+  if (window)   /* When destroyed when shutting down, not always a window defined... */
+    glutPostRedisplay () ;
 }
 
 static void 
@@ -421,19 +463,20 @@ static void Idle ( void )
 int 
 main(int argc, char *argv[])
 {
-#define STRING_LENGTH   10
-  int freeglut_window, aux_window ;
   char dummy_string[STRING_LENGTH];
 
   int menuID, subMenuA, subMenuB;
 
+  glutInitWarningFunc(Warning);
+  glutInitErrorFunc(Error);
   glutInitWindowSize(500, 250);
   glutInitWindowPosition ( 140, 140 );
   glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE );
   glutInit(&argc, argv);
+  glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_CONTINUE_EXECUTION);
 
-  freeglut_window = glutCreateWindow( "Callback Demo" );
-  printf ( "Creating window %d as 'Callback Demo'\n", freeglut_window ) ;
+  windows[0] = glutCreateWindow( "Callback Demo" );
+  printf ( "Creating window %d as 'Callback Demo'\n", windows[0] ) ;
 
   glClearColor(1.0, 1.0, 1.0, 1.0);
 
@@ -460,7 +503,6 @@ main(int argc, char *argv[])
   glutDialsFunc ( Dials ) ;
   glutTabletMotionFunc ( TabletMotion ) ;
   glutTabletButtonFunc ( TabletButton ) ;
-  glutMenuDestroyFunc ( MenuDestroy );
   glutMenuStatusFunc ( MenuStatus );
   glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF) ;
 
@@ -468,12 +510,14 @@ main(int argc, char *argv[])
   glutAddMenuEntry( "Sub menu A1 (01)", 1 );
   glutAddMenuEntry( "Sub menu A2 (02)", 2 );
   glutAddMenuEntry( "Sub menu A3 (03)", 3 );
+  glutMenuDestroyFunc ( MenuDestroy );
 
   subMenuB = glutCreateMenu( MenuCallback );
   glutAddMenuEntry( "Sub menu B1 (04)", 4 );
   glutAddMenuEntry( "Sub menu B2 (05)", 5 );
   glutAddMenuEntry( "Sub menu B3 (06)", 6 );
   glutAddSubMenu( "Going to sub menu A", subMenuA );
+  glutMenuDestroyFunc ( MenuDestroy );
 
   menuID = glutCreateMenu( MenuCallback );
   glutAddMenuEntry( "Entry one",   1 );
@@ -483,11 +527,14 @@ main(int argc, char *argv[])
   glutAddMenuEntry( "Entry five",  5 );
   glutAddSubMenu( "Enter sub menu A", subMenuA );
   glutAddSubMenu( "Enter sub menu B", subMenuB );
+  glutMenuDestroyFunc ( MenuDestroy );
 
   glutAttachMenu( GLUT_LEFT_BUTTON );
 
-  aux_window = glutCreateWindow( "Second Window" );
-  printf ( "Creating window %d as 'Second Window'\n", aux_window ) ;
+  glutInitWindowPosition ( 140+500+2*glutGet(GLUT_WINDOW_BORDER_WIDTH), 140 );
+  /* Position second window right next to the first */
+  windows[1] = glutCreateWindow( "Second Window" );
+  printf ( "Creating window %d as 'Second Window'\n", windows[1] ) ;
 
   glClearColor(1.0, 1.0, 1.0, 1.0);