Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (516)
Games in Android Showcase (123)
games submitted by our members
Games in WIP (577)
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  
  Load large background image  (Read 3183 times)
0 Members and 1 Guest are viewing this topic.
Offline ChouChou

Senior Newbie





« Posted 2010-03-25 13:49:09 »

Hi, this probably came up before, but I'm just wondering how are you guys dealing with loading large background images? I have a tile based map and my entire map is consist of 72 X 72 tiles, I have no problem building the map, just when it comes to loading a large background its lagging my game pretty badly. How would you guys load an 2363 X 1265 background image?

I've thought about chopping it into blocks, but that would be harder to do parallel scrolling, my idea is use the getSubImage method from the bufferImage to do the scrolling, but with large image lagging my game this doesn't seem viable. Any tips welcome, thanks in advance.  Grin
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #1 - Posted 2010-03-25 14:52:17 »

Solution: Don't use Java2D. Wink

See my work:
OTC Software
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #2 - Posted 2010-03-25 15:26:20 »

getSubimage returns an image which just references a region of the larger image. While that's good for memory it's bad for performance. I'd suggest that at load time you load your huge image then carve it up into tiles of 256x256 (using getSubImage and then drawing those onto brand new 256x256 tile images). Then just draw the tiles of the background that are visible at any given time.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline DzzD
« Reply #3 - Posted 2010-03-25 17:22:41 »

why dont you load the whole image ?
and then use drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null) ?

with :
sx,sy,sx2,sy2 variing depending on your scroll (viewable area)
dx,dy,dx2,dy2 fixed to your game area => 0,0,gameWidth,gameHeight
largeBackgroundImage => your large background


or do you mean that each tile is an 2363 X 1265 image ?

Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #4 - Posted 2010-03-25 17:54:03 »

why dont you load the whole image ?
and then use drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null) ?
As mentioned, this is horribly slow in J2D. I suspect that under the hood the whole image needs to be shuttled to the graphics card before the subsection can be drawn, regardless of how small the subsection is.

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Offline DzzD
« Reply #5 - Posted 2010-03-25 18:09:18 »

As mentioned, this is horribly slow in J2D. I suspect that under the hood the whole image needs to be shuttled to the graphics card before the subsection can be drawn, regardless of how small the subsection is.
hum I did not really had any performance issue in java2d but, never try the above with such image, so if it is so slow you can do the following :

store you big image in a int array[2363 X 1265] and create a buffered image of the size of your display area then when requiere to render backgorund :
copye the viewable area to the buffered image with System.arrayCopy or a raw destInt[n]=sourceInt[n] and then display your buffered image

Online Riven
« League of Dukes »

JGO Overlord


Medals: 823
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2010-03-25 18:19:29 »

As mentioned, this is horribly slow in J2D. I suspect that under the hood the whole image needs to be shuttled to the graphics card before the subsection can be drawn, regardless of how small the subsection is.

Not slow for me.

drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null) is fast as long as the source/destination bounds require no scaling. I've been scrolling 4000*4000px with this, with a smooth framerate.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline DzzD
« Reply #7 - Posted 2010-03-25 18:21:37 »

Not slow for me.

drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null) is fast as long as the source/destination bounds require no scaling. I've been scrolling 4000*4000px with this, with a smooth framerate.
not for me too, hope it was clear that I was talking of non-scaled area, it was not ?

Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #8 - Posted 2010-03-25 18:33:54 »

Drawing a sub-image in J2D was never slow for me, as far as I can remember.

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline Orangy Tang

JGO Kernel


Medals: 56
Projects: 11


Monkey for a head


« Reply #9 - Posted 2010-03-25 23:57:51 »

Hmm, I wonder if it depends on the backend. On the DirectDraw backend it's pretty bad, but I'll admit to not testing it with the D3D backend.

Such is the difficulty of giving performance advice for a platform with three different backends across three different platforms. :S

[ TriangularPixels.com - Play Growth Spurt, Rescue Squad and Snowman Village ] [ Rebirth - game resource library ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ChouChou

Senior Newbie





« Reply #10 - Posted 2010-03-26 01:29:13 »

Hi guys, thanks alot for the replies! I have loaded the background image into an array first( tiles are in a separate array). I'll try the drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null) method as soon as I get off work. I'm just curious, what's better for performance:
- drawImage(Image largeBackgroundImage,sx,sy,sx2,sy2,dx,dy,dx2,dy2,null)
- getSubImage from bufferedImage(seems like this is really bad for performance)
- store the image into an int array like suggested int array[2363 X 1265], althought I'm not quite sure how to do this one, do  you mean store the pixels into the array?

Again, thanks alot for the help guys!
Offline DzzD
« Reply #11 - Posted 2010-03-26 12:08:03 »

- store the image into an int array like suggested int array[2363 X 1265], althought I'm not quite sure how to do this one, do  you mean store the pixels into the array?
BufferedImage let you access their pixels using an int array, so you can create a bufferedimage of the same size as your rendering window and use it as a backbuffer

1  
2  
3  
4  
5  
6  
7  
BufferedImage BIImage;
if(alpha)
   BIImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_ARGB);
else
   BIImage = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
pixels=((DataBufferInt)(BIImage.getRaster().getDataBuffer())).getData();
image=(Image)BIImage
;   

you fill the BI pixels with the portion of the big image that is visible and then call   g.drawImage(image,0,0,null);   

Offline ChouChou

Senior Newbie





« Reply #12 - Posted 2010-03-27 02:46:09 »

O that looks interesting, I'll try it out and see how it goes  Grin
Offline ChouChou

Senior Newbie





« Reply #13 - Posted 2010-03-27 06:29:35 »

Scrolling background causes no lag now thanks alot for the help guys!


Edit: It still causes minor lag when I try to load more parts of the same image, but I'll try fix that.

Edit2: Load large background Image is making my graphics card's fan pretty loud, why is that? I can play some pretty graphics intensive games like crysis, fallout3 and the fan would be abit loud, but aren't 2D games less graphics intensive?
Offline DzzD
« Reply #14 - Posted 2010-03-27 14:06:15 »

the difference is that in your case you are probably not using a lot your graphics card (or very few, probably your cpu fan , no ? ), 3d game like crysis and such perform most task on the GPU, you can also do that using opengl with java (lwjgl / jogl / gl4java ) but as long as you dont "requiere" them due to complexe/heavy 2D graphics and/or 3D, I would recommend you to stick with java2d wich is easier to use and offer a better compatibility. and then later when you will feel confortable with java & graphics you may try them.

Quote
Edit: It still causes minor lag when I try to load more parts of the same image, but I'll try fix that.
you must use a different thread to asynchronously load other part of the image, this thread can set a flag once new parts of image are loaded and in your main thread you check this flag to know when new data are available

Offline ChouChou

Senior Newbie





« Reply #15 - Posted 2010-03-27 15:03:08 »

I've made my background image smaller (1600 * 765) and it is making things alot better, I guess I'll have to load a couple smaller image this size and connect them through code to speed up performance, the parallel scrolling is working good and thanks for the help!
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.

TehJavaDev (32 views)
2014-10-27 03:28:38

TehJavaDev (26 views)
2014-10-27 03:27:51

DarkCart (41 views)
2014-10-26 19:37:11

Luminem (22 views)
2014-10-26 10:17:50

Luminem (27 views)
2014-10-26 10:14:04

theagentd (33 views)
2014-10-25 15:46:29

Longarmx (61 views)
2014-10-17 03:59:02

Norakomi (58 views)
2014-10-16 15:22:06

Norakomi (47 views)
2014-10-16 15:20:20

lcass (43 views)
2014-10-15 16:18:58
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!