Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Better ideas for handling objects?

  1. #1
    Junior Member HappySad-'s Avatar
    Join Date
    Jun 2014
    Location
    United Kingdom of Tea Lovers
    Posts
    6
    My Mood
    Cynical
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Better ideas for handling objects?

    I am making a 2D top down view game in Java. And right now I have the bones set up which work.

    One question though, I have set up a class to handle all of the rendering.
    I want to know if there is more efficient way of rendering objects?
    I am calling the render method from my main gameloop. Also I am sorting the array in the order of priority. So background would be rendered first, then player, etc; this means that background will be the bottom layer and player would be the top.
    I know iterating an array is not the best method but I'm out of ideas...

    Project GitHub repository - https://github.com/happysad-/Souls

    package com.engine.handlers;
     
    import java.util.ArrayList;
     
    import com.engine.utils.GlobalObject;
     
    public class RenderManager 
    {
    	private static RenderManager instance;
     
    	private ArrayList<GlobalObject> toRender;
     
    	public RenderManager()
    	{
    		instance = this;
     
    		toRender = new ArrayList<GlobalObject>();
    	}
     
    	public void add(GlobalObject object)
    	{
    		toRender.add(object);
    		System.out.println("Added " + object.getClass().getSimpleName() + " to rendering que.");
    	}
     
    	public void remove(GlobalObject object)
    	{
    		toRender.remove(object);
    		System.out.println("Removed " + object.getClass().getSimpleName() + " from rendering que.");
    	}
     
    	private void sort()
    	{
    		GlobalObject temp;
     
    		if(toRender.size() > 1)
    		{
    			for(int i = 0; i < toRender.size(); i++)
    			{
    				for(int j = 0; j < toRender.size() - i - 1; j++)
    				{
    					if(toRender.get(j).compareTo(toRender.get(j + 1).getRenderPriority(), 1) > 0)
    					{
    						temp = toRender.get(j);
    						toRender.set(j, toRender.get(j + 1));
    						toRender.set(j + 1, temp);
    					}
    				}
    			}
    		}
    	}
     
    	public void render()
    	{
    		sort();
     
    		for(GlobalObject o : toRender)
    		{
    			o.render();
    		}
    	}
     
    	public static RenderManager getRenderManager()
    	{
    		return instance;
    	}
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Better ideas for handling objects?

    You could use a different data structure that handles the sorting for you. At the very least you should be using the built-in sort function instead of writing your own.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Better ideas for handling objects?

    I am not quite sure I understand what your question is.
    You have to render objects from back to front or otherwise blending would not be correct. So you have no other choice but to sort your objects or use a self-sorting data structure, for example Red-Black-Trees.
    Unless, of course, all of your graphics are opaque and you have some sort of depth testing with your rendering pipeline (I could not quite see what you use for rendering), In this case sorting would not be necessary.

  4. #4
    Junior Member HappySad-'s Avatar
    Join Date
    Jun 2014
    Location
    United Kingdom of Tea Lovers
    Posts
    6
    My Mood
    Cynical
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Better ideas for handling objects?

    Quote Originally Posted by Cornix View Post
    In this case sorting would not be necessary.
    I would need to sort it due to this happening if it wast...

    Unsorted:


    Sorted:


    Anyways thanks for throwing few ideas in.. I know I wasnt too clear..

    Quote Originally Posted by KevinWorkman View Post
    You could use a different data structure that handles the sorting for you. At the very least you should be using the built-in sort function instead of writing your own.
    Yeah I know, i considered it, but then couldnt really implement it to work in the desired way. But ill try it again ^^

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Better ideas for handling objects?

    Quote Originally Posted by HappySad- View Post
    Yeah I know, i considered it, but then couldnt really implement it to work in the desired way.
    What exactly does that mean?

    If you're using LWJGL, you might also want to look into something called the "depth buffer" or "z buffer", which does all of this for you.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member HappySad-'s Avatar
    Join Date
    Jun 2014
    Location
    United Kingdom of Tea Lovers
    Posts
    6
    My Mood
    Cynical
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Better ideas for handling objects?

    Quote Originally Posted by KevinWorkman View Post
    What exactly does that mean?

    If you're using LWJGL, you might also want to look into something called the "depth buffer" or "z buffer", which does all of this for you.
    Added the depth buffer to my drawing methods, worked like a charm! Cheers, no need for custom sorting methods now

Similar Threads

  1. Project ideas
    By seal308 in forum Collections and Generics
    Replies: 2
    Last Post: August 3rd, 2012, 02:27 PM
  2. NEED HELP WITH IDEAS
    By ash12 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 21st, 2012, 04:52 PM
  3. [Question] Objects instantiated within objects.
    By Xerosigma in forum Object Oriented Programming
    Replies: 6
    Last Post: April 25th, 2012, 10:53 AM
  4. Any ideas?
    By ahender1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2012, 03:58 PM

Tags for this Thread