Thread: Frame problem

  1. #1
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46

    Frame problem

    Hey I am writing an Allegro Program and I want to be able to test the frame rate. I am using the header file time.h to measure time. When it is supposed to display the framerate, it displays weird symbols. I figure this means that there must be a problem with my data conversions somewhere, but I can't figure it out. I'm stumped.

    Here's my code:

    Code:
    #include <allegro.h>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    
    void main()
    {
    	int decimal;
    	int sign;
    
    	time_t newtime;
    	time_t oldtime;
    	double change;
    	double framerate=2.33637343;
    
    	string officialframerate;
    
    	char *rate;
    	char frames[]="Frame Rate: ";
    	allegro_init();
    	install_keyboard();
    	set_uformat(U_ASCII);
    	set_window_title("Greg's Game");
    	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    	text_mode(-1);
    
    	newtime=clock();
    
    
    
    	while(!key[KEY_ESC]) //game loop
    	{
    		oldtime=newtime;
    		newtime=clock();
    		change=difftime(newtime, oldtime);
    
    		framerate=1.00/change;
    		rate=_ecvt(framerate, 10, &decimal, &sign);  //converts double
    //to c string
    
    		officialframerate.append(frames);
    		officialframerate.append(rate);
    
    
    		acquire_screen();
    		textout(screen, font, officialframerate.c_str(), 7, 5, 9);  //// 
    //converts c++ string to c string for allegro
    		release_screen();
    		officialframerate.erase();
    	}
    
    }
    END_OF_MAIN()
    Hopefully someone can help me out.
    Thanks!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > void main()
    Well what can I say?

    > string officialframerate;
    What is this initialised to?

    > rate=_ecvt(framerate, 10, &decimal, &sign);
    Using a standard function like sprintf would mean more people could easily figure out what is going on.

    > textout(screen, font, officialframerate.c_str(), 7, 5, 9);
    Does
    textout(screen, font, "hello world", 7, 5, 9);
    work?
    It would help you figure out wether the problem is with the drawing code or the conversion code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    What exactly is the problem with Void Main?

    > rate=_ecvt(framerate, 10, &decimal, &sign);
    ecvt converts specified double(framerate) to a string. 10 is the maximum number of places to display on the String. Decimal returns how many decimal places there are, and sign returns if it is positive or negative.

    Yes textout(screen, font, "hello world", 7, 5, 9); does work.

    I will try using sprintf right now, but are there any other issues?

  4. #4
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    Ok I tried sprintf and I still have the same problem. Gibberish is posted after "Framerate: " Ok here is the updated code. Only change is now I use sprintf(); Oh and I changed it to int main, for Salem. Still same problem.


    Code:
     
    #include <allegro.h>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    #include <stdio.h>
    
    using namespace std;
    
    
    int main()
    {
    
    	time_t newtime;
    	time_t oldtime;
    	double change;
    	double framerate=2.33637343;
    
    	string officialframerate;
    
    	char rate[30];
    	char frames[]="Frame Rate: ";
    	allegro_init();
    	install_keyboard();
    	set_uformat(U_ASCII);
    	set_window_title("Greg's Game");
    	set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
    	text_mode(-1);
    
    	newtime=clock();
    
    
    
    	while(!key[KEY_ESC]) //game loop
    	{
    		oldtime=newtime;
    		newtime=clock();
    		change=difftime(newtime, oldtime);
    
    		framerate=1.00/change;
    		sprintf(rate, "%f", framerate);	//change now using sprintf 
    
    
    		officialframerate.append(frames);
    		officialframerate.append(rate);
    
    
    		acquire_screen();
    		textout(screen, font, officialframerate.c_str(), 7, 5, 9);  //converts c++ string to c string for allegro
    		release_screen();
    		officialframerate.erase();
    	}
           return 0;
    }
    END_OF_MAIN()
    Thanks,
    Last edited by gell10; 03-25-2005 at 03:15 PM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try reading the FAQ (re void main)
    Try doing a board search
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    Okay, I understand that void main() is not legal C++. I didn't know that and it's a shame many tutorials write void main(). Oh well, my problem still persists. Is it because the frame rate is too high, if I drew something on the screen would it go back to normal? I do not understand what is wrong with my code. Thanks for anyone that is able to help me, and thanks Salem for clearing up the void main thing for me.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps because
    Code:
    		change=difftime(newtime, oldtime);
    
    		framerate=1.00/change;
    If the first thing returns 0 (because you're not doing anything), the second is in deep doo-doo with a division by 0 error.

    > it displays weird symbols
    It wouldn't be something along the lines of "#Ind" or "#inf" or anything like that would it?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    I put in:
    Code:
    		if(change==0)
    		{
    			change=1;
    		}
    		framerate=1.00/change;
    But the problem persists.

    What's #Ind or #inf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM