From http://www.jwz.org/xscreensaver/xscreensaver-5.30.tar.gz
[xscreensaver] / hacks / glx / fps-gl.c
1 /* fps, Copyright (c) 2001-2014 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 #else /* !HAVE_COCOA -- real Xlib */
20 # include <GL/glx.h>
21 # include <GL/glu.h>
22 #endif /* !HAVE_COCOA */
23
24 #ifdef HAVE_JWZGLES
25 # include "jwzgles.h"
26 #endif /* HAVE_JWZGLES */
27
28 #include "xlockmoreI.h"
29 #include "fpsI.h"
30 #include "glxfonts.h"
31
32 /* These are in xlock-gl.c */
33 extern void clear_gl_error (void);
34 extern void check_gl_error (const char *type);
35
36
37 static void
38 xlockmore_gl_fps_init (fps_state *st)
39 {
40   st->gl_fps_data = load_texture_font (st->dpy, "fpsFont");
41 }
42
43
44
45 /* Callback in xscreensaver_function_table, via xlockmore.c.
46  */
47 void
48 xlockmore_gl_compute_fps (Display *dpy, Window w, fps_state *fpst, 
49                           void *closure)
50 {
51   ModeInfo *mi = (ModeInfo *) closure;
52   if (! mi->fpst)
53     {
54       mi->fpst = fpst;
55       xlockmore_gl_fps_init (fpst);
56     }
57
58   fps_compute (fpst, mi->polygon_count, mi->recursion_depth);
59 }
60
61
62 /* Called directly from GL programs (as `do_fps') before swapping buffers.
63  */
64 void
65 xlockmore_gl_draw_fps (ModeInfo *mi)
66 {
67   fps_state *st = mi->fpst;
68   if (st)   /* might be too early */
69     {
70       XWindowAttributes xgwa;
71       int lines = 1;
72       const char *s;
73       int lh = st->font->ascent + st->font->descent;
74       int y = st->y;
75
76       XGetWindowAttributes (st->dpy, st->window, &xgwa);
77       for (s = st->string; *s; s++) 
78         if (*s == '\n') lines++;
79
80       if (y < 0)
81         y = xgwa.height + y - lines*lh;
82       y += lines*lh + st->font->descent;
83
84       glColor3f (1, 1, 1);
85       print_gl_string (st->dpy, st->gl_fps_data,
86                        xgwa.width, xgwa.height,
87                        st->x, y, st->string, st->clear_p);
88     }
89 }