c85e3500d810c83ad8348122e019cc8e448c9cde
[xscreensaver] / hacks / glx / b_lockglue.c
1 #if 0
2 static const char sccsid[] = "@(#)b_lockglue.c  4.11 98/06/16 xlockmore";
3 #endif
4
5 /*-
6  * BUBBLE3D (C) 1998 Richard W.M. Jones.
7  * b_lockglue.c: Glue to make this all work with xlockmore.
8  */
9
10 #include "bubble3d.h"
11
12 /* XXX This lot should eventually be made configurable using the
13  * options stuff below.
14  */
15 struct glb_config glb_config =
16 {
17 #if GLB_SLOW_GL
18         2,                      /* subdivision_depth */
19 #else
20         3,                      /* subdivision_depth */
21 #endif
22         5,                      /* nr_nudge_axes */
23         0.01,                   /* nudge_angle_factor */
24         0.20,                   /* nudge_factor */
25         0.1,                    /* rotation_factor */
26         8,                      /* create_bubbles_every */
27         8,                      /* max_bubbles */
28         {0.7, 0.8, 0.9, 1.0},   /* p_bubble_group */
29         0.5,                    /* max_size */
30         0.1,                    /* min_size */
31         0.03,                   /* max_speed */
32         0.005,                  /* min_speed */
33         1.5,                    /* scale_factor */
34         -4,                     /* screen_bottom */
35         4,                      /* screen_top */
36 #if 0
37         {0.1, 0.0, 0.4, 0.0},   /* bg_colour */
38 #else
39         {0.0, 0.0, 0.0, 0.0},   /* bg_colour */
40 #endif
41 #if 0
42         {0.7, 0.7, 0.0, 0.3}    /* bubble_colour */
43 #else
44         {0.0, 0.0, 0.7, 0.3}    /* bubble_colour */
45 #endif
46 };
47
48 #ifdef STANDALONE
49 #define PROGCLASS "Bubble3D"
50 #define HACK_INIT init_bubble3d
51 #define HACK_RESHAPE reshape_bubble3d
52 #define HACK_DRAW draw_bubble3d
53 #define bubble3d_opts xlockmore_opts
54
55 # define DEFAULTS       "*delay:        10000   \n"     \
56                         "*showFPS:      False   \n"
57
58 #include "xlockmore.h"
59 #else
60 #include "xlock.h"
61 #include "vis.h"
62 #endif
63
64 #ifdef USE_GL
65
66 ModeSpecOpt bubble3d_opts =
67 {0, NULL, 0, NULL, NULL};
68
69 #ifdef USE_MODULES
70 ModStruct   bubbles3d_description =
71 {"bubbles3d",
72  "init_bubble3d",
73  "draw_bubble3d",
74  "release_bubble3d",
75  "change_bubble3d",
76  "init_bubble3d",
77  NULL,
78  &bubble3d_opts,
79  1000, 1, 2, 1, 64, 1.0, "",
80  "Richard Jones's GL bubbles",
81  0,
82  NULL
83 };
84
85 #endif /* USE_MODULES */
86
87 struct context {
88         GLXContext *glx_context;
89         void       *draw_context;
90 };
91
92 static struct context *contexts = 0;
93
94 static void
95 init(struct context *c)
96 {
97         glb_sphere_init();
98         c->draw_context = glb_draw_init();
99 }
100
101 void
102 reshape_bubble3d(ModeInfo *mi, int w, int h)
103 {
104         glViewport(0, 0, (GLsizei) w, (GLsizei) h);
105         glMatrixMode(GL_PROJECTION);
106         glLoadIdentity();
107         gluPerspective(45, (GLdouble) w / (GLdouble) h, 3, 8);
108         glMatrixMode(GL_MODELVIEW);
109         glLoadIdentity();
110         glTranslatef(0, 0, -5);
111 }
112
113 static void
114 do_display(struct context *c)
115 {
116         glb_draw_step(c->draw_context);
117 }
118
119 void
120 init_bubble3d(ModeInfo * mi)
121 {
122         Display    *display = MI_DISPLAY(mi);
123         Window      window = MI_WINDOW(mi);
124         int         screen = MI_SCREEN(mi);
125         struct context *c;
126
127         if (contexts == 0) {
128                 contexts = (struct context *) malloc(sizeof (struct context) * MI_NUM_SCREENS(mi));
129
130                 if (contexts == 0)
131                         return;
132         }
133         c = &contexts[screen];
134         c->glx_context = init_GL(mi);
135         if (c->glx_context != 0) {
136                 init(c);
137                 reshape_bubble3d(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
138                 do_display(c);
139                 glFinish();
140                 glXSwapBuffers(display, window);
141         } else
142                 MI_CLEARWINDOW(mi);
143 }
144
145 void
146 draw_bubble3d(ModeInfo * mi)
147 {
148         struct context *c = &contexts[MI_SCREEN(mi)];
149         Display    *display = MI_DISPLAY(mi);
150         Window      window = MI_WINDOW(mi);
151
152         MI_IS_DRAWN(mi) = True;
153
154         if (!c->glx_context)
155                 return;
156
157         glXMakeCurrent(display, window, *(c->glx_context));
158
159         do_display(c);
160
161         if (mi->fps_p) do_fps (mi);
162         glFinish();
163         glXSwapBuffers(display, window);
164 }
165
166 void
167 change_bubble3d(ModeInfo * mi)
168 {
169         /* nothing */
170 }
171
172 void
173 release_bubble3d(ModeInfo * mi)
174 {
175         struct context *c = &contexts[MI_SCREEN(mi)];
176
177         if (contexts != 0) {
178                 glb_draw_end(c->draw_context);
179                 (void) free((void *) contexts);
180                 contexts = 0;
181         }
182         FreeAllGL(mi);
183 }
184
185 #endif /* USE_GL */