Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (761)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (845)
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  
  [Slick2D] Why the F is it so slow? [SOLVED]  (Read 10666 times)
0 Members and 1 Guest are viewing this topic.
Offline GNecro1
« Posted 2015-01-09 10:35:27 »

I am recreating my previous project https://github.com/GNecro1/TROTMGRO, it was java2D no libs nothing, pure java. Now i am using Slick2D and it is crap. In Java2D i was creating a world 128x128 tiles(300+ FPS), now i am creating (in Slick2D) a world 32x32 tiles (9 FPS). So wtf is wrong?
How I do the rendering:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
 // Only 1 chunk, rendering
public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
   for (int i = 0; i < tile.length; i++) {
      for (int j = 0; j < tile[0].length; j++) {
         tile[i][j].render(c, game, g);
      }
   }
}
// tile rendering

public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
   img.draw(x, y,2);
}


And how i did it in Java2D :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
// world
public void render(Graphics2D g) {
   for (int i = 0; i < tiles.length; i++) {
      for (int j = 0; j < tiles[0].length; j++) {
         tiles[i][j].render(g);
      }
   }
}
//tile

public void render(Graphics2D g) {
   g.drawImage(RL.grass, x, y, width, height, null);
}


So anyone care to explain?

Java freak! Cheesy
Offline Gibbo3771

JGO Kernel


Medals: 128
Projects: 5
Exp: 1 year


Currently inactive on forums :(


« Reply #1 - Posted 2015-01-09 11:00:29 »

Slick is an outdated library, however that is not the issue here.

Are you positive it is this block of code that is the problem and not something else ? Try cutting the tile count right down or even just draw one to see if it is still slow.

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline GNecro1
« Reply #2 - Posted 2015-01-09 11:03:57 »

I did i draw one tile and it still slows down... when i dont draw tiles the FPS goes to 60 (target FPS 60) and when i draw one it goes to 57 FPS

Java freak! Cheesy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline trollwarrior1
« Reply #3 - Posted 2015-01-09 11:05:24 »

Maybe you are reading images from disk every time you draw?

Retro Pixel Castles is written with Slick2D I believe and it seems to work pretty nicely Tongue
Offline GNecro1
« Reply #4 - Posted 2015-01-09 11:10:11 »

Nope that is not the problem i checked that first when i saw the FPS.

Java freak! Cheesy
Offline kappa
« League of Dukes »

JGO Kernel


Medals: 120
Projects: 15


★★★★★


« Reply #5 - Posted 2015-01-09 11:15:16 »

You need to post more of your slick code, not enough to isolate the problem from the above snippet.
Offline GNecro1
« Reply #6 - Posted 2015-01-09 11:41:22 »

Here you go the whole project https://www.mediafire.com/?9h7o6mmy97919di

Java freak! Cheesy
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2015-01-09 11:42:52 »

I suspect you're not using the start() end() of the tileset use, if you don't then every time you use an image it'll start a new geometry underneath which will be very costly.

Cheers,

Kev

Offline GNecro1
« Reply #8 - Posted 2015-01-09 12:01:29 »

When i use start() and end() my game brakes.... ? It does not show the player (Animation) and it only shows some of the sprites.....

Java freak! Cheesy
Offline kevglass

« JGO Spiffy Duke »


Medals: 319
Projects: 25
Exp: 22 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #9 - Posted 2015-01-09 12:08:33 »

You'd need to render the tiles in one block, then render the players in a separate block. If you want to swap to a different sprite set (say the player) mid tile render you'd need to end() and start() around it. However, this would break the efficiency of the OpenGL rendering under neath.

There used to be a bunch of samples showing how tile rendering would work, are they not there any more?

Cheers,

Kev

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline GNecro1
« Reply #10 - Posted 2015-01-09 12:13:09 »

I dont think so......

Java freak! Cheesy
Offline lcass
« Reply #11 - Posted 2015-01-09 19:53:15 »

I don't know much about slick but I'm pretty sure it's more efficient when you batch the renders
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 379
Projects: 2
Exp: 2 years


Resident Crazyman


« Reply #12 - Posted 2015-01-09 20:10:50 »

I suspect you're not using the start() end() of the tileset use, if you don't then every time you use an image it'll start a new geometry underneath which will be very costly.

Cheers,

Kev

^ no doubt this. RPC would scream if I tried to render my tile map without using .start(), .end() and .drawEmbedded().

Assuming all your tiles are on a single sprite sheet, you can simply just do this:
1  
2  
3  
4  
5  
6  
7  
8  
9  
public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
   referenceToYourTileSpriteSheet.startUse();
   for (int i = 0; i < tile.length; i++) {
      for (int j = 0; j < tile[0].length; j++) {
         tile[i][j].render(c, game, g);
      }
   }
   referenceToYourTileSpriteSheet.endUse();
}


1  
2  
3  
public void render(GameContainer c, StateBasedGame game, Graphics g) throws SlickException {
   img.drawEmbedded(x, y);
}



Slick is actually quite fast when you use it correctly, Retro-Pixel Castles renders about 36,000 tiles and 1,000 entities on screen all at once (including lighting and particles) at 60FPS, and like trollwarrior1 said, it uses slick. Tongue

EDIT: Goofed on the render code, all fixed now.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
LIVE-STREAMING DEVELOPMENT: http://www.hitbox.tv/rayvolution
Offline GNecro1
« Reply #13 - Posted 2015-01-09 21:50:52 »

Thx it runs very fast! <3

Java freak! Cheesy
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 379
Projects: 2
Exp: 2 years


Resident Crazyman


« Reply #14 - Posted 2015-01-09 23:07:32 »

Thx it runs very fast! <3

Good rule with slick I've learned, use sprite sheets whenever possible, and try your best to render everything on that sheet you use all at once if you can. For example, render your entire map with startUse() endUse() and drawEmbedded(), then render your entities on top of that, then the shadows/lighting/particles/whatever else using the same start/end/drawEmbedded combo. But try to keep each batch of things you want to render on a single sprite sheet. There's fancier ways to do it even faster, but there's a point where optimizing is just going to get silly. Unless you're trying to push LWJGL and Slick's limits you should be fine just grouping sprite sheets together and rendering everything related to them all in one go.

Another future optimization you'll want to look at is render culling, I don't know if you plan on having a movable map that's larger than the screen, but in your current code you're rendering the entire map, even if it's off screen. You can get a huge performance boost on larger maps if you limit rendering to only what is actually visible to the player.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
LIVE-STREAMING DEVELOPMENT: http://www.hitbox.tv/rayvolution
Offline GNecro1
« Reply #15 - Posted 2015-01-10 10:02:49 »

Yea i am doing that, one more problem i am using only one spritesheet for everything and how do i draw animation it only has draw? And when i draw it it goes to the 0,0 no matter what!

Java freak! Cheesy
Offline GNecro1
« Reply #16 - Posted 2015-01-10 12:22:51 »

I solved it, i just get the image from the animation. YAy

Java freak! Cheesy
Offline Riven
Administrator

« JGO Overlord »


Medals: 1341
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #17 - Posted 2015-01-10 14:59:16 »

@kevglass: just throwing it out there: if the slow path is too slow for basically anything (OP said: 32*32 tiles @ 9fps), then why expose this slow path to developers? Why not throw a YoureDoingItWrongException instead?

I know Slick2D is no longer supported by you, so it's more of a question regarding why you made this decision a long time ago. Smiley (when the slow path ran on 'worse' hardware, too)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings!
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 379
Projects: 2
Exp: 2 years


Resident Crazyman


« Reply #18 - Posted 2015-01-10 20:37:55 »

@kevglass: just throwing it out there: if the slow path is too slow for basically anything (OP said: 32*32 tiles @ 9fps), then why expose this slow path to developers? Why not throw a YoureDoingItWrongException instead?

I know Slick2D is no longer supported by you, so it's more of a question regarding why you made this decision a long time ago. Smiley (when the slow path ran on 'worse' hardware, too)

While I can't really speak for Kev obviously, I think it's for simplicity's sake. Any time you need to draw just one single image (Like a logo, or a single GUI button) you can just use .draw() and skip having to have startUse() and endUse() in your methods since they're already inside the Image.draw() method.

For me, .draw() is used for larger images or places where optimizing would be totally pointless. My main menu's GUI is all using .draw() for example, since there's only actually 3 or 4 images total on the screen and all of them are on different sprite sheets (or single images) anyway. But things like entities, tiles, particles, shadows, etc, I use drawEmbedded().

There are also cases where you're overlapping elements, like say drawing geometry, rendering an image, rendering some font, then some more geometry, and you just can't use drawEmbedded easily.

If Slick was still supported, my advice would be to somehow notify people that .draw is the less efficient way of doing it, but I'm not sure how other than renaming it to drawOnlyOne() or something awkward.

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
LIVE-STREAMING DEVELOPMENT: http://www.hitbox.tv/rayvolution
Offline Rayvolution

« JGO Spiffy Duke »


Medals: 379
Projects: 2
Exp: 2 years


Resident Crazyman


« Reply #19 - Posted 2015-01-12 02:52:09 »

If you don't mind editing Slick itself, you can add drawEmbedded() support to Animation, it's actually not that complicated. Honestly it's just a ripoff of the regular draw() code, it just runs drawEmbedded when it's all said and done.

Add this below line 380:
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  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
   /**
    * Draw the animation to the screen without startUse() and endUse().
    */

   public void drawEmbedded() {
      drawEmbedded(0,0);
   }

   /**
    * Draw the animation at a specific location without startUse() and endUse().
    *
    * @param x The x position to draw the animation at
    * @param y The y position to draw the animation at
    */

   public void drawEmbedded(float x,float y) {
      drawEmbedded(x,y,getWidth(),getHeight());
   }

   /**
    * Draw the animation at a specific location without startUse() and endUse().
    *
    * @param x The x position to draw the animation at
    * @param y The y position to draw the animation at
    * @param filter The filter to apply
    */

   public void drawEmbedded(float x,float y, Color filter) {
      drawEmbedded(x,y,getWidth(),getHeight(), filter);
   }
   
   /**
    * Draw the animation without startUse() and endUse().
    *
    * @param x The x position to draw the animation at
    * @param y The y position to draw the animation at
    * @param width The width to draw the animation at
    * @param height The height to draw the animation at
    */

   public void drawEmbedded(float x,float y,float width,float height) {
      drawEmbedded(x,y,width,height,Color.white);
   }
   
   /**
    * Draw the animation without startUse() and endUse().
    *
    * @param x The x position to draw the animation at
    * @param y The y position to draw the animation at
    * @param width The width to draw the animation at
    * @param height The height to draw the animation at
    * @param col The colour filter to use
    */

   public void drawEmbedded(float x,float y,float width, float height, Color col) {
      if (frames.size() == 0) {
         return;
      }
     
      if (autoUpdate) {
         long now = getTime();
         long delta = now - lastUpdate;
         if (firstUpdate) {
            delta = 0;
            firstUpdate = false;
         }
         lastUpdate = now;
         nextFrame(delta);
      }
     
      Frame frame = (Frame) frames.get(currentFrame);
      frame.image.drawEmbedded(x,y,x+width,y+height,0,0,width,height,col);
   }

- Raymond "Rayvolution" Doerr.
Retro-Pixel Castles - Now on Steam!
LIVE-STREAMING DEVELOPMENT: http://www.hitbox.tv/rayvolution
Offline GNecro1
« Reply #20 - Posted 2015-01-12 08:39:04 »

Well it is better than what i did. I just did all of that in my game.

Java freak! Cheesy
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (351 views)
2018-06-10 19:43:48

EgonOlsen (344 views)
2018-06-10 19:43:44

EgonOlsen (292 views)
2018-06-10 19:43:20

DesertCoockie (515 views)
2018-05-13 18:23:11

nelsongames (843 views)
2018-04-24 18:15:36

nelsongames (829 views)
2018-04-24 18:14:32

ivj94 (1251 views)
2018-03-24 14:47:39

ivj94 (395 views)
2018-03-24 14:46:31

ivj94 (1058 views)
2018-03-24 14:43:53

Solater (413 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
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!