From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / glx / tangram.c
index a5aa8a9d3bba1656b5d32366338b77180f1243f9..82a1f4102e18ba143ad50942d39cff97bdde7000 100644 (file)
@@ -1,4 +1,4 @@
-/* tangram, Copyright (c) 2005 Jeremy English <jhe@jeremyenglish.org>
+/* tangram, Copyright (c) 2005-2014 Jeremy English <jhe@jeremyenglish.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software
  * and its documentation for any purpose is hereby granted without
  */
 
 
-#include <X11/Intrinsic.h>
-
-extern XtAppContext app;
-
-#define PROGCLASS        "Tangram"
-#define HACK_INIT        init_tangram
-#define HACK_DRAW        draw_tangram
-#define HACK_RESHAPE  reshape_tangram
-#define sws_opts         xlockmore_opts
-
-
 #define DEFAULTS       "*delay:        10000            \n" \
-  "*wireframe: False            \n" \
+                       "*wireframe:    False         \n" \
+       "*titleFont:  -*-helvetica-medium-r-normal-*-*-180-*-*-*-*-*-*\n" \
+       "*titleFont2: -*-helvetica-medium-r-normal-*-*-120-*-*-*-*-*-*\n" \
+       "*titleFont3: -*-helvetica-medium-r-normal-*-*-80-*-*-*-*-*-*\n"  \
 
+# define refresh_tangram 0
+# define release_tangram 0
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
@@ -40,12 +34,10 @@ extern XtAppContext app;
 
 #ifdef USE_GL                   /* whole file */
 
-
-#include <GL/glu.h>
 #include <time.h>
 #include <math.h>
 #include "tangram_shapes.h"
-#include "glxfonts.h"
+#include "texfont.h"
 
 typedef struct {
     GLubyte r;
@@ -105,49 +97,55 @@ typedef struct {
     XColor *colors;
     int ccolor;
 
-    XFontStruct *xfont1;
-    XFontStruct *xfont2;
-    XFontStruct *xfont3;
-    GLuint font1_dlist, font2_dlist, font3_dlist;
-    GLuint name_list;
+    texture_font_data *font1_data, *font2_data, *font3_data;
+    GLfloat theta[3];
+    Bool going_down[3];
+
+    const char *pn;
+    int display_counter;
+
 } tangram_configuration;
 
 static tangram_configuration *tps = NULL;
 
 #define DEF_VIEWING_TIME "5"
-#define DEF_ROTATE_CAMERA "True"
+#define DEF_ROTATE "True"
 #define DEF_X_CAMERA_ROTATE "0.2"
 #define DEF_Y_CAMERA_ROTATE "0.5"
 #define DEF_Z_CAMERA_ROTATE "0"
-
-
-static GLuint viewing_time = 0;
-static Bool do_rotate = True;
-static GLfloat x_camera_rotate = 0.3;
-static GLfloat y_camera_rotate = 0.75;
-static GLfloat z_camera_rotate = 0;
+#define DEF_LABELS "True"
+
+static GLuint viewing_time;
+static Bool do_rotate;
+static Bool do_labels;
+static GLfloat x_camera_rotate;
+static GLfloat y_camera_rotate;
+static GLfloat z_camera_rotate;
 static int wire;
 
 static XrmOptionDescRec opts[] = {
-    {"-viewing_time", ".viewing_time", XrmoptionSepArg, 0},
+    {"-viewing_time", ".viewingTime", XrmoptionSepArg, 0},
     {"-rotate", ".rotate", XrmoptionNoArg, "True"},
     {"+rotate", ".rotate", XrmoptionNoArg, "False"},
-    {"-x_camera_rotate", ".x_camera_rotate", XrmoptionSepArg, 0},
-    {"-y_camera_rotate", ".y_camera_rotate", XrmoptionSepArg, 0},
-    {"-z_camera_rotate", ".z_camera_rotate", XrmoptionSepArg, 0}
+    {"-labels", ".labels", XrmoptionNoArg, "True"},
+    {"+labels", ".labels", XrmoptionNoArg, "False"},
+    {"-x_camera_rotate", ".xCameraRotate", XrmoptionSepArg, 0},
+    {"-y_camera_rotate", ".yCameraRotate", XrmoptionSepArg, 0},
+    {"-z_camera_rotate", ".zCameraRotate", XrmoptionSepArg, 0}
 };
 
 static argtype vars[] = {
-    {&viewing_time, "viewing_time", "Viewing Time", DEF_VIEWING_TIME, t_Int},
-    {&do_rotate, "rotate", "Rotate", DEF_ROTATE_CAMERA, t_Bool},
-    {&x_camera_rotate, "x_camera_rotate", "X Camera Rotate", DEF_X_CAMERA_ROTATE, t_Float},
-    {&y_camera_rotate, "y_camera_rotate", "Y Camera Rotate", DEF_Y_CAMERA_ROTATE, t_Float},
-    {&z_camera_rotate, "z_camera_rotate", "Z Camera Rotate", DEF_Z_CAMERA_ROTATE, t_Float}
+    {&viewing_time, "viewingTime", "ViewingTime", DEF_VIEWING_TIME, t_Int},
+    {&do_rotate, "rotate", "Rotate", DEF_ROTATE, t_Bool},
+    {&do_labels, "labels", "Labels", DEF_LABELS, t_Bool},
+    {&x_camera_rotate, "xCameraRotate", "XCameraRotate", DEF_X_CAMERA_ROTATE, t_Float},
+    {&y_camera_rotate, "yCameraRotate", "YCameraRotate", DEF_Y_CAMERA_ROTATE, t_Float},
+    {&z_camera_rotate, "zCameraRotate", "ZCameraRotate", DEF_Z_CAMERA_ROTATE, t_Float}
 };
 
-ModeSpecOpt sws_opts = { countof(opts), opts, countof(vars), vars, NULL };
+ENTRYPOINT ModeSpecOpt tangram_opts = { countof(opts), opts, countof(vars), vars, NULL };
 
-static puzzle solved[] = {
+static const puzzle solved[] = {
   {"Teapot", {
       {{-1.664000, -1.552000, 0}, 135, 0, 0, INIT_DZ, -SPEED, 0, True},
       {{-1.696000, 0.944000, 0}, 315, 0, 0, INIT_DZ, -SPEED, 0,  False},
@@ -695,7 +693,7 @@ static void print_shape(char *s, tangram_shape sh)
 #endif
 \f
 
-void reset_shape(tangram_shape * ts)
+static void reset_shape(tangram_shape * ts)
 {
     GLfloat r = random() % 10;
     GLfloat f = r / 10;
@@ -704,7 +702,7 @@ void reset_shape(tangram_shape * ts)
     ts->ddz = -SPEED;
 }
 
-void bounce(tangram_shape * ts)
+static void bounce(tangram_shape * ts)
 {
     ts->crd.z *= -1;            /* ignore this */
     ts->dz += ts->ddz;
@@ -735,15 +733,24 @@ static void draw_tangram_shape(tangram_shape ts)
 static void load_fonts(ModeInfo * mi)
 {
     tangram_configuration *tp = &tps[MI_SCREEN(mi)];
-    load_font(mi->dpy, "titleFont", &tp->xfont1, &tp->font1_dlist);
-    load_font(mi->dpy, "titleFont2", &tp->xfont2, &tp->font2_dlist);
-    load_font(mi->dpy, "titleFont3", &tp->xfont3, &tp->font3_dlist);
+    tp->font1_data = load_texture_font (mi->dpy, "titleFont");
+    tp->font2_data = load_texture_font (mi->dpy, "titleFont2");
+    tp->font3_data = load_texture_font (mi->dpy, "titleFont3");
 }
 
 static void draw_shapes(ModeInfo * mi)
 {
     tangram_configuration *tp = &tps[MI_SCREEN(mi)];
 
+# ifdef HAVE_MOBILE    /* Keep it the same relative size when rotated. */
+    {
+      GLfloat h = MI_HEIGHT(mi) / (GLfloat) MI_WIDTH(mi);
+      int o = (int) current_device_rotation();
+      if (o != 0 && o != 180 && o != -180)
+        glScalef (h, 1/h, 1);
+    }
+# endif
+
     draw_tangram_shape(tp->tsm1);
 
     draw_tangram_shape(tp->tsm2);
@@ -752,33 +759,31 @@ static void draw_shapes(ModeInfo * mi)
     draw_tangram_shape(tp->tlg2);
     draw_tangram_shape(tp->sq);
     draw_tangram_shape(tp->rh);
-    glCallList(tp->name_list);
-}
 
-static void set_perspective(void)
-{
-    glPushMatrix();
-    glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluPerspective(60, -1, 0.1, 50);
-    gluLookAt(0, 5, -5, 0, 0, 0, 0, -1, 0);
-    glMatrixMode(GL_MODELVIEW);
-    glPopMatrix();
+    if (do_labels)
+      {
+        texture_font_data *f;
+        if (MI_WIDTH(mi) >= 500 && MI_HEIGHT(mi) >= 375)
+            f = tp->font1_data;
+        else if (MI_WIDTH(mi) >= 350 && MI_HEIGHT(mi) >= 260)
+            f = tp->font2_data;
+        else
+            f = tp->font3_data;
 
+        glColor3f(0.8, 0.8, 0);
+        print_texture_label (mi->dpy, f,
+                             mi->xgwa.width, mi->xgwa.height,
+                             1, tp->pn);
+      }
 }
 
-void reshape_tangram(ModeInfo * mi, int w, int h)
+ENTRYPOINT void reshape_tangram(ModeInfo * mi, int w, int h)
 {
     glViewport(0, 0, w, h);
-    set_perspective();
-    glLoadIdentity();
 }
 
-static void set_camera(void)
+static void set_camera(tangram_configuration *tp)
 {
-    static GLfloat theta[3] = { 1, 1, 1 };
-    static Bool going_down[3] = { False, False, False };
-
     glPushMatrix();
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
@@ -788,36 +793,36 @@ static void set_camera(void)
     gluLookAt(0, 5, -5, 0, 0, 0, 0, -1, 0);
 
     if (do_rotate) {
-        glRotatef(theta[0], 1, 0, 0);
-        glRotatef(theta[1], 0, 1, 0);
-        glRotatef(theta[2], 0, 0, 1);
+        glRotatef(tp->theta[0], 1, 0, 0);
+        glRotatef(tp->theta[1], 0, 1, 0);
+        glRotatef(tp->theta[2], 0, 0, 1);
     }
 
     glMatrixMode(GL_MODELVIEW);
     glPopMatrix();
 
 
-    if (going_down[0] && theta[0] < 0) {
+    if (tp->going_down[0] && tp->theta[0] < 0) {
 
-        going_down[0] = False;
-    } else if ((!going_down[0]) && theta[0] > 90) {
+        tp->going_down[0] = False;
+    } else if ((!tp->going_down[0]) && tp->theta[0] > 90) {
 
-        going_down[0] = True;
+        tp->going_down[0] = True;
     }
 
-    if (theta[1] > 360.0)
-        theta[1] -= 360.0;
+    if (tp->theta[1] > 360.0)
+        tp->theta[1] -= 360.0;
 
-    if (theta[2] > 360.0)
-        theta[2] -= 360.0;
+    if (tp->theta[2] > 360.0)
+        tp->theta[2] -= 360.0;
 
-    if (going_down[0])
-      theta[0] -= x_camera_rotate;
+    if (tp->going_down[0])
+      tp->theta[0] -= x_camera_rotate;
     else
-      theta[0] += x_camera_rotate;
+      tp->theta[0] += x_camera_rotate;
 
-    theta[1] += y_camera_rotate;
-    theta[2] += z_camera_rotate;
+    tp->theta[1] += y_camera_rotate;
+    tp->theta[2] += z_camera_rotate;
 }
 
 static void init_shapes(ModeInfo * mi)
@@ -828,13 +833,13 @@ static void init_shapes(ModeInfo * mi)
                       &tp->tlg2, &tp->sq, &tp->rh);
     get_solved_puzzle(mi, &tp->n_tsm1, &tp->n_tsm2, &tp->n_tm, &tp->n_tlg1,
                       &tp->n_tlg2, &tp->n_sq, &tp->n_rh);
-    tp->tsm1.dl = get_sm_tri_dl(wire);
-    tp->tsm2.dl = get_sm_tri_dl(wire);
-    tp->tm.dl = get_md_tri_dl(wire);
-    tp->tlg1.dl = get_lg_tri_dl(wire);
-    tp->tlg2.dl = get_lg_tri_dl(wire);
-    tp->sq.dl = get_square_dl(wire);
-    tp->rh.dl = get_rhomboid_dl(wire);
+    tp->tsm1.dl = tangram_get_sm_tri_dl(wire);
+    tp->tsm2.dl = tangram_get_sm_tri_dl(wire);
+    tp->tm.dl = tangram_get_md_tri_dl(wire);
+    tp->tlg1.dl = tangram_get_lg_tri_dl(wire);
+    tp->tlg2.dl = tangram_get_lg_tri_dl(wire);
+    tp->sq.dl = tangram_get_square_dl(wire);
+    tp->rh.dl = tangram_get_rhomboid_dl(wire);
 }
 
 static void gl_init(ModeInfo * mi)
@@ -868,20 +873,11 @@ static void gl_init(ModeInfo * mi)
 
 }
 
-void init_tangram(ModeInfo * mi)
+ENTRYPOINT void init_tangram(ModeInfo * mi)
 {
     tangram_configuration *tp;
 
-    if (!tps) {
-        tps = (tangram_configuration *)
-            calloc(MI_NUM_SCREENS(mi), sizeof(tangram_configuration));
-        if (!tps) {
-            fprintf(stderr, "%s: out of memory\n", progname);
-            exit(1);
-        }
-
-        tp = &tps[MI_SCREEN(mi)];
-    }
+    MI_INIT (mi, tps, NULL);
 
     tp = &tps[MI_SCREEN(mi)];
 
@@ -889,10 +885,15 @@ void init_tangram(ModeInfo * mi)
         gl_init(mi);
     }
 
+    reshape_tangram (mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+
     wire = MI_IS_WIREFRAME(mi);
 
     load_fonts(mi);
     init_shapes(mi);
+
+    tp->theta[0] = tp->theta[1] = tp->theta[2] = 1;
+
 }
 
 static Bool all_solved(tangram_shape * ls[])
@@ -940,7 +941,7 @@ static void set_not_solved(tangram_shape * ls[])
 }
 \f
 
-void draw_tangram(ModeInfo * mi)
+ENTRYPOINT void draw_tangram(ModeInfo * mi)
 {
     Display *dpy = MI_DISPLAY(mi);
     Window window = MI_WINDOW(mi);
@@ -952,13 +953,15 @@ void draw_tangram(ModeInfo * mi)
 
     int i;
     int MAX_DISPLAY;
-    static char *pn = "";
-    static int display_counter = 0;
 
-    static GLfloat color[4] = { 0.0, 0.0, 0.0, 1.0 };
-    static GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
+    GLfloat color[4] = { 0.0, 0.0, 0.0, 1.0 };
+    GLfloat white[4] = { 1.0, 1.0, 1.0, 1.0 };
     MAX_DISPLAY = viewing_time * 100;
 
+    if (! tp->glx_context)
+      return;
+    glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(tp->glx_context));
+
     ls[small_triangle1] = &tp->tsm1;
     ls[small_triangle2] = &tp->tsm2;
     ls[medium_triangle] = &tp->tm;
@@ -975,14 +978,14 @@ void draw_tangram(ModeInfo * mi)
     nls[square] = &tp->n_sq;
     nls[rhomboid] = &tp->n_rh;
 
-    set_camera();
+    set_camera(tp);
 
-    if (display_counter <= 0) {
+    if (tp->display_counter <= 0) {
         for (i = 0; i < NUM_SHAPES; i++) {
             if (ls[i]->solved) {
                 if (all_solved(ls)) {
-                    display_counter = MAX_DISPLAY;
-                    pn = tp->puzzle_name;
+                    tp->display_counter = MAX_DISPLAY;
+                    tp->pn = tp->puzzle_name;
                     get_solved_puzzle(mi, nls[small_triangle1],
                                       nls[small_triangle2],
                                       nls[medium_triangle],
@@ -1010,45 +1013,48 @@ void draw_tangram(ModeInfo * mi)
                     break;
                 }
             } else {
-                pn = "";
+                tp->pn = "";
                 bounce(ls[i]);
                 solve(nls[i], ls[i]);
             }
         }
     } else {
-        display_counter--;
+        tp->display_counter--;
     }
 
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glPushMatrix();
 
     glLoadIdentity();
-    glNewList(tp->name_list, GL_COMPILE);
-    {
-        XFontStruct *f;
-        GLuint fl;
-        if (MI_WIDTH(mi) >= 500 && MI_HEIGHT(mi) >= 375)
-            f = tp->xfont1, fl = tp->font1_dlist;
-        else if (MI_WIDTH(mi) >= 350 && MI_HEIGHT(mi) >= 260)
-            f = tp->xfont2, fl = tp->font2_dlist;
-        else
-            f = tp->xfont3, fl = tp->font3_dlist;
-
-        glColor3f(0.8, 0.8, 0);
-        print_gl_string(mi->dpy, f, fl,
-                        mi->xgwa.width, mi->xgwa.height,
-                        10, mi->xgwa.height - 10, pn);
-    }
-    glEndList();
 
     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, white);
     glMateriali(GL_FRONT_AND_BACK, GL_SHININESS, 128);
 
     draw_shapes(mi);
 
+    if (mi->fps_p) do_fps (mi);
+
     glFlush();
     glPopMatrix();
     glXSwapBuffers(dpy, window);
 }
 
+ENTRYPOINT Bool
+tangram_handle_event (ModeInfo *mi, XEvent *event)
+{
+  tangram_configuration *tp = &tps[MI_SCREEN(mi)];
+
+  if (screenhack_event_helper (MI_DISPLAY(mi), MI_WINDOW(mi), event))
+    {
+      tp->display_counter = 0;
+      return True;
+    }
+
+  return False;
+}
+
+
+
+XSCREENSAVER_MODULE ("Tangram", tangram)
+
 #endif                          /* USE_GL */