Thread: Solutions for slow performance?

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Solutions for slow performance?

    Hey everyone! I'm just about done my game... But it runs really really slowly. I've done all I can think of to streamline the execution, but it's still not much faster. Although it runs fine most of the time, whenever there's a lot of objects on the screen, it'll slow to a crawl, and that sorta throws off the timings.

    I know that I can slow down the general pace of the game to make it run at the same speed at all times, but then it'd run slow all the time, which I don't want; I tried skipping frames, but sometimes it would skip up to 100+ frames, and you would start on one side of the screen and end up on the other... even though without skipping frames, it would run almost just as fast.

    So my question is, does anybody know of any alternate solutions? After all, any 3d game, being much more complex than my 2d shooter, would run at <3fps if there was no way to speed things up...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Sorry but I'm going to need a lot more information than that. What API are you using? Any code that would be relevant for us to see?

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm... I tried keeping the post short, but whatever

    API: I never did figure out what was an API and what wasn't... but for graphics and stuff I'm just using Windows GDI. And I really have no clue what code would be relevant... You can see my whole program if you want though Just click the link in my sig.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Rambling Man
    Join Date
    Jan 2002
    Posts
    1,050
    Well, I'm not sure if I can really help you specifically with you using Windows programming (me no know ). However, I might be able to help you out on a general level.

    Try to look at the drawing algorithm you are using. Try to weed out any unneccasary drawing actions in your algorithm. Such as if you have already drawn an object that has not changed then don't redraw it again. Try to figure out on every drawing routine what needs to be redrawn and what doesn't. Possibly think of a faster algorithm. Try to cut down on the calculations inbetween the drawing routines. Basically the main thing to do is to cut down on the processes you are performing in the program. The less amount of processes the more efficient your program will be. I'm sure if you look hard enough you'll be able to find inefficient code that can be improved.

  5. #5
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    I think I played your game before. If I remember correctly the background does not move? If you are redrawing the whole background, don't. Just draw the background over where the ship is, then draw the new ship. Understand? Basicly, dont redraw the whole screen, just erase the ships, then draw them at there new locations, and be sure to check if the ships even moved, if they didnt no need to redraw.

  6. #6
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Actually... if you click the link in my sig, you can compile the game yourself and try it I think it's been significantly improved since you last tried it (if it was my game you tried). The background does move, so I don't know... the background will always have to be redrawn, and I think it would slow it down to only redraw the background where nothing has changed.

    Unless... I could try creating a 'Star' class, and to draw each just draw a white dot, instead of using a bitmap for the background. Although then, I'd have to be constantly moving and redrawing several hundred Star's. Would that be inefficient?


    Hehe btw, nice poll on the website
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Hunter2
    Actually... if you click the link in my sig, you can compile the game yourself and try it I think it's been significantly improved since you last tried it (if it was my game you tried). The background does move, so I don't know... the background will always have to be redrawn, and I think it would slow it down to only redraw the background where nothing has changed.

    Unless... I could try creating a 'Star' class, and to draw each just draw a white dot, instead of using a bitmap for the background. Although then, I'd have to be constantly moving and redrawing several hundred Star's. Would that be inefficient?


    Hehe btw, nice poll on the website
    You could try creating the background on the fly. Make each star a single pixel so you can use (SetPixelV( ) ) when making them. Then store those bad boys in an STL Vector or the like and draw them. I think this would probably be a little faster, maybe not.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Then wouldn't I have to keep a variable for each star's position relative to the scrolling, and calculate where to plot each pixel every time I redraw?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Hunter2
    Then wouldn't I have to keep a variable for each star's position relative to the scrolling, and calculate where to plot each pixel every time I redraw?
    Just have an UpdateStars routine (in a CStar class for example) and just update the position there. When I get my MS VC++ back online i'll look at your code further but for now I was just thinking of stuff off the top of my head.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  10. #10
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Ok, thanks! I'll try that, see how it works out.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Humm. Well, I tried making individual stars as you suggested.... I believe it looks significantly more pleasant, and I daresay the download will be smaller now But the problem is, the performance isn't really noticeably faster Truthfully, now I think the slowdown has something to do with my rockets' movement code. This is the code:

    PHP Code:
    bool Rocket::move()
    {
         
    POINTFLOAT dest;
         if(
    aliens.size() > 0)
         {
              if(
    alienIndex == -|| prevAlienSize != aliens.size())
              {
                   
    alienIndex rand() % aliens.size();
                   
    targetStage 0;
                   
    prevAlienSize aliens.size();
              }

              
    dest.aliens[alienIndex].pos.+ (aliens[alienIndex].bmpDimensions.right bmpDimensions.right 2);
              
    dest.aliens[alienIndex].pos.+ (aliens[alienIndex].bmpDimensions.bottom - (bmpDimensions.bottom frames) / 2);

              
    double dist distanceTo(posdest);
              
    int angle2pf angleTo(posdest);
              
    int rot rotation 10;

              if(((
    angle2pf rot) >= && (angle2pf rot) < 180) || ((angle2pf rot) < -180))
                   ++
    rotation;
              else
                   --
    rotation;

              if(
    rotation 35)
                   
    rotation 0;
              if(
    rotation 0)
                   
    rotation 35;
         }
         else
         {
              
    alienIndex = -1;
         }

         
    POINTFLOAT temp angle2Point(rotation 10);

         
    thrust.+= .3f temp.x;
         
    thrust.+= .3f temp.y;

         if((
    thrust.thrust.x) + (thrust.thrust.y) > 100// >100 to avoid sqrt()
         
    {
              
    POINTFLOAT zeropf;
              
    zero.0;
              
    zero.0;

              
    float dst distanceTo(thrustzero) - 6;

              
    zero.0;
              
    zero.0;

              
    pf angle2Point(angleTo(thrustzero));

              
    thrust.+= dst pf.x;
              
    thrust.+= dst pf.y;
         }

         
    pos.+= thrust.x;
         
    pos.+= thrust.y;

         if(
    GetTickCount() - timeCreated >= 15000)
              return 
    false;
         else if(
    collide())
              return 
    false;
         else
              return 
    true;

    The angle functions take a lot of trig, and there are a lot of calls to them That's why I think the movement code takes a long time. Is there any way I can simplify this code or make it better?
    Last edited by Hunter2; 08-20-2002 at 03:57 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Well I briefly skimmed over your code. What compiler are you using? I do have 2 quick suggestions. One use floating point rather than double precision in your beginning code block. Also instead of the divide by 2 you can right shift 1 ( variable >> 1 ). Usually compilers will do this optimization for you but I know that some versions do not.

    Have you ever used a profiler? If you can do this I strongly suggest it. The profiler will write out a report of what function your program is in and for how long. You may find that a very unlikely function is taking more time than you orginally thought. Definitely look into this it is an awesome tool.

    Again, when I get my Visual C++ back online I'll look through your code and see if I can find some more optimizations. Also I have the profiler before mentioned and I will use it if you don't have it.

    EDIT: Just saw this...
    Code:
    if((thrust.x_*_thrust.x)_+_(thrust.y_*_thrust.y)_>_100)_// >100 to avoid sqrt()
    
    _____{
    
    __________POINTFLOAT_zero,_pf;
    
    __________zero.x_=_0;
    
    __________zero.y_=_0;
    
    
    
    __________float_dst_=_distanceTo(thrust,_zero)_-_6;
    
    
    
    __________zero.x_=_0;
    
    __________zero.y_=_0;
    
    
    
    __________pf_=_angle2Point(angleTo(thrust,_zero));
    
    
    
    __________thrust.x_+=_dst_*_pf.x;
    
    __________thrust.y_+=_dst_*_pf.y;
    
    _____}
    When you declare POINTFLOAT zero and you want it to initialize to zero you can do this to get the same effect quicker. Also I don't know why you set it to zero 2 times in the function. Either use the method below or a simple ZeroMemory( ). The way I show below is faster than ZeroMemory though.

    POINTFLOAT zero = { 0 };
    Last edited by MrWizard; 08-20-2002 at 04:38 PM.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  13. #13
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I'm using MSVC++6.0 professional edition (I thought that was in my sig?). Anyways, thanks for pointing out the double-setzero thingee; I never did notice it... And no I don't have a profiler (actually, I've never heard the word before). Could you use it for me?

    Oh, by the way, do you know where I can get a profiler? Sounds like a really useful tool
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  14. #14
    I would say "don't use GDI" but you would probably get mad

  15. #15
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Originally posted by Hunter2
    I'm using MSVC++6.0 professional edition (I thought that was in my sig?). Anyways, thanks for pointing out the double-setzero thingee; I never did notice it... And no I don't have a profiler (actually, I've never heard the word before). Could you use it for me?

    Oh, by the way, do you know where I can get a profiler? Sounds like a really useful tool
    You have one and you don't even know it!! It comes standard with MSVC++ 6.0 Pro. Just go into compiler options and enable profiling.
    Then there is another option I think somewhere around the "compile , exexcute, link" stuff. Shouldn't be too hard to find and man that tool is priceless.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  2. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 04:18 AM
  3. Observer Pattern and Performance questions
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2007, 11:12 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM