Slick Forums

Discuss the Slick 2D Library
It is currently Thu Oct 18, 2018 5:29 am

All times are UTC




Post new topic Reply to topic  [ 9 posts ] 
Author Message
PostPosted: Sun Jul 24, 2011 12:27 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Inspired by this Thread: http://slick.javaunlimited.net/viewtopic.php?t=3493 I started thinking that sorting sprites via Collections sort-method is way to slow.
I thought: "Hey, OpenGL provides 2 extra axis we can use, can't we use them?"

Well yes, we can. The Depth Buffer can be used to make your sprites autosort along the y-axis (and more). So I made a class which is basically a Image, which copies some methods and adds a boolean telling me if the Depthbuffer is in use.

Code:
package radicalDude;
import java.io.InputStream;

import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.opengl.ImageData;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.renderer.SGL;

/**
 * An {@link Image} which can be used for sprite-sorting without much effort, using the depth buffer. When drawing just
 * add a z value (keep in mind that the z-buffer Slick uses has a range of -1 to 1) and the {@link Image} will be drawn
 * to the depth. The most common way is to say z = y (this class uses z = y / 1000 to fit Slick), which will overlap
 * images along there y-axis.
 *
 * Also, keep mind that Slick starts the Display with no depth test! So use the startDepthUse() and endDepthUse()
 * methods to activate the depth test. This is also a good way to separate non-depth images and depth images.
 * <p></p>
 * =================================================================== <br>
 *
 * Version Status: <br>
 *    - Simple drawing is possible use drawS methods <br>
 *  - enable startDepthUse and endDepthUse working <br>
 *  - will add more methods on demand.
 *
 * @author Stefan Lange aka R.D.
 * @version 0.5.0
 * @since 18.07.2011
 */
public class SortedImage extends Image {
   
   // tells us if the Image is currently in  depth mode (might make a static value
   // since we draw more then one image inside the start/end depth methods)
   private boolean deptTestinUse;
   
   // CONSTRUCTORS
   // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   /**
    * Create an image from a image data source. Note that this method uses
    *
    * @param data
    *            The pixelData to use to create the image
    * @param f
    *            The filter to use when scaling this image
    */
   public SortedImage(ImageData data, int f) {
      super(data, f);
   }
   /**
    * Create an image from a image data source
    *
    * @param data
    *            The pixelData to use to create the image
    */
   public SortedImage(ImageData data) {
      super(data);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param in
    *            The input stream to read the image from
    * @param ref
    *            The name that should be assigned to the image
    * @param flipped
    *            True if the image should be flipped on the y-axis on load
    * @param filter
    *            The filter to use when scaling this image
    * @throws SlickException
    *             Indicates a failure to load the image
    */
   public SortedImage(InputStream in, String ref, boolean flipped, int filter) throws SlickException {
      super(in, ref, flipped, filter);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param in The input stream to read the image from
    * @param ref The name that should be assigned to the image
    * @param flipped True if the image should be flipped on the y-axis  on load
    * @throws SlickException Indicates a failure to load the image
    */
   public SortedImage(InputStream in, String ref, boolean flipped) throws SlickException {
      super(in, ref, flipped, FILTER_LINEAR);
   }
   /**
    * Create an empty image
    *
    * @param width The width of the image
    * @param height The height of the image
    * @param f The filter to apply to scaling the new image
    * @throws SlickException Indicates a failure to create the underlying resource
    */
   public SortedImage(int width, int height, int f) throws SlickException {
      super(width, height, f);
   }
   /**
    * Create an empty image
    *
    * @param width The width of the image
    * @param height The height of the image
    * @throws SlickException Indicates a failure to create the underlying resource
    */
   public SortedImage(int width, int height) throws SlickException {
      super(width, height, FILTER_NEAREST);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param ref The location of the image file to load
    * @param flipped True if the image should be flipped on the y-axis on load
    * @param f The filtering method to use when scaling this image
    * @param transparent The color to treat as transparent
    * @throws SlickException Indicates a failure to load the image
    */
   public SortedImage(String ref, boolean flipped, int f, Color transparent) throws SlickException {
      super(ref, flipped, f, transparent);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param ref The location of the image file to load
    * @param flipped True if the image should be flipped on the y-axis on load
    * @param filter The filtering method to use when scaling this image
    * @throws SlickException Indicates a failure to load the image
    */
   public SortedImage(String ref, boolean flipped, int filter) throws SlickException {
      super(ref, flipped, filter, null);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param ref The location of the image file to load
    * @param flipped True if the image should be flipped on the y-axis on load
    * @throws SlickException Indicates a failure to load the image
    */
   public SortedImage(String ref, boolean flipped) throws SlickException {
      super(ref, flipped, FILTER_LINEAR, null);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param ref The location of the image file to load
    * @param trans The color to be treated as transparent
    * @throws SlickException Indicates a failure to load the image
    */
   public SortedImage(String ref, Color trans) throws SlickException {
      super(ref, false, FILTER_LINEAR, trans);
   }
   /**
    * Create an image based on a file at the specified location
    *
    * @param ref
    *            The location of the image file to load
    * @throws SlickException
    *             Indicates a failure to load the image
    */
   public SortedImage(String ref) throws SlickException {
      super(ref, false, FILTER_LINEAR, null);
   }
   /**
    * Creates an image using the specified texture
    *
    * @param texture
    *            The texture to use
    */
   public SortedImage(Texture texture) {
      super(texture);
   }
   
   // METHODS
   // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   /**
    * Draws this Image to a specific depth at x, y.
    */
   public void drawS(float x, float y, float depth) {
      drawS(x, y, width, height, depth, Color.white);
   }
   /**
    * Draws this Image to a specific depth at x, y and with a specific size.
    */
   public void drawS(float x, float y, float width, float height, float depth) {
      drawS(x, y, width, height, depth, Color.white);
   }
   /**
    * Draws this Image to a specific depth at x, y and with a specific size and a filter to use.
    */
   public void drawS(float x, float y, float width, float height, float depth, Color filter) {
      if (alpha != 1) {
         if (filter == null) {
            filter = Color.white;
         }
         
         filter = new Color(filter);
         filter.a *= alpha;
      }
      if (filter != null) {
         filter.bind();
      }
      
      texture.bind();
      GL.glTranslatef(x, y, -depth / 1000);
      
      GL.glBegin(SGL.GL_QUADS);
         drawEmbedded(0, 0, width, height);
      GL.glEnd();
      GL.glTranslatef(-x, -y, depth / 1000);
   }
   
   // SETTER & GETTER
   // ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
   /**
    * Starts the depth buffer.
    */
   public void startDepthUse() {
      if (deptTestinUse)
         return;
      deptTestinUse = true;
      
      // SGL in SVN provides Depth test but no LEQUAL? why?
      GL.glEnable(GL11.GL_DEPTH_TEST);
      GL.glEnable(GL11.GL_ALPHA_TEST);
      GL11.glAlphaFunc(GL11.GL_GEQUAL, 1f);
      GL.glDepthFunc(GL11.GL_LEQUAL);
   }
   /**
    * ends the depth buffer.
    */
   public void endDepthUse() {
      if(!deptTestinUse)
         return;
      deptTestinUse = false;
      GL.glDisable(GL11.GL_ALPHA_TEST);
      GL.glDisable(GL11.GL_DEPTH_TEST);
   }
   
}


It would be cool to see this in the Image class itself. Just add some methods which delegate do drawEmbedded and so on and use a new parameter for the gltranslate.
As said in the comment making the depthTestInUse static would save some bits 8D

Using it is easy, just draw images, then use the startDepthUse() method to activate the depth buffer and draw you images with the DrawS methods, then end the depth buffer and draw overlaying stuff.

It's a bit more to change but would gives us auto sorting at no cost,since using the depth buffer is so common today it's for free :D

Anything else should be in the Doc of the class^^

Edit:
Thx to MatthiasM post, a simple Alphachannel should work now!

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Last edited by R.D. on Sun Sep 04, 2011 12:39 pm, edited 2 times in total.

Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 25, 2011 11:27 am 
Offline
Slick Zombie

Joined: Wed Apr 02, 2008 1:32 pm
Posts: 1340
Location: Italy
really cool! We need it :D

_________________
Blog | Last game Drone Swarm


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 04, 2011 3:40 am 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Oh boy, sorry... the forum did not tell me that someone was answering :D

I would say so too. It's really just some new mtehods and the old one onws in the Image class could still be used, just add a parameter :D

Also it really comes at NO COST!!!11! Can't stress that enough it's so cool xD

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 04, 2011 6:14 am 
Offline
Slick Zombie

Joined: Fri Jan 29, 2010 7:02 pm
Posts: 1242
And it won't work for transparency

_________________
TWL - The Themable Widget Library


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 04, 2011 12:31 pm 
Offline
Game Developer
User avatar

Joined: Thu Mar 03, 2011 6:22 pm
Posts: 534
Oh dammit, your right! Argh~ Thanks I totally forgot to test it with a alpha...

Edit:
If you know how to solve, tell me. Currently I don't have much time to work on anything :(

Edit²:
Okay, solved it xD Just need to add in a Aplha_Test... tho this might mean you can't use fancy light pictures... I will see!

_________________
Current Projects:
Image Mr. Hat I
Image Vegan Vs. Zombies
Projects:
RadicalFish Engine - Build on top of Slick2D, Ideas, Bugs? Open an Issue ticket!


Top
 Profile  
 
PostPosted: Wed Feb 15, 2012 8:28 pm 
Offline

Joined: Thu Jan 26, 2012 5:40 pm
Posts: 79
I'm trying to implement this while using MarteEngine, so any tips there would be appreciated, but is it possible to use this with Slick's SpriteSheet and animations?

Also, I'm getting "The constructor Image(int, int, int) is undefined" errors on some of the SortedImage constructors.


Top
 Profile  
 
PostPosted: Fri Jun 01, 2012 8:22 am 
Offline

Joined: Sun May 27, 2012 12:47 pm
Posts: 1
How do I use this with slick2d spritesheets and animations? :P


Top
 Profile  
 
PostPosted: Sun Jun 24, 2012 5:04 pm 
Offline
User avatar

Joined: Sun Jun 24, 2012 4:43 pm
Posts: 1
Hello there, this is my first time in the forums.

Thanks for the Z-depth code - i'm wondering if i can use it for Animations without modifying
the Slick2D library? I'm now having my own version of Slick2D which supports glDepth for Animations.
I'm not even sure if i'm doing this the right way, but at least it is working. :roll:

I have modified files Graphics.java and Image.java.
Image.java is basically having same code than SortedImage.java, but function "public void draw(float x, float y, float width, float height, Color filter)" always draws using depth, which is taken from Y-axis (and this have been sufficient for me so far). If i don't want to use depth, then i just don't call startDepthUse().

Then i added startDepthUse() and endDepthUse() to Graphics.java so i can call g.startDepthUse() etc.

I hope that someday Slick2D has native support for Z-axis - at least i'm planning to use it a lot! :-)
Oh and thanks for this great java library - i'm not yet building games with it, just fooling around and building my own test engine.


Top
 Profile  
 
PostPosted: Sun Jul 29, 2012 2:55 pm 
Offline

Joined: Sun Jul 29, 2012 2:51 pm
Posts: 1
Is there a way to rotate the image using the SortedImage class. I have tried to accomplish this myself but my knowledge of opengl is verry slim. Does anyone have this working or have an idea of how it should work? Any help will be appreciated.

PS: (i post it in this older topic because i am using the class defined by the OP)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group