Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » STL...any help please...

Credits go to Carrus85, CGamesPlay, HoHo, IonBlade, kentl, and Wil Renczes for helping out!
This thread is locked; no one can reply to it. rss feed Print
STL...any help please...
Don Freeman
Member #5,110
October 2004
avatar

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//////////////////////////////////////////////////////////////////////////////////////////////////
2class Clock
3{
4public:
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//////////////////////////////////////////////////////////////////////////////////////////////////
16class ClockManager
17{
18public:
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... :( )

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

CGamesPlay
Member #2,559
July 2002
avatar

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Don Freeman
Member #5,110
October 2004
avatar

I had:

return (Clock*)*&i;

I tryed:

return (Clock*)&*i;

Still get an error when I try to use it with:

textprintf(screen,font,0,0,makecol(255,255,255),"0:%i,%i",pClockMGR->FindClock(0)->iBeat,
           pClockMGR->FindClock(0)->iTicksPerBeat);
textprintf(screen,font,0,8,makecol(255,255,255),"1:%i,%i",pClockMGR->FindClock(1)->iBeat,
     pClockMGR->FindClock(1)->iTicksPerBeat);

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.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

CGamesPlay
Member #2,559
July 2002
avatar

I have no idea what you're talking about:

Quote:

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.

Quote:

Still get an error when I try to use it with:

YOu'd have better luck telling me what the error is.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Don Freeman
Member #5,110
October 2004
avatar

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//////////////////////////////////////////////////////////////////////////////////////////////////
10class Clock;
11//////////////////////////////////////////////////////////////////////////////////////////////////
12using namespace std;
13typedef list<Clock> ClockList;
14extern ClockList *pClockList;
15//////////////////////////////////////////////////////////////////////////////////////////////////
16extern volatile int iGameTick;
17void InitSYS( void );
18//////////////////////////////////////////////////////////////////////////////////////////////////
19class Clock
20{
21public:
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//////////////////////////////////////////////////////////////////////////////////////////////////
33class ClockManager
34{
35public:
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//////////////////////////////////////////////////////////////////////////////////////////////////
6volatile int iGameTick = 0;
7//////////////////////////////////////////////////////////////////////////////////////////////////
8void GameClock( void )
9{
10 iGameTick++;
11}
12END_OF_FUNCTION(GameClock);
13//////////////////////////////////////////////////////////////////////////////////////////////////
14void 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//////////////////////////////////////////////////////////////////////////////////////////////////
28Clock::Clock()
29{
30 bActive = false;
31 iID = 0;
32 iTick = 0;
33 iBeat = 0;
34 iTicksPerBeat = 1;
35}
36Clock::~Clock()
37{
38}
39void Clock::Reset( void )
40{
41 iBeat = 0;
42}
43void Clock::Update( void )
44{
45 if ( !bActive )
46 return;
47 iTick++;
48 if ( iTick >= iTicksPerBeat )
49 {
50 iBeat++;
51 }
52}
53//////////////////////////////////////////////////////////////////////////////////////////////////
54ClockManager::ClockManager()
55{
56 iNumClocks = 0;
57 pClockList = new ClockList;
58}
59ClockManager::~ClockManager()
60{
61 if ( pClockList )
62 delete pClockList;
63}
64void ClockManager::Update()
65{
66 ClockList::iterator i;
67 for ( i = pClockList->begin(); i != pClockList->end(); ++i )
68 {
69 i->Update();
70 }
71}
72void ClockManager::RegisterClock( Clock *pClock )
73{
74 if ( !pClock )
75 return;
76 pClockList->push_back(*pClock);
77 iNumClocks++;
78}
79void ClockManager::RemoveClock( long iClockID )
80{
81 if ( iClockID < 0 || iClockID >= iNumClocks )
82 return;
83}
84void ClockManager::ResetClock( long iClockID )
85{
86 if ( iClockID < 0 || iClockID >= iNumClocks )
87 return;
88}
89Clock * 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//////////////////////////////////////////////////////////////////////////////////////////////////
104ClockList *pClockList = NULL;
105//////////////////////////////////////////////////////////////////////////////////////////////////

Main.cpp:

1//////////////////////////////////////////////////////////////////////////////////////////////////
2// <Main.cpp>
3//////////////////////////////////////////////////////////////////////////////////////////////////
4#include "UI.h"
5//////////////////////////////////////////////////////////////////////////////////////////////////
6int 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}
74END_OF_MAIN()
75//////////////////////////////////////////////////////////////////////////////////////////////////

And the error code:

The instruction at "0x00401494" referenced memory at "0x00000010". The memory could not be "read".

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

IonBlade
Member #3,521
May 2003
avatar

Put the clocks in structs, and put a string in the struct for the clock's name, then search for the name.

1typedef struct {
2 cClock* Clock; //note the capitol C as not to interfere with the clock() function
3 std::string name;
4}clock_t;
5 
6typedef list<clock_t*> ClockList;
7 
8class cClockManager {
9 
10public:
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 
15private:
16 
17 ClockList Clocks;
18};
19 
20///////
21 
22int 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 
39cClock* 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.

------

"I've got to choose my words carefully so that I'm not misunderstood, but I think there are fewer developers around today who think about making a good game. As a creator, I find that sad..." - Hideo Kojima

Wil Renczes
Member #5,921
June 2005
avatar

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.

CGamesPlay
Member #2,559
July 2002
avatar

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 :P

Quote:

typedef list<Clock> ClockList;

This means that your container is a std::list.

Quote:

extern ClockList *pClockList;

You don't need this global variable, get rid of it.

Quote:

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.

Quote:

  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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Don Freeman
Member #5,110
October 2004
avatar

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...

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Carrus85
Member #2,633
August 2002
avatar

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

HoHo
Member #4,534
April 2004
avatar

Quote:

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.

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

kentl
Member #2,905
November 2002

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.

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Don Freeman
Member #5,110
October 2004
avatar

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>
3using namespace std;
4class A
5{
6public:
7 A(){iData = 0;}
8 ~A(){}
9 int iData;
10};
11class AListMGR
12{
13public:
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};
32void InitSYS();
33int 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}
59END_OF_MAIN()
60void 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!

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

CGamesPlay
Member #2,559
July 2002
avatar

Quote:

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.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Don Freeman
Member #5,110
October 2004
avatar

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//////////////////////////////////////////////////////////////////////////////////////////////////
10class Clock;
11//////////////////////////////////////////////////////////////////////////////////////////////////
12using namespace std;
13typedef list<Clock> ClockList;
14//////////////////////////////////////////////////////////////////////////////////////////////////
15extern volatile int iGameTick;
16void InitSYS( void );
17//////////////////////////////////////////////////////////////////////////////////////////////////
18class Clock
19{
20public:
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//////////////////////////////////////////////////////////////////////////////////////////////////
32class ClockManager
33{
34public:
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//////////////////////////////////////////////////////////////////////////////////////////////////
6volatile int iGameTick = 0;
7//////////////////////////////////////////////////////////////////////////////////////////////////
8void GameClock( void )
9{
10 iGameTick++;
11}
12END_OF_FUNCTION(GameClock);
13//////////////////////////////////////////////////////////////////////////////////////////////////
14void 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//////////////////////////////////////////////////////////////////////////////////////////////////
28Clock::Clock()
29{
30 bActive = false;
31 iID = 0;
32 iTick = 0;
33 iBeat = 0;
34 iTicksPerBeat = 1;
35}
36Clock::~Clock()
37{
38}
39void Clock::Reset( void )
40{
41 iBeat = 0;
42}
43void Clock::Update( void )
44{
45 if ( !bActive )
46 return;
47 iTick++;
48 if ( iTick >= iTicksPerBeat )
49 {
50 iBeat++;
51 }
52}
53//////////////////////////////////////////////////////////////////////////////////////////////////
54ClockManager::ClockManager()
55{
56 iNumClocks = 0;
57}
58ClockManager::~ClockManager()
59{
60}
61void ClockManager::Update()
62{
63 list<Clock>::iterator i;
64 for ( i = pClockList.begin(); i != pClockList.end(); )
65 {
66 i->Update();
67 i++;
68 }
69}
70void ClockManager::RegisterClock( Clock *pClock )
71{
72 if ( !pClock )
73 return;
74 pClock->iID = iNumClocks;
75 pClockList.push_back(*pClock);
76 iNumClocks++;
77}
78void ClockManager::RemoveClock( long iClockID )
79{
80 if ( iClockID < 0 || iClockID >= iNumClocks )
81 return;
82}
83void ClockManager::ResetClock( long iClockID )
84{
85 if ( iClockID < 0 || iClockID >= iNumClocks )
86 return;
87}
88Clock * 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//////////////////////////////////////////////////////////////////////////////////////////////////
6int 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}
77END_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.

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Carrus85
Member #2,633
August 2002
avatar

Quote:

http://mindview.net/Books/TICPP/ThinkingInCPP2e.html
Download link is on below. The second volume talks about STL.

HAH!

Cover of Volume Two said:

Chuck Allision

Chuck Allision is one of my Computer Science Professors this year! w00t!
;D

EDIT: Actually, make that two classes: Computational Theory and C++ Software Development

Go to: