Sequential Parallax Background

  ... the case you feel the need for a new feature or want to submit one.

Sequential Parallax Background

Postby darkshot » Sun Feb 19, 2012 8:14 am

The game I am designing required a much longer background than is possible in AndEngine (as far as I know), so I modified ParallaxBackground to be able to display a list of images "end to end" in each parallax layer.

This allows you to be able to seam together multiple background images for variation or scenery changes. Currently there are no transitions and the list of images cannot be changed after creation, but it works well enough for the purposes of my own game.

This can be used in the same way as AndEngine's ParallaxBackground, and anything that extends it can easily extend this as well. I have no idea if anyone else will find this useful but I decided to put it up here anyway.

First is the code of interest, and lastly I will attach the full class code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static class ParallaxEntities implements IParallaxEntity {
  2.         final float mParallaxFactor;
  3.         final ArrayList<IAreaShape> mAreaShapes;
  4.  
  5.         public ParallaxEntities(final float pParallaxFactor, final ArrayList<IAreaShape> pAreaShapes) {
  6.                 mParallaxFactor = pParallaxFactor;
  7.                 this.mAreaShapes = pAreaShapes;
  8.         }
  9.  
  10.         public void onDraw(final GLState pGLState, final Camera pCamera, final float pParallaxValue) {
  11.                 if (mAreaShapes.size() > 0) {
  12.                         pGLState.pushModelViewGLMatrix();
  13.                         {
  14.                                 int index = 0;
  15.                                 final float cameraWidth = pCamera.getWidth();
  16.  
  17.                                 float shapeWidthScaled = 0;
  18.  
  19.                                 for (IAreaShape shape : mAreaShapes)
  20.                                         shapeWidthScaled += shape.getWidthScaled();
  21.  
  22.                                 float baseOffset = (pParallaxValue * mParallaxFactor)   % shapeWidthScaled;
  23.  
  24.                                 shapeWidthScaled = mAreaShapes.get(index).getWidthScaled();
  25.  
  26.                                 while (baseOffset > 0)
  27.                                         baseOffset -= shapeWidthScaled;
  28.  
  29.                                 pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);
  30.  
  31.                                 float currentMaxX = baseOffset;
  32.  
  33.                                 do {
  34.                                         mAreaShapes.get(index).onDraw(pGLState, pCamera);
  35.                                         pGLState.translateModelViewGLMatrixf(shapeWidthScaled,  0, 0);
  36.                                         currentMaxX += shapeWidthScaled;
  37.  
  38.                                         index = (index + 1) % mAreaShapes.size();
  39.                                         shapeWidthScaled = mAreaShapes.get(index).getWidthScaled();
  40.                                 } while (currentMaxX < cameraWidth);
  41.                         }
  42.                         pGLState.popModelViewGLMatrix();
  43.                 }
  44.         }
  45. }
  46.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Full Class:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.Parallax;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. import org.andengine.engine.camera.Camera;
  6. import org.andengine.entity.scene.background.Background;
  7. import org.andengine.entity.shape.IAreaShape;
  8. import org.andengine.opengl.util.GLState;
  9.  
  10. /**
  11.  * Original code for ParallaxBackground class from AndEngine written by: (c)
  12.  * 2010 Nicolas Gramlich (c) 2011 Zynga Inc.
  13.  *
  14.  * @author Nicolas Gramlich
  15.  * @since 15:36:26 - 19.07.2010
  16.  *
  17.  *        This version has been modified to use the generic interface
  18.  *        IParallaxEntity instead of ParallaxEntity.
  19.  */
  20. public class SequentialParallaxBackground extends Background {
  21.  
  22.         private final ArrayList<IParallaxEntity> mParallaxEntities = new ArrayList<IParallaxEntity>();
  23.         private int mParallaxEntityCount;
  24.  
  25.         protected float mParallaxValue;
  26.  
  27.         public SequentialParallaxBackground(final float pRed, final float pGreen,
  28.                         final float pBlue) {
  29.                 super(pRed, pGreen, pBlue);
  30.         }
  31.  
  32.         public void setParallaxValue(final float pParallaxValue) {
  33.                 this.mParallaxValue = pParallaxValue;
  34.         }
  35.  
  36.         @Override
  37.         public void onDraw(final GLState pGLState, final Camera pCamera) {
  38.                 super.onDraw(pGLState, pCamera);
  39.  
  40.                 final float parallaxValue = this.mParallaxValue;
  41.                 final ArrayList<IParallaxEntity> parallaxEntities = this.mParallaxEntities;
  42.  
  43.                 for (int i = 0; i < this.mParallaxEntityCount; i++) {
  44.                         parallaxEntities.get(i).onDraw(pGLState, pCamera, parallaxValue);
  45.                 }
  46.         }
  47.  
  48.         public void attachParallaxEntity(final IParallaxEntity pParallaxEntity) {
  49.                 this.mParallaxEntities.add(pParallaxEntity);
  50.                 this.mParallaxEntityCount++;
  51.         }
  52.  
  53.         public boolean detachParallaxEntity(final IParallaxEntity pParallaxEntity) {
  54.                 this.mParallaxEntityCount--;
  55.                 final boolean success = this.mParallaxEntities.remove(pParallaxEntity);
  56.                 if (!success)
  57.                         this.mParallaxEntityCount++;
  58.                 return success;
  59.         }
  60.  
  61.         /*
  62.          * Interface for ParallaxEntity objects. Only requires onDraw method.
  63.          */
  64.         public static interface IParallaxEntity {
  65.                 public void onDraw(final GLState pGLState, final Camera pCamera,
  66.                                 final float pParallaxValue);
  67.         }
  68.  
  69.         /*
  70.          * ParallaxEntity code copied from AndEngine, written by Nicolas Gramlich.
  71.          * Modified to implement IParallaxEntity (functionality intact)
  72.          */
  73.         public static class ParallaxEntity implements IParallaxEntity {
  74.  
  75.                 final float mParallaxFactor;
  76.                 final IAreaShape mAreaShape;
  77.  
  78.                 public ParallaxEntity(final float pParallaxFactor,
  79.                                 final IAreaShape pAreaShape) {
  80.                         this.mParallaxFactor = pParallaxFactor;
  81.                         this.mAreaShape = pAreaShape;
  82.                 }
  83.  
  84.                 public void onDraw(final GLState pGLState, final Camera pCamera,
  85.                                 final float pParallaxValue) {
  86.                         pGLState.pushModelViewGLMatrix();
  87.                         {
  88.                                 final float cameraWidth = pCamera.getWidth();
  89.                                 final float shapeWidthScaled = this.mAreaShape.getWidthScaled();
  90.                                 float baseOffset = (pParallaxValue * this.mParallaxFactor)
  91.                                                 % shapeWidthScaled;
  92.  
  93.                                 while (baseOffset > 0) {
  94.                                         baseOffset -= shapeWidthScaled;
  95.                                 }
  96.                                 pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);
  97.  
  98.                                 float currentMaxX = baseOffset;
  99.  
  100.                                 do {
  101.                                         this.mAreaShape.onDraw(pGLState, pCamera);
  102.                                         pGLState.translateModelViewGLMatrixf(shapeWidthScaled, 0, 0);
  103.                                         currentMaxX += shapeWidthScaled;
  104.                                 } while (currentMaxX < cameraWidth);
  105.                         }
  106.                         pGLState.popModelViewGLMatrix();
  107.                 }
  108.         }
  109.  
  110.         /*
  111.          * ParallaxEntities is a modified version of ParallaxEntity. Contains an
  112.          * array of IAreaShape objects rather than one IAreaShape.
  113.          *
  114.          * Draws each object sequentially "end to end" to form a chain of different
  115.          * images in one parallax layer.
  116.          */
  117.         public static class ParallaxEntities implements IParallaxEntity {
  118.                 final float mParallaxFactor;
  119.                 final ArrayList<IAreaShape> mAreaShapes;
  120.  
  121.                 public ParallaxEntities(final float pParallaxFactor,
  122.                                 final ArrayList<IAreaShape> pAreaShapes) {
  123.                         mParallaxFactor = pParallaxFactor;
  124.                         this.mAreaShapes = pAreaShapes;
  125.                 }
  126.  
  127.                 public void onDraw(final GLState pGLState, final Camera pCamera,
  128.                                 final float pParallaxValue) {
  129.                         if (mAreaShapes.size() > 0) {
  130.                                 pGLState.pushModelViewGLMatrix();
  131.                                 {
  132.                                         int index = 0;
  133.                                         final float cameraWidth = pCamera.getWidth();
  134.  
  135.                                         float shapeWidthScaled = 0;
  136.  
  137.                                         for (IAreaShape shape : mAreaShapes)
  138.                                                 shapeWidthScaled += shape.getWidthScaled();
  139.  
  140.                                         float baseOffset = (pParallaxValue * mParallaxFactor)
  141.                                                         % shapeWidthScaled;
  142.  
  143.                                         shapeWidthScaled = mAreaShapes.get(index).getWidthScaled();
  144.  
  145.                                         while (baseOffset > 0)
  146.                                                 baseOffset -= shapeWidthScaled;
  147.  
  148.                                         pGLState.translateModelViewGLMatrixf(baseOffset, 0, 0);
  149.  
  150.                                         float currentMaxX = baseOffset;
  151.  
  152.                                         do {
  153.                                                 mAreaShapes.get(index).onDraw(pGLState, pCamera);
  154.                                                 pGLState.translateModelViewGLMatrixf(shapeWidthScaled,
  155.                                                                 0, 0);
  156.                                                 currentMaxX += shapeWidthScaled;
  157.  
  158.                                                 index = (index + 1) % mAreaShapes.size();
  159.                                                 shapeWidthScaled = mAreaShapes.get(index)
  160.                                                                 .getWidthScaled();
  161.                                         } while (currentMaxX < cameraWidth);
  162.                                 }
  163.                                 pGLState.popModelViewGLMatrix();
  164.                         }
  165.                 }
  166.         }
  167. }
Parsed in 0.016 seconds, using GeSHi 1.0.8.4
darkshot
 
Posts: 5
Joined: Wed Feb 15, 2012 7:32 pm

Re: Sequential Parallax Background

Postby chigas » Fri Mar 09, 2012 8:52 pm

Hi! I believe this is what I'm looking for, but where can I get those imports? I do not have that library. I'm using org.anddev.andengine..
chigas
 
Posts: 1
Joined: Wed Feb 01, 2012 2:33 pm

Re: Sequential Parallax Background

Postby darkshot » Fri Mar 09, 2012 8:59 pm

I checked out AndEngine using Git and org.andengine is the import that is available to me. I think if you are using the binary jar AndEngine it uses org.anddev.andegine instead.

I'm not an expert on this or anything but I would just try replacing org.andengine with org.anddev.andengine in your case.
darkshot
 
Posts: 5
Joined: Wed Feb 15, 2012 7:32 pm

Re: Sequential Parallax Background

Postby Scorpic » Mon Jul 30, 2012 6:52 am

Thanks so much for posting this it worked perfectly, I combined your revision of the ParallaxBackground with Jaym's ParallaxLayer which made it even easier lol.
Scorpic
 
Posts: 10
Joined: Sat Jul 28, 2012 6:55 am

Re: Sequential Parallax Background

Postby shrikpl » Thu Feb 14, 2013 8:18 pm

Thanks for the post. I also did the similar thing mentioned by scorpic. Here is the complete example code: http://www.sourcetricks.com/index.php/c ... tor-scene/
shrikpl
 
Posts: 2
Joined: Thu Feb 14, 2013 8:14 pm


Return to Features

Who is online

Users browsing this forum: No registered users and 6 guests