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  
  Large image rendering  (Read 2030 times)
0 Members and 1 Guest are viewing this topic.
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Posted 2012-04-17 21: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?

Getting a project done is by far the most hard thing in game development.
Offline davedes
« Reply #1 - Posted 2012-04-17 21: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...

Offline DrZoidberg

Senior Devvie


Medals: 17



« Reply #2 - Posted 2012-04-17 23: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!
Legends of Yore - The Casual Retro Roguelike
Offline marcuiulian13

Senior Devvie


Medals: 5
Exp: 3 years



« Reply #3 - Posted 2012-04-17 23:55:39 »

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

Getting a project done is by far the most hard thing in game development.
Offline StumpyStrust
« Reply #4 - Posted 2012-04-17 23: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 Devvie


Medals: 5
Exp: 3 years



« Reply #5 - Posted 2012-04-18 00: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?

Getting a project done is by far the most hard thing in game development.
Offline davedes
« Reply #6 - Posted 2012-04-18 00: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?

Offline DrZoidberg

Senior Devvie


Medals: 17



« Reply #7 - Posted 2012-04-18 00: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 00: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 Devvie


Medals: 5
Exp: 3 years



« Reply #9 - Posted 2012-04-18 00: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?

Getting a project done is by far the most hard thing in game development.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline 65K
« Reply #10 - Posted 2012-04-18 06: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 21: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.

 

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 (55 views)
2014-11-13 12:34:49

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

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

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

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

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

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

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

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

DarkCart (148 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!