Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (407)
games submitted by our members
Games in WIP (293)
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  
  Large image rendering  (Read 1040 times)
0 Members and 1 Guest are viewing this topic.
Offline marcuiulian13

Senior Member


Medals: 3



« Posted 2012-04-17 23:25:07 »

Hello there! Again, i met a problem.

Until now i used to render only 32x32 or 40x40 sprites. But now when i needed to render some bigger image my game went from 60FPS to 4-5 FPS.

Here is my rendering code:
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  
      g.clearRect(0, 0, screen.getWidth(), screen.getHeight());
      g.setColor(new Color(0x000000));
      g.fillRect(0, 0, screen.getWidth(), screen.getHeight());
     
      g.translate(xOffset, yOffset);
     
      for(int i=0; i<onscreenTiles.size(); i++) {
         Tile t = (Tile) onscreenTiles.get(i);
         if(t instanceof FloorTile)
            g.drawImage(t.getImage(), null, (int) t.getX(), (int) t.getY());
         else if(t instanceof HoleTile)
            g.drawImage(t.getImage(), null, (int) t.getX(), (int) t.getY());
         else if(t instanceof PathTile)
            g.drawImage(t.getImage(), null, (int) t.getX(), (int) t.getY());
      }
      for(int i=0; i<level.getOthers().size(); i++) {
         if(level.getOthers().get(i) instanceof Drop) {
            Drop d = (Drop) level.getOthers().get(i);
            d.getAnim().draw(g, d.getX(), d.getY());
         }
      }
      for(int px=Math.max(0 ,(int) -yOffset)-24; px<=Math.max(0, Math.abs((int) yOffset)) + screen.getHeight(); px++) {
         for(int i=0; i<onscreenEntities.size(); i++) {
            Entity e = (Entity) onscreenEntities.get(i);
            if(e != null && (int)e.getY() == px) {
               if(e instanceof Mummy) {
                  if(((Entity) e).isDead()) ((Mummy) e).getDieAnim().draw(g, (float) (e.getX()-e.getWidth()/2), (float) (e.getY()-e.getHeight()/2));
                  else ((Mummy) e).getMoveAnim().draw(g, e.getX(), e.getY());
               } else if(e instanceof Spawner) {
                  if(((Entity) e).isDead()) ((Spawner) e).getDieAnim().draw(g, (float) (e.getX()-e.getWidth()/2), (float) (e.getY()-e.getHeight()/2));
                  else g.drawImage(e.getImage(), null, (int) e.getX(), (int) e.getY());
               }
            }
         }
         
         for(int i=0; i<pl.getBullets().size(); i++) {
            Entity e = (Entity) pl.getBullets().get(i);
            if((int)e.getY() == px && isInScreen(e)) {
               g.drawImage(e.getImage(), null, (int) e.getX(), (int) e.getY());
            }
         }
         
         if((int)pl.getY() == px) {
            if(!pl.isWalking()) g.drawImage(pl.getImage(), null, (int) pl.getX(), (int) pl.getY());
            else pl.getMoveAnim().draw(g, pl.getX(), pl.getY());
            if(pl.isFiring()) pl.getmFlash().draw(g, pl.getMuzzleX(), pl.getMuzzleY());
         }
         
         for(int i=0; i<level.getBuildings().size(); i++) {
            Building b = (Building) level.getBuildings().get(i);
            g.drawImage(b.getImage(), null, (int) b.getX(), (int) b.getY());
         }
         
         for(int i=0; i<onscreenTiles.size(); i++) {
            Tile t = (Tile) onscreenTiles.get(i);
            if((int)t.getY() == px && t instanceof WallTile) {
               g.drawImage(t.getImage(), null, (int) t.getX(), (int) t.getY()-24);
            }
         }
      }



The problem is at
1  
2  
3  
4  
for(int i=0; i<level.getBuildings().size(); i++) {
            Building b = (Building) level.getBuildings().get(i);
            g.drawImage(b.getImage(), null, (int) b.getX(), (int) b.getY());
         }


The image is 256x224 and is loaded (as the others) at the application start.
What can i do?

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Offline davedes
« Reply #1 - Posted 2012-04-17 23:51:52 »

There are some good optimization tips here:
http://stackoverflow.com/questions/148478/java-2d-drawing-optimal-performance

I'd personally recommend ditching Java2D altogether and going with OpenGL (i.e. LibGDX or Slick). Especially if you're depending on the "java2d.opengl" flag to have your game run at a reasonable framerate (which I'm not even sure works on MacOSX), then you may as well not use Java2D...

Online DrZoidberg

Junior Member


Medals: 6



« Reply #2 - Posted 2012-04-18 01:50:46 »

256x224 - that's only 60,000 pixels. Java2D should be able to draw 100,000 of those sprites per second.

g.drawImage(t.getImage(), null, (int) t.getX(), (int) t.getY());
You should change that to
g.drawImage(t.getImage(), (int) t.getX(), (int) t.getY(), null);

You were using this method
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics2D.html#drawImage(java.awt.image.BufferedImage, java.awt.image.BufferedImageOp, int, int)
But you probably want this one
http://docs.oracle.com/javase/6/docs/api/java/awt/Graphics.html#drawImage(java.awt.Image, int, int, java.awt.image.ImageObserver)
I'm not sure though if that makes much of a difference. The reason for the low performance is probably somewhere else.

In my experience Slick is not faster than Java2D. So if you want to part with Java2D, I would say go for LibGDX.
Games published by our own members! Check 'em out!
Play the free demo of Revenge of the Titans!
Offline marcuiulian13

Senior Member


Medals: 3



« Reply #3 - Posted 2012-04-18 01:55:39 »

I am using g.drawImage(image, null x, y); because i am working with bufferedimage, not image.

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Offline StumpyStrust
« Reply #4 - Posted 2012-04-18 01:58:55 »

You can draw with BufferedImages using g.drawImage(image,x1,y1,null);

I do it all the time.

I can render 120+ images that are 1200x1200 pixels at 60 fps no problem. You start to lose performance when you are rendering with things like filters, transformations, and alpha changes. Also scaling has some performance hit. (although in my testing its very little)
Offline marcuiulian13

Senior Member


Medals: 3



« Reply #5 - Posted 2012-04-18 02:07:46 »

okay, thanks.

one more question: is changing from Java2D in libGDX too hard, i mean, do i have to change all my code or only some simple parts of it?

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Offline davedes
« Reply #6 - Posted 2012-04-18 02:11:52 »

Quote
In my experience Slick is not faster than Java2D. So if you want to part with Java2D, I would say go for LibGDX.
And how did you come to that conclusion?

Online DrZoidberg

Junior Member


Medals: 6



« Reply #7 - Posted 2012-04-18 02:18:27 »

Quote
In my experience Slick is not faster than Java2D. So if you want to part with Java2D, I would say go for LibGDX.
And how did you come to that conclusion?

I wrote a program that draws and fills a large number of polygons. Using Java2D I actually get more fps than with slick.

okay, thanks.

one more question: is changing from Java2D in libGDX too hard, i mean, do i have to change all my code or only some simple parts of it?

LibGDX is very different from Java2D, but if you kept rendering and game logic separate you only have to change the rendering code.
Offline davedes
« Reply #8 - Posted 2012-04-18 02:51:09 »

Quote
I wrote a program that draws and fills a large number of polygons. Using Java2D I actually get more fps than with slick.
Huh

Slick (hardware accelerated) should definitely outperform Java2D (software renderer) in image/geometry rendering; if it's slower, then you're very likely doing something wrong.

Offline marcuiulian13

Senior Member


Medals: 3



« Reply #9 - Posted 2012-04-18 02:52:48 »

I don't really like to switch to using a library... And if i go, i will go with lwjgl.

Is okay to firstly start playing with lwjgl in 2D ? And after some time start using 3D?

Check out my current project: http://cube3dev.blogspot.com/
Please comment/review/suggest.
Games published by our own members! Check 'em out!
Try the Free Demo of Droid Assault
Offline 65K
« Reply #10 - Posted 2012-04-18 08:14:09 »

Like the others pointed out, for drawing images, Java2d should be more than fast enough.
Results of my very unscientific measurements:
Java2d dropped under 60fps at ~ 20.000 images of 64x64 pixels
I canceled the Libgdx test run at > 40.000 images

Nevertheless, I just switched to LibGdx for easier lighting in a couple of days.

Offline ReBirth
« Reply #11 - Posted 2012-04-19 23:45:29 »

First, you always created new object and casting every loop.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Get high quality music tracks for your game!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (88 views)
2013-05-17 21:29:12

alaslipknot (96 views)
2013-05-16 21:24:48

gouessej (128 views)
2013-05-16 00:53:38

gouessej (123 views)
2013-05-16 00:17:58

theagentd (130 views)
2013-05-15 15:01:13

theagentd (118 views)
2013-05-15 15:00:54

StreetDoggy (161 views)
2013-05-14 15:56:26

kutucuk (184 views)
2013-05-12 17:10:36

kutucuk (185 views)
2013-05-12 15:36:09

UnluckyDevil (191 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.3 seconds with 21 queries.