Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  rendering problem  (Read 296 times)
0 Members and 1 Guest are viewing this topic.
Offline Yemto

Junior Devvie


Exp: 3 years



« Posted 2014-05-08 20:11:10 »

So I'm making a 2D sprite based isometric game in java without any libraries. It go well so far but I have encountered a problem with my rendering. My levels consist of a 3D array of a abstract class called Tile, but now I want to be able to render entities on the map too. So what would be a good way of doing that? Since the Entity class is in a ArrayList i can't simply loop through it. I have been thinking of having an ArrayList that consists of WeakReferences to a class called LevelComponent that only contains x, y, z, and sprite. While the update method loops through the Tile, and Entity class respectively

Here is some made up code on how I thinking of doing it.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
public class Map{
   List<WeakReference<Entity>> render = new ArrayList<>();

   public void update(float alpha){
      for(int i=0; i<entities.size(); i++){
         Entity e = entities.get(i);
         if(e.isDead()){
            entities.remove(i); i--;
            continue;
         }
         
         e.update(alpha);
      }
     
      for(int x=0; x<SIZE_WIDTH; x++)
         for(int y=0; y<SIZE_HEIGHT; y++)
            for(int z=0; z<SIZE_TOP; z++)
               if(t != null)
                  t.update(alpha);
                 
   }

   public void render(Graphics2D g2, int offetX, int offsetY){
      for(int i=0; i<levelComp.size(); i++){
         LevelComponent l = levelComp.get(i).get();
         if(l == null){
            levelComp.remove(i); i--;
            continue;
         }
         
         l.render(g2, offetX, offetY);
      }
   }
}


Is there a better way I don't see?
Offline princec

« JGO Spiffy Duke »


Medals: 421
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #1 - Posted 2014-05-08 20:27:12 »

You don't need to use WeakReferences or anything like that.
Why can't you simply iterate through your List of Entities?

Cas Smiley

Offline Yemto

Junior Devvie


Exp: 3 years



« Reply #2 - Posted 2014-05-08 20:32:56 »

Because the Tiles and Entities will end up overlapping each other. Like if I render the entities first, and then the tiles all entities will be under the level, and if I do it in the reverse order all entities wouldn't be able to be behind terrain.

I use WeakReferences so I don't need to check in the rendering if the tile or entity exists, because then the garbage collector will take care of that.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Yemto

Junior Devvie


Exp: 3 years



« Reply #3 - Posted 2014-05-08 23:54:13 »

Since no one have protested against this, I take this is an okay idea. I really did one change, I made "LevelComponent" into an interface.

1  
2  
3  
4  
public interface RenderComponent {
    public void render(Graphics2D g2, int offsetX, int offsetY, int offsetZ);
    public int getRenderPriority();
}


This is what the rendering method ended up looking like.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public void render(Graphics2D g2, int offsetX, int offsetY, int offsetZ){
        //Sort everything that is going to get rendered
        render.sort((w1, w2) -> {
            int renderScore1 = w1.get() != null ? ((RenderComponent)w1.get()).getRenderPriority() : -1;
            int renderScore2 = w2.get() != null ? ((RenderComponent)w2.get()).getRenderPriority() : -1;
            return renderScore2 - renderScore1;
        });
       
        //Check everything that's going to get rendered, and remove garbage data.
        for(int i=0; i<render.size(); i++){
            RenderComponent r = render.get(i).get();
            if(r == null){
                render.remove(i); i--;
                continue;
            }
           
            //Render
            r.render(g2, offsetX, offsetY, offsetZ);
        }
    }
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xFryIx (60 views)
2014-11-13 12:34:49

digdugdiggy (39 views)
2014-11-12 21:11:50

digdugdiggy (32 views)
2014-11-12 21:10:15

digdugdiggy (28 views)
2014-11-12 21:09:33

kovacsa (50 views)
2014-11-07 19:57:14

TehJavaDev (54 views)
2014-11-03 22:04:50

BurntPizza (53 views)
2014-11-03 18:54:52

moogie (68 views)
2014-11-03 06:22:04

CopyableCougar4 (67 views)
2014-11-01 23:36:41

DarkCart (153 views)
2014-11-01 14:51:03
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!