From http://www.jwz.org/xscreensaver/xscreensaver-5.39.tar.gz
[xscreensaver] / hacks / glx / glplanet.c
index 20ccc5841f5a16db4e3469eff00bdd5a8e779f37..c30b306ede1f0168f28b4cbb5a9c375e200934cb 100644 (file)
@@ -38,7 +38,7 @@
                                        "*imageBackground:      Blue    \n" \
                                        "*suppressRotationAnimation: True\n" \
 
-# define refresh_planet 0
+# define release_planet 0
 # include "xlockmore.h"                                /* from the xscreensaver distribution */
 #else  /* !STANDALONE */
 # include "xlock.h"                                    /* from the xlockmore distribution */
@@ -113,21 +113,16 @@ ENTRYPOINT ModeSpecOpt planet_opts = {countof(opts), opts, countof(vars), vars,
 
 #ifdef USE_MODULES
 ModStruct   planet_description =
-{"planet", "init_planet", "draw_planet", "release_planet",
- "draw_planet", "init_planet", NULL, &planet_opts,
+{"planet", "init_planet", "draw_planet", NULL,
+ "draw_planet", "init_planet", "free_planet", &planet_opts,
  1000, 1, 2, 1, 4, 1.0, "",
  "Animates texture mapped sphere (planet)", 0, NULL};
 #endif
 
-# 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/earth.xpm"
-#include "../images/earth_night.xpm"
+#include "images/gen/earth_png.h"
+#include "images/gen/earth_night_png.h"
 
-#include "xpm-ximage.h"
+#include "ximage-loader.h"
 #include "rotator.h"
 #include "gltrackball.h"
 
@@ -165,10 +160,10 @@ static planetstruct *planets = NULL;
 
 /* Set up and enable texturing on our object */
 static void
-setup_xpm_texture (ModeInfo *mi, char **xpm_data)
+setup_xpm_texture (ModeInfo *mi, const unsigned char *data, unsigned long size)
 {
-  XImage *image = xpm_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
-                                  MI_COLORMAP (mi), xpm_data);
+  XImage *image = image_data_to_ximage (MI_DISPLAY (mi), MI_VISUAL (mi),
+                                        data, size);
   char buf[1024];
   clear_gl_error();
   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
@@ -177,10 +172,7 @@ setup_xpm_texture (ModeInfo *mi, char **xpm_data)
   */
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                image->width, image->height, 0,
-               GL_RGBA,
-               /* GL_UNSIGNED_BYTE, */
-               GL_UNSIGNED_INT_8_8_8_8_REV,
-               image->data);
+               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
   sprintf (buf, "builtin texture (%dx%d)", image->width, image->height);
   check_gl_error(buf);
 }
@@ -193,8 +185,7 @@ setup_file_texture (ModeInfo *mi, char *filename)
   Visual *visual = mi->xgwa.visual;
   char buf[1024];
 
-  Colormap cmap = mi->xgwa.colormap;
-  XImage *image = xpm_file_to_ximage (dpy, visual, cmap, filename);
+  XImage *image = file_to_ximage (dpy, visual, filename);
   if (!image) return False;
 
   clear_gl_error();
@@ -202,10 +193,7 @@ setup_file_texture (ModeInfo *mi, char *filename)
   glPixelStorei(GL_UNPACK_ROW_LENGTH, image->width);
   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
                image->width, image->height, 0,
-               GL_RGBA,
-               /* GL_UNSIGNED_BYTE, */
-               GL_UNSIGNED_INT_8_8_8_8_REV,
-               image->data);
+               GL_RGBA, GL_UNSIGNED_BYTE, image->data);
   sprintf (buf, "texture: %.100s (%dx%d)",
            filename, image->width, image->height);
   check_gl_error(buf);
@@ -233,7 +221,7 @@ setup_texture(ModeInfo * mi)
          !strcmp(which_image, "BUILTIN"))
     {
     BUILTIN1:
-      setup_xpm_texture (mi, earth_xpm);
+      setup_xpm_texture (mi, earth_png, sizeof(earth_png));
     }
   else
     {
@@ -258,7 +246,7 @@ setup_texture(ModeInfo * mi)
          !strcmp(which_image2, "BUILTIN"))
     {
     BUILTIN2:
-      setup_xpm_texture (mi, earth_night_xpm);
+      setup_xpm_texture (mi, earth_night_png, sizeof(earth_night_png));
     }
   else
     {
@@ -287,12 +275,18 @@ init_stars (ModeInfo *mi)
   int max_size = 3;
   GLfloat inc = 0.5;
   int steps = max_size / inc;
+  GLfloat scale = 1;
+
+  if (MI_WIDTH(mi) > 2560) {  /* Retina displays */
+    scale *= 2;
+    nstars /= 2;
+  }
 
   gp->starlist = glGenLists(1);
   glNewList(gp->starlist, GL_COMPILE);
   for (j = 1; j <= steps; j++)
     {
-      glPointSize(inc * j);
+      glPointSize(inc * j * scale);
       glBegin (GL_POINTS);
       for (i = 0; i < nstars / steps; i++)
         {
@@ -325,12 +319,15 @@ init_stars (ModeInfo *mi)
 ENTRYPOINT void
 reshape_planet (ModeInfo *mi, int width, int height)
 {
+  planetstruct *gp = &planets[MI_SCREEN(mi)];
   GLfloat h = (GLfloat) height / (GLfloat) width;
 
+  glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
+
   glViewport(0, 0, (GLint) width, (GLint) height);
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
-  glFrustum(-1.0, 1.0, -h, h, 5.0, 100.0);
+  glFrustum(-1.0, 1.0, -h, h, 5.0, 200.0);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
   glTranslatef(0.0, 0.0, -40);
@@ -368,13 +365,11 @@ init_planet (ModeInfo * mi)
   int screen = MI_SCREEN(mi);
   Bool wire = MI_IS_WIREFRAME(mi);
 
-  if (planets == NULL) {
-       if ((planets = (planetstruct *) calloc(MI_NUM_SCREENS(mi),
-                                                                                 sizeof (planetstruct))) == NULL)
-         return;
-  }
+  MI_INIT (mi, planets);
   gp = &planets[screen];
 
+  gp->window = MI_WINDOW(mi);
+
   if ((gp->glx_context = init_GL(mi)) != NULL) {
        reshape_planet(mi, MI_WIDTH(mi), MI_HEIGHT(mi));
   }
@@ -634,29 +629,18 @@ draw_planet (ModeInfo * mi)
 
 
 ENTRYPOINT void
-release_planet (ModeInfo * mi)
+free_planet (ModeInfo * mi)
 {
-  if (planets != NULL) {
-       int screen;
-
-       for (screen = 0; screen < MI_NUM_SCREENS(mi); screen++) {
-         planetstruct *gp = &planets[screen];
-
-         if (gp->glx_context) {
-               /* Display lists MUST be freed while their glXContext is current. */
-        /* but this gets a BadMatch error. -jwz */
-               /*glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));*/
-
-               if (glIsList(gp->platelist))
-                 glDeleteLists(gp->platelist, 1);
-               if (glIsList(gp->starlist))
-                 glDeleteLists(gp->starlist, 1);
-         }
-       }
-       (void) free((void *) planets);
-       planets = NULL;
+  planetstruct *gp = &planets[MI_SCREEN(mi)];
+
+  if (gp->glx_context) {
+       glXMakeCurrent(MI_DISPLAY(mi), gp->window, *(gp->glx_context));
+
+       if (glIsList(gp->platelist))
+         glDeleteLists(gp->platelist, 1);
+       if (glIsList(gp->starlist))
+         glDeleteLists(gp->starlist, 1);
   }
-  FreeAllGL(mi);
 }