From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / glx / atunnel.c
index 966b1be3c29c69dad17face448b823426ae7b3b2..f21d04fd5d964e6915b4ab20f53c9f08ee8c2139 100644 (file)
@@ -1,11 +1,11 @@
 /* -*- Mode: C; tab-width: 4 -*- */
-/* atunnels --- OpenGL Advanced Tunnel Screensaver */
+/* atunnel --- OpenGL Advanced Tunnel Screensaver */
 
 #if 0
-static const char sccsid[] = "@(#)atunnels.c   5.07 2003/02/12 xlockmore";
+static const char sccsid[] = "@(#)atunnel.c    5.13 2004/05/25 xlockmore";
 #endif
 
-/* Copyright (c) E. Lassauge, 2003. */
+/* Copyright (c) E. Lassauge, 2003-2004. */
 
 /*
  * Permission to use, copy, modify, and distribute this software and its
@@ -24,11 +24,13 @@ static const char sccsid[] = "@(#)atunnels.c        5.07 2003/02/12 xlockmore";
  * Email: romka@ut.ee
  * WEB: http://romka.demonews.com
  *
- * Eric Lassauge  (March-16-2002) <lassauge@mail.dotcom.fr>
+ * Eric Lassauge  (May-25-2004) <lassauge@users.sourceforge.net>
  *                                 http://lassauge.free.fr/linux.html
  *
  * REVISION HISTORY:
  *
+ * E.Lassauge - 25-May-2004:
+ *     - added more texture !
  * E.Lassauge - 16-Mar-2002:
  *     - created based on the Roman demo.
  *     - deleted all external file stuff to use xpm textures and
@@ -37,24 +39,19 @@ static const char sccsid[] = "@(#)atunnels.c        5.07 2003/02/12 xlockmore";
  */
 
 #ifdef STANDALONE              /* xscreensaver mode */
-# define PROGCLASS             "Atunnels"
-# define HACK_INIT             init_atunnels
-# define HACK_DRAW             draw_atunnels
-# define HACK_RESHAPE          reshape_atunnels
-# define atunnels_opts         xlockmore_opts
 #define        DEFAULTS                "*delay:        10000   \n" \
-                               "*showFPS:      False   \n" \
-                                "*light:       True    \n" \
-                                "*wire:                False   \n" \
-                                "*texture:     True    \n"
+                                "*showFPS:  False   \n" \
+                                                               "*suppressRotationAnimation: True\n" \
 
-#define MODE_atunnels
+# define release_atunnel 0
+# define atunnel_handle_event 0
+#define MODE_atunnel
 # include "xlockmore.h"                /* from the xscreensaver distribution */
 #else                          /* !STANDALONE */
 # include "xlock.h"            /* from the xlockmore distribution */
 #endif                                 /* !STANDALONE */
 
-#ifdef MODE_atunnels /* whole file */
+#ifdef MODE_atunnel /* whole file */
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -63,25 +60,23 @@ static const char sccsid[] = "@(#)atunnels.c        5.07 2003/02/12 xlockmore";
 
 #if defined( USE_XPM ) || defined( USE_XPMINC ) || defined(STANDALONE)
 /* USE_XPM & USE_XPMINC in xlock mode ; STANDALONE in xscreensaver mode */
-#include "xpm-ximage.h"
+#include "ximage-loader.h"
 #define I_HAVE_XPM
 
-#ifdef STANDALONE
-#include "../images/tunnel0.xpm"
-#include "../images/tunnel1.xpm"
-#include "../images/tunnel2.xpm"
-#else /* !STANDALONE */
-#include "pixmaps/tunnel0.xpm"
-#include "pixmaps/tunnel1.xpm"
-#include "pixmaps/tunnel2.xpm"
-#endif /* !STANDALONE */
+#include "images/gen/tunnel0_png.h"
+#include "images/gen/tunnel1_png.h"
+#include "images/gen/tunnel2_png.h"
+#include "images/gen/tunnel3_png.h"
+#include "images/gen/tunnel4_png.h"
+#include "images/gen/tunnel5_png.h"
 #endif /* HAVE_XPM */
 
+
 #undef countof
 #define countof(x) (sizeof((x))/sizeof((*x)))
 
 #define DEF_LIGHT      "True"
-#define DEF_WIRE       "False"
+#define DEF_WIRE    "False"
 #define DEF_TEXTURE    "True"
 
 static Bool do_light;
@@ -89,12 +84,12 @@ static Bool do_wire;
 static Bool do_texture;
 
 static XrmOptionDescRec opts[] = {
-  {(char *)"-light",   (char *)".atunnels.light",      XrmoptionNoArg, (caddr_t) "true" },
-  {(char *)"+light",   (char *)".atunnels.light",      XrmoptionNoArg, (caddr_t) "false" },
-  {(char *)"-wire",    (char *)".atunnels.wire",       XrmoptionNoArg, (caddr_t) "true" },
-  {(char *)"+wire",    (char *)".atunnels.wire",       XrmoptionNoArg, (caddr_t) "false" },
-  {(char *)"-texture", (char *)".atunnels.texture",    XrmoptionNoArg, (caddr_t) "true" },
-  {(char *)"+texture", (char *)".atunnels.texture",    XrmoptionNoArg, (caddr_t) "false" },
+  {"-light",   ".atunnel.light",      XrmoptionNoArg, "true" },
+  {"+light",   ".atunnel.light",      XrmoptionNoArg, "false" },
+  {"-wireframe",".atunnel.wire",       XrmoptionNoArg, "true" },
+  {"+wireframe",".atunnel.wire",       XrmoptionNoArg, "false" },
+  {"-texture", ".atunnel.texture",    XrmoptionNoArg, "true" },
+  {"+texture", ".atunnel.texture",    XrmoptionNoArg, "false" },
 };
 
 static argtype vars[] = {
@@ -105,17 +100,17 @@ static argtype vars[] = {
 
 static OptionStruct desc[] =
 {
-  {(char *)"-/+ light",   (char *)"whether to do enable lighting (slower)"},
-  {(char *)"-/+ wire",    (char *)"whether to do use wireframe instead of filled (faster)"},
-  {(char *)"-/+ texture", (char *)"whether to apply a texture (slower)"},
+  {"-/+ light",   "whether to do enable lighting (slower)"},
+  {"-/+ wire",    "whether to do use wireframe instead of filled (faster)"},
+  {"-/+ texture", "whether to apply a texture (slower)"},
 };
 
-ModeSpecOpt atunnels_opts = {countof(opts), opts, countof(vars), vars, desc};
+ENTRYPOINT ModeSpecOpt atunnel_opts = {countof(opts), opts, countof(vars), vars, desc};
 
 #ifdef USE_MODULES
-ModStruct   atunnels_description =
-{"atunnels", "init_atunnels", "draw_atunnels", "release_atunnels",
- "draw_atunnels", "init_atunnels", NULL, &atunnels_opts,
+ModStruct   atunnel_description =
+{"atunnel", "init_atunnel", "draw_atunnel", NULL,
+ "draw_atunnel", "init_atunnel", "free_atunnel", &atunnel_opts,
  1000, 1, 2, 1, 4, 1.0, "",
  "OpenGL advanced tunnel screensaver", 0, NULL};
 #endif
@@ -125,21 +120,26 @@ typedef struct {
   int screen_width, screen_height;
   GLXContext *glx_context;
   Window window;
-} atunnelsstruct;
+  struct tunnel_state *ts;
+  GLuint texture[MAX_TEXTURE]; /* texture id: GL world */
+} atunnelstruct;
 
-static atunnelsstruct *Atunnels = NULL;
-static GLuint texture[3]; /* texture id: GL world */
+static atunnelstruct *Atunnel = NULL;
 
 /*=================== Load Texture =========================================*/
-static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
+static void LoadTexture(ModeInfo * mi,
+                        const unsigned char *data, unsigned long size,
+                        int t_num)
 {
 #if defined( I_HAVE_XPM )
+       atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
        XImage *teximage;    /* Texture data */
  
-        if ((teximage = xpm_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
-                        MI_COLORMAP(mi), fn)) == None) {
+        if ((teximage = image_data_to_ximage(MI_DISPLAY(mi), MI_VISUAL(mi),
+                                             data, size))
+            == None) {
            (void) fprintf(stderr, "Error reading the texture.\n");
-           glDeleteTextures(1, &texture[t_num]);
+           glDeleteTextures(1, &sa->texture[t_num]);
             do_texture = False;
 #ifdef STANDALONE
            exit(0);
@@ -149,12 +149,12 @@ static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
        }
 
 #ifdef HAVE_GLBINDTEXTURE
-       glBindTexture(GL_TEXTURE_2D, texture[t_num]);
+       glBindTexture(GL_TEXTURE_2D, sa->texture[t_num]);
 #endif /* HAVE_GLBINDTEXTURE */
        glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
        clear_gl_error();
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, teximage->width, teximage->height, 
-                       0, GL_RGBA, GL_UNSIGNED_BYTE, teximage->data);
+                 0, GL_RGBA, GL_UNSIGNED_BYTE, teximage->data);
        check_gl_error("texture");
 
        /* Texture parameters, LINEAR scaling for better texture quality */
@@ -170,22 +170,25 @@ static void LoadTexture(ModeInfo * mi, char **fn, int t_num)
 /*=================== Main Initialization ==================================*/
 static void Init(ModeInfo * mi)
 {
+       atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
        GLfloat light_ambient[] = {1.0, 1.0, 1.0, 1.0};
        GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
        GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
        GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
        GLfloat fogColor[4] = {0.8, 0.8, 0.8, 1.0};
 
-       glClearColor(0, 0, 0, 0);
        if (do_texture)
        {
-               glGenTextures(3, texture);
-               LoadTexture(mi, texture0,0);
-               LoadTexture(mi, texture1,1);
-               LoadTexture(mi, texture2,2);
+               glGenTextures(MAX_TEXTURE, sa->texture);
+               LoadTexture(mi, tunnel0_png, sizeof(tunnel0_png),0);
+               LoadTexture(mi, tunnel1_png, sizeof(tunnel1_png),1);
+               LoadTexture(mi, tunnel2_png, sizeof(tunnel2_png),2);
+               LoadTexture(mi, tunnel3_png, sizeof(tunnel3_png),3);
+               LoadTexture(mi, tunnel4_png, sizeof(tunnel4_png),4);
+               LoadTexture(mi, tunnel5_png, sizeof(tunnel5_png),5);
                glEnable(GL_TEXTURE_2D);
        }
-       InitTunnel();
+       sa->ts = atunnel_InitTunnel();
        
        /* Set lighting parameters */
        if (do_light)
@@ -201,6 +204,11 @@ static void Init(ModeInfo * mi)
        
                glEnable(GL_LIGHTING);
        }
+
+# ifdef HAVE_JWZGLES /* #### glPolygonMode other than GL_FILL unimplemented */
+    do_wire = 0;
+# endif
+
        if (do_wire) {
                glDisable(GL_NORMALIZE);
                glDisable(GL_CULL_FACE);
@@ -230,23 +238,29 @@ static void Init(ModeInfo * mi)
 
 
 /* Standard reshape function */
-void
-reshape_atunnels(ModeInfo *mi, int width, int height)
+ENTRYPOINT void
+reshape_atunnel(ModeInfo *mi, int width, int height)
 {
-       float a;
+  double h = (GLfloat) height / (GLfloat) width;  
+  int y = 0;
 
-       glViewport(0, 0, width, height);
-       glMatrixMode(GL_PROJECTION);
-       glLoadIdentity();
-       a = (float)width/(float)height;
-       glFrustum(-0.1*a, 0.1*a, -0.1, 0.1, 0.1, 10);
-       glMatrixMode(GL_MODELVIEW);
+  if (width > height * 2) {   /* tiny window: show middle */
+    height = width;
+    y = -height/2;
+    h = height / (GLfloat) width;
+  }
+
+  glViewport(0, y, width, height);
+  glMatrixMode(GL_PROJECTION);
+  glLoadIdentity();
+  glFrustum(-0.1*(1/h), 0.1*(1/h), -0.1, 0.1, 0.1, 10);
+  glMatrixMode(GL_MODELVIEW);
 }
 
 /* draw the screensaver once */
-void draw_atunnels(ModeInfo * mi)
+ENTRYPOINT void draw_atunnel(ModeInfo * mi)
 {
-       atunnelsstruct *sa = &Atunnels[MI_SCREEN(mi)];
+       atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
        Display    *display = MI_DISPLAY(mi);
        Window      window = MI_WINDOW(mi);
 
@@ -259,8 +273,8 @@ void draw_atunnels(ModeInfo * mi)
 
        glLoadIdentity();
 
-       DrawTunnel(do_texture, do_light, texture);
-       SplashScreen(do_wire, do_texture, do_light);
+       atunnel_DrawTunnel(sa->ts, do_texture, do_light, sa->texture);
+       atunnel_SplashScreen(sa->ts, do_wire, do_texture, do_light);
 
        glFlush();  
        /* manage framerate display */
@@ -271,20 +285,17 @@ void draw_atunnels(ModeInfo * mi)
 
 
 /* xscreensaver initialization routine */
-void init_atunnels(ModeInfo * mi)
+ENTRYPOINT void init_atunnel(ModeInfo * mi)
 {
   int screen = MI_SCREEN(mi);
-  atunnelsstruct *sa;
+  atunnelstruct *sa;
 
-  if (Atunnels == NULL) {
-       if ((Atunnels = (atunnelsstruct *) calloc(MI_NUM_SCREENS(mi), sizeof (atunnelsstruct))) == NULL)
-         return;
-  }
-  sa = &Atunnels[screen];
+  MI_INIT(mi, Atunnel);
+  sa = &Atunnel[screen];
 
   sa->window = MI_WINDOW(mi);
   if ((sa->glx_context = init_GL(mi)) != NULL) {
-       reshape_atunnels(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
+       reshape_atunnel(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
        Init(mi);
   } else {
        MI_CLEARWINDOW(mi);
@@ -293,16 +304,14 @@ void init_atunnels(ModeInfo * mi)
 }
 
 /* all sorts of nice cleanup code should go here! */
-void release_atunnels(ModeInfo * mi)
+ENTRYPOINT void free_atunnel(ModeInfo * mi)
 {
-  int screen;
-  if (Atunnels != NULL) {
-       for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-         /*      atunnelsstruct *sa = &Atunnels[screen];*/
-       }
-       (void) free((void *) Atunnels);
-       Atunnels = NULL;
-  }
-  FreeAllGL(mi);
+#if 0
+  atunnelstruct *sa = &Atunnel[MI_SCREEN(mi)];
+  FreeTunnel(sa->ts);
+#endif
 }
+
+XSCREENSAVER_MODULE ("Atunnel", atunnel)
+
 #endif