-
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!
-
> 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.
-
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?
-
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,
-
Try reading the FAQ (re void main)
Try doing a board search
-
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.
-
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?
-
I put in:
Code:
if(change==0)
{
change=1;
}
framerate=1.00/change;
But the problem persists.
What's #Ind or #inf