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

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