Thread: My First Attempt at a Game...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    10

    My First Attempt at a Game...

    Okay, so I've hardly ever used C++ before yesterday, but found out that using DirectX in C++ is actually quite similar to using DirectX in VB, at least, the way you blt things out and all that. So, I spent all of last night learning how to set up a C++ window and initializing DirectX and all that, and I've started on a simple space shooter.

    The one thing I'm worried about is as to whether or not I'm calculating my FPS correctly. The code I'm using is as follows:

    Code:
    Frames++;
    	
    	if((((End.QuadPart - StartTime.QuadPart) / 500000) / 2) / 5 == 1)
    	{
    		FrameRate = Frames;
    		QueryPerformanceCounter(&StartTime);
    		Frames = 0;
    	}
    The equation in the if is just checking to see if it's been a second since start time and end time. I'm not sure if it's exactly a second or not though, is there a better way to do this?

    BTW, right now I'm hitting a max FPS of 85 with 2500 moving sprites on the screen... Is this good for a C++ DirectX program?

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    bool Display(float timeDelta)
    {
      
      if (Device)
      {
        //Clear back buffer to BLACK R:0 G:0 B:0 or 0x00000000
        Device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,d3d::BLACK,1.0f,0);
            
        //Start scene
        Device->BeginScene();
        
        //Compute FPS
        ElapsedTime+=timeDelta;
        FrameCount++;
        if (ElapsedTime>=1.0f)
        {
          FPS=(float)FrameCount/ElapsedTime;
          ElapsedTime=0.0f;
          FrameCount=0;
        } 
    ...
    ...

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here is a screenie with 2500 asteroids, alpha blended fog (10 of em), spaceship, backdrop, and sound in the background.

    I think your FPS calc is off a bit.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Of course every asteroid there is going through z rotation, scaling, rotation and rendering.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    The way I do it:
    Code:
    QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
    while(!done)    // My maingame loop
    {
        QueryPerformanceCounter((LARGE_INTEGER*)&time0);
        // Do all the stuff that should be done
        QueryPerformanceCounter((LARGE_INTEGER*)&time1);
        time = (float)(time1-time0)/(float)freq;
        fps = 1.0f/time;
    }
    freq, time0 and time1 are all __int64
    time and fps are either double or float

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here is another shot with 2500 asteroids and vsync off - it seems that this is the best frame rate I can get with the engine using 2500 asteroids.

  7. #7
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Thanks for the replies! I'll try out these other methods and see what I get then... I just added in enemies to the game and I can get about 3000 of those on the screen before I get below 50 FPS.

  8. #8
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Try timing it with our code. That sounds little too much.

  9. #9
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Okay, thanks for the code Shakti!

    That looks like it's running pretty nice there Bubba! Looks much better than my crappy shooter, hehe.

    It looks like I can actually get about 10000 sprites on the screen before I get below 30 FPS now. Of course, the sprites aren't doing anything except moving right now. My computer is very crappy too, so this is pretty good FPS for me.

    I didn't realize how much faster C++ really was compared to VB :P

  10. #10
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Oh, I have another question. How can I lock the FPS at 30? I don't want it to go above that, because it changes too much right now, and I don't want the game looking like it's running really slow at 30 FPS like it does right now.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You can lock the FPS at 30 only if the frames are above 30. If the frames go below 30 then you must remove objects until the frames are back at 30. Some sort of auto-detail system is needed for that.

  12. #12
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Okay, so I can't just keep the FPS below 30 at all times?

  13. #13
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    Well, that would envoke timers and sleep and whatnot. Why would you want to lock the fps any way?? Look into timebased movements.

  14. #14
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Thanks, I'll look into those.

    After optimizing some code, I can get it running at 70 FPS with 10000 sprites on the screen now

  15. #15
    Registered User
    Join Date
    Dec 2004
    Posts
    10
    Okay, apparently I was calculating the FPS wrong. I finally fixed it and can get 2500 sprites, along with collision detection on all of them, running at 30 FPS.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. 2D RPG Online Game Project. 30% Complete. To be released and marketed.
    By drallstars in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 10-28-2006, 12:48 AM
  3. Try my game
    By LuckY in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-15-2004, 11:58 AM
  4. Game Design Topic #1 - AI Behavior
    By TechWins in forum Game Programming
    Replies: 13
    Last Post: 10-11-2002, 10:35 AM
  5. My Maze Game --- A Few Questions
    By TechWins in forum Game Programming
    Replies: 18
    Last Post: 04-24-2002, 11:00 PM