From http://www.jwz.org/xscreensaver/xscreensaver-5.32.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 #elif defined(HAVE_ANDROID)
20 # include <GLES/gl.h>
21 #else /* real Xlib */
22 # include <GL/glx.h>
23 # include <GL/glu.h>
24 #endif /* !HAVE_COCOA */
25
26 #ifdef HAVE_JWZGLES
27 # include "jwzgles.h"
28 #endif /* HAVE_JWZGLES */
29
30 #include "xlockmoreI.h"
31 #include "fpsI.h"
32 #include "texfont.h"
33
34 /* These are in xlock-gl.c */
35 extern void clear_gl_error (void);
36 extern void check_gl_error (const char *type);
37
38 typedef struct {
39   texture_font_data *texfont;
40   int line_height;
41   Bool top_p;
42 } gl_fps_data;
43
44
45 static void
46 xlockmore_gl_fps_init (fps_state *st)
47 {
48   gl_fps_data *data = (gl_fps_data *) calloc (1, sizeof(*data));
49   data->top_p = get_boolean_resource (st->dpy, "fpsTop", "FPSTop");
50   data->texfont = load_texture_font (st->dpy, "fpsFont");
51   texture_string_width (data->texfont, "M", &data->line_height);
52   st->gl_fps_data = data;
53 }
54
55
56 /* Callback in xscreensaver_function_table, via xlockmore.c.
57  */
58 void
59 xlockmore_gl_compute_fps (Display *dpy, Window w, fps_state *fpst, 
60                           void *closure)
61 {
62   ModeInfo *mi = (ModeInfo *) closure;
63   if (! mi->fpst)
64     {
65       mi->fpst = fpst;
66       xlockmore_gl_fps_init (fpst);
67     }
68
69   fps_compute (fpst, mi->polygon_count, mi->recursion_depth);
70 }
71
72
73 /* Called directly from GL programs (as `do_fps') before swapping buffers.
74  */
75 void
76 xlockmore_gl_draw_fps (ModeInfo *mi)
77 {
78   fps_state *st = mi->fpst;
79   if (st)   /* might be too early */
80     {
81       gl_fps_data *data = (gl_fps_data *) st->gl_fps_data;
82       XWindowAttributes xgwa;
83       int lines = 1;
84       const char *s;
85       int y = st->y;
86
87       XGetWindowAttributes (st->dpy, st->window, &xgwa);
88       for (s = st->string; *s; s++) 
89         if (*s == '\n') lines++;
90
91       if (y < 0)
92         y = xgwa.height + y - (lines * data->line_height);
93       y += lines * data->line_height;
94
95       glColor3f (1, 1, 1);
96       print_texture_label (st->dpy, data->texfont,
97                            xgwa.width, xgwa.height,
98                            (data->top_p ? 1 : 2),
99                            st->string);
100     }
101 }