330 likes | 543 Views
component based programming. Sajad Beigjani sajad.b@gmail.com seganx.com cpp studio. topics :. notes structure / function class hierarchy data hierarchy function hierarchy component function driven component data driven component component management
E N D
component based programming SajadBeigjani sajad.b@gmail.com seganx.com cpp studio
topics : notes structure / function class hierarchy data hierarchy function hierarchy component function driven component data driven component component management communication between components messaging registry table Components every where ?
notes • use memory manager • to show you memory leaks • use call stack system • to show you where your code crashed • free memory before use it • we are humans and humans forget to free allocated memory • every where you allocate a memory block, find a place to free it before using that
notes • some things we should beware of • use some huge data as function parameters • structure • class • array • pass some huge data out from function • return structure • return class • return array • return allocated memory
structure / function define objects as structure ( a pattern for memory ) write functions to operate on memory
class same as structure specific functions constructor destructor member scope
hierarchy http://library.thinkquest.org/C0120962/java/section3/ extend data ( data hierarchy ) override functions ( function hierarchy )
data hierarchy paste structures consecutively
function hierarchy • member functions have __thiscall conventions • these functions have ‘this’ pointer • virtual member functions are just pointer to functions • table of virtual function • table filled at calling constructor
component components are everywhere each Atari film is a component each program in OS is a component
component • we can make a software system while some data and or processes are placed into separated components • first became prominent with ‘Douglas McIlroy’ • 1968, “Mass Produced Software Components” • components are substitutable • replace component at runtime • components can contain huge data • design minimal objects • less programming, more variation of objects • design different objects by attaching different components
component • software components • application services, web services • object components • attached in/to the other applications • component communicate with application via interface • interface !?
component • interface can be an string as a message • browsers, web services, … • interface is a class or structure with some sort of virtual functions or pointer to functions
function driven component component has no additional data replace component means changing functions functions process data on application
data driven component component has additional data replace component means changing functions functions process own data and application data smaller objects more complexity
component management • objects has a list of components attached to • easy to implement • CPU enemy • object has a list of components ID • components placed in the manager • application updates all components • more complexity • CPU friendly
component management • use prefab component library • name , type name , type .. • each component have serialization functions • Component::Save( Stream* stream ); • Component::Load( Stream* stream ); • save components at saving object • write number of components • for each component write it’s type • call Component::Save(…) function • load components at loading object • read number of components • in the loop, read type of component • create component from prefab library • call Component::Load(…) function
communication between components • what kind of communications ? • notify to a component that some things happen • call a functions of a component • read or write some values of a component • where is the solutions ? • search through components and find what we want • message processing • registry table
messaging • messages are the part of the event based architecture system • easy to handle performance penalties • no need to know the type of object • just using interfaces • no need to know component exist or not • set/get values by message • get pointers from objects by message • call a function • notify to the another • broadcasting • just say to an object “fire to him” • say to allies “oh ! leave me alone! I’m goanna die!”
messaging can be string null or pointer to anything Do you have any weapon ? if yes, please fill this form :D Your Type : …………….. Your Health : ……………. Your Speed : ……………… Pointer to weapon : …………. Thank you for your patient Mr. Killer agent 1 agent 2 • what is a message ? • unique integer code • additional data
message data • message data can be an structure • data can contain some conditional rules • ignore processing by the others • choose the specified object among the others • call a function • client can read/write from/to message data
process message • Object processes the message and then gives the message to his components • who process the message first ? • set a priority for components • sorting components by priority • place flags ( conditions ) in data
process message editor message data can be an structure client can read from message data client can write to message data
registry table • messaging is a simple event driven architectures • messaging system is not good for use in game loop • traversing a message through all components is time consuming for real time applications registry table is a simple solution for keep components communication alive in real time • registry table is safe • it is fast enough to use in game loop • no need to connect components directly to each other • no need to know the type of other components • components use the table to share variables and objects • avoid messaging in game loop
registry table Object registry table key , pointer … … … • components use the table to share variables and objects • components register variables when they attached • components search for variables when they initialized component On Attach ( …. On Initialize ( …. component On Attach ( …. On Initialize ( …. • contain a unique Key and a pointer • key can be a code which generated by CRC … • key can be an string as name • in addition can contain a reference counter • components use the table to share variables and objects • components register variables when they attached • components search for variables when they initialized
components every where ? • using components is the best programming pattern ? • there is no best solution • every programming paradigm has pros and cons • depend on game or application, we should use different patterns for every parts • it depends on : • performance • game mechanics • game scale • world scale in game • number of active entities • ...