Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
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  
  Fastest way to render 2D tiles  (Read 2310 times)
0 Members and 1 Guest are viewing this topic.
Offline Doubstract
« Posted 2013-11-03 07:37:52 »

I am trying to write 2D tile based game, tiles are 10 x 10 pixels. And when i am rendering blocks my fps is 40... So, what is best way render 2D tiles? Currently, i am using LWJGL, but maybe there is faster library than LWJGL? I heard something about libGDX, it is faster?
Offline Dxu1994
« Reply #1 - Posted 2013-11-03 07:43:28 »

what code are you using to render blocks?

I'm rendering over 2000 objects per frame in my MMO and it runs at 200 FPS on average consumer hardware (LWJGL) and 160 FPS (Java2D) so you're clearly doing something wrong..

Also the objects I render are a LOT bigger than 10x10 pixels.

Offline Doubstract
« Reply #2 - Posted 2013-11-03 07:58:46 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
 for(int x = 0; x < blocks.length; x++){
                 for(int y = 0; y < blocks[0].length; y++){
                    if(x + 30 > player.x/Block.blockSize && x - 30 < Player.x /Block.blockSize){
                       if(y + 30 > player.y / Block.blockSize && y - 30 < Player.y /Block.blockSize){
                          blocks[x][y].draw();            
                       }
                    }
                 }
          }



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  
public void draw(){
         
         if(id == ids.dirt){
            GL11.glColor3f(1.5f, 0.25f, 0.1f);
         }
         if(id == ids.tree){
   
            GL11.glColor3f(0.35f, 0.2f, 0.1f);
   
         }
         if(id == ids.rock){
            GL11.glColor3f(0.9f, 0.9f, 0.9f);
         }
         if(id == ids.water){
            GL11.glColor3f(0.0f, 0.2f, 1.0f);
         }
         
      GL11.glPushMatrix();
         
         
         GL11.glBegin(GL11.GL_QUADS);
            GL11.glVertex2f(x , y );
            GL11.glVertex2f(x + Block.blockSize, y );
            GL11.glVertex2f(x + Block.blockSize, y + Block.blockSize);
            GL11.glVertex2f(x , y + Block.blockSize);
         GL11.glEnd();
      GL11.glPopMatrix();
     
         GL11.glColor3f(1f, 1f, 1f);

      }


I think i can edit this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
if(id == ids.dirt){
            GL11.glColor3f(1.5f, 0.25f, 0.1f);
         }
         if(id == ids.tree){
   
            GL11.glColor3f(0.35f, 0.2f, 0.1f);
   
         }
         if(id == ids.rock){
            GL11.glColor3f(0.9f, 0.9f, 0.9f);
         }
         if(id == ids.water){
            GL11.glColor3f(0.0f, 0.2f, 1.0f);
         }


Because its fps wasting, but it's can't be main problem
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Dxu1994
« Reply #3 - Posted 2013-11-03 08:21:05 »

Oh yeah.. immediate mode.

You dont want to use immediate mode, you want to use glDrawArrays or glDrawElements to draw your tiles. Also, you should be using a textureAtlas to cut down on the amount of glBindTexture calls you need to make.

Offline StumpyStrust
« Reply #4 - Posted 2013-11-03 08:25:14 »

Do you know what push and pop matrix does? Probably not as you are doing nothing with them. LWJGL is JUST opengl bindings. Libgdx is an actually engine of sorts.

Even in immediate mode you should get 10k plus tiles on ancient computers.

It doesn't look like you are doing any texturing so that can't be the performance drop. How many are you trying to render?

If you want more then 10-20k tiles, then you need to batch everything.


Offline SHC
« Reply #5 - Posted 2013-11-03 08:47:50 »

Come out of the immediate mode and use VBOs.

Offline Doubstract
« Reply #6 - Posted 2013-11-03 09:30:39 »

Thanks for help to everyone, but is VBO best opinion for render tiles?

And one more thing what i just remembered. I am creating a rectangle for each tile, because i need to build or destroy tiles. Is rectangles best opinion for tiles?
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #7 - Posted 2013-11-03 11:31:59 »

Ok, a bunch of things.

1. Don't use plain immediate mode, at least use display lists which are incredibly easy to implement using your code.
2. Don't call glPushMatrix and glPopMatrix unless you are translating, rotating or scaling (or any of those other operations) a tile. Pushing and popping the matrix basically saves the state of the matrix before you translate, rotate etc... and then loads the old matrix after. I've tried to put it into simple terms. So basically, if you need to only rotate one quad, wrap it in the push and pop matrix calls. That way it doesn't affect the other tiles.
3. Don't call glBegin and glEnd every time you render a tile. Do something like this:
1  
2  
3  
4  
5  
6  
7  
for(int x = 0; x < mapWidth; x++){
    for(int y = 0; y < mapHeight; y++){
        glBegin();
        draw();
        glEnd();
    }
}

This should increase performance.

4. Statically create your tiles. This might be a bit over your head, but do something like this:

1  
2  
   
public static final Tile Grass = new TileGrass();


And then TileGrass just extends Tile. I just horribly explained that, but basically if you have a tile class, you need to only create your tiles once.

Offline Phibedy

Senior Member


Medals: 8



« Reply #8 - Posted 2013-11-03 11:45:34 »

if the tiles don't change a lot, I would use fbos: http://lwjgl.org/wiki/index.php?title=Using_Frame_Buffer_Objects_%28FBO%29
Offline Doubstract
« Reply #9 - Posted 2013-11-03 12:01:44 »

Thanks for help, now i understand how i should create my game Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alexis.pautrot

Senior Newbie


Projects: 1



« Reply #10 - Posted 2013-11-03 12:24:40 »

VBO for everything !
You might want to take a look at the VertexBatch class of LibGDX for a good VBO example / code base.
Offline opiop65

JGO Kernel


Medals: 154
Projects: 7
Exp: 3 years


JumpButton Studios


« Reply #11 - Posted 2013-11-03 12:26:18 »

VBO is not for everything. Just because it seems the fastest does not mean you should use it for every situation. Plus, OP here is obviously new to OpenGL and probably couldn't grasp the concept yet as its advanced OpenGL. Let him warm up to the easy stuff first before he has to learn the hard stuff.

Offline theagentd
« Reply #12 - Posted 2013-11-03 16:58:11 »

VBO is not for everything. Just because it seems the fastest does not mean you should use it for every situation. Plus, OP here is obviously new to OpenGL and probably couldn't grasp the concept yet as its advanced OpenGL. Let him warm up to the easy stuff first before he has to learn the hard stuff.
But everything else is rightfully deprecated. =P

Myomyomyo.
Offline pitbuller
« Reply #13 - Posted 2013-11-03 18:10:24 »

8 redudant branch and 10 opengl calls per tile.

Ps.
Also solve visibilty outside of loop. You got uniform grid, its acceleration structure so use that.
Offline Doubstract
« Reply #14 - Posted 2013-11-04 05:06:28 »

I already solved visibility outside loop, but thanks Smiley
Offline SHC
« Reply #15 - Posted 2013-11-04 12:51:04 »

1  
2  
3  
4  
5  
6  
7  
for(int x = 0; x < mapWidth; x++){
    for(int y = 0; y < mapHeight; y++){
        glBegin();
        draw();
        glEnd();
    }
}

This should increase performance.

Since every tile is just a rectangle, you can even do like this to increase the speed.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
glBegin(GL_QUADS);
{
    for (int x = 0; x < mapWidth; x++)
    {
        for (int y = 0; y < mapHeight; y++)
        {
            draw();
        }
    }
}
glEnd();

In this way, you can even increase the performance by decreasing the gl calls. Even though, I recommend VBOs since they are much faster. If you need even more performance, you may look into topics like Sprite Batching.

Offline Doubstract
« Reply #16 - Posted 2013-11-04 13:27:25 »

Quote
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
glBegin(GL_QUADS);
{
    for (int x = 0; x < mapWidth; x++)
    {
        for (int y = 0; y < mapHeight; y++)
        {
            draw();
        }
    }
}
glEnd();

Nice and simple, but i will try to use VBO for this game.
Offline pitbuller
« Reply #17 - Posted 2013-11-04 18:09:06 »

I already solved visibility outside loop, but thanks Smiley

No you didn't you solve it. You do it per tile aka brute force.
It can be done in constant time. You calculate first and last tile per axis that you can see and then just loop only those. No need for branches at inner loop and tiles out of sight don't waste any performance.
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.

BurntPizza (22 views)
2014-09-19 03:14:18

Dwinin (37 views)
2014-09-12 09:08:26

Norakomi (65 views)
2014-09-10 13:57:51

TehJavaDev (91 views)
2014-09-10 06:39:09

Tekkerue (45 views)
2014-09-09 02:24:56

mitcheeb (66 views)
2014-09-08 06:06:29

BurntPizza (49 views)
2014-09-07 01:13:42

Longarmx (36 views)
2014-09-07 01:12:14

Longarmx (42 views)
2014-09-07 01:11:22

Longarmx (38 views)
2014-09-07 01:10:19
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!