Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (487)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (552)
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  
  Images loaded , make the game/program really really slow  (Read 1490 times)
0 Members and 1 Guest are viewing this topic.
Avenger
Guest
« Posted 2006-03-04 11:19:59 »

Hello all.My exams are over and I can once again continue learning programming Cheesy

Well I got back to my messing around code , after having done the 2D asteroids tutorial form cokeandcode and from a spanish site , and  now I am trying to make a simple ( the more simple the better) platform game.So messing around wth code I discovered that when I add  an image  ... really simple image the game goes really slow .. I mean with that image not loaded the player's movement is ok and smooth but when I paint that image ... it goes really slow and the fps drop A LOT.
Maybe the way I do it is wrong ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setColor(Color.blue);
        g.fillRect(0,0,getWidth(),getHeight());
       
        ground = spriteCache.getSprite("ground.jpg");
       
        player.paint(g);
        //g.drawImage(ground , 0 , 690 , this);  <---- with that one ..... the game goes slow as hell .. why is that ? :S
       
         paintfps(g);
         
        strategy.show();
    }



I am 100% sure that the answer is really really simple .. but I am a real noob Sad
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #1 - Posted 2006-03-04 12:42:32 »

How are you loading the image within "spriteCache.getSprite(String)" ?
Avenger
Guest
« Reply #2 - Posted 2006-03-04 12:44:01 »

Like that :

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  
private  BufferedImage loadImage(String filename)
        {
           url = this.getClass().getClassLoader().getResource(filename);
            try
            {
               
                return ImageIO.read(url);
            }
             catch(Exception e)
             {
                 e.printStackTrace();
                 System.exit(1);
                 return null;
             }
        }
   
   
    public BufferedImage getSprite(String name)
    {
        BufferedImage img = (BufferedImage)sprites.get(name);
        if(img == null)
        {
            img = loadImage("images/"+name);
            sprites.put(name , img);
        }
        return img;
    }
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Mr_Light

Senior Member




shiny.


« Reply #3 - Posted 2006-03-04 13:48:34 »

does the image get loaded at all?

your not initiating x streams? where x=your fps.

It's harder to read code than to write it. - it's even harder to write readable code.

The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
Offline Kova

Senior Member





« Reply #4 - Posted 2006-03-04 13:50:25 »

what kind of picture is it? transparent? how big is it?... if paintWorld() is renderer I don't see the problem.
Be carefull to make picture right and to show it right... once I drew picture wrong, trying to paint it beyond screen or it was corrupter (width and height in viewer didn't match what java got with loaded image) or something, it painted in the middle and everything slowed down with picture flickering.
Avenger
Guest
« Reply #5 - Posted 2006-03-04 14:10:13 »

it is just a big brown rectangle ..... maube it does go beyond the screen's limits .. maybe it's that ?

Hmmmm it had a width of 1024 ... , so did my application .. I lowered its width to 1020 , removed the comments .. and it worked.It no longer slows down the whole thing.But I would ike to understand the reason , so if anyone can explain it to me I would be grateful


Lol , another edit/question !

I wanted to find a nice sky picture .. I found one for testing only , but it was small.So somewhere I found this :

But now it starts to go slow as hell again ... why ?

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        sky = spriteCache.getSprite("background.jpg");
        g.setPaint(new TexturePaint(sky,new Rectangle(0,0, sky.getWidth(), sky.getHeight())));
        g.fillRect(0,0,getWidth(),getHeight());
        ground = spriteCache.getSprite("ground.png");
       
        player.paint(g);
        g.drawImage(ground , 0 , 690 , this);  
       
         paintfps(g);
         
        strategy.show();
    }
Offline Anon666

Junior Member




aka Abuse/AbU5e/TehJumpingJawa


« Reply #6 - Posted 2006-03-04 17:22:05 »

Are you using 1.5?

If not, that way of loading an image will not result in the image being elligable for hardware acceleration.
Avenger
Guest
« Reply #7 - Posted 2006-03-04 17:37:31 »

Yes I am using jdk1.5.0
Offline Kova

Senior Member





« Reply #8 - Posted 2006-03-04 18:13:18 »

yeah I would also like to know why pics acts like that when you drow them off screen (why that part isn't simply discarded). About texture thing... don't have a clue, worked only with pics  Undecided

Edit: Again, don't just give up when it dosen't work and post here... try to solve it yourself. First time it was drown beyond screen... maybe it's this time also? Try first drowing on half of screen... try, you'll never learn if others do it for you.
Avenger
Guest
« Reply #9 - Posted 2006-03-04 22:39:29 »

Yes ofcourse you are right , but I would like to use every means available to me to ask questions and from the answers learn.Of course I am still messing around with code and experimenting while expecting a reply here Smiley
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Avenger
Guest
« Reply #10 - Posted 2006-03-07 18:30:51 »

I know that replying to myself is not nice , but I would like someone to at least plz answer the question me and kova presented.So I bump this thread so the gurus in here help plz  Embarrassed
Offline Jeff

JGO Coder




Got any cats?


« Reply #11 - Posted 2006-03-08 10:25:56 »

My guess is that you have more then one bit of alpah in the image.

That will defeat the hardware rendering unless you set some special flags on windows...

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Avenger
Guest
« Reply #12 - Posted 2006-03-08 11:47:30 »

what is an alpah? Or even if u made a spellinmg mistake alpha?
Offline Jeff

JGO Coder




Got any cats?


« Reply #13 - Posted 2006-03-08 15:02:27 »

what is an alpah? Or even if u made a spellinmg mistake alpha?

Yup its a spellinmg mistake.

Got a question about Java and game programming?  Just new to the Java Game Development Community?  Try my FAQ.  Its likely you'll learn something!

http://wiki.java.net/bin/view/Games/JeffFAQ
Offline Kova

Senior Member





« Reply #14 - Posted 2006-03-08 16:10:23 »

Alpha is transparency as I remember it. It works but it's tricky with Java, book Killer Game Programming has good text about images and how to accelerate them.
Why do you even use TexturePaint? Why not just use picture with drawImage()? Here's what JavaDoc has to say about TexturePaint:

Quote from: JavaDoc
The TexturePaint class provides a way to fill a Shape with a texture that is specified as a BufferedImage. The size of the BufferedImage  object should be small because the BufferedImage data is copied by the TexturePaint object.

So maybe if you put your image in like 100x100, repeating, it would be better then one large image... and that is if image can be split into smaller repeating ones (I hope you know what I mean with "repeating").
Another thing... don't know if it has any effect on performace but in your paint() you calculate and copy image data every time. You shuold do that one time, in constructor, like:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
constuctor or something:
...
sky = spriteCache.getSprite("background.jpg");
sky_texture = new TexturePaint(sky,new Rectangle(0,0, sky.getWidth(), sky.getHeight()));
ground = spriteCache.getSprite("ground.png");
...
public void paintWorld()
    {
        Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
        g.setPaint(sky_texture);
        g.fillRect(0,0,getWidth(),getHeight());
       
        player.paint(g);
        g.drawImage(ground , 0 , 690 , this);
       
        paintfps(g);
         
        strategy.show();
    }

Also at the end, you might want to put g.dispose(); ... it releases system resources that g was using (you recreate it every time so it dosen't matter).
Try something of this, good luck.
Avenger
Guest
« Reply #15 - Posted 2006-03-11 11:37:20 »

Thanks a lot m8 , I will try to use all that you people adviced me to in here.I guess it all comes down to trying and trying and trying on my own Cheesy
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.

CopyableCougar4 (23 views)
2014-08-22 19:31:30

atombrot (34 views)
2014-08-19 09:29:53

Tekkerue (30 views)
2014-08-16 06:45:27

Tekkerue (28 views)
2014-08-16 06:22:17

Tekkerue (18 views)
2014-08-16 06:20:21

Tekkerue (27 views)
2014-08-16 06:12:11

Rayexar (65 views)
2014-08-11 02:49:23

BurntPizza (41 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (41 views)
2014-08-06 19:49:38
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!