Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
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  
  Slow fade effect  (Read 4703 times)
0 Members and 1 Guest are viewing this topic.
Offline leemon

Senior Newbie




Java games rock!


« Posted 2004-12-31 18:03:32 »

Hi!

I'm using the following code to do a fade transition effect between two images:

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  
      public void render(Graphics g, Image imageFrom, Image imageTo, int width, int height) {
 
            BufferedImage buffer=gc.createCompatibleImage(width,height);
 
            Graphics2D g2 = (Graphics2D)buffer.getGraphics();  
                               
            int counter=0;
            while (counter<90) {
                  counter=counter+2;
                  double alphaScalar = Math.sin(Math.toRadians(counter));
 
                  g2.setComposite(AlphaComposite.SrcOver);
                  g2.drawImage(imageFrom, 0, 0, null);  
                  g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)alphaScalar));
                  g2.drawImage(imageTo, 0, 0, null);
                  g.drawImage(buffer, 0, 0, null);
                  try {
                        Thread.sleep(5);  
                  }
                  catch(InterruptedException ex){}
            }                                                                    
           
            g.dispose();
            g2.dispose();
            buffer.flush();
      }
 


My main problem is that using a buffer to save the image composite before drawing it over the panel's Graphic context slows the effect considerably. I have other effects (scrolling, sliding and some more) which don't require the use of an intermediate buffer and they are way faster without it. I'm looking for a way to improve the perfomance of my fade effect. I've tried to draw the image composite over the panel's Graphic context directly (no buffer) but the screen flickers. Can anyone help me, please ?

Regards
Offline leemon

Senior Newbie




Java games rock!


« Reply #1 - Posted 2005-01-01 11:05:09 »

Before you ask me, I've already tried to use the BufferStrategy class instead of my intermediate buffer with fairly similar results. I cannot use FSEM, therefore I'm unable to use the page flipping, and I have to resort to blit the contents from the buffer unto the panel's graphic context. Is there any way to speed up blit operations in java ?
Offline woogley
« Reply #2 - Posted 2005-01-01 11:56:24 »

this is kind of off the subject, because I don't have an answer for you.

anyway just because you can't use FSEM doesn't mean you can't use BufferStrategy. BufferStrategy can work in a windowed JFrame. Or if you're stuck with AWT, did you know Canvas supports BufferStrategy? Tongue

either way, I doubt that would help alot, since you already said BufferStrategy wasn't much faster for you..
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #3 - Posted 2005-01-01 15:00:17 »

The problem with the slowness is that you create a new BufferedImage every frame of your rendering.  This is completely unnecessary.  Take out that intermediate image.  Draw it straight on to your destination.  Also... you never even use 'g'.  Why is it there and why do you dispose it if you never draw anything on it?  By the structure of your code I would assume that 'g' is the Graphics object for your screen buffer.  So remove the 'buffer' and 'g2' obects and just draw onto 'g'.

This is exactly what I do in Rimscape to fade images.  What you'll want to do in drawing on 'g' is draw one image at (float)alphaScalar and the other image at 1f - (float)alphaScalar.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline woogley
« Reply #4 - Posted 2005-01-01 15:48:33 »

Malokhan, buddy, I think you're head is a bit clouded today Tongue
Quote
The problem with the slowness is that you create a new BufferedImage every frame of your rendering.

I don't know where you see that. That render method is actually rendering 90 frames. Notice his code:
1  
2  
3  
4  
5  
6  
while (counter<90) {
  ... rendering ...
  try {
    Thread.sleep(5);
  }
  catch (InterruptedException e) {}

Quote

Also... you never even use 'g'.  Why is it there and why do you dispose it if you never draw anything on it?

again, look at his code:
1  
g.drawImage(buffer, 0, 0, null);

maybe you need a nap Malo Wink
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #5 - Posted 2005-01-01 16:13:49 »

haha whoops Wink  Yes it's not re-created EVERY frame, but it's existance can still be done without.  Sorry for the misreadings... new years, you know Wink  Yeah, the advice still holds though.  You don't need that buffer at all.

EDIT:  Another idea you could try in keeping the buffer would be to draw imageFrom once on your buffer, and then each from draw on imageTo with a constant alpha, like .01f and see how that works.  It might produce a similar effect to just slowly add on the new image, rather than totally redraw the whole effect each frame.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline leemon

Senior Newbie




Java games rock!


« Reply #6 - Posted 2005-01-01 16:42:43 »

Quote
haha whoops Wink  Yes it's not re-created EVERY frame, but it's existance can still be done without.  Sorry for the misreadings... new years, you know Wink  Yeah, the advice still holds though.  You don't need that buffer at all.


I've already tried to draw directly unto the panel's graphic context resulting in an annoying ghosting effect between the two images which doesn't appear if I use that intermediate buffer to draw the composite.

BTW, I know it's the buffer slowing the effect to a crawl, thanks for reminding me of this. I have other effects (scrolling and sliding) where I draw directly unto the panel's graphic context (no intermediate buffer) and they are pretty fast.

I'd like to know how to make the same effect without using this buffer.
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #7 - Posted 2005-01-02 00:22:36 »

does Rimscape do the effect you're looking for?  Try changing from one menu to another and see it.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline leemon

Senior Newbie




Java games rock!


« Reply #8 - Posted 2005-01-02 05:36:06 »

Quote
does Rimscape do the effect you're looking for?  Try changing from one menu to another and see it.



I've tried playing your game without success. BTW, do you mean this thread ?

http://www.java-gaming.org/cgi-bin/JGNetForums/YaBB.cgi?board=2D;action=display;num=1103525254

Could I see the new code for your ScreenFader class ?


Thanks
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #9 - Posted 2005-01-02 14:10:46 »

Bummer!!  What OS are you on.  Does the game say it sent me an error message?  I'd like to find out what's wrong with that so I'll check the logs just in case.  Here's the ScreenFader class now:

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  
import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

/**
 * @author Malohkan
 */

public class ScreenFader {

      int[]            pixelsToAdd;
      double            currentAlpha, totalAlpha, currentTicks;
      final double      MAX_TICKS = 6;
     
      int                  xOffset, yOffset, w, h;
     
      BufferedImage      backGround, foreGround;
     
      public ScreenFader(int width, int height) {
           
            totalAlpha = 255;
      }
     
      public void setBackground(BufferedImage image) {
         
          backGround = image;
      }
     
      public void setForeGround(BufferedImage image) {
         
            totalAlpha = 0;
            currentTicks = 0;
            currentAlpha = 0;
           
            foreGround = image;
      }
     
      public void run(double ticks) {
          currentTicks += ticks;
          currentAlpha = 255d*currentTicks/MAX_TICKS - currentAlpha;
     
          totalAlpha += currentAlpha;
           
      }
     
      public void render(Graphics2D g2d) {
           
          Composite c = g2d.getComposite();
         
          g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1.0f - (float)(totalAlpha/255.0)));
          g2d.drawImage(backGround, 0, 0, null);
          g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float)(totalAlpha/255.0)));
          g2d.drawImage(foreGround, 0, 0, null);
         
          g2d.setComposite(c);
      }
     
      public boolean isActive() {
            return totalAlpha < 255;
      }
}

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline leemon

Senior Newbie




Java games rock!


« Reply #10 - Posted 2005-01-02 18:23:22 »

Quote
Bummer!!  What OS are you on.  Does the game say it sent me an error message?  I'd like to find out what's wrong with that so I'll check the logs just in case.


Sorry, it was my fault Tongue. It works nicely, a bit slow in my computer, though.

I copied your render() code and I get that annoying ghosting effect still. I don't know what I'm doing wrong.  :-/
How do you call the render() op ?

Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #11 - Posted 2005-01-02 18:36:50 »

explain what you mean by "ghosting" please.

If I'm drawing the fade effect it's render method is called and nothing else.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Offline Catharsis

JGO Ninja


Medals: 73
Projects: 1
Exp: 21 years


TyphonRT rocks!


« Reply #12 - Posted 2005-01-04 03:20:58 »

I used to have a link to some really cool pixel based tutorials for fading... The links were gone, but I tracked down the author Remon Van Vliet and he put them back online:

http://www.codeberry.com/tutorials/tutorial-1-2-1.html

This might be right up your alley Leemon...

Check out the TyphonRT Video Suite:
http://www.typhonvideo.com/

Founder & Principal Architect; TyphonRT, Inc.
http://www.typhonrt.org/
http://www.egrsoftware.com/
https://plus.google.com/u/0/+MichaelLeahy/
Offline Malohkan

Senior Devvie




while (true) System.out.println("WOO!!!!");


« Reply #13 - Posted 2005-01-04 14:04:58 »

yeah that guy rocks Smiley  That page is where I got my code to saturate images with color.

Admin and Game Developer at
GameLizard.com
Play Rimscape!    |    Play Conquer!
Pages: [1]
  ignore  |  Print  
 
 

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

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

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

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

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

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

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

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

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

Solater (172 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!