http://www.jwz.org/xscreensaver/xscreensaver-5.09.tar.gz
[xscreensaver] / hacks / glx / fps-gl.c
1 /* fps, Copyright (c) 2001-2009 Jamie Zawinski <jwz@jwz.org>
2  * Draw a frames-per-second display (Xlib and OpenGL).
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that
7  * copyright notice and this permission notice appear in supporting
8  * documentation.  No representations are made about the suitability of this
9  * software for any purpose.  It is provided "as is" without express or 
10  * implied warranty.
11  */
12
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif /* HAVE_CONFIG_H */
16
17 #ifdef HAVE_COCOA
18 # include "jwxyz.h"
19 # include <OpenGL/gl.h>
20 # include <OpenGL/glu.h>
21 # include <AGL/agl.h>
22 #else /* !HAVE_COCOA -- real Xlib */
23 # include <GL/glx.h>
24 # include <GL/glu.h>
25 #endif /* !HAVE_COCOA */
26
27 #include "xlockmoreI.h"
28 #include "fpsI.h"
29 #include "glxfonts.h"
30
31 /* These are in xlock-gl.c */
32 extern void clear_gl_error (void);
33 extern void check_gl_error (const char *type);
34
35
36 static void
37 xlockmore_gl_fps_init (fps_state *st)
38 {
39   XFontStruct *f = st->font;
40   int first = f->min_char_or_byte2;
41   int last  = f->max_char_or_byte2;
42
43   clear_gl_error ();
44   st->font_dlist = glGenLists ((GLuint) last+1);
45   check_gl_error ("glGenLists");
46
47   xscreensaver_glXUseXFont (st->dpy, f->fid,
48                             first, last-first+1, st->font_dlist + first);
49   check_gl_error ("xscreensaver_glXUseXFont");
50 }
51
52
53
54 /* Callback in xscreensaver_function_table, via xlockmore.c.
55  */
56 void
57 xlockmore_gl_compute_fps (Display *dpy, Window w, fps_state *fpst, 
58                           void *closure)
59 {
60   ModeInfo *mi = (ModeInfo *) closure;
61   if (! mi->fpst)
62     {
63       mi->fpst = fpst;
64       xlockmore_gl_fps_init (fpst);
65     }
66
67   fps_compute (fpst, mi->polygon_count);
68 }
69
70
71 /* Called directly from GL programs (as `do_fps') before swapping buffers.
72  */
73 void
74 xlockmore_gl_draw_fps (ModeInfo *mi)
75 {
76   fps_state *st = mi->fpst;
77   if (st)   /* might be too early */
78     {
79       XWindowAttributes xgwa;
80       int lines = 1;
81       const char *s;
82       int lh = st->font->ascent + st->font->descent;
83       int y = st->y;
84
85       XGetWindowAttributes (st->dpy, st->window, &xgwa);
86       for (s = st->string; *s; s++) 
87         if (*s == '\n') lines++;
88
89       if (y < 0)
90         y = xgwa.height + y - lines*lh;
91       y += lines*lh + st->font->descent;
92
93       glColor3f (1, 1, 1);
94       print_gl_string (st->dpy, st->font, st->font_dlist, 
95                        xgwa.width, xgwa.height,
96                        st->x, y, st->string, st->clear_p);
97     }
98 }