From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / glx / atlantis.c
index 2656543bcc2ca89c31c6140a7565071d4013862a..04bde12e5a5434f62c8543019428cedc83da6fb3 100644 (file)
@@ -29,7 +29,7 @@ static const char sccsid[] = "@(#)atlantis.c  5.08 2003/04/09 xlockmore";
  * Thanks goes also to Brian Paul for making it possible and inexpensive
  * to use OpenGL at home.
  *
- * My e-mail address is lassauge@sourceforge.net
+ * My e-mail address is lassauge@users.sourceforge.net
  *
  * Eric Lassauge  (May-13-1998)
  *
@@ -102,34 +102,27 @@ static const char sccsid[] = "@(#)atlantis.c      5.08 2003/04/09 xlockmore";
  */
 
 #define DEF_TEXTURE "True"
-#define DEF_GRADIENT "False"
+#define DEF_GRADIENT "True"
 #define DEF_WHALESPEED  "250"
 
 #ifdef STANDALONE
-# define PROGCLASS     "Atlantis"
-# define HACK_INIT     init_atlantis
-# define HACK_DRAW     draw_atlantis
-# define HACK_RESHAPE  reshape_atlantis
-# define atlantis_opts xlockmore_opts
 # define DEFAULTS      "*delay:       25000 \n" \
                         "*count:          4 \n" \
                         "*showFPS:    False \n" \
                         "*cycles:       100 \n" \
                         "*size:        6000 \n" \
-                        "*wireframe:  False \n" \
-                        "*texture:    " DEF_TEXTURE    " \n" \
-                        "*gradient:   " DEF_GRADIENT   " \n" \
-                        "*whalespeed: " DEF_WHALESPEED " \n"
+                        "*wireframe:  False \n"
+# define release_atlantis 0
+# define atlantis_handle_event xlockmore_no_events
 # include "xlockmore.h"                /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"            /* from the xlockmore distribution */
-#include "vis.h"
+# include "vis.h"
 #endif /* !STANDALONE */
 
 #ifdef USE_GL
 
 #include "atlantis.h"
-#include <GL/glu.h>
 
 
 static int  whalespeed;
@@ -137,11 +130,11 @@ static int do_texture;
 static int do_gradient;
 static XrmOptionDescRec opts[] =
 {
-     {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, (caddr_t) NULL},
-     {"-texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"true"},
-     {"+texture",    ".atlantis.texture",    XrmoptionNoArg, (caddr_t)"false"},
-     {"-gradient",   ".atlantis.gradient",   XrmoptionNoArg, (caddr_t)"true"},
-     {"+gradient",   ".atlantis.gradient",   XrmoptionNoArg, (caddr_t)"false"},
+     {"-whalespeed", ".atlantis.whalespeed", XrmoptionSepArg, 0},
+     {"-texture",    ".atlantis.texture",    XrmoptionNoArg, "true"},
+     {"+texture",    ".atlantis.texture",    XrmoptionNoArg, "false"},
+     {"-gradient",   ".atlantis.gradient",   XrmoptionNoArg, "true"},
+     {"+gradient",   ".atlantis.gradient",   XrmoptionNoArg, "false"},
 };
 
 static argtype vars[] =
@@ -158,13 +151,13 @@ static OptionStruct desc[] =
        {"-gradient",       "whether to introduce gradient-filled background"},
 };
 
-ModeSpecOpt atlantis_opts =
+ENTRYPOINT ModeSpecOpt atlantis_opts =
 {sizeof opts / sizeof opts[0], opts, sizeof vars / sizeof vars[0], vars, desc};
 
 #ifdef USE_MODULES
 ModStruct   atlantis_description =
-{"atlantis", "init_atlantis", "draw_atlantis", "release_atlantis",
- "refresh_atlantis", "change_atlantis", NULL, &atlantis_opts,
+{"atlantis", "init_atlantis", "draw_atlantis", NULL,
+ "refresh_atlantis", "change_atlantis", "free_atlantis", &atlantis_opts,
  1000, NUM_SHARKS, SHARKSPEED, SHARKSIZE, 64, 1.0, "",
  "Shows moving sharks/whales/dolphin", 0, NULL};
 
@@ -172,19 +165,18 @@ ModStruct   atlantis_description =
 
 static atlantisstruct *atlantis = NULL;
 
-#include "xpm-ximage.h"
+#include "ximage-loader.h"
 
-#include "../images/sea-texture.xpm"
+#include "images/gen/sea-texture_png.h"
 
 
 static void
 parse_image_data(ModeInfo *mi)
 {
   atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
-  ap->texture = xpm_to_ximage (mi->dpy,
-                              mi->xgwa.visual,
-                              mi->xgwa.colormap,
-                              sea_texture);
+  ap->texture = image_data_to_ximage (mi->dpy, mi->xgwa.visual,
+                                      sea_texture_png,
+                                      sizeof(sea_texture_png));
 }
 
 static void
@@ -230,24 +222,16 @@ Init(ModeInfo *mi)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
 
-       static float ambient[] =
-       {0.1, 0.1, 0.1, 1.0};
-       static float diffuse[] =
-       {1.0, 1.0, 1.0, 1.0};
-       static float position[] =
-       {0.0, 1.0, 0.0, 0.0};
-       static float mat_shininess[] =
-       {90.0};
-       static float mat_specular[] =
-       {0.8, 0.8, 0.8, 1.0};
-       static float mat_diffuse[] =
-       {0.46, 0.66, 0.795, 1.0};
-       static float mat_ambient[] =
-       {0.0, 0.1, 0.2, 1.0};
-       static float lmodel_ambient[] =
-       {0.4, 0.4, 0.4, 1.0};
-       static float lmodel_localviewer[] =
-       {0.0};
+       static const float ambient[]        = {0.1, 0.1, 0.1, 1.0};
+       static const float diffuse[]        = {1.0, 1.0, 1.0, 1.0};
+       static const float position[]       = {0.0, 1.0, 0.0, 0.0};
+       static const float mat_shininess[]  = {90.0};
+       static const float mat_specular[]   = {0.8, 0.8, 0.8, 1.0};
+       static const float mat_diffuse[]    = {0.46, 0.66, 0.795, 1.0};
+       static const float mat_ambient[]    = {0.0, 0.1, 0.2, 1.0};
+       static const float lmodel_ambient[] = {0.4, 0.4, 0.4, 1.0};
+       static const float lmodel_localviewer[] = {0.0};
+
        float        fblue = 0.0, fgreen;
 
        glFrontFace(GL_CCW);
@@ -287,8 +271,6 @@ Init(ModeInfo *mi)
           }
         else
           {
-            GLfloat s_plane[] = { 1, 0, 0, 0 };
-            GLfloat t_plane[] = { 0, 0, 1, 0 };
             GLfloat scale = 0.0005;
 
             if (!ap->texture)
@@ -308,13 +290,18 @@ Init(ModeInfo *mi)
 
             glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
 
-            glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-            glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
-            glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
-            glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
-
-            glEnable(GL_TEXTURE_GEN_S);
-            glEnable(GL_TEXTURE_GEN_T);
+# ifndef HAVE_JWZGLES
+            {
+              GLfloat s_plane[] = { 1, 0, 0, 0 };
+              GLfloat t_plane[] = { 0, 0, 1, 0 };
+              glTexGeni (GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+              glTexGeni (GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
+              glTexGenfv(GL_S, GL_EYE_PLANE, s_plane);
+              glTexGenfv(GL_T, GL_EYE_PLANE, t_plane);
+              glEnable(GL_TEXTURE_GEN_S);
+              glEnable(GL_TEXTURE_GEN_T);
+            }
+# endif
             glEnable(GL_TEXTURE_2D);
 
             glMatrixMode(GL_TEXTURE);
@@ -328,20 +315,28 @@ Init(ModeInfo *mi)
        /* Add a little randomness */
        fblue = ((float) (NRAND(30)) / 100.0) + 0.70;
        fgreen = fblue * 0.56;
-       glClearColor(0.0, fgreen, fblue, 0.0);
+       glClearColor(0.0, fgreen, fblue, 1.0);
 }
 
-void
+ENTRYPOINT void
 reshape_atlantis(ModeInfo * mi, int width, int height)
 {
-       atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
+  double h = (GLfloat) height / (GLfloat) width;  
+  atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
+  int y = 0;
+
+  if (width > height * 5) {   /* tiny window: show middle */
+    height = width * 9/16;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
 
-       glViewport(0, 0, ap->WinW = (GLint) width, ap->WinH = (GLint) height);
+  glViewport(0, y, ap->WinW = (GLint) width, ap->WinH = (GLint) height);
 
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       gluPerspective(400.0, (GLdouble) width / (GLdouble) height, 1.0, 2000000.0);
-       glMatrixMode(GL_MODELVIEW);
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  gluPerspective(400.0, 1/h, 1.0, 2000000.0);
+  glMatrixMode(GL_MODELVIEW);
 }
 
 
@@ -356,8 +351,8 @@ clear_tank (atlantisstruct * ap)
 
   if (do_gradient && !ap->wire)
     {
-      GLfloat top[3] = { 0.00, 0.40, 0.70 };
-      GLfloat bot[3] = { 0.00, 0.05, 0.18 };
+      GLfloat top[4] = { 0.00, 0.40, 0.70, };
+      GLfloat bot[4] = { 0.00, 0.05, 0.18, };
 
       glMatrixMode(GL_PROJECTION);
       glPushMatrix();
@@ -367,26 +362,22 @@ clear_tank (atlantisstruct * ap)
         glPushMatrix();
         {
           glLoadIdentity();
-
-          /* save GL_COLOR_MATERIAL, GL_COLOR_MATERIAL_FACE, etc.
-             This stalls the pipeline, so it would be better to do this
-             with explicit enable/disable calls, but I can't figure
-             out how to undo the glEnable() and glColor() calls below!
-             Simply calling glDisable(GL_COLOR_MATERIAL) is insufficient!
-           */
-          glPushAttrib (GL_LIGHTING_BIT);
-          {
-            glEnable (GL_COLOR_MATERIAL);
-
-            glShadeModel(GL_SMOOTH);
-            glBegin(GL_QUADS);
-            glColor3f (bot[0], bot[1], bot[2]); glVertex3f (-1, -1, 1);
-            glColor3f (bot[0], bot[1], bot[2]); glVertex3f ( 1, -1, 1);
-            glColor3f (top[0], top[1], top[2]); glVertex3f ( 1,  1, 1);
-            glColor3f (top[0], top[1], top[2]); glVertex3f (-1,  1, 1);
-            glEnd();
-          }
-          glPopAttrib();
+          /* glRotatef(current_device_rotation(), 0, 0, 1); */
+
+# ifndef HAVE_JWZGLES
+          glShadeModel (GL_SMOOTH);
+# endif
+          glDisable (GL_LIGHTING);
+          glBegin (GL_QUADS);
+          glColor3fv (bot);
+          glVertex3f (-1, -1, 1); glVertex3f ( 1, -1, 1);
+          glColor3fv (top);
+          glVertex3f ( 1,  1, 1); glVertex3f (-1,  1, 1);
+          glEnd();
+          glEnable (GL_LIGHTING);
+
+          /* Need to reset this because jwzgles conflates color and material */
+          glColor3f (0.0, 0.1, 0.2);
         }
         glPopMatrix();
       }
@@ -460,7 +451,7 @@ AllDisplay(atlantisstruct * ap)
  *-----------------------------------------------------------------------------
  */
 
-void
+ENTRYPOINT void
 init_atlantis(ModeInfo * mi)
 {
        int         screen = MI_SCREEN(mi);
@@ -468,11 +459,7 @@ init_atlantis(ModeInfo * mi)
        Display    *display = MI_DISPLAY(mi);
        Window      window = MI_WINDOW(mi);
 
-       if (atlantis == NULL) {
-               if ((atlantis = (atlantisstruct *) calloc(MI_NUM_SCREENS(mi),
-                                          sizeof (atlantisstruct))) == NULL)
-                       return;
-       }
+       MI_INIT(mi, atlantis);
        ap = &atlantis[screen];
        ap->num_sharks = MI_COUNT(mi);
        if (ap->sharks == NULL) {
@@ -520,7 +507,7 @@ init_atlantis(ModeInfo * mi)
  *    Called by the mainline code periodically to update the display.
  *-----------------------------------------------------------------------------
  */
-void
+ENTRYPOINT void
 draw_atlantis(ModeInfo * mi)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
@@ -537,8 +524,11 @@ draw_atlantis(ModeInfo * mi)
 
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
+        glPushMatrix();
+        glRotatef(current_device_rotation(), 0, 0, 1);
         AllDisplay(ap);
         Animate(ap);
+        glPopMatrix();
 
         if (mi->fps_p) do_fps (mi);
        glXSwapBuffers(display, window);
@@ -548,35 +538,28 @@ draw_atlantis(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_atlantis(ModeInfo * mi)
+ENTRYPOINT void
+free_atlantis(ModeInfo * mi)
 {
-       int         screen;
-
-       if (atlantis != NULL) {
-               for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-                       atlantisstruct *ap = &atlantis[screen];
+#if 0
+       atlantisstruct *ap = &atlantis[screen];
 
-                       if (ap->sharks)
-                               (void) free((void *) ap->sharks);
-               }
-               (void) free((void *) atlantis);
-               atlantis = NULL;
-       }
-       FreeAllGL(mi);
+       if (ap->sharks)
+               (void) free((void *) ap->sharks);
+#endif
 }
 
-void
+#ifndef STANDALONE
+ENTRYPOINT void
 refresh_atlantis(ModeInfo * mi)
 {
 }
 
-void
+ENTRYPOINT void
 change_atlantis(ModeInfo * mi)
 {
        atlantisstruct *ap = &atlantis[MI_SCREEN(mi)];
@@ -587,5 +570,8 @@ change_atlantis(ModeInfo * mi)
        glXMakeCurrent(MI_DISPLAY(mi), MI_WINDOW(mi), *(ap->glx_context));
        Init(mi);
 }
+#endif /* !STANDALONE */
+
+XSCREENSAVER_MODULE ("Atlantis", atlantis)
 
 #endif /* USE_GL */