subwin.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*! \file subwin.c
  2. \ingroup demos
  3. This program is a test harness for the subwindows
  4. in OpenGLUT. Based Originally on shape.c demo.
  5. \author Written by Evan Felix February 2011
  6. \author Portions Copyright (C) 2004, the OpenGLUT project contributors. <br>
  7. OpenGLUT branched from freeglut in February, 2004.
  8. \image html openglut_subwin.png OpenGLUT Sub Window Demonstration
  9. \include demos/subwin/subwin.c
  10. */
  11. #include <GL/freeglut.h>
  12. #include <stdarg.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #ifdef _MSC_VER
  17. /* DUMP MEMORY LEAKS */
  18. #include <crtdbg.h>
  19. #endif
  20. #define MAXSTR 16
  21. char **strings;
  22. int mainwin;
  23. /*!
  24. Does printf()-like work using freeglut/OpenGLUT
  25. glutBitmapString(). Uses a fixed font. Prints
  26. at the indicated row/column position.
  27. Limitation: Cannot address pixels.
  28. Limitation: Renders in screen coords, not model coords.
  29. */
  30. static void shapesPrintf (int row, int col, const char *fmt, ...)
  31. {
  32. static char buf[256];
  33. int viewport[4];
  34. void *font = GLUT_BITMAP_9_BY_15;
  35. va_list args;
  36. va_start(args, fmt);
  37. #if defined(WIN32) && !defined(__CYGWIN__)
  38. (void) _vsnprintf (buf, sizeof(buf), fmt, args);
  39. #else
  40. (void) vsnprintf (buf, sizeof(buf), fmt, args);
  41. #endif
  42. va_end(args);
  43. glGetIntegerv(GL_VIEWPORT,viewport);
  44. glPushMatrix();
  45. glLoadIdentity();
  46. glMatrixMode(GL_PROJECTION);
  47. glPushMatrix();
  48. glLoadIdentity();
  49. glOrtho(0,viewport[2],0,viewport[3],-1,1);
  50. glRasterPos2i
  51. (
  52. glutBitmapWidth(font, ' ') * col,
  53. - glutBitmapHeight(font) * (row+2) + viewport[3]
  54. );
  55. glutBitmapString (font, (unsigned char*)buf);
  56. glPopMatrix();
  57. glMatrixMode(GL_MODELVIEW);
  58. glPopMatrix();
  59. }
  60. /* GLUT callback Handlers */
  61. static void
  62. resize(int width, int height)
  63. {
  64. glViewport(0, 0, width, height);
  65. glMatrixMode(GL_PROJECTION);
  66. glLoadIdentity();
  67. /*gluOrtho2D(0, width, 0, height);*/
  68. glMatrixMode(GL_MODELVIEW);
  69. glLoadIdentity() ;
  70. }
  71. static void display(void)
  72. {
  73. int win = glutGetWindow();
  74. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  75. glColor3d(1,0,0);
  76. glDisable(GL_LIGHTING);
  77. glColor3d(0.1,0.1,0.4);
  78. if (win == mainwin)
  79. {
  80. shapesPrintf (2, 3, "Move The mouse into different windows");
  81. shapesPrintf (3, 3, "pressing keys will add to the string");
  82. shapesPrintf (5, 3, "Window: %d", win);
  83. shapesPrintf (6, 3, "String: %s", strings[win]);
  84. }
  85. else
  86. {
  87. shapesPrintf (1, 3, "Window: %d", win);
  88. shapesPrintf (2, 3, "String: %s", strings[win]);
  89. }
  90. glutSwapBuffers();
  91. }
  92. static void
  93. key(unsigned char key, int x, int y)
  94. {
  95. char *s,str[2];
  96. int win = glutGetWindow();
  97. switch (key)
  98. {
  99. case 27 :
  100. case 'Q':
  101. case 'q': glutLeaveMainLoop () ; break;
  102. default:
  103. s=strings[win];
  104. if (strlen(s)+1>MAXSTR) {
  105. s[0]=0;
  106. }
  107. str[0]=key;
  108. str[1]=0;
  109. strcat(s,str);
  110. break;
  111. }
  112. glutPostRedisplay();
  113. }
  114. static void special (int key, int x, int y)
  115. {
  116. switch (key)
  117. {
  118. default:
  119. break;
  120. }
  121. glutPostRedisplay();
  122. }
  123. static void
  124. entry(int state)
  125. {
  126. int win = glutGetWindow();
  127. printf("Win: %d, state: %d\n",win,state);
  128. }
  129. /* Program entry point */
  130. int
  131. main(int argc, char *argv[])
  132. {
  133. int winmax,sw1,sw2,sw2sw,i;
  134. glutInitWindowSize(640,480);
  135. glutInitWindowPosition(40,40);
  136. glutInit(&argc, argv);
  137. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  138. glutCreateWindow("FreeGLUT Sub Windows");
  139. glutReshapeFunc(resize);
  140. glutDisplayFunc(display);
  141. glutKeyboardFunc(key);
  142. glutSpecialFunc(special);
  143. glutEntryFunc(entry);
  144. glutSetOption ( GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION ) ;
  145. glClearColor(1,1,1,1);
  146. mainwin = glutGetWindow();
  147. winmax=mainwin;
  148. sw1=glutCreateSubWindow(mainwin,4,240,314,236);
  149. glutReshapeFunc(resize);
  150. glutDisplayFunc(display);
  151. glutKeyboardFunc(key);
  152. glutSpecialFunc(special);
  153. glutEntryFunc(entry);
  154. glClearColor(0.7f,0.7f,0.7f,1);
  155. winmax = sw1 > winmax ? sw1 : winmax;
  156. sw2=glutCreateSubWindow(mainwin,322,240,314,236);
  157. glutReshapeFunc(resize);
  158. glutDisplayFunc(display);
  159. glutKeyboardFunc(key);
  160. glutSpecialFunc(special);
  161. glutEntryFunc(entry);
  162. glClearColor(0.7f,0.7f,0.7f,1);
  163. winmax = sw2 > winmax ? sw2 : winmax;
  164. sw2sw=glutCreateSubWindow(sw2,10,128,294,98);
  165. glutReshapeFunc(resize);
  166. glutDisplayFunc(display);
  167. glutKeyboardFunc(key);
  168. glutSpecialFunc(special);
  169. glutEntryFunc(entry);
  170. glClearColor(0.4f,0.4f,0.4f,1);
  171. winmax = sw2sw > winmax ? sw2sw : winmax;
  172. strings = malloc(sizeof(char *)*(winmax+1));
  173. for (i=0;i<winmax+1;i++) {
  174. strings[i] = malloc(sizeof(char)*MAXSTR+1);
  175. strings[i][0]=0;
  176. }
  177. glutMainLoop();
  178. #ifdef _MSC_VER
  179. /* DUMP MEMORY LEAK INFORMATION */
  180. _CrtDumpMemoryLeaks () ;
  181. #endif
  182. return EXIT_SUCCESS;
  183. }