#1
  1. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11

    Displaying FPS, gdi32.


    This is my msgloop.
    Code:
    	while(1) { 
    		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)) {
    			if(msg.message == WM_QUIT) 
    				break; 
    			else {
    				TranslateMessage(&msg); 
    				DispatchMessage(&msg); 
    			}
    		}
    		else {
    			//caLc's 
    			Render(hwnd, hdc, 30, 20, 20, 60, 60); 
    		}
    	}
    Render is below, I want it to display the FPS, and in this case, go at 30ish FPS.
    Code:
    int DrawCircle(HWND hwnd, HDC hdc, int topx, int topy, int bottomx, int bottomy) {
    	Ellipse(hdc, topx, topy, bottomx, bottomy); 
    	return 0; 
    }
    
    void clr(HDC hdc)
    {
    	RECT rect={
    				0,\
    				0,\
    				WINWIDTH,\
    				WINHEIGHT}; 
    	FillRect(hdc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH)); 
    	return; 
    }
    
    int Render(HWND hwnd, HDC hdc, unsigned int FPS, int topx, int topy, int bottomx, int bottomy) {
    	static unsigned int older=0; 
    	static unsigned int older1=0; 
    	static int FPScount=0; 
    	char buff[128]; 
    
    	//If 1 second passed, reset fps counter.
    	if(GetTickCount() >= older1+1000) {
    		older1=GetTickCount(); 
    		FPScount=0; 
    	}
    
    	//Display FPS.
    	sprintf(buff, "%d FPS", FPScount); 
    	TextOut(hdc, 3, 3, buff, lstrlen(buff)); 
    
    	if(GetTickCount() >= older+FPS) {
    		older=GetTickCount(); 
    
    		//Clear screen. 
    		clr(hdc); 
    
    		//Draw ****.
    		DrawCircle(hwnd, hdc, topx, topy, bottomx, bottomy); 
    		FPScount++; 	
    
    	}
    
    	return 0; 
    }
    It somewhat works but it shows the fps increase and reset, I want it to just display the # (constant 30ish).
  2. #2
  3. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    Here is an update, it kind of works now but 1 thing. I want the user to enter the FPS to run it at.. What in my code do I fix to do that? right now if I enter 10, the counter shows 100FPS.

    Code:
    int Render(HWND hwnd, HDC hdc, unsigned int FPS, int topx, int topy, int bottomx, int bottomy) {
    	static unsigned int older=0; 
    	static unsigned int older1=0; 
    	static int FPScount=0; 
    	static int FPStemp=0; 
    	char buff[128]; 
    
    	if(GetTickCount() >= older+FPS) {
    		older=GetTickCount(); 
    
    		//Clear screen. 
    		clr(hdc); 
    
    		//Draw ****.
    		DrawCircle(hwnd, hdc, topx, topy, bottomx, bottomy); 
    		FPScount++; 
    
    	}
    	//If 1 second passed, reset fps counter.
    	if(GetTickCount() >= older1+1000) {
    		older1=GetTickCount(); 
    		FPStemp=FPScount; //record fps before reseting. 
    		FPScount=0; 
    	}
    
    	//Display FPS.
    	sprintf(buff, "%d FPS", FPStemp); 
    	TextOut(hdc, 3, 3, buff, lstrlen(buff)); 
    
    	return 0; 
    }
  4. #3
  5. Cast down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    321
    Rep Power
    11
    Alright, I had to divide by 1000. (fps to ms).. Is their anything wrong with my code/obvious optimizations? Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo