Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » A simple egg timer

This thread is locked; no one can reply to it. rss feed Print
A simple egg timer
Samuel Henderson
Member #3,757
August 2003
avatar

I have a very simple question and I hope there is a very simple answer. ;D

I am trying to make a simple egg timer that will keep track of and display the number of seconds elapsed since pressing a "start timer" button. I'm just wondering what the best way of modifying my timing routine (which is the one mention in the manual FAQ) to count the seconds and then update the number of seconds on the screen. I suppose the screen only needs to be redrawn every 1 second, but would it be better if the screen was drawn at a normal rate?

I tried doing some googling for C++ timing routines and time counters and such, most of came up involved using <ctime> and "for (time_t t = time() + 5; time() < t; ) {}" or "Sleep(ms_to_sleep);" Either way, I figured there is probably a better way in my case.

Anyways, here is the code thus far...

1volatile int speed_counter = 0;
2 
3int checkQuit();
4 
5void increment_speed_counter()
6{
7 speed_counter++;
8}
9 
10int main()
11{
12 allegro_init();
13 install_keyboard();
14 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,300,240,0,0)<0)
15 {
16 allegro_message("ERROR SETTING GRAPHICS MODE :(");
17 exit(1);
18 }
19
20 BITMAP *bmpBackBuffer = create_bitmap(320,240);
21
22 LOCK_VARIABLE(speed_counter);
23 LOCK_FUNCTION(increment_speed_counter);
24 
25 
26 
27 install_int_ex(increment_speed_counter, BPS_TO_TIMER(1)); //should this be 1 or 60?
28
29 while (!checkQuit())
30 {
31 while (speed_counter > 0)
32 {
33 //update number of seconds
34 //check to see if buttons were pressed
35 speed_counter--;
36 }
37 blit(bmpBackBuffer,screen,0,0,0,0,320,240);
38 }
39
40 return 1;
41}END_OF_MAIN()
42 
43int checkQuit()
44{
45 if(key[KEY_ESC])
46 {
47 return 1;
48 }
49 return 0;
50
51
52}

This works great! Except it does nothing of what I want it to do ;D;)

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

KnightWhoSaysNi
Member #7,339
June 2006
avatar

I think this will count the seconds since the program started. I am still new to allegro though.

1#include <allegro.h>
2 
3volatile int speed_counter = 0;
4 
5int checkQuit();
6 
7void increment_speed_counter()
8{
9 speed_counter++;
10}
11 
12int main()
13{
14 allegro_init();
15 install_keyboard();
16 if(set_gfx_mode(GFX_AUTODETECT_WINDOWED,300,240,0,0)<0)
17 {
18 allegro_message("ERROR SETTING GRAPHICS MODE :(");
19 exit(1);
20 }
21 
22 BITMAP *bmpBackBuffer = create_bitmap(320,240);
23 
24 LOCK_VARIABLE(speed_counter);
25 LOCK_FUNCTION(increment_speed_counter);
26 
27 
28 
29 install_int_ex(increment_speed_counter, BPS_TO_TIMER(1)); //should this be 1 or 60?
30 
31 int seccount = 0;
32 
33 while (!checkQuit())
34 {
35 while (speed_counter > 0)
36 {
37 //update number of seconds
38 //check to see if buttons were pressed
39 seccount++;
40 speed_counter--;
41 }
42 clear(bmpBackBuffer);
43 textprintf_ex(bmpBackBuffer, font, 10, 10, makecol(255, 255, 255), -1, "Secs: %d", seccount);
44 blit(bmpBackBuffer,screen,0,0,0,0,320,240);
45 }
46 
47 return 1;
48}END_OF_MAIN()
49 
50int checkQuit()
51{
52 if(key[KEY_ESC])
53 {
54 return 1;
55 }
56 return 0;
57}

Samuel Henderson
Member #3,757
August 2003
avatar

I'll give that a shot Knight :)

[edit]
seems to be working ;D

I'm thinking now maybe I want it a bit more granular since I see how slow 1 second really is.

[edit 2]

Hmm.. I just noticed that the timer does not keep running when my computer is locked... this is going to be a bit of a problem. Is there a simple way to have the app run even while windows is locked?

[edit 3]

I think I may have just figured it out... what I can do is: at the start of program before the main loop get the current time and store that in a variable, in the main loop I can then subtract the value stored in that variable from the current time and store the result in seccount or whatever. I don't see any reason why this won't work, but I am too tired to test it right now.

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

Audric
Member #907
January 2001

The program is probably considered "switched in the background". If this is the case, it will not "beep" when the eggs are cooked, because execution does not continue at all.
You may want to set set_display_switch_mode(SWITCH_BACKAMNESIA) so your program doesn't stop when in background, and
set_display_switch_callback() to activate/deactivate your own displaying code.
Maybe somebody can confirm, I never used them.

edit: ahh, I was thinking of an alarm type, with countdown...If you only need the elapsed time, locked computer is no problem.

Samuel Henderson
Member #3,757
August 2003
avatar

Quote:

edit: ahh, I was thinking of an alarm type, with countdown...If you only need the elapsed time, locked computer is no problem.

I guess I should actually do both. I should have the egg timer function for counting down, as well as a stopwatch for counting up. Then the user can just select which option they want.

edit:

where would I be calling these functions?

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

BAF
Member #2,981
December 2002
avatar

Why are you using allegro for this? You'd probably have better luck with the whole running in the background thing using C#/it's GUI (if you're on windows), or if you won't want that, just use wx or something. (I mentioned C# because for an app like this, it would be easier).

Samuel Henderson
Member #3,757
August 2003
avatar

I think I got it... I just want to make sure...

edit:
Woohoo! Cookies and Kudos's for all!

edit 2:
ahh buggers. I can't give cookies while editting a post apparently..

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

BAF
Member #2,981
December 2002
avatar

Samuel Henderson
Member #3,757
August 2003
avatar

Ahh crap. I think I started the thread as the wrong type, I don't have the option for cookies even on a fresh post. Ahh well....

cookies go to: KnightWhoSaysNi, Audric and BAF for helping out! 8-)

=================================================
Paul whoknows: Why is this thread still open?
Onewing: Because it is a pthread: a thread for me to pee on.

Go to: