FragmentActivty, FixedStepMaxFPSEngine, PaletteSwapShader

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

FragmentActivty, FixedStepMaxFPSEngine, PaletteSwapShader

Postby Niffy » Fri Jun 08, 2012 12:32 am

Hello all, last month I've implemented Fragment Activity (compatibility and native support) and a new Engine.

just clone my repo and get the niffy branch
https://github.com/Niffy/AndEngine.git

FixedStepMaxFPSEngine
https://github.com/Niffy/AndEngine/comm ... 7bc43b26be
Want to have a constant update speed but maximum FPS without linking the two? Well now you can with this engine!

When creating a new engine use the following. This will fire 25 updates a seconds, put what you want.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. new FixedStepMaxFPSEngine(pEngineOptions, 25);
  2.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


Then to get the constant update you just register to a different handler.
So create a TimerHandler, like you normally would.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. TimerHandler mTimeHandler = new  TimerHandler(5, new ITimerCallback() {
  2.     @Override
  3.     public void onTimePassed(TimerHandler arg0) {
  4.         //do some stuff
  5.     }
  6. });
  7.  
  8. this.mEngine.registerConstantUpdateHandler(this.mTimeHandler);
  9. this.mEngine.unregisterConstantUpdateHandler(this.mTimeHandler);
  10. this.mEngine.clearConstantUpdateHandlers();
  11.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


You can still use the standard way of updates if you like, but I've implemented this so I can at least get a constant speed across different devices. I've no idea or intention of using this engine with Physics

Fragment Activity
https://github.com/Niffy/AndEngine/comm ... 8a3faaa746
lintfordpickle used Fragments, he posted some code which I've not yet looked at as before hand I published my own on github, his is probably very similar if not the same as Andengine GLES1 had this activity, not GLES2 never got it.
So thanks to lintfordpickle for kick starting this and providing help and details on his game! < Check out his game and thread!

Updated: 18/05/2012
http://www.youtube.com/watch?v=sr9RdwXlf1A
^Example of using LayoutGameFragment

The packages are
org.andengine.ui.activity.fragments.BaseGameFragment
org.andengine.ui.activity.fragments.LayoutGameFragment
org.andengine.ui.activity.fragments.compatibility.BaseGameFragment
org.andengine.ui.activity.fragments.compatibility.LayoutGameFragment

Use the compatibility classes if you're below API Level 11,

I strongly suggest you read the following links, this is where you can learn fragments(That's how I did it!)
http://developer.android.com/sdk/compat ... brary.html
http://developer.android.com/guide/topi ... ments.html

You extend the LayoutGameFragment class and implement the following
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. protected int getLayoutID() {
  2.                 return R.layout.main;
  3.         }
  4.  
  5.         @Override
  6.         protected int getRenderSurfaceViewID() {
  7.                 return R.id.xmllayoutexample_rendersurfaceview;
  8.         }
  9.  
  10.         @Override
  11.         protected void onSetContentView() {
  12.                 super.onSetContentView();
  13.  
  14.         }
  15.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Here is a basic XML layout (res/layout/main.xml)
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:layout_width="match_parent"
  4.    android:layout_height="match_parent"
  5.    android:orientation="vertical" >
  6.  
  7.     <FrameLayout
  8.        android:layout_width="match_parent"
  9.        android:layout_height="0px"
  10.        android:layout_weight="1" >
  11.  
  12.         <!-- <org.andengine.opengl.view.RenderSurfaceView -->
  13.         <!-- <android.opengl.GLSurfaceView -->
  14.  
  15.         <org.andengine.opengl.view.RenderSurfaceView
  16.            android:id="@+id/xmllayoutexample_rendersurfaceview"
  17.            android:layout_width="match_parent"
  18.            android:layout_height="match_parent" />
  19.  
  20. <!-- This is where you then add your other layouts!-->
  21.     </FrameLayout>
  22.  
  23. </LinearLayout>
  24.  
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


Remember to add all your thingy bobs inside the framelayout!
If you want to see how things are looking graphical in the editor, then swap
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. <org.andengine.opengl.view.RenderSurfaceView
  2. with
  3. <android.opengl.GLSurfaceView
  4.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4

Remember to swap back when you want to run the game
This is useful if you'd like to prototype quickly, but useless when using fragments.

When setting engine options use a FillResolutionPolicy, otherwise you might find the GL surface doesn't really fill the screen.

From here on you can start using fragments! (Remember learn how to use them!)

Bugs
Bound to be some,
do post them, but I cannot guarantee to get around to fixing the.
If you do find a solution to one then please post.

Updated: 25/08/2012
Created a new shader to use with andengine, rather simply but useful for some people. A dual palette swap shader, you can swap one colour with another and if you want, swap another colour with another.

https://github.com/Niffy/DualPaletteSwapShader
https://github.com/Niffy/DualPaletteSwapShaderExample

Updated: 26/08/2012
FixedStepMaxFPSEngine updated so you can pause the scene and updates.

Updated: 28/08/2012
AndEngineTMXTiledMapExtension Isometric branch updated. You can now have a map draw origin, might come in handy if you want to stitch maps together in a scene! TMXLayerObjectTitles, ConvertIsometricPixelToScene, TMXLayer now take in to account the map draw origin when converting polypoints, getting tile at touch point.

Updated: 08/10/2012
Quite a bit of AStarPathing work in package org.andengine.util.algorithm.path.astar.tile
You can now stop the AStarPathTileModifier, it doesn't stop all together, it stops after the current subsequence has finished. (to stop it all together just unregister like before). This makes it easy for path finding, since path finding is tile based and so is movement(centre of tile to centre of tile)

New modifier which can move an entity from A to C with the Z index changing at B! Useful for isometric movement...

Engine, FixedStepMaxFPSEngine, Entity can now handle a time modifier, so you can increase the update speed of entities and constant update handlers, but it would be easy to include the normal update handlers into this increase. This will enable you increase the updates, ie implement a speed up and slow down game functions.

So on each update we can multiply by the time modifier to increase the seconds elapsed. You can call on the engine.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. setTimeModifer(final int pTimeModifier)
  2. int getTimeModifier(); //get the time modifier (this is also for implementing ITimeModifiedUpdater)
  3. increaseTimeModifier() //Increase by 1
  4. decreaseTimeModifier() //decrease by 1, lowest it can go is 1
  5. resetTimeModifier() //set back to 1
  6.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4

On the entity you can call
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. registerForTimeModifier(boolean pRegisterForTimeModifier) //call this on things such as sprites
  2. setTimeModifedUpdater(ITimeModifiedUpdater pTimeModifiedUpdater) //Just pass the engine you are using since the engine implements the required interface
  3. boolean isRegisteredForTimeModifier() //Is the entity registered for a time modified update?
  4.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Never register a scene for a time modified update(since it is derived from Entity), so never call scene.registerForTimeModifier(true); Although if you know what you're attaching to your scene then you can do this so all its children get update quicker. The reason I this is because I found that applying the time modifier on updating the scene caused a decrease in FPS, I assume its on the runnable's attached to the scene and/or large spritebatches (like a TMXLayer)

But you do have to call setTimeModifedUpdater on the scene and pass the engine
So when you attach (or before) a sprite to a scene, say an animated sprite, just remember to call registerForTimeModifier(true), then any modifiers you apply will automatically increase.

Updated: 18/03/2013
You can have an isometric world now!
Simply grab my branches (they have been updated)
Grab the example
https://github.com/Niffy/IsometricWorldExample
https://github.com/Niffy/IsometricWorldExample/wiki
Look at the wiki and the code to understand what s going on.
Oh and there it uses fragments!!! - The wiki does not explain fragments as this is a standard Android feature.
Last edited by Niffy on Mon Mar 18, 2013 4:54 pm, edited 7 times in total.
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby OzLark » Fri Jun 08, 2012 5:43 am

Niffy wrote:FixedStepMaxFPSEngine
https://github.com/Niffy/AndEngine/comm ... 7bc43b26be
Want to have a constant update speed but maximum FPS without linking the two? Well now you can with this engine!


Probably a silly question and I should probably just inspect your code and compare, but what difference will this make over a FixedStepEngine? I though that a fixed step engine is inherently a max fps engine also as it will always run at the step rate and therefore I assumed will never go faster than it's fixed step rate..

I think I'm showing that I don't understand what the different engine types really do :)
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby Niffy » Fri Jun 08, 2012 1:35 pm

The FixedStepEngine will try and run its speed up to what you set. e.g 25 steps = the engine attempting 25FPS
Using a FixedStepEngine means most devices are being held back in FPS simply because we want consistency.

So with my engine we try and get the max FPS but have a consistent update speed. So across many devices devices I should get a consistent update speed, and graphics should perform as best as they can.
This means I could have a fixed step engine of 1! Meaning I get 1 update a second, rather than have 25 updates a second, do I really need 25 updates a second for my data if its a sim and all its doing it ticking an in game clock?

I've not done much testing, but its based on the following link which is a good read.

http://www.koonsolo.com/news/dewitters-gameloop/
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby OzLark » Sat Jun 09, 2012 6:38 am

That's a really good article niffy, thanks.

I'll give your FixedStepMaxFPSEngine a go!
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby OzLark » Fri Jun 15, 2012 3:28 am

Hi Niffy,

Although you stated explicitly that you do not intend to use this with physics, could you possibly describe how you would set this up to render my scene at 30fps but step my physics world at 60fps?

I'm being lazy here, I could try things out my self and work it out, but seeing that you're familiar with this engine configuration I figured you might have a quick and simple answer.

Cheers,
Lark
Did this post help you? Feel free to return the gesture by downloading Ninja Bees, creating a level (or more) and sharing it to the Ninja Bees website!

Ninja Bees - Our latest Android game, check it out on Google Play (free) or paid with no ads
OzLark
 
Posts: 1130
Joined: Sun Feb 05, 2012 3:19 am

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby Niffy » Fri Jun 15, 2012 4:42 pm

Hmmm I'll have a think about it.
This week I've spent most of my time (physically) cleaning my computer, cleaning my hard drives and backing up and doing a clean install of windows. So Currently I don't have much installed!

I've never used physics so I've had a look at the source on github.

If you want physics at 60 updates a second and max FPS just register the physics world using registerConstantUpdateHandler. from what I can see, the engine does not treat the physics world any different to anything else which requires updating, it just plonks the world into a list of objects which require updating.

If you want your physics to run at a fixed rate of 60 fps a second and fixed FPS at 30fps.
I would think something along the lines of this. No idea if it compiles as I've got no tools installed!

So you would pass in (EngineOptions, 60, 30)
you would register your physics world to the engine using registerConstantUpdateHandler.

Again, I've never used physics and no idea even if my original idea runs well over a distance.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package org.andengine.engine;
  2.  
  3. import org.andengine.engine.handler.IUpdateHandler;
  4. import org.andengine.engine.handler.UpdateHandlerList;
  5. import org.andengine.engine.options.EngineOptions;
  6. import org.andengine.util.time.TimeConstants;
  7.  
  8. import android.R.integer;
  9.  
  10. /**
  11.  * A subclass of {@link Engine} that is derived from {@link FixedStepEngine} and {@link FixedStepMaxFPSEngine}
  12.  * It will try and update a list of {@link IUpdateHandler} objects so many times
  13.  * a second as passed into the constructor.  It will also try and have a fixed rate of FPS.
  14.  * <br> For example you may want to run FPS at 30FPS and have constant updates of 60 updates a second.
  15.  * <br><b>Usage:</b>
  16.  * <br>To make use of this constant game speed just
  17.  * <br><i>register</i> an {@link IUpdateHandler} using {@link #registerConstantUpdateHandler(IUpdateHandler)}
  18.  * <br> <i>unregister</i> using {@link #unregisterConstantUpdateHandler(IUpdateHandler)}
  19.  * <br> <i>clear</i> using {@link #clearConstantUpdateHandlers()}
  20.  *
  21.  * @see <a href="http://www.koonsolo.com/news/dewitters-gameloop/">Koen Witters - deWitters Game Loop</a>
  22.  * @author Paul Robinson
  23.  *
  24.  */
  25. public class FixedStepFixedFPSEngine extends Engine {
  26.  
  27.         // ===========================================================
  28.         // Constants
  29.         // ===========================================================
  30.  
  31.         // ===========================================================
  32.         // Fields
  33.         // ===========================================================
  34.  
  35.         private final long mStepLength; //For fixed updates
  36.         private final long mStepLength_FPS; //For fixed FPS
  37.         private long mSecondsElapsedAccumulator; //For fixed updates
  38.         private long mSecondsElapsedAccumulator_FPS; //For fixed FPS
  39.         private final UpdateHandlerList mConstantUpdateHandlers = new UpdateHandlerList(Engine.UPDATEHANDLERS_CAPACITY_DEFAULT);
  40.  
  41.         // ===========================================================
  42.         // Constructors
  43.         // ===========================================================
  44.  
  45.         /**
  46.          * Create a new Fixed Step Fixed FPS engine.
  47.          * @param pEngineOptions {@link EngineOptions} Engine options to use.
  48.          * @param pStepsPerSecond {@link integer} How many updates a second? e.g the rate of constant updates.
  49.          * @param pStepsPerSecond_FPS {@link integer} How many updates a second for FPS? e.g the frame rate you want.
  50.          */
  51.         public FixedStepFixedFPSEngine(EngineOptions pEngineOptions, final int pStepsPerSecond, final int pStepsPerSecond_FPS) {
  52.                 super(pEngineOptions);
  53.                 this.mStepLength = TimeConstants.NANOSECONDS_PER_SECOND / pStepsPerSecond;
  54.                 this.mStepLength_FPS = TimeConstants.NANOSECONDS_PER_SECOND / pStepsPerSecond_FPS;
  55.         }
  56.  
  57.         // ===========================================================
  58.         // Getter & Setter
  59.         // ===========================================================
  60.  
  61.         // ===========================================================
  62.         // Methods for/from SuperClass/Interfaces
  63.         // ===========================================================
  64.  
  65.         @Override
  66.         public void onUpdate(final long pNanosecondsElapsed) throws InterruptedException {
  67.                 this.mSecondsElapsedAccumulator += pNanosecondsElapsed;
  68.                 final long stepLength = this.mStepLength;
  69.                 while(this.mSecondsElapsedAccumulator >= stepLength) {
  70.                         final float pSecondsElapsed = stepLength * TimeConstants.SECONDS_PER_NANOSECOND;
  71.                         this.onConstantUpdateUpdateHandlers(pSecondsElapsed);
  72.                         this.mSecondsElapsedAccumulator -= stepLength;
  73.                 }
  74.                 //This is for Fixed FPS
  75.                 this.mSecondsElapsedAccumulator_FPS += pNanosecondsElapsed;
  76.                 final long stepLength_FPS = this.mStepLength_FPS
  77.                 while(this.mSecondsElapsedAccumulator_FPS >= stepLength_FPS) {
  78.                         super.onUpdate(stepLength_FPS);
  79.                         this.mSecondsElapsedAccumulator_FPS -= stepLength_FPS;
  80.                 }
  81.         }
  82.  
  83.         protected void onConstantUpdateUpdateHandlers(final float pSecondsElapsed) {
  84.                 this.mConstantUpdateHandlers.onUpdate(pSecondsElapsed);
  85.         }
  86.  
  87.         /**
  88.          * Register an {@link IUpdateHandler} to be updated using our constant game speed.
  89.          * @param pUpdateHandler {@link IUpdateHandler} to update.
  90.          * @see FixedStepMaxFPSEngine
  91.          */
  92.         public void registerConstantUpdateHandler(final IUpdateHandler pUpdateHandler) {
  93.                 this.mConstantUpdateHandlers.add(pUpdateHandler);
  94.         }
  95.  
  96.         /**
  97.          * Unregister a {@link IUpdateHandler} from being updated using our constant game speed.
  98.          * @param pUpdateHandler {@link IUpdateHandler} to unregister.
  99.          */
  100.         public void unregisterConstantUpdateHandler(final IUpdateHandler pUpdateHandler) {
  101.                 this.mConstantUpdateHandlers.remove(pUpdateHandler);
  102.         }
  103.  
  104.         /**
  105.          * Clear all {@link IUpdateHandler} registered for constant game speed updates.
  106.          */
  107.         public void clearConstantUpdateHandlers() {
  108.                 this.mConstantUpdateHandlers.clear();
  109.         }
  110.  
  111.         // ===========================================================
  112.         // Methods
  113.         // ===========================================================
  114.  
  115.         // ===========================================================
  116.         // Inner and Anonymous Classes
  117.         // ===========================================================
  118. }
  119.  
Parsed in 0.049 seconds, using GeSHi 1.0.8.4
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby Niffy » Mon Jun 18, 2012 9:06 pm

http://www.youtube.com/watch?v=sr9RdwXlf1A

You can see me using LayoutGameFragment above!!
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby nazgee » Wed Jun 20, 2012 11:51 am

Looks good, though I would not despair if here would be some kind of an example available for peeking ;]
Dirt Rider Mayhem is PUBLISHED now!
Image
User avatar
nazgee
 
Posts: 527
Joined: Fri Oct 21, 2011 10:31 pm
Location: Poland, Wrocław

Re: FragmentActivty And FixedStepMaxFPSEngine

Postby Niffy » Wed Jun 20, 2012 5:04 pm

I've not got time to produce an example sadly.
http://developer.android.com/guide/topi ... ments.html
I learnt how to use fragments here, there are probably more fragments tutorials about.

But I provided at the top some XML code and a what is required to use fragments above the surface view. You then just set out your layout(which can take some time) and implement your fragment (Again android docs shows this)
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Re: FragmentActivty, FixedStepMaxFPSEngine, PaletteSwapShade

Postby Niffy » Sat Aug 25, 2012 10:18 pm

Now have a dual palette swap up with example.
Niffy
 
Posts: 284
Joined: Sat Sep 17, 2011 8:39 pm

Next

Return to Features

Who is online

Users browsing this forum: No registered users and 4 guests