From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / glx / sballs.c
index 2eb2bcd92708f030877cd843df44ec741254ce45..6cd95f5d16b7c34d6f44fe95212464fed9b2de06 100644 (file)
@@ -41,82 +41,50 @@ static const char sccsid[] = "@(#)sballs.c  5.02 2001/03/10 xlockmore";
  */
 
 #ifdef STANDALONE      /* xscreensaver mode */
-#define PROGCLASS      "Sballs"
-#define HACK_INIT      init_sballs
-#define HACK_DRAW      draw_sballs
-#define HACK_RESHAPE   reshape_sballs
-#define sballs_opts    xlockmore_opts
 #define DEFAULTS       "*delay:        30000 \n" \
                        "*size:             0 \n" \
                        "*cycles:           4 \n" \
-                       "*object:           0 \n" \
-                       "*trackmouse:   False \n" \
                        "*showFPS:      False \n" \
                        "*wireframe:    False \n" \
-                       "*texture:      True  \n"
 
+# define release_sballs 0
 #define MODE_sballs
 #include "xlockmore.h"         /* from the xscreensaver distribution */
+#include "gltrackball.h"
 #else                          /* !STANDALONE */
 #include "xlock.h"             /* from the xlockmore distribution */
 #include "visgl.h"
 #endif                         /* !STANDALONE */
 
-#ifdef MODE_sballs
-
 #define MINSIZE        32      /* minimal viewport size */
 #define FRAME           50      /* frame count interval */
 #define MAX_OBJ                8       /* number of 3D objects */
 
-#include <GL/gl.h>
-#include <GL/glx.h>
-#include <GL/glu.h>
-
-#if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( HAVE_XPM )
+#if defined( USE_XPM ) || defined( USE_XPMINC ) || defined( STANDALONE )
 /* USE_XPM & USE_XPMINC in xlock mode ; HAVE_XPM in xscreensaver mode */
-# include "xpm-ximage.h"
+# include "ximage-loader.h"
 # define I_HAVE_XPM
 
-# ifdef STANDALONE
-
-#  ifdef __GNUC__
-   __extension__ /* don't warn about "string length is greater than the length
-                    ISO C89 compilers are required to support" when including
-                    the following XPM file... */
-#  endif
-#  include "../images/sball.xpm"
-#  ifdef __GNUC__
-   __extension__
-#  endif
-#  include "../images/sball-bg.xpm"
-# else /* !STANDALONE */
-#  include "pixmaps/sball.xpm"
-#  include "pixmaps/sball-bg.xpm"
-# endif /* !STANDALONE */
-#endif /* HAVE_XPM */
+# include "images/gen/sball_png.h"
+# include "images/gen/sball-bg_png.h"
 
 /* Manage option vars */
 #define DEF_TEXTURE    "True"
-#define DEF_TRACKMOUSE  "False"
 #define DEF_OBJECT     "0"
 static Bool do_texture;
-static Bool do_trackmouse;
-static int  object;
+static int  object, object_arg;
 static int  spheres;
 
 static XrmOptionDescRec opts[] = {
     {"-texture", ".sballs.texture", XrmoptionNoArg, "on"},
     {"+texture", ".sballs.texture", XrmoptionNoArg, "off"},
-    {"-trackmouse", ".sballs.trackmouse", XrmoptionNoArg, "on"},
-    {"+trackmouse", ".sballs.trackmouse", XrmoptionNoArg, "off"},
     {"-object", ".sballs.object", XrmoptionSepArg, 0},
 
 };
 
 static argtype vars[] = {
     {&do_texture,    "texture",    "Texture",    DEF_TEXTURE,    t_Bool},
-    {&do_trackmouse, "trackmouse", "TrackMouse", DEF_TRACKMOUSE, t_Bool},
-    {&object,        "object",     "Object",     DEF_OBJECT,     t_Int},
+    {&object_arg,    "object",     "Object",     DEF_OBJECT,     t_Int},
 
 };
 
@@ -124,17 +92,16 @@ static OptionStruct desc[] = {
     /*{"-count spheres", "set number of spheres"},*/
     /*{"-cycles speed", "set ball speed value"},*/
     {"-/+texture", "turn on/off texturing"},
-    {"-/+trackmouse", "turn on/off the tracking of the mouse"},
     {"-object num", "number of the 3D object (0 means random)"},
 };
 
-ModeSpecOpt sballs_opts =
+ENTRYPOINT ModeSpecOpt sballs_opts =
  { sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc };
 
 #ifdef USE_MODULES
 ModStruct sballs_description =
-    { "sballs", "init_sballs", "draw_sballs", "release_sballs",
-    "draw_sballs", "change_sballs", (char *) NULL, &sballs_opts,
+    { "sballs", "init_sballs", "draw_sballs", NULL,
+    "draw_sballs", "change_sballs", "free_sballs", &sballs_opts,
     /*
     delay,count,cycles,size,ncolors,sat
      */
@@ -166,20 +133,22 @@ typedef struct {
     GLuint faceid;             /* face texture id: GL world */
 
     vec3_t eye;
-    vec3_t rot;
     vec3_t rotm;
     int speed;
     float radius[MAX_BALLS];
 
+    trackball_state *trackball;
+    Bool button_down_p;
+
 } sballsstruct;
 
 /* array of sballsstruct indexed by screen number */
 static sballsstruct *sballs = (sballsstruct *) NULL;
 
 /* lights */
-static float LightAmbient[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
-static float LightDiffuse[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
-static float LightPosition[]=  { 0.0f, 0.0f, 4.0f, 1.0f };
+static const float LightAmbient[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
+static const float LightDiffuse[]=   { 1.0f, 1.0f, 1.0f, 1.0f };
+static const float LightPosition[]=  { 0.0f, 0.0f, 4.0f, 1.0f };
 
 /* structure of the polyhedras */
 typedef struct {
@@ -190,7 +159,7 @@ typedef struct {
         vec3_t      v[MAX_BALLS];/* the vertices */
 } Polyinfo;
 
-static Polyinfo polygons[] =
+static const Polyinfo polygons[] =
 {
 
 /* 0: objtetra - structure values for tetrahedron */
@@ -370,54 +339,6 @@ static Polyinfo polygons[] =
  *-----------------------------------------------------------------------------
  */
 
-static void clamp(vec3_t v)
-{
-    int i;
-
-    for (i = 0; i < 3; i ++)
-        if (v[i] > 360 || v[i] < -360)
-            v[i] = 0;
-}
-
-/* track the mouse in a joystick manner : not perfect but it works */
-static void trackmouse(ModeInfo * mi)
-{
-    sballsstruct *sb = &sballs[MI_SCREEN(mi)];
-    /* we keep static values (not per screen) for the mouse stuff: in general you have only one mouse :-> */
-    static int max[2] = { 0, 0 };
-    static int min[2] = { 0x7fffffff, 0x7fffffff }, center[2];
-    Window r, c;
-    int rx, ry, cx, cy;
-    unsigned int m;
-
-    (void) XQueryPointer(MI_DISPLAY(mi), MI_WINDOW(mi),
-                        &r, &c, &rx, &ry, &cx, &cy, &m);
-
-    if (max[0] < cx)
-       max[0] = cx;
-    if (min[0] > cx)
-       min[0] = cx;
-    center[0] = (max[0] + min[0]) / 2;
-
-    if (max[1] < cy)
-       max[1] = cy;
-    if (min[1] > cy)
-       min[1] = cy;
-    center[1] = (max[1] + min[1]) / 2;
-
-    if (fabs(center[0] - (float) cx) > 0.1 * (max[0] - min[0]))
-       sb->rot[0] -= ((center[0] - (float) cx) / (max[0] - min[0]) * 180.0f) / 200.0f;
-    if (fabs(center[1] - (float) cy) > 0.1 * (max[1] - min[1]))
-        sb->rot[1] -= ((center[1] - (float) cy) / (max[1] - min[1]) * 180.0f) / 200.0f;;
-    clamp(sb->rot);
-
-    /* oops: can't get those buttons */
-    if (m & Button4Mask)
-        sb->speed++;
-    if (m & Button5Mask)
-        sb->speed--;
-
-}
 
 /* initialise textures */
 static void inittextures(ModeInfo * mi)
@@ -432,10 +353,9 @@ static void inittextures(ModeInfo * mi)
        glBindTexture(GL_TEXTURE_2D, sb->backid);
 #endif /* HAVE_GLBINDTEXTURE */
 
-        sb->btexture = xpm_to_ximage(MI_DISPLAY(mi),
-                                     MI_VISUAL(mi),
-                                     MI_COLORMAP(mi),
-                                     sball_bg);
+        sb->btexture = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
+                                            sball_bg_png,
+                                            sizeof(sball_bg_png));
        if (!(sb->btexture)) {
            (void) fprintf(stderr, "Error reading the background texture.\n");
             glDeleteTextures(1, &sb->backid);
@@ -449,17 +369,19 @@ static void inittextures(ModeInfo * mi)
         clear_gl_error();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                     sb->btexture->width, sb->btexture->height, 0,
-                    GL_RGBA,
-                     /* GL_UNSIGNED_BYTE, */
-                     GL_UNSIGNED_INT_8_8_8_8_REV,
-                     sb->btexture->data);
+                    GL_RGBA, GL_UNSIGNED_BYTE, sb->btexture->data);
         check_gl_error("texture");
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
+/*
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        Let's pixellate it instead:
+*/
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
@@ -468,10 +390,8 @@ static void inittextures(ModeInfo * mi)
        glBindTexture(GL_TEXTURE_2D, sb->faceid);
 #endif /* HAVE_GLBINDTEXTURE */
 
-        sb->ftexture = xpm_to_ximage(MI_DISPLAY(mi),
-                                     MI_VISUAL(mi),
-                                     MI_COLORMAP(mi),
-                                     sball);
+        sb->ftexture = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
+                                            sball_png, sizeof(sball_png));
        if (!(sb->ftexture)) {
            (void) fprintf(stderr, "Error reading the face texture.\n");
             glDeleteTextures(1, &sb->faceid);
@@ -483,17 +403,19 @@ static void inittextures(ModeInfo * mi)
         clear_gl_error();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                     sb->ftexture->width, sb->ftexture->height, 0,
-                    GL_RGBA,
-                     /* GL_UNSIGNED_BYTE, */
-                     GL_UNSIGNED_INT_8_8_8_8_REV,
-                     sb->ftexture->data);
+                    GL_RGBA, GL_UNSIGNED_BYTE, sb->ftexture->data);
         check_gl_error("texture");
 
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
+/*
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+        Let's pixellate it instead:
+*/
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
 
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     }
@@ -569,7 +491,7 @@ static void drawSphere(ModeInfo * mi,int sphere_num)
 #ifndef STANDALONE
 static void Reshape(ModeInfo * mi)
 #else
-void reshape_sballs(ModeInfo * mi, int width, int height)
+ENTRYPOINT void reshape_sballs(ModeInfo * mi, int width, int height)
 #endif
 {
 
@@ -587,6 +509,12 @@ void reshape_sballs(ModeInfo * mi, int width, int height)
         sb->WIDTH = (size > MI_WIDTH(mi)) ? MI_WIDTH(mi) : size;
         sb->HEIGHT = (size > MI_HEIGHT(mi)) ? MI_HEIGHT(mi) : size;
     }
+
+    if (width > height * 5) {   /* tiny window: show middle */
+      sb->WIDTH = width;
+      sb->HEIGHT = sb->WIDTH*0.75;
+    }
+
     glViewport((MI_WIDTH(mi) - sb->WIDTH) / 2, (MI_HEIGHT(mi) - sb->HEIGHT) / 2, sb->WIDTH, sb->HEIGHT);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
@@ -603,9 +531,6 @@ static void Draw(ModeInfo * mi)
 
     mi->polygon_count = 0;
 
-    if (do_trackmouse && !MI_IS_ICONIC(mi))
-       trackmouse(mi);
-
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
     glPushMatrix();
@@ -629,33 +554,41 @@ static void Draw(ModeInfo * mi)
        glColor3f(0, 0, 0);
     }
     glBegin(GL_QUAD_STRIP);
+#ifndef HAVE_MOBILE
+    /* Letterbox the background image */
     glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(8, 4.1, -4);
     glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(8, -4.1, -4);
     glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-8, 4.1, -4);
     glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-8, -4.1, -4);
+#else
+    /* Fill the iPhone screen. Letterboxing looks dumb there. */
+    glNormal3f(0, 0, 1); glTexCoord2f(0,0); glVertex3f(4, 5.2, -4);
+    glNormal3f(0, 0, 1); glTexCoord2f(0,1); glVertex3f(4, -5.2, -4);
+    glNormal3f(0, 0, 1); glTexCoord2f(1,0); glVertex3f(-4, 5.2, -4);
+    glNormal3f(0, 0, 1); glTexCoord2f(1,1); glVertex3f(-4, -5.2, -4);
+#endif
     glEnd();
     mi->polygon_count++;
 
-    /* rotate the mouse */
-    glRotatef(sb->rot[0], 1.0f, 0.0f, 0.0f);
-    glRotatef(sb->rot[1], 0.0f, 1.0f, 0.0f);
-    glRotatef(sb->rot[2], 0.0f, 0.0f, 1.0f);
+    gltrackball_rotate (sb->trackball);
 
     /* rotate the balls */
     glRotatef(sb->rotm[0], 1.0f, 0.0f, 0.0f);
     glRotatef(sb->rotm[1], 0.0f, 1.0f, 0.0f);
     glRotatef(sb->rotm[2], 0.0f, 0.0f, 1.0f);
 
-    sb->rotm[0] += sb->speed;
-    sb->rotm[1] += -(sb->speed);
-    sb->rotm[2] += 0;
+    if (! sb->button_down_p) {
+      sb->rotm[0] += sb->speed;
+      sb->rotm[1] += -(sb->speed);
+      sb->rotm[2] += 0;
+    }
 
     /* draw the balls */
     if (do_texture)
 #ifdef HAVE_GLBINDTEXTURE
        glBindTexture(GL_TEXTURE_2D, sb->faceid);
-#endif /* HAVE_GLBINDTEXTURE */
     else
+#endif /* HAVE_GLBINDTEXTURE */
        glEnable(GL_LIGHTING);
     for (sphere=0;sphere<spheres;sphere++)
     {
@@ -689,14 +622,13 @@ static void Init(ModeInfo * mi)
     }
 
     vinit(sb->eye   ,0.0f, 0.0f, 6.0f);
-    vinit(sb->rot   ,0.0f, 0.0f, 0.0f);
     vinit(sb->rotm  ,0.0f, 0.0f, 0.0f);
     sb->speed = MI_CYCLES(mi);
 
     /* initialise object number */
-    if ((object == 0) || (object > MAX_OBJ))
-      object = NRAND(MAX_OBJ-1)+1;
-    object--;
+    object = object_arg-1;
+    if (object < 0 || object >= MAX_OBJ)
+      object = NRAND(MAX_OBJ);
 
     /* initialise sphere number */
     spheres = MI_COUNT(mi);
@@ -733,8 +665,6 @@ static void Init(ModeInfo * mi)
         glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
         glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);
         glEnable(GL_LIGHT1);
-
-        glClearColor(0, 0, 0, 0);
 }
 
 /*
@@ -751,17 +681,15 @@ static void Init(ModeInfo * mi)
  *-----------------------------------------------------------------------------
  */
 
-void init_sballs(ModeInfo * mi)
+ENTRYPOINT void init_sballs(ModeInfo * mi)
 {
     sballsstruct *sb;
 
-    if (sballs == NULL) {
-       if ((sballs = (sballsstruct *) calloc(MI_NUM_SCREENS(mi),
-                                         sizeof(sballsstruct))) == NULL)
-           return;
-    }
+    MI_INIT(mi, sballs);
     sb = &sballs[MI_SCREEN(mi)];
 
+    sb->trackball = gltrackball_init (True);
+
     if ((sb->glx_context = init_GL(mi)) != NULL) {
 
 #ifndef STANDALONE
@@ -782,7 +710,7 @@ void init_sballs(ModeInfo * mi)
  *    Called by the mainline code periodically to update the display.
  *-----------------------------------------------------------------------------
  */
-void draw_sballs(ModeInfo * mi)
+ENTRYPOINT void draw_sballs(ModeInfo * mi)
 {
     Display *display = MI_DISPLAY(mi);
     Window window = MI_WINDOW(mi);
@@ -812,36 +740,47 @@ void draw_sballs(ModeInfo * mi)
 /*
  *-----------------------------------------------------------------------------
  *    The display is being taken away from us.  Free up malloc'ed
- *      memory and X resources that we've alloc'ed.  Only called
- *      once, we must zap everything for every screen.
+ *      memory and X resources that we've alloc'ed.
  *-----------------------------------------------------------------------------
  */
 
-void release_sballs(ModeInfo * mi)
+ENTRYPOINT void free_sballs(ModeInfo * mi)
 {
-    int screen;
-
-    if (sballs != NULL) {
-       for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-           sballsstruct *sb = &sballs[screen];
-           if (sb->btexture)
-           {
-               glDeleteTextures(1,&sb->backid);
-               XDestroyImage(sb->btexture);
-           }
-           if (sb->ftexture)
-           {
-               glDeleteTextures(1,&sb->faceid);
-               XDestroyImage(sb->ftexture);
-           }
+    sballsstruct *sb = &sballs[MI_SCREEN(mi)];
+    if (sb->glx_context)
+    {
+       glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
+       if (sb->btexture)
+       {
+           glDeleteTextures(1,&sb->backid);
+           XDestroyImage(sb->btexture);
+            sb->btexture = 0;
+       }
+       if (sb->ftexture)
+       {
+           glDeleteTextures(1,&sb->faceid);
+           XDestroyImage(sb->ftexture);
+            sb->ftexture = 0;
        }
-       (void) free((void *) sballs);
-       sballs = (sballsstruct *) NULL;
     }
-    FreeAllGL(mi);
 }
 
-void change_sballs(ModeInfo * mi)
+ENTRYPOINT Bool
+sballs_handle_event (ModeInfo *mi, XEvent *event)
+{
+  sballsstruct *sb = &sballs[MI_SCREEN(mi)];
+
+  if (gltrackball_event_handler (event, sb->trackball,
+                                 MI_WIDTH (mi), MI_HEIGHT (mi),
+                                 &sb->button_down_p))
+    return True;
+
+  return False;
+}
+
+
+#ifndef STANDALONE
+ENTRYPOINT void change_sballs(ModeInfo * mi)
 {
     sballsstruct *sb;
 
@@ -877,4 +816,8 @@ void change_sballs(ModeInfo * mi)
     glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(sb->glx_context));
 
 }
+#endif /* !STANDALONE */
+
+XSCREENSAVER_MODULE ("SBalls", sballs)
+
 #endif /* MODE_sballs */