dev:element_tutorial
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
dev:element_tutorial [2014/09/03 19:21] – tsmall1 | dev:element_tutorial [2014/09/17 20:00] (current) – [Create the C++ Compilation file] tsmall1 | ||
---|---|---|---|
Line 27: | Line 27: | ||
< | < | ||
- | MFMv2/ | + | MFMv2/ |
</ | </ | ||
Line 46: | Line 46: | ||
</ | </ | ||
- | Which supply you with renaming instructions. | + | Which supply you with renaming instructions. |
+ | ===== Set up the Configurable Parameter ===== | ||
+ | There is a sample configurable parameter in the Template, called m_sampleParameter. We are going to reuse this parameter as the attempted regulated density by the Creg. Rename it to m_targetDensity. | ||
+ | |||
+ | We need to set up the constructor for the Configurable Parameter next. Find the lines in the template constructor which set it up, and replace those lines with: | ||
+ | |||
+ | < | ||
+ | m_targetDensity(this, | ||
+ | "The Creg will try to fill this many spots in its event " | ||
+ | " | ||
+ | </ | ||
+ | |||
+ | The first argument is a pointer to this Element, which needs to be " | ||
+ | |||
+ | The second argument is a very short name for this parameter. | ||
+ | |||
+ | The third argument is a display name for this parameter. | ||
+ | |||
+ | The fourth argument is a longer description of what this parameter does. | ||
+ | |||
+ | The fifth argument is the lowest value this parameter may be. | ||
+ | |||
+ | The sixth argument is the default value of this parameter. | ||
+ | |||
+ | The seventh argument is the highest value this parameter may be. | ||
+ | |||
+ | The eighth argument is the resolution of this parameter, meaning the the value of this parameter may only be multiples of this resolution. | ||
+ | |||
+ | ===== Give values to virtual methods ===== | ||
+ | |||
+ | The following virtual methods need different definitions corresponding to the Creg behavior. | ||
+ | |||
+ | ^ **Method** | ||
+ | | PercentMovable | ||
+ | | DefaultPhysicsColor | ||
+ | | DefaultLowlightColor | 0xff774100 | ||
+ | | GetDescription | ||
+ | |||
+ | |||
+ | ===== Write behavior method ===== | ||
+ | |||
+ | This is the most important part of the Element. Inside the behavior method, put: | ||
+ | |||
+ | < | ||
+ | virtual void Behavior(EventWindow< | ||
+ | { | ||
+ | const MDist< | ||
+ | Random& rand = window.GetRandom(); | ||
+ | |||
+ | SPoint cregAtom; | ||
+ | s32 cregCount = 0; | ||
+ | SPoint nonCregAtom; | ||
+ | s32 nonCregCount = 0; | ||
+ | |||
+ | for(u32 i = md.GetFirstIndex(0); | ||
+ | { | ||
+ | const SPoint& rel = md.GetPoint(i); | ||
+ | const T& atom = window.GetRelativeAtom(rel); | ||
+ | if(Atom< | ||
+ | { | ||
+ | cregCount++; | ||
+ | if(rand.OneIn(cregCount)) | ||
+ | { | ||
+ | cregAtom = rel; | ||
+ | } | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | nonCregCount++; | ||
+ | if(rand.OneIn(nonCregCount)) | ||
+ | { | ||
+ | nonCregAtom = rel; | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | |||
+ | if(cregCount > m_targetDensity.GetValue()) | ||
+ | { | ||
+ | window.SetRelativeAtom(cregAtom, | ||
+ | Element_Empty< | ||
+ | } | ||
+ | else if(cregCount < m_targetDensity.GetValue()) | ||
+ | { | ||
+ | window.SetRelativeAtom(nonCregAtom, | ||
+ | Element_Creg< | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | The first loop scans the event window. If it finds an atom of the same type as itself, it keeps track of where it is located and increments the count of Cregs. If not, it keeps track of where other elements are located. | ||
+ | |||
+ | If the number of Cregs is greater than the thresold, it will delete one of the elements that isn't a Creg. Otherwise, it will delete one that is a Creg. | ||
+ | |||
+ | ===== Register Element witn StdElement.inc ===== | ||
+ | |||
+ | Open: | ||
+ | |||
+ | < | ||
+ | MFMv2/ | ||
+ | </ | ||
+ | |||
+ | Put, at the end of the file: | ||
+ | < | ||
+ | #include " | ||
+ | </ | ||
+ | |||
+ | This elemental include file is used by all drivers so they know about the elements to use. | ||
+ | |||
+ | ===== Register Element with a Driver ===== | ||
+ | |||
+ | Open: | ||
+ | |||
+ | < | ||
+ | MFMv2/ | ||
+ | </ | ||
+ | |||
+ | In the bottom of the method " | ||
+ | |||
+ | < | ||
+ | NeedElement(& | ||
+ | </ | ||
+ | |||
+ | This places the Element into the toolbox, among other things. | ||
+ | |||
+ | ===== Rebuild the MFM ===== | ||
+ | |||
+ | Go to the root of the project and type | ||
+ | < | ||
+ | make | ||
+ | </ | ||
+ | |||
+ | Sometimes make doesn' | ||
+ | |||
+ | < | ||
+ | make realclean; make | ||
+ | </ | ||
+ | |||
+ | This will rebuild the entire project from scratch. |
dev/element_tutorial.1409772117.txt.gz · Last modified: 2014/09/03 19:21 by tsmall1