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.

xrotfontdemo.c 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 glXUseRotatedXFontMESA().
  25. * 24 Jan 2004
  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. #include "xuserotfont.h"
  34. static const char *ProgramName = "xfont";
  35. static const char *FontName = "fixed";
  36. static GLuint FontBase[4];
  37. static void redraw( Display *dpy, Window w )
  38. {
  39. static const char *text = " Rotated bitmap text";
  40. int i;
  41. glClear( GL_COLOR_BUFFER_BIT );
  42. /* triangle */
  43. glColor3f( 0.2, 0.2, 1.0 );
  44. glBegin(GL_TRIANGLES);
  45. glVertex2f( -0.8, 0.7 );
  46. glVertex2f( -0.8, -0.7 );
  47. glVertex2f( 0.8, 0.0 );
  48. glEnd();
  49. /* marker */
  50. glColor3f( 0, 1, 0 );
  51. glBegin(GL_POINTS);
  52. glVertex2f(0, 0);
  53. glEnd();
  54. /* text */
  55. glColor3f( 1, 1, 1 );
  56. for (i = 0; i < 4; i++) {
  57. glRasterPos2f(0, 0);
  58. glListBase(FontBase[i]);
  59. glCallLists(strlen(text), GL_UNSIGNED_BYTE, (GLubyte *) text);
  60. }
  61. glXSwapBuffers( dpy, w );
  62. }
  63. static void resize( unsigned int width, unsigned int height )
  64. {
  65. glViewport( 0, 0, width, height );
  66. glMatrixMode( GL_PROJECTION );
  67. glLoadIdentity();
  68. glOrtho( -1.0, 1.0, -1.0, 1.0, -1.0, 1.0 );
  69. }
  70. static void setup_font( Display *dpy )
  71. {
  72. XFontStruct *fontInfo;
  73. Font id;
  74. unsigned int first, last;
  75. int i;
  76. fontInfo = XLoadQueryFont(dpy, FontName);
  77. if (!fontInfo) {
  78. printf("Error: font %s not found\n", FontName);
  79. exit(0);
  80. }
  81. id = fontInfo->fid;
  82. first = fontInfo->min_char_or_byte2;
  83. last = fontInfo->max_char_or_byte2;
  84. for (i = 0; i < 4; i++) {
  85. FontBase[i] = glGenLists((GLuint) last + 1);
  86. if (!FontBase[i]) {
  87. printf("Error: unable to allocate display lists\n");
  88. exit(0);
  89. }
  90. glXUseRotatedXFontMESA(id, first, last - first + 1, FontBase[i] + first,
  91. i * 90);
  92. }
  93. }
  94. static Window make_rgb_db_window( Display *dpy, int xpos, int ypos,
  95. unsigned int width, unsigned int height )
  96. {
  97. int attrib[] = { GLX_RGBA,
  98. GLX_RED_SIZE, 1,
  99. GLX_GREEN_SIZE, 1,
  100. GLX_BLUE_SIZE, 1,
  101. GLX_DOUBLEBUFFER,
  102. None };
  103. int scrnum;
  104. XSetWindowAttributes attr;
  105. unsigned long mask;
  106. Window root;
  107. Window win;
  108. GLXContext ctx;
  109. XVisualInfo *visinfo;
  110. scrnum = DefaultScreen( dpy );
  111. root = RootWindow( dpy, scrnum );
  112. visinfo = glXChooseVisual( dpy, scrnum, attrib );
  113. if (!visinfo) {
  114. printf("Error: couldn't get an RGB, Double-buffered visual\n");
  115. exit(1);
  116. }
  117. /* window attributes */
  118. attr.background_pixel = 0;
  119. attr.border_pixel = 0;
  120. attr.colormap = XCreateColormap( dpy, root, visinfo->visual, AllocNone);
  121. attr.event_mask = StructureNotifyMask | ExposureMask | KeyPressMask;
  122. mask = CWBackPixel | CWBorderPixel | CWColormap | CWEventMask;
  123. win = XCreateWindow( dpy, root, 0, 0, width, height,
  124. 0, visinfo->depth, InputOutput,
  125. visinfo->visual, mask, &attr );
  126. {
  127. XSizeHints sizehints;
  128. sizehints.x = xpos;
  129. sizehints.y = ypos;
  130. sizehints.width = width;
  131. sizehints.height = height;
  132. sizehints.flags = USSize | USPosition;
  133. XSetNormalHints(dpy, win, &sizehints);
  134. XSetStandardProperties(dpy, win, ProgramName, ProgramName,
  135. None, (char **)NULL, 0, &sizehints);
  136. }
  137. ctx = glXCreateContext( dpy, visinfo, NULL, True );
  138. glXMakeCurrent( dpy, win, ctx );
  139. return win;
  140. }
  141. static void event_loop( Display *dpy )
  142. {
  143. XEvent event;
  144. while (1) {
  145. XNextEvent( dpy, &event );
  146. switch (event.type) {
  147. case Expose:
  148. redraw( dpy, event.xany.window );
  149. break;
  150. case ConfigureNotify:
  151. resize( event.xconfigure.width, event.xconfigure.height );
  152. break;
  153. case KeyPress:
  154. exit(0);
  155. default:
  156. ; /* no-op */
  157. }
  158. }
  159. }
  160. int main( int argc, char *argv[] )
  161. {
  162. Display *dpy;
  163. Window win;
  164. dpy = XOpenDisplay(NULL);
  165. win = make_rgb_db_window( dpy, 0, 0, 300, 300 );
  166. setup_font( dpy );
  167. glShadeModel( GL_FLAT );
  168. glClearColor( 0.5, 0.5, 1.0, 1.0 );
  169. XMapWindow( dpy, win );
  170. event_loop( dpy );
  171. return 0;
  172. }