Title says it all.
I am simply trying to keep track of time
double startTime = al_get_time(); <-- Crash because al_get_time() returns -6.0120304019-18e etc.
(This is in my IntroState constructor)
in my timed logic, though it doesn't really has anything to say as it never reaches this point.
if (al_get_time() - startTime >= 5.0) {
nextState = STATE_MENU;
}
Never had this issue before, and this very same program, that hasn't been changed since actually worked yesterday.
Been looking for hours for a solution, can't find one..
I am so confused
I'm guessing you have a global IntroState object? Is that correct? If so, al_init won't have been called before calling al_get_time and it will crash. That's probably why you're getting the crazy return value for the time.
No I don't use globals, been trying for days now, still an error.
Still looking for solution.
Okay, but are you sure you're initializing allegro before you create your IntroState object?
Things like that just shouldn't happen unless you're calling allegro functions before allegro_init.
My other guess is that you updated allegro and you're using a different version of the allegro dll that is no longer compatible with your current allegro headers. It could cause problems like that. Ie. you updated your headers, but not your dlls. That could cause a runtime crash.
Otherwise, you're gonna have to show more code. Show main at least.
I initialize everything before trying to use any allegro functions.
I don't use globals.
Etc.. I follow everything that must be done.
Here's code.
GameState.h
IntroState.h
Seems like you've thought of everything! Questions:
Is this Windows?
Which version of Allegro?
Can you post the code where you're calling al_get_time() ?
As an aside, where you have
display = al_create_display(640, 480); if (!display) { al_destroy_display(display); return -1; }
this doesn't make sense; if al_create_display returns NULL, it's failed and there is nothing to call al_destroy_display on.
If al_get_time() is returning insane values, that's usually because you've called it before initializing Allegro. That doesn't appear to be the case here though, so it's definitely weird.
I see global variables though.
My other guess is that you updated allegro and you're using a different version of the allegro dll that is no longer compatible with your current allegro headers. It could cause problems like that. Ie. you updated your headers, but not your dlls. That could cause a runtime crash.
That's a possibility. You check for al_init succeeding, but you don't check for Initialize succeeding. al_init might fail, and and you might proceed anyway and get all sorts of crashes. Weird that al_start_timer doesn't crash though .
From my experience, when al_init fails, it's usually because of a version mismatch between the headers compiled against and the dll in use. Allegro does a version check on start up inside al_init that will fail if the runtime is different from the headers used.
Sorry for the late reply, been busy lately.
Well, actually there are no globals, there are scope variables, but they don't interfer with anything else.
Peter Hull:
Yes, windows.
Allegro 5.0.10, I also use 5.0.10 dlls, no mismatch in versions.I have posted the code where I am calling al_get_time() in IntroState class.
Nevermind that, I didn't actually.. Here's the code:
On your aside text, do you propose I should say:
if(!display = al_create_display(640,480)) { return 0; }
instead?
Still having issues with this, very weird.. As I said, it works on my desktop computer.
Looking at that code and what you posted above, you're definitely calling al_init() before al_get_time(), so not sure why you're getting insane values. The only thing I can think of is that initialization is failing behind your back (as mentioned above, you aren't checking the return value of Initialize() ).
al_init is most likely failing for some reason or another. Check the return value. :/
if(!display = al_create_display(640,480)) { return 0; }
I would just change your original code you posted for this to...
if(display == NULL) { return -1; }
You're also making the same mistake with your font loading...
arial = al_load_font("arial.ttf", 12, 0); if (!arial) { al_destroy_font(arial); exit(-1); }
You're trying to destroy the arial font, but that is only needed if you successfully loading it in the first place. If loading fails, than there is nothing to destroy, no need to call that. Just use
arial = al_load_font("arial.ttf", 12, 0); if (arial == NULL) { exit(-1); }
As for your main problem, according to the online docs (https://www.allegro.cc/manual/5/al_get_time): "The return value is undefined if Allegro is uninitialised."
Looking at your code, I see a problem right away... what is this...
int Initialize(ALLEGRO_DISPLAY *&display, ALLEGRO_EVENT_QUEUE *&event_queue, ALLEGRO_TIMER *&timer);
You have *&display? ? You only need &display in C++ or *display but not both.
So the problem is that Allegro is not being initialized properly.
So your function prototype should be...
int Initialize(ALLEGRO_DISPLAY &display, ALLEGRO_EVENT_QUEUE &event_queue, ALLEGRO_TIMER &timer);
and in main() you should have...
ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *event_queue; ALLEGRO_TIMER *timer; ALLEGRO_EVENT ev; Initialize(display, event_queue, timer);
No, *& is correct - he's passing the ALLEGRO_DISPLAY* by reference. I'm pretty sure ALLEGRO_DISPLAY& won't compile because that's an opaque type (i.e. Allegro doesn't expose the actual struct contents so the compiler doesn't know how large it is).
He's passing a pointer. So the function should take a pointer or a reference, but not both. Never used both together before and honestly, I am surprised it compiled like that.
He's passing a reference to the pointer, that's correct. al_create_display() returns ALLEGRO_DISPLAY*, so you have to have a reference to the pointer type if you want to store it.
It might clarify things to say that, if this were C where pass-by-reference isn't allowed, for Initialize() to work the way it's set up now, it would have to take an ALLEGRO_DISPLAY** (a pointer to the pointer).
As I said, it works on my desktop computer.
Dunno, did you say that? Do you mean the exact same binary copied from one computer to the other works on one and not the other? Or are you compiling the same source code on each one?
As has been mentioned al_init failing (somehow) seems likely - another possibility is that al_get_time is OK but the stored value (CIntroState::startTime) is being corrupted subsequently. If you get your debugger on the place where it's being set, is it always garbage?
Exact same source code, not binary.
But that doesn't really matter as I have set it up correctly, and as I said, it used to work before too. Never had any problems with this sort of code on my laptop before.
Anyways, matter is closed, will just not program on my laptop