473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Beginning design patterns

whew. This must be my tenth post or so just this
week - thank you to everyone who took the time
to answer my questions!

Now, I'm helping a friend with an OpenGL application
and we need some help in figuring out a design pattern.
Here's the deal:

Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.

There is probably a very simple solution to
this, but I'm all out of ideas (or *cough* knowledge...)

-- Pelle
Jul 23 '05 #1
6 1571
Pelle Beckman wrote:
Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.


Give everything a Brain *, and set them all to NULL.

Point some Actors' Brain pointers to a real Brain object. At draw time, test
the Brain * and if it's not NULL call its Think().

(If I only had a ...)

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 23 '05 #2
Val

"Pelle Beckman" <he******@chello.se> wrote in message news:ANR6e.807$184.543@amstwist00...
| whew. This must be my tenth post or so just this
| week - thank you to everyone who took the time
| to answer my questions!
|
| Now, I'm helping a friend with an OpenGL application
| and we need some help in figuring out a design pattern.
| Here's the deal:
|
| Each frame we draw lots of "3DObjects" and
| if they implement "Actor" they should also
| think (ie call Think(), or whatever).
| So, we tought of using RTTI to check
| wheter it's an object that is also an Actor -
| but thats complicated and a bit too slow.
|
| There is probably a very simple solution to
| this, but I'm all out of ideas (or *cough* knowledge...)
|
| -- Pelle

Isn't this typically work for inherritance and polymorphism?

class Player
{
public:
virtual void do_it() = 0;
};

class HumanPlayer : public Player
{
public:
virtual void do_it()
{
//Process result.
}
};

class NPC : public Player
{
public:
virtual void do_it()
{
think_first();
//Process result.
}
private:
void think_first()
{
//Use smart engine.
}
};
Jul 23 '05 #3
Pelle Beckman wrote:
whew. This must be my tenth post or so just this
week - thank you to everyone who took the time
to answer my questions!

Now, I'm helping a friend with an OpenGL application
and we need some help in figuring out a design pattern.
Here's the deal:

Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.

There is probably a very simple solution to
this, but I'm all out of ideas (or *cough* knowledge...)


One obvious way:

struct Actor {
virtual void Think() = 0;
virtual ~Actor() {}
};

struct real_Actor : public Actor {
virtual void Think() {
// do thinking.
}
};

struct stupid_Actor : public Actor {
virtual void Think() {
return;
}
};

Then create the real actors as (of course) real_actors, and the ones
you now have as non-actors as stupid_actors. All of them can respond to
being told to think, but if it's a stupid_actor, its attempt at
thinking does nothing. If you want to fix the terminology, you might
want to change the name to something saying it's supposed to try to
think.

I should caution against trying to draw too close of a real-world
analogy to what's done in programs. Based on the movies I've seen
recently, it would appear that real-world actors bear a much closer
resemblance to stupid_actor than to real_actor! :-)

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 23 '05 #4
Pelle Beckman wrote:
whew. This must be my tenth post or so just this
week - thank you to everyone who took the time
to answer my questions!

Now, I'm helping a friend with an OpenGL application
and we need some help in figuring out a design pattern.
Here's the deal:

Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.

There is probably a very simple solution to
this, but I'm all out of ideas (or *cough* knowledge...)

-- Pelle


Yes, there is an elegant solution to this described in "Game Programming
Gems pt. 2". It sort of works like Microsoft's COM and is realized using
type trait interfaces.

You let each of your classes which you want to perform some action on
(like your 3DObject) inherit from an ABC Object, which provides a method
to query which operations it supports:

class Object
{
public:
enum InterfaceType
{
RENDERABLE,
PLAYABLE,
WHATEVER-ABLE
};

virtual bool QueryInterface(InterfaceType, void**) = 0;
};

Now all child classes have to implement this function, like this:

class 3DObject: public Object, public Renderable
{
public:
void render();
// ...
};

bool 3DObject::QueryInterface( InterfaceType type, void ** pObject )
{
bool bSucceeded = false;
if( type == RENDERABLE )
{
*pObject = dynamic_cast<Renderable*>(this);
assert( *pObject != 0 );
bSucceeded = true;
}
return bSucceeded;
}
In your code, you can now use these objects like this:

// be objects a list of all kinds of ObjectS, and you now want to call a
// render() method on all those objects in the list, which implement the
// Renderable() interface:

// (loop through list, be obj the current object)
//...
Renderable *pRenderable = 0;
if( obj->QueryInterface( Object::RENDERABLE, &pRenderable ) )
pRenderable->render();
// ...

In this loop all renderable objects will call render(), cool eh?

Hope that helps.
--
Matthias Kaeppler
Jul 23 '05 #5

"Pelle Beckman" <he******@chello.se> wrote in message
news:ANR6e.807$184.543@amstwist00...
whew. This must be my tenth post or so just this
week - thank you to everyone who took the time
to answer my questions!

Now, I'm helping a friend with an OpenGL application
and we need some help in figuring out a design pattern.
Here's the deal:

Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.

There is probably a very simple solution to
this, but I'm all out of ideas (or *cough* knowledge...)

-- Pelle


If you look at the composite pattern:

class Composite;
class Component {
public:
virtual Composite* GetComposite() {return 0; };
};

class Composite : public Component {
public:
virtual Composite* GetComposite() {return this; };
};

it has as GetComposite() function...
You can use this same technique for multiple functions..

If you make an interface class with all the "objects" you can query... Let
them own or aquire their pointers..

Jesper Madsen, SAXOTECH
Jul 23 '05 #6
Matthias Kaeppler skrev:
Pelle Beckman wrote:
whew. This must be my tenth post or so just this
week - thank you to everyone who took the time
to answer my questions!

Now, I'm helping a friend with an OpenGL application
and we need some help in figuring out a design pattern.
Here's the deal:

Each frame we draw lots of "3DObjects" and
if they implement "Actor" they should also
think (ie call Think(), or whatever).
So, we tought of using RTTI to check
wheter it's an object that is also an Actor -
but thats complicated and a bit too slow.

There is probably a very simple solution to
this, but I'm all out of ideas (or *cough* knowledge...)

-- Pelle

Yes, there is an elegant solution to this described in "Game Programming
Gems pt. 2". It sort of works like Microsoft's COM and is realized using
type trait interfaces.

You let each of your classes which you want to perform some action on
(like your 3DObject) inherit from an ABC Object, which provides a method
to query which operations it supports:

class Object
{
public:
enum InterfaceType
{
RENDERABLE,
PLAYABLE,
WHATEVER-ABLE
};

virtual bool QueryInterface(InterfaceType, void**) = 0;
};

Now all child classes have to implement this function, like this:

class 3DObject: public Object, public Renderable
{
public:
void render();
// ...
};

bool 3DObject::QueryInterface( InterfaceType type, void ** pObject )
{
bool bSucceeded = false;
if( type == RENDERABLE )
{
*pObject = dynamic_cast<Renderable*>(this);
assert( *pObject != 0 );
bSucceeded = true;
}
return bSucceeded;
}
In your code, you can now use these objects like this:

// be objects a list of all kinds of ObjectS, and you now want to call a
// render() method on all those objects in the list, which implement the
// Renderable() interface:

// (loop through list, be obj the current object)
//...
Renderable *pRenderable = 0;
if( obj->QueryInterface( Object::RENDERABLE, &pRenderable ) )
pRenderable->render();
// ...

In this loop all renderable objects will call render(), cool eh?

Hope that helps.


Sounds very good to me Matthias, but
how would you say the class "Renderable" would
look like?

-- Pelle
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Don Vaillancourt | last post by:
Hello all, Over the years as I design more database schemas the more I come up with patterns in database design. The more patterns I recognize the more I want to try to design some kind of...
1
by: Jay | last post by:
The GOF text is widely considered the definitive book on the topic. Design Patterns: Elements of Reusable Object-Oriented Softare, Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides ...
1
by: Josh28 | last post by:
Hi We are a group of two chaps just out of undergrad, we created a software to automate the use of Design Patterns. We have put it up at Source Forge--http://dpatoolkit.sourceforge.net/ The...
13
by: John Salerno | last post by:
Here are a few I'm considering: Design Patterns Explained : A New Perspective on Object-Oriented Design (2nd Edition) (Software Patterns Series) by Alan Shalloway Design Patterns C# by...
2
by: Carlo Stonebanks | last post by:
I have the infamous GoF Design Patterns boo - it's been sittin gon my shelf for years. I have a huge reading list and find this book a rather dry read and am always putting it off. I have...
22
by: Krivenok Dmitry | last post by:
Hello All! I am trying to implement my own Design Patterns Library. I have read the following documentation about Observer Pattern: 1) Design Patterns by GoF Classic description of Observer....
5
by: Ludwig Wittgenstein | last post by:
Other than the Design Patterns book, which book(s) is/are the best to learn object-oriented software design/architecture from ?
7
by: =?Utf-8?B?bWF2cmlja18xMDE=?= | last post by:
Hi, I would like to know more about design patterns and specifically using C#. Can any one recommend a good book? Thanks
5
by: macca | last post by:
Hi, I'm looking for a good book on PHP design patterns for a OOP beginner - Reccommendations please? Thanks Paul
10
by: vital | last post by:
Hi, I am designing the middle tier of a project. It has 6 classes and microsoft application data access block. The six classes are DBServices, Logger, ProjectServices ... etc. and all these...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.