|
Timer encapsulaton |
Aniconic
Member #15,133
May 2013
|
Hey there everyone, I've been working on encapsulating some allegro functions. Though I do have some experience with programming, I'm not as confident as I'd like to be. that being said I'm looking for some opinions on what I have for my timer class. I wrote the member function definitions in the class's header file because it's functions are pretty short and sweet, and I don't plan on changing them much. I'd like to keep all of the smaller components as concise as possible. 1#ifndef _TIMER_H
2#define _TIMER_H
3
4#include <allegro5/allegro5.h>
5
6class timer {
7 private:
8 ALLEGRO_TIMER *t;
9 int lastTime;
10
11 public:
12 /*default constructor*/
13 timer() {
14 t = al_create_timer(1);
15 lastTime = 0;
16 }
17
18 /*re-installer function*/
19 void init(double ticksPerSecond) {
20 al_destroy_timer(t);
21 t = al_create_timer(1.0/ticksPerSecond);
22 }
23
24 /*interface functions*/
25 void start() {al_start_timer(t); }
26 void stop() {al_stop_timer(t); }
27 bool running() {return al_get_timer_started(t); }
28 int count() {al_get_timer_count(t);}
29 bool tick() {
30 int curTime = timer::count();
31 if (curTime > lastTime) lastTime = curTime;
32 else return false;
33 return true;
34 }
35
36 /*default destructor*/
37 ~timer() {al_destroy_timer(t);}
38};
39
40#endif
Essentially, I plan to glue instances of this class together with other components to create new objects with a common "entity" interface, with the "tick" function being called in it's parent object's update function. I was just going to add the ticker to an event queue, but I wanted to keep it as independent as possible. Here's an example of how I was planning on using it as a countdown timer: 1#include <stdio.h>
2#include <allegro5/allegro.h>
3#include "timer.h"
4
5
6int main(int argc, char **argv)
7{
8 ALLEGRO_DISPLAY *display = NULL;
9
10 if(!al_init()) {
11 fprintf(stderr, "failed to initialize allegro!\n");
12 return -1;
13 }
14
15 display = al_create_display(640, 480);
16 if(!display) {
17 fprintf(stderr, "failed to create display!\n");
18 return -1;
19 }
20 timer ticker; //default constructor creates timer defaulting to 1 second
21 int counter = 1000; //countdown time
22
23 al_clear_to_color(al_map_rgb(0,0,0));
24
25 ticker.init(1000); //reinitialize to tick every millisecond
26
27 al_flip_display();
28
29 ticker.start();
30 while (counter != 0) {
31 if (ticker.tick()) counter--;
32 }
33
34 al_destroy_display(display);
35
36 //ticker automatically stopped and destroyed.
37
38 return 0;
39}
I personally think it will work quite well, but then again this is my first attempt with the Allegro library, and I am using it to sort of expand my horizons. I suppose my primary concern is any leaks I may have missed, as well as any performance related issues I didn't consider. I was also wondering if there was a way to allow an instance of this class to be created and then added to a specified event queue without giving it an event queue argument? I doubt it, but I would appreciate any suggestions for possible solutions. Also, I'd like to take a second to thank Schyfis and furinkan for their feedback on my previous thread. Although I didn't have time to post it earlier before the topic was locked, I really appreciate your feedback. The suggestions you guys offered gave me a lot of ideas for approaches to solving the aforementioned problems. |
Edgar Reynaldo
Major Reynaldo
May 2007
|
That's not the best way to use an ALLEGRO_TIMER. You're polling it constantly, which will use 100% cpu every time you do so. Use an event queue and wait for a timer event. That way the cpu can sleep, and your prog won't do anything until it gets a timer event. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Aniconic
Member #15,133
May 2013
|
What if instances of this class were only used within an entity's update function, which is only called after a global FPS timer event? Then would it be appropriate to use? My problem is that entities might not always have a reference to the event queue. Unless its beneficial to somehow have more then one? |
Edgar Reynaldo
Major Reynaldo
May 2007
|
Entities in your program shouldn't have any idea what an event queue is nor have access to one. Pipe the events from the queue to your entities in a HandleEvent function or similar or use an Update function that you call when you receive a timer event. Register your timer with an outside event queue and then monitor that queue for events, dispatching them to your engine / entities from there. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
|