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; } }