Archived

This topic is now archived and is closed to further replies.

iliak

Abstract base class & Particle System

Recommended Posts

Hello I''m desiging my particle engine and I have 3 classes: cParticleManager() (manages all particle systems) cParticleSystem() (mother class of all particle systems) cFire() (a type of particle system) Here''s the source code : class cParticleManager { public: cParticleManager(); ~cParticleManager(); void Render(void); // Browse the linked list and ask them to render private: cParticleSystem *SystemList; // Linked list of particle systems } class cParticleSystem { public: cParticleSystem(); ~cParticleSystem(); virtual void Render() = 0; // Function rendering the particle system private: cParticleSystem Next; // Linked list of particle systems } class cFire : public cParticleSystem { public: cFire(cParticleManager *manager); // manager is a pointer to the cParticleManager class ~cFire(); void Render(); // Render the system } As you can see, cFire() is derivated from cParticleSystem() as it''s a special type of particle system. In my cFire() constructor, I add this class in the particle manager as a linked list. In my main loop when i call Render() from cParticleManager I''ve got a "violation access", here''s the source void cParticleManager::Render(void) { cParticleSystem *next = SystemList; // Pointer to the first system if (!next) return; // No system ? Then exit do { next->Render(); // Error here ! next = next->GetNext(); // Get the next particle system } while(next); } Can you help me ? Thx - Iliak -

Share this post


Link to post
Share on other sites
I''m REALLY not sure if this is it, but I think...

cParticleSystem *next = SystemList;

...will tell your program to assign the SystemList pointer to whatever ''next'' points to (as opposed to making it point to SystemList). Though I''m not sure how C++ handles a case such as this (wether it does what I described or automatically assumes that you''re simply declaring the pointer and asking it to make it point to ''x'')...

Hope this helps.

Share this post


Link to post
Share on other sites
Does this happen on the first pass through the loop?

If it does then are you pointing the SystemList ptr to anything at construction - otherwise it will be uninitialised (and probably not null) so the check to see if next is NULL will not catch it. You'll then get an access violation when you try to dereference next.

If you are initialising SystemList and it happens on a couple of passed thru the loop - then are you sure GetNext() returns NULL if you've reached the end of the list?

Is the exception being raised on the actual call to Render or on code within render? ie Are you sure it's not something in your rendering code.

Also - why not use STL's list?

[edited by - stubble on October 24, 2003 7:53:40 AM]

Share this post


Link to post
Share on other sites
dmikesell:
It acts as a linked list.

stubble:
The error happens on the first call of the function. The debugger tells me that next is not NULL and the cFire::Render() function is empty.

"Why not use STL''s list ?"
I can use it, but i''m not enough skilled to use it. But if you can link me to good sites, i''ll try.

If you want, i can send you the project (done with Devc++).


- Iliak -

Share this post


Link to post
Share on other sites
Ok, here's your code changed to use std::list (and btw, you *must* declare your dtors with virtual if you have virtual methods in your class, at least if you mean for the class to be derived from )

class cParticleSystem
{
public:
cParticleSystem();
virtual ~cParticleSystem(); // is virtual

virtual void Render() = 0; // Function rendering the particle system
};

// store pointers in the list so you can store derived objects as well
typedef std::list<cParticleSystem *> ParticleSystemList;

class cParticleManager
{
public:
cParticleManager();
~cParticleManager();
void Render(void); // Browse the linked list and ask them to render
private:
ParticleSystemList SystemList; // Linked list of particle systems

};

// in cpp-file

void cParticleManager::Render(void)
{
for(ParticleSystemList::iterator it = SystemList.begin(); it != SystemList.end(); ++it)
(*it)->Render(); // it behaves like a pointer-to-a-pointer of cParticleSystem
}


[edited by - amag on October 24, 2003 9:59:09 AM]

Share this post


Link to post
Share on other sites