Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xfont.c 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Mesa 3-D graphics library
  3. *
  4. * Copyright (C) 1999 Brian Paul All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * BRIAN PAUL BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  20. * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. /*
  24. * Example of using glXUseXFont().
  25. * 5 November 1999
  26. * Brian Paul
  27. */
  28. #include <GL/gl.h>
  29. #include <GL/glx.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. static const char *ProgramName = "xfont";
  34. static const char *FontName = "fixed";
  35. static GLuint FontBase = 0;
  36. static void redraw( Display *dpy, Window w )
  37. {
  38. static const char *text = "This is glXUseXFont()";
  39. glClear( GL_COLOR_BUFFER_BIT );
  40. /* triangle */
  41. glColor3f( 0.2, 0.2, 1.0 );
  42. glBegin(GL_TRIANGLES);
  43. glVertex2f( 0, 0.8 );
  44. glVertex2f( -0.8, -0.7 );
  45. glVertex2f( 0.8, -0.7 );
  46. glEnd();
  47. /* text */
  48. glColor3f( 1, 1, 1 );
  49. glRasterPos2f(-0.8, 0);
  50. glListBase(FontBase);
  51. glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text);
  52. glXSwapBuffers( dpy, w );
  53. }
  54. static void resize( unsigned int width, unsigned int height )
  55. {
  56. glViewport( 0, 0, width, height );
  57. glMatrixMode( GL_PROJECTION );
  58. glLoadIdentity();
  59. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  60. }
  61. static void setup_font( Display *dpy )
  62. {
  63. XFontStruct *fontInfo;
  64. Font id;
  65. unsigned int first, last;
  66. fontInfo = XLoadQueryFont(dpy, FontName);
  67. if (!fontInfo) {
  68. printf("Error: font %s not found\n", FontName);
  69. exit(0);
  70. }
  71. id = fontInfo->fid;
  72. first = fontInfo->min_char_or_byte2;
  73. last = fontInfo->max_char_or_byte2;
  74. FontBase = glGenLists((GLuint) last + 1);
  75. if (!FontBase) {
  76. printf("Error: unable to allocate display lists\n");
  77. exit(0);
  78. }
  79. glXUseXFont(id, first, last - first + 1, FontBase + first);
  80. }
  81. static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
  82. unsigned int width, unsigned int height )
  83. {
  84. int attrib[] = { GLX_RGBA,
  85. GLX_RED_SIZE, 1,
  86. GLX_GREEN_SIZE, 1,
  87. GLX_BLUE_SIZE, 1,
  88. GLX_DOUBLEBUFFER,
  89. None };
  90. int scrnum;
  91. XSetWindowAttributes attr;
  92. unsigned long mask;
  93. Window root;
  94. Window win;
  95. GLXContext ctx;
  96. XVisualInfo *visinfo;
  97. scrnum = DefaultScreen( dpy );
  98. root = RootWindow( dpy, scrnum );
  99. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  100. if (!visinfo) {
  101. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  102. exit(1);
  103. }
  104. /* window attributes */
  105. attr.background_pixel = 0;
  106. attr.border_pixel = 0;
  107. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  108. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  109. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  110. win = XCreateWindow( dpy, root, 0, 0, width, height,
  111. 0, visinfo->depth, InputOutput,
  112. visinfo->visual, mask, &attr );
  113. {
  114. XSizeHints sizehints;
  115. sizehints.x = xpos;
  116. sizehints.y = ypos;
  117. sizehints.width = width;
  118. sizehints.height = height;
  119. sizehints.flags = USSize | USPosition;
  120. XSetNormalHints(dpy, win, &sizehints);
  121. XSetStandardProperties(dpy, win, ProgramName, ProgramName,
  122. None, (char **)NULL, 0, &sizehints);
  123. }
  124. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  125. glXMakeCurrent( dpy, win, ctx );
  126. return win;
  127. }
  128. static void event_loop( Display *dpy )
  129. {
  130. XEvent event;
  131. while (1) {
  132. XNextEvent( dpy, &event );
  133. switch (event.type) {
  134. case Expose:
  135. redraw( dpy, event.xany.window );
  136. break;
  137. case ConfigureNotify:
  138. resize( event.xconfigure.width, event.xconfigure.height );
  139. break;
  140. case KeyPress:
  141. exit(0);
  142. default:
  143. ; /* no-op */
  144. }
  145. }
  146. }
  147. int main( int argc, char *argv[] )
  148. {
  149. Display *dpy;
  150. Window win;
  151. dpy = XOpenDisplay(NULL);
  152. win = make_rgb_db_window( dpy, 0, 0, 300, 300 );
  153. setup_font( dpy );
  154. glShadeModel( GL_FLAT );
  155. glClearColor( 0.5, 0.5, 1.0, 1.0 );
  156. XMapWindow( dpy, win );
  157. event_loop( dpy );
  158. return 0;
  159. }