I can remember how to do this correctly, so any advice would be welcome.
Also...I am still learning how to use STL, so please explain...
I am trying to make a clock manager that will update all the clocks that get registered. The clock manager will continue to update the clocks until one gets to it's time limit. Then the clock manager will signal that a clock needs servicing, and which clock it is. The clock manager does not reset the clock by itself, it is left up to the program to reset the clock...might add a feature that will reset automatically after a certain amount of time.
So we have the clock class and a clock manager. Both are listed below:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | class Clock |
3 | { |
4 | public: |
5 | Clock(); |
6 | ~Clock(); |
7 | void Reset( void ); |
8 | void Update( void ); |
9 | long iID; |
10 | long iBeat; |
11 | long iTick; |
12 | bool bActive; |
13 | int iTicksPerBeat; |
14 | }; |
15 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
16 | class ClockManager |
17 | { |
18 | public: |
19 | ClockManager(); |
20 | ~ClockManager(); |
21 | void Update(); |
22 | void RegisterClock( Clock *pClock ); |
23 | void RemoveClock( long iClockID ); |
24 | void ResetClock( long iClockID ); |
25 | Clock * FindClock( long iClockID ); |
26 | ClockList *pClockList; |
27 | long iNumClocks; |
28 | }; |
29 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
I guess my question is: How do I return a pointer from the STL list for the FindClock function. I am using the iterators of the list, but it does not allow for a pointer to be returned. I need a pointer, because if there is no match...then I can't use a reference as there would be nothing to reference!
Thanks in advance...and HAPPY NEW YEARS EVERYONE! ( I'm not doing so great right now...had this fever of 103F for like 2 days now and hurt all over... )
containerType<Clock>::iterator i; Clock* c = &*i;
That's a pointer from an iterator. If the iterator become invalid, so will that pointer though. Keep that in mind when choosing your container.
I had:
return (Clock*)*&i;
I tryed:
return (Clock*)&*i;
Still get an error when I try to use it with:
I add the clocks using:
1 | /////////////////////////////////////////////////////////////////////////////////// |
2 | // Clock System Test |
3 | Clock *pClock = NULL; |
4 | ClockManager *pClockMGR = new ClockManager; |
5 | pClock = new Clock; |
6 | pClock->bActive = true; |
7 | pClockMGR->RegisterClock(pClock); |
8 | delete pClock; |
9 | pClock = NULL; |
10 | pClock = new Clock; |
11 | pClock->bActive = true; |
12 | pClock->iTicksPerBeat = 12; |
13 | pClockMGR->RegisterClock(pClock); |
14 | delete pClock; |
15 | pClock = NULL; |
16 | /////////////////////////////////////////////////////////////////////////////////// |
I believe that is the right way to add them, right? I still get errors when trying to access any element (clock) other than the first one.
I have no idea what you're talking about:
I believe that is the right way to add them, right?
You're writing the class. How should I know what the correct way to use it is? Furthermore, you so far have not shown one line of STL code.
Still get an error when I try to use it with:
YOu'd have better luck telling me what the error is.
Ok...thanks for the harsh reality...
Here is the entire code:
UI.h:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <UI.h> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #ifndef UI_H |
5 | #define UI_H |
6 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
7 | #include <allegro.h> |
8 | #include <list> |
9 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
10 | class Clock; |
11 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
12 | using namespace std; |
13 | typedef list<Clock> ClockList; |
14 | extern ClockList *pClockList; |
15 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
16 | extern volatile int iGameTick; |
17 | void InitSYS( void ); |
18 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
19 | class Clock |
20 | { |
21 | public: |
22 | Clock(); |
23 | ~Clock(); |
24 | void Reset( void ); |
25 | void Update( void ); |
26 | long iID; |
27 | long iBeat; |
28 | long iTick; |
29 | bool bActive; |
30 | int iTicksPerBeat; |
31 | }; |
32 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
33 | class ClockManager |
34 | { |
35 | public: |
36 | ClockManager(); |
37 | ~ClockManager(); |
38 | void Update(); |
39 | void RegisterClock( Clock *pClock ); |
40 | void RemoveClock( long iClockID ); |
41 | void ResetClock( long iClockID ); |
42 | Clock * FindClock( long iClockID ); |
43 | ClockList *pClockList; |
44 | long iNumClocks; |
45 | }; |
46 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
47 | #endif // UI_H |
48 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
UI.cpp:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <UI.cpp> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #include "UI.h" |
5 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
6 | volatile int iGameTick = 0; |
7 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
8 | void GameClock( void ) |
9 | { |
10 | iGameTick++; |
11 | } |
12 | END_OF_FUNCTION(GameClock); |
13 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
14 | void InitSYS( void ) |
15 | { |
16 | allegro_init(); |
17 | install_timer(); |
18 | install_keyboard(); |
19 | install_mouse(); |
20 | LOCK_VARIABLE(iGameTick); |
21 | LOCK_FUNCTION(GameClock); |
22 | install_int_ex(GameClock,MSEC_TO_TIMER(10)); |
23 | set_color_depth(32); |
24 | set_gfx_mode(GFX_DIRECTX_WIN,400,400,0,0); |
25 | set_color_conversion(COLORCONV_TOTAL); |
26 | } |
27 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
28 | Clock::Clock() |
29 | { |
30 | bActive = false; |
31 | iID = 0; |
32 | iTick = 0; |
33 | iBeat = 0; |
34 | iTicksPerBeat = 1; |
35 | } |
36 | Clock::~Clock() |
37 | { |
38 | } |
39 | void Clock::Reset( void ) |
40 | { |
41 | iBeat = 0; |
42 | } |
43 | void Clock::Update( void ) |
44 | { |
45 | if ( !bActive ) |
46 | return; |
47 | iTick++; |
48 | if ( iTick >= iTicksPerBeat ) |
49 | { |
50 | iBeat++; |
51 | } |
52 | } |
53 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
54 | ClockManager::ClockManager() |
55 | { |
56 | iNumClocks = 0; |
57 | pClockList = new ClockList; |
58 | } |
59 | ClockManager::~ClockManager() |
60 | { |
61 | if ( pClockList ) |
62 | delete pClockList; |
63 | } |
64 | void ClockManager::Update() |
65 | { |
66 | ClockList::iterator i; |
67 | for ( i = pClockList->begin(); i != pClockList->end(); ++i ) |
68 | { |
69 | i->Update(); |
70 | } |
71 | } |
72 | void ClockManager::RegisterClock( Clock *pClock ) |
73 | { |
74 | if ( !pClock ) |
75 | return; |
76 | pClockList->push_back(*pClock); |
77 | iNumClocks++; |
78 | } |
79 | void ClockManager::RemoveClock( long iClockID ) |
80 | { |
81 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
82 | return; |
83 | } |
84 | void ClockManager::ResetClock( long iClockID ) |
85 | { |
86 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
87 | return; |
88 | } |
89 | Clock * ClockManager::FindClock( long iClockID ) |
90 | { |
91 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
92 | return NULL; |
93 | ClockList::iterator i; |
94 | for ( i = pClockList->begin(); i != pClockList->end(); ++i ) |
95 | { |
96 | if ( i->iID == iClockID ) |
97 | { |
98 | return (Clock*)&*i; |
99 | } |
100 | } |
101 | return NULL; |
102 | } |
103 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
104 | ClockList *pClockList = NULL; |
105 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
Main.cpp:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <Main.cpp> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #include "UI.h" |
5 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
6 | int main( void ) |
7 | { |
8 | //////////////////////////////////////////// |
9 | // Setup the system |
10 | InitSYS(); |
11 | //////////////////////////////////////////// |
12 | |
13 | |
14 | |
15 | /////////////////////////////////////////////////////////////////////////////////// |
16 | // Clock System Test |
17 | Clock *pClock = NULL; |
18 | ClockManager *pClockMGR = new ClockManager; |
19 | pClock = new Clock; |
20 | pClock->bActive = true; |
21 | pClockMGR->RegisterClock(pClock); |
22 | delete pClock; |
23 | pClock = NULL; |
24 | pClock = new Clock; |
25 | pClock->bActive = true; |
26 | pClock->iTicksPerBeat = 12; |
27 | pClockMGR->RegisterClock(pClock); |
28 | delete pClock; |
29 | pClock = NULL; |
30 | /////////////////////////////////////////////////////////////////////////////////// |
31 | |
32 | |
33 | |
34 | // Main loop |
35 | while ( true ) |
36 | { |
37 | //////////////////////////////////////// |
38 | // Check for a key |
39 | if ( keypressed() ) |
40 | { |
41 | //////////////////////////////////// |
42 | // Exit on ESCAPE key |
43 | if ( key[KEY_ESC] ) |
44 | break; |
45 | //////////////////////////////////// |
46 | // Reset key buffer |
47 | clear_keybuf(); |
48 | //////////////////////////////////// |
49 | } |
50 | //////////////////////////////////////// |
51 | // Process Game Clock |
52 | if ( iGameTick >= 1 ) |
53 | { |
54 | //////////////////////////////////// |
55 | // Do some action... |
56 | pClockMGR->Update(); |
57 | //////////////////////////////////// |
58 | // Reset the game tick |
59 | iGameTick = 0; |
60 | } |
61 | //////////////////////////////////////// |
62 | textprintf(screen,font,0,0,makecol(255,255,255),"0:%i,%i",pClockMGR->FindClock(0)->iBeat, |
63 | pClockMGR->FindClock(0)->iTicksPerBeat); |
64 | // This line creates an error: |
65 | //textprintf(screen,font,0,8,makecol(255,255,255),"1:%i,%i",pClockMGR->FindClock(1)->iBeat, |
66 | // pClockMGR->FindClock(1)->iTicksPerBeat); |
67 | } |
68 | //////////////////////////////////////////// |
69 | // Shutdown and exit |
70 | delete pClockMGR; |
71 | return 0; |
72 | //////////////////////////////////////////// |
73 | } |
74 | END_OF_MAIN() |
75 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
And the error code:
The instruction at "0x00401494" referenced memory at "0x00000010". The memory could not be "read".
Put the clocks in structs, and put a string in the struct for the clock's name, then search for the name.
1 | typedef struct { |
2 | cClock* Clock; //note the capitol C as not to interfere with the clock() function |
3 | std::string name; |
4 | }clock_t; |
5 | |
6 | typedef list<clock_t*> ClockList; |
7 | |
8 | class cClockManager { |
9 | |
10 | public: |
11 | |
12 | int AddClock(cClock* src, char* name); //add a clock and give it a name |
13 | cClock* GetClock(char* name); //search for a clock by name |
14 | |
15 | private: |
16 | |
17 | ClockList Clocks; |
18 | }; |
19 | |
20 | /////// |
21 | |
22 | int cClockManager::AddClock(cClock* src, char* name) |
23 | { |
24 | //this function will add a new clock |
25 | |
26 | if (!src) |
27 | return -1; //return an error if the clock isn't there |
28 | |
29 | clock_t* dest = new clock_t; |
30 | dest->Clock = src; |
31 | dest->name = name; |
32 | Clocks.push_back(dest); //add the new one to the list |
33 | |
34 | return 0; |
35 | } |
36 | |
37 | ////// |
38 | |
39 | cClock* cClockManager::GetClock(char* name) |
40 | { |
41 | //this function will get a clock by name |
42 | for (ClockList::iterator i = Clocks.begin(); i != Clocks.end(); i++) |
43 | { |
44 | if ((*i)->name == name) |
45 | { |
46 | return (*i)->Clock; //found the clock |
47 | } |
48 | } |
49 | |
50 | return NULL; //no clock by that name was found. |
51 | } |
and using it...
//main.cpp cClock* myclock = new cClock(); cClockManager* MyClockManager = new cClockManager(); MyClockManager->AddClock(myclock,"clock_1"); cClock* otherclock = MyClockManager->GetClock("clock_1"); //voila
I have not tested this code, but I use something similar for various data types in my game.
Instead of stashing naked Clock pointers in your container, what you could do instead is stash smart pointers - basically, some kind of reference countrf wrapper to your object (although I've never used it, I hear boost should do the trick). I think you'll find that'll solve all your problems, since you can simply return the object itself, ie *iter, and you don't need to worry about the iterator pointer becoming invalid, etc.
IonBlade's method is unnecessary. If you want to give clocks a name, make them hold the name, don't make a seperate struct for holding a name and a reference to a clock.
Wil's method is unnecessary because this application has no need for pointers at all.
Your method has some problems, but you're on the right track
typedef list<Clock> ClockList;
This means that your container is a std::list.
extern ClockList *pClockList;
You don't need this global variable, get rid of it.
class ClockManager { // ... ClockList *pClockList; };
There is no need for this variable to be a pointer. Making it a pointer is just going to complicate things.
delete pClock; pClock = NULL; pClock = new Clock;
This code isn't actually necessary, because RegisterClock makes a copy of the Clock that it stores. You can remove all 3 of these lines and your code will still work.
Thanks all...can anyone test this on their machine? I want to make sure it's my fault and not windows xp or my hardware. I've seen this stupid error message before from other programs...tested my memory for like 3 hours and everything seemed fine. Did a quick google search for "The memory could not be "read"" and it looks like it might be a problem with xp that microsoft doesn't acknowledge yet.
Edit:
I found that it is my program: Access violation. That would be me! I've never really used the STL before...so it's all pretty new to me and the templates are a little weird to learn. Anyone have a good link for STL docs? Thanks again all...
Um, CGames, the way his code is currently implemented, that delete, setting of the pointer to null, IS necessary. The code generates a copy of the object, NOT the pointer, because the container he has is of Clock objects, not Clock Pointers.
Now, if his code was something like this, the delete call would be unnecessary:
std::list<Clock*> clockList; Clock* myClock = new Clock( 1, 2, 3 ); clockList.push_back(myClock); // the pointer itself is copied, thus, the clock object is being refrenced by the pointer in the container, avoiding a memory leak // In other words, myClock == *i, where i is an iterator pointing to the new object just added // Of course, you'll have to do something like this at the end std::list<Clock*>::iterator i; for ( i = clockList.begin(); i != clockList.end(); ++i ) delete *i; // Delete the pointer the iterator is pointing to.
However, since his code is this:
std::list<Clock> clockList; // Note, this is not a list of pointers, it is a list of clock objects. Clock* myClock = new Clock ( 1, 2, 3 ); clockList.push_back(*myClock); // This calls the COPY CONSTRUCTOR to generate the object in the list // in other words, myClock != &*i, where i is an iterator pointing to the new object. // If you don't delete here, you're going to have a memory leak in the myClock object
In other words, since RegisterClock makes a copy of the Clock itself, not the Clock pointer, you must delete the Clock*.
EDIT: Clarified some things
Anyone have a good link for STL docs?
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
Download link is on below. The second volume talks about STL.
For a reference I use http://cppreference.com but it might not prove to be a good source to learn how/when to use STL.
Um, CGames, the way his code is currently implemented, that delete, setting of the pointer to null, IS necessary. The code generates a copy of the object, NOT the pointer, because the container he has is of Clock objects, not Clock Pointers.
Therefore, he will modify one single object and generate multiple copies of it. The lines are not necessary.
CGamesPlay:
Yes...I do need to delete those objects. They are being created by new and are copyed seperately into the stl::list...therefore, without being deleted, I would have created a memory leak since the stl::list copy is a completely seperate copy than the original.
I tryed a new quick program to test if I did it right or whatever...and it seems to work, so the problem is somewhere else it seems. Here is the new code that I tested and it does just what I need my other one to do...
1 | #include <allegro.h> |
2 | #include <list> |
3 | using namespace std; |
4 | class A |
5 | { |
6 | public: |
7 | A(){iData = 0;} |
8 | ~A(){} |
9 | int iData; |
10 | }; |
11 | class AListMGR |
12 | { |
13 | public: |
14 | AListMGR(){} |
15 | ~AListMGR(){} |
16 | void Update(){} |
17 | A * FindA( int i ) |
18 | { |
19 | if ( i < 0 || i > 30 ) |
20 | return NULL; |
21 | list<A>::iterator it; |
22 | for ( it = aList.begin(); it != aList.end(); ) |
23 | { |
24 | if ( it->iData == i ) |
25 | return (&*it); |
26 | it++; |
27 | } |
28 | return NULL; |
29 | } |
30 | list<A> aList; |
31 | }; |
32 | void InitSYS(); |
33 | int main( void ) |
34 | { |
35 | InitSYS(); |
36 | AListMGR aListMGR; |
37 | A *pA = NULL; |
38 | for ( int i = 0; i < 10; i++ ) |
39 | { |
40 | pA = new A; |
41 | pA->iData = i+1; |
42 | aListMGR.aList.push_back(*pA); |
43 | delete pA; |
44 | pA = NULL; |
45 | } |
46 | int y = 0; |
47 | for ( i = 0; i < 15; i++ ) |
48 | { |
49 | pA = aListMGR.FindA(i); |
50 | if ( pA ) |
51 | textprintf(screen,font,0,y,makecol(255,255,255),"Found A: %i", pA->iData ); |
52 | y+=8; |
53 | } |
54 | while ( !keypressed() ) |
55 | { |
56 | } |
57 | return 0; |
58 | } |
59 | END_OF_MAIN() |
60 | void InitSYS( void ) |
61 | { |
62 | allegro_init(); |
63 | install_timer(); |
64 | install_keyboard(); |
65 | install_mouse(); |
66 | set_color_depth(32); |
67 | set_gfx_mode(GFX_DIRECTX_WIN,400,400,0,0); |
68 | set_color_conversion(COLORCONV_TOTAL); |
69 | } |
Sorry for the crappy code...was just really wanting to see if it works or if I was doing the FindObject code wrong. I may have missed something somewhere, but it seems like the same code to me...but it works on this and not the other.
At least I know why I've been feeling so bad lately...I've got Strep Throat! Sucks balls! Can't wait to go see the doc tomarrow. I can deal with just a sore throat or a fever...but man, Strep makes you feel like real Sh.t! Hurt all over, tired but can't sleep, just all together sucks!
Yes...I do need to delete those objects. They are being created by new and are copyed seperately into the stl::list...therefore, without being deleted, I would have created a memory leak since the stl::list copy is a completely seperate copy than the original.
Sorry, you misunderstood me. I said the deleteing and making a new one is not necessary. If you make 1 new clock object and modify it and delete it once, it will have the same effect as what you do now.
I'm going to wai tuntil you try doing what I've suggested before I post again.
Ok....I found the stupid error!
I was missing:
pClock->iID = iNumClocks;
This was meant to be in the pClockMGR->Register() function. Obviously since all of the IDs where the same...it couldn't return the correct one! Stupid and easy to forget...but hard to find. I type to fast for my own good some times!
CGamesPlay:
I did remove several things from the above code and converted the pClockList from a * to value. I didn't think of it like that...still learning C++ too.
So here is the updated code for those who care:
UI.h:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <UI.h> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #ifndef UI_H |
5 | #define UI_H |
6 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
7 | #include <allegro.h> |
8 | #include <list> |
9 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
10 | class Clock; |
11 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
12 | using namespace std; |
13 | typedef list<Clock> ClockList; |
14 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
15 | extern volatile int iGameTick; |
16 | void InitSYS( void ); |
17 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
18 | class Clock |
19 | { |
20 | public: |
21 | Clock(); |
22 | ~Clock(); |
23 | void Reset( void ); |
24 | void Update( void ); |
25 | long iID; |
26 | long iBeat; |
27 | long iTick; |
28 | bool bActive; |
29 | int iTicksPerBeat; |
30 | }; |
31 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
32 | class ClockManager |
33 | { |
34 | public: |
35 | ClockManager(); |
36 | ~ClockManager(); |
37 | void Update(); |
38 | void RegisterClock( Clock *pClock ); |
39 | void RemoveClock( long iClockID ); |
40 | void ResetClock( long iClockID ); |
41 | Clock * FindClock( long iClockID ); |
42 | ClockList pClockList; |
43 | long iNumClocks; |
44 | }; |
45 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
46 | #endif // UI_H |
47 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
UI.cpp:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <UI.cpp> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #include "UI.h" |
5 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
6 | volatile int iGameTick = 0; |
7 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
8 | void GameClock( void ) |
9 | { |
10 | iGameTick++; |
11 | } |
12 | END_OF_FUNCTION(GameClock); |
13 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
14 | void InitSYS( void ) |
15 | { |
16 | allegro_init(); |
17 | install_timer(); |
18 | install_keyboard(); |
19 | install_mouse(); |
20 | LOCK_VARIABLE(iGameTick); |
21 | LOCK_FUNCTION(GameClock); |
22 | install_int_ex(GameClock,MSEC_TO_TIMER(10)); |
23 | set_color_depth(32); |
24 | set_gfx_mode(GFX_DIRECTX_WIN,400,400,0,0); |
25 | set_color_conversion(COLORCONV_TOTAL); |
26 | } |
27 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
28 | Clock::Clock() |
29 | { |
30 | bActive = false; |
31 | iID = 0; |
32 | iTick = 0; |
33 | iBeat = 0; |
34 | iTicksPerBeat = 1; |
35 | } |
36 | Clock::~Clock() |
37 | { |
38 | } |
39 | void Clock::Reset( void ) |
40 | { |
41 | iBeat = 0; |
42 | } |
43 | void Clock::Update( void ) |
44 | { |
45 | if ( !bActive ) |
46 | return; |
47 | iTick++; |
48 | if ( iTick >= iTicksPerBeat ) |
49 | { |
50 | iBeat++; |
51 | } |
52 | } |
53 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
54 | ClockManager::ClockManager() |
55 | { |
56 | iNumClocks = 0; |
57 | } |
58 | ClockManager::~ClockManager() |
59 | { |
60 | } |
61 | void ClockManager::Update() |
62 | { |
63 | list<Clock>::iterator i; |
64 | for ( i = pClockList.begin(); i != pClockList.end(); ) |
65 | { |
66 | i->Update(); |
67 | i++; |
68 | } |
69 | } |
70 | void ClockManager::RegisterClock( Clock *pClock ) |
71 | { |
72 | if ( !pClock ) |
73 | return; |
74 | pClock->iID = iNumClocks; |
75 | pClockList.push_back(*pClock); |
76 | iNumClocks++; |
77 | } |
78 | void ClockManager::RemoveClock( long iClockID ) |
79 | { |
80 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
81 | return; |
82 | } |
83 | void ClockManager::ResetClock( long iClockID ) |
84 | { |
85 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
86 | return; |
87 | } |
88 | Clock * ClockManager::FindClock( long iClockID ) |
89 | { |
90 | if ( iClockID < 0 || iClockID >= iNumClocks ) |
91 | return NULL; |
92 | list<Clock>::iterator i; |
93 | for ( i = pClockList.begin(); i != pClockList.end(); ) |
94 | { |
95 | if ( i->iID == iClockID ) |
96 | return (&*i); |
97 | i++; |
98 | } |
99 | return NULL; |
100 | } |
101 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
Main.cpp:
1 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
2 | // <Main.cpp> |
3 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
4 | #include "UI.h" |
5 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
6 | int main( void ) |
7 | { |
8 | //////////////////////////////////////////// |
9 | // Setup the system |
10 | InitSYS(); |
11 | //////////////////////////////////////////// |
12 | |
13 | |
14 | |
15 | /////////////////////////////////////////////////////////////////////////////////// |
16 | // Clock System Test |
17 | ClockManager *pClockMGR = new ClockManager; |
18 | Clock *pClock = NULL; |
19 | for ( int i = 0; i < 5; i++ ) |
20 | { |
21 | pClock = new Clock; |
22 | pClock->bActive = true; |
23 | pClock->iTicksPerBeat = i; |
24 | pClockMGR->RegisterClock(pClock); |
25 | delete pClock; |
26 | pClock = NULL; |
27 | } |
28 | /////////////////////////////////////////////////////////////////////////////////// |
29 | |
30 | |
31 | |
32 | // Main loop |
33 | while ( true ) |
34 | { |
35 | //////////////////////////////////////// |
36 | // Check for a key |
37 | if ( keypressed() ) |
38 | { |
39 | //////////////////////////////////// |
40 | // Exit on ESCAPE key |
41 | if ( key[KEY_ESC] ) |
42 | break; |
43 | //////////////////////////////////// |
44 | // Reset key buffer |
45 | clear_keybuf(); |
46 | //////////////////////////////////// |
47 | } |
48 | //////////////////////////////////////// |
49 | // Process Game Clock |
50 | if ( iGameTick >= 1 ) |
51 | { |
52 | //////////////////////////////////// |
53 | // Do some action... |
54 | pClockMGR->Update(); |
55 | //////////////////////////////////// |
56 | // Reset the game tick |
57 | iGameTick = 0; |
58 | } |
59 | //////////////////////////////////////// |
60 | pClock = pClockMGR->FindClock(0); |
61 | if ( pClock ) |
62 | textprintf(screen,font,0,0,makecol(255,255,255),"0:%i,%i",pClock->iBeat, |
63 | pClock->iTicksPerBeat); |
64 | pClock = NULL; |
65 | pClock = pClockMGR->FindClock(1); |
66 | if ( pClock ) |
67 | textprintf(screen,font,0,8,makecol(255,255,255),"0:%i,%i",pClock->iBeat, |
68 | pClock->iTicksPerBeat); |
69 | pClock = NULL; |
70 | } |
71 | //////////////////////////////////////////// |
72 | // Shutdown and exit |
73 | delete pClockMGR; |
74 | return 0; |
75 | //////////////////////////////////////////// |
76 | } |
77 | END_OF_MAIN() |
78 | ////////////////////////////////////////////////////////////////////////////////////////////////// |
That's all of it and it works now! I kinda thought it was something simple an stupid that I forgot! Thanks for everyone's help on this! Still got a lot of work for this planned...but now that I got this fixed, it should be easy from there.
http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
Download link is on below. The second volume talks about STL.
HAH!
Chuck Allision
Chuck Allision is one of my Computer Science Professors this year! w00t!
EDIT: Actually, make that two classes: Computational Theory and C++ Software Development