Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (109)
games submitted by our members
Games in WIP (537)
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  
  Whats wrong here, advice please!  (Read 3904 times)
0 Members and 1 Guest are viewing this topic.
Offline rackham

Junior Member





« Posted 2009-05-06 12:25:19 »

Hello everyone! I wanted to post a method of mine I use in a game which isnt performing as well as id like. When this method is called, which is every frame rendered for x frames after a game item has left the screen, it just renders some squares from the departure point, which expand whilst also fading away. Whenever this happens it causes the screen to flicker, could anyone tell me if anything i am doing in this code could be causing this?

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  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
public void drawDeathAnims(Graphics g){
     
     
      //loop through arraylist
     //while looping if any are incountered which are too old delete them
     for(int i = 0; i < departurePoints.size(); i++){
         int[] dpt = departurePoints.get(i);
         //check that it's not too old, for now just go up to 400;
        if(dpt[3]>deathAnimFrames){
            //delete
           departurePoints.remove(i);
            i--;
         } else{
            //if its the first time get the chainsize form last frame
           if(dpt[3]==0){
               dpt[4]=gameState.getLastScore()/gameState.getLastChain();
               departurePoints.get(i)[4]=dpt[4];
               
            }

            //has to be centered
           
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
                       (RenderingHints.VALUE_TEXT_ANTIALIAS_ON));
            g2.setFont(new Font("SansSerif",Font.BOLD,18));
           
            int opacity = 255-(255*dpt[3])/deathAnimFrames;
            String score = ""+(dpt[4]);
            int scoreX = GraphicUtils.getXPointForCenteringText(g2, score, Global.TILE_DIM);
           
            g2.setColor(new Color(0,0,0,opacity));            
            g2.drawString(""+score,
                  GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)+scoreX+2,
                  //(-dpt[3]/3)+
                 27+dpt[1]);
           
            g2.setColor(new Color(255,255,255,opacity));
            g2.drawString(""+score,
                  GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)+scoreX,
                  //(-dpt[3]/3)+
                 25+dpt[1]);
           
            int boxSize = Global.TILE_DIM+(dpt[3]*2);
            int boxXPos = GamePanel2.LEFT_BORDER+(dpt[0]*Global.TILE_DIM)-dpt[3];
           
            //g.setColor(Tile.getTileColor(dpt[2]));
           g.drawRect(boxXPos,
                  (dpt[1])-dpt[3],
                  boxSize,
                  boxSize);
           
            g.drawRect(boxXPos+1,
                  (dpt[1])-dpt[3]+1,
                  boxSize-2,
                  boxSize-2);
           
            int lessOpacity = opacity-50;
            if(lessOpacity < 0){
               lessOpacity = 0;
            }
           
            g.setColor(new Color(255,255,255,lessOpacity));
            g.drawRect(boxXPos+2,
                  (dpt[1])-dpt[3]+2,
                  boxSize-4,
                  boxSize-4);
           
           
            g.drawRect(boxXPos+3,
                  (dpt[1])-dpt[3]+3,
                  boxSize-6,
                  boxSize-6);
           
            int evenLessOpacity = opacity-50;
            if(evenLessOpacity < 0){
               evenLessOpacity = 0;
            }
           
            g.setColor(new Color(255,255,255,evenLessOpacity));
            g.drawRect(boxXPos+4,
                  (dpt[1])-dpt[3]+4,
                  boxSize-8,
                  boxSize-8);
           
           
            g.drawRect(boxXPos+5,
                  (dpt[1])-dpt[3]+5,
                  boxSize-10,
                  boxSize-10);
           
           
           
     
            //increse age by one
           dpt[3]++;
         }
     
      }      
   }


Thanks for any advice!

Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #1 - Posted 2009-05-06 14:16:16 »

I'm at work so I didn't have the time to read through it all to understand it but while quickly looking at it don't EVER use "new" in your display function, especially not in a loop. Reuse as much as possible by for example creating colors in the construct function and storing them for later usage, don't create new colors several times per frame.

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline rackham

Junior Member





« Reply #2 - Posted 2009-05-06 14:42:49 »

Hello, thanks for your reply, much appreciated.

I thought that might be the case, creating objects is an expensive process i hear, the only problem is as each frame i want the box to get more transparent, is there a better way of doing this that the way i am currently, which is creating a new Color object each update with a reducing Alpha channel?

Thanks

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #3 - Posted 2009-05-06 14:44:16 »

You are free to create thousands or even tens of thousands objects per second.


If anything, it will slow you down a bit, but it will certainly not cause flickering.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline rackham

Junior Member





« Reply #4 - Posted 2009-05-06 14:49:31 »

is this conflicting advice?!

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2009-05-06 15:30:27 »

What Mickelukas says is simply not true. As said, creating lots of objects might reduce performance by a few percent, if that, but in your post you complain about flickering. That's a totally different problem.

If you'd cache all your objects, your framerate might go up a tiny big, which will only make it flicker faster...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Mike

JGO Ninja


Medals: 71
Projects: 1
Exp: 5 years


Java guru wanabee


« Reply #6 - Posted 2009-05-06 15:35:03 »

What Mickelukas says is simply not true. As said, creating lots of objects might reduce performance by a few percent, if that, but in your post you complain about flickering. That's a totally different problem.

If you'd cache all your objects, your framerate might go up a tiny big, which will only make it flicker faster...

Creating lots of objects will slow down the application. If you read my post I'm not saying anything about the flickering. I didn't have the time to read through everything to know what was the cause.

"What Mickelukas says is simply not true." - Yea, right...

My current game, Minecraft meets Farmville and goes online Smiley
State of Fortune | Discussion thread @ JGO
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 757
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #7 - Posted 2009-05-06 15:42:56 »

Profile it, then you'll know.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline GDX

Senior Newbie





« Reply #8 - Posted 2009-05-06 15:43:38 »

Does everything on the screen flicker? What happens if you remove all the drawString code? Are you using double buffering for rendering?
Offline Hansdampf

Senior Member


Projects: 3


too offending?


« Reply #9 - Posted 2009-05-06 16:03:46 »

this won't heal your flickering, but it looks nicer... and who knows...
instead of
1  
2  
3  
4  
5  
6  
7  
8  
9  
for(int i = 0; i < departurePoints.size(); i++){
      ....  
      if(dpt[3]>deathAnimFrames){
         //delete
        departurePoints.remove(i);
         i--;
      } else{
               .....
    }

do
1  
2  
3  
4  
5  
6  
7  
8  
for(int i = departurePoints.size()-1; i > 0; i--){
      ....  
      if(dpt[3]>deathAnimFrames){
         //delete
        departurePoints.remove(i);
         continue;
      }
               .....


The creation of new Colors does not slow down your code more than 0.0000001%. I just tested creating millions of new Colors per second without a noticeable frame drop.

lots of sillystupid games: http://www.emaggame.com
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Wildern

Junior Member





« Reply #10 - Posted 2009-05-06 16:04:51 »

Just a guess, but it looks like you are drawing to the active graphics and no longer page flipping.  You may want to move this loop
for(int i = 0; i < departurePoints.size(); i++){ outside of your method and pass in the size as an argument so you can continue to render off-screen.
Offline indexunknown

Junior Member





« Reply #11 - Posted 2009-05-06 16:07:12 »

I'm almost definitely sure it's because of the drawing of rectangles with semi transparent color, because java2d can't handle beyond drawing couple rectangles with semi transparent color. It can handle primitives with solid colors fine, so that's my opinion.
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #12 - Posted 2009-05-06 16:15:06 »

A quick guess is that it has something to do with your use of Java2D, or more specifically alpha blending (which can be slow, depending on the JVM you're using) and/or drawing strings (especially with anti-aliasing, this can be slow).

I'd definitely profile to check what's wrong, but you can also deduce by removing drawing of your alpha blended squares and removing drawing of strings (just to see if one of them is your bottleneck).
Once you know where the bottleneck is, we can go from there.

I agree with Riven that it's unlikely that creating new objects is creating your problem, after all it seems like you're not creating that many objects anyway.

Offline rackham

Junior Member





« Reply #13 - Posted 2009-05-06 16:17:26 »

wow, lots of responces, thanks everyone! I will have a look in to the questions & suggestions raised and post back what i find.

Thanks again everyone!

Offline i30817

Junior Member





« Reply #14 - Posted 2009-05-06 17:00:43 »

Removing from a array list is death to performance especially if at the beginning (needs to move everything else you see). If you need removal and not random access use a linkedlist. If you need random access and not removal use a arraylist. If you need both either use a skiplist or check which is more used or use a arraylist and put your objects to remove at the end and remove right to left or if order is indifferent use a set.
Offline rackham

Junior Member





« Reply #15 - Posted 2009-05-08 14:11:07 »

Aha, i have found a solution!

I wasnt using active rendering (which i thought i had already implemeted) and was calling the repaint() method to paint my buffer to the screen.

Now no flickering / tearing, wahay!

Thanks for everyones help, a good few pointers there to make my game a bit more efficent, still have lots to learn!

Thanks! Grin

Offline ewjordan

Junior Member





« Reply #16 - Posted 2009-05-09 01:20:34 »

Creating lots of objects will slow down the application. If you read my post I'm not saying anything about the flickering. I didn't have the time to read through everything to know what was the cause.

"What Mickelukas says is simply not true." - Yea, right...
The issue is how many objects counts as "lots."  What Riven is saying is that you can allocate even 50k small objects per frame and still maintain 60 fps with no problems on any recent computer.  And that's in client mode!  Yes, it takes time, but it's not exactly slow.

I'd say that unless you're allocating at least 10k small objects per frame, it's not even worth worrying about, as it's almost certainly not your bottleneck.  Even if you are allocating that many, profile first and make sure that's actually where your time is best spent; unless you're working on a physics or graphics engine, there are probably other things that will give you more speed per hour spent optimizing.
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.

CogWheelz (17 views)
2014-08-01 22:53:16

CogWheelz (15 views)
2014-08-01 22:51:43

CopyableCougar4 (20 views)
2014-08-01 19:37:19

CogWheelz (19 views)
2014-07-30 21:08:39

Riven (27 views)
2014-07-29 18:09:19

Riven (16 views)
2014-07-29 18:08:52

Dwinin (14 views)
2014-07-29 10:59:34

E.R. Fleming (42 views)
2014-07-29 03:07:13

E.R. Fleming (13 views)
2014-07-29 03:06:25

pw (44 views)
2014-07-24 01:59:36
Resources for WIP games
by CogWheelz
2014-08-01 18:20:17

Resources for WIP games
by CogWheelz
2014-08-01 18:19:50

List of Learning Resources
by SilverTiger
2014-07-31 18:29:50

List of Learning Resources
by SilverTiger
2014-07-31 18:26:06

List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22
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!