![]() |
Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
|
The main class. More...
#include <CMain.h>
Public Member Functions | |
| CMain (const HWND hwnd) | |
| Constructor. More... | |
| ~CMain () | |
| Destructor. More... | |
| void | Draw () |
| Draw turtle graphics. More... | |
| void | Generate () |
| Generate L-system string. More... | |
| void | OnPaint () |
| Paint the client area. More... | |
| void | SetType (UINT t) |
| Set type. More... | |
| Gdiplus::Bitmap * | GetBitmap () |
| Get pointer to bitmap. More... | |
| const bool | IsStochastic () const |
| Is a stochastic L-system. More... | |
| void | ToggleShowRules () |
| Toggle the show rules flag. More... | |
| void | ToggleLineThickness () |
| Toggle the line thickness flag. More... | |
Private Member Functions | |
| void | SetRules () |
| Create the L-system rules. More... | |
| void | Draw (const TurtleDesc &d) |
| Draw turtle graphics. More... | |
| void | DrawRules (Gdiplus::Graphics &graphics, Gdiplus::PointF p) |
| Draw rules. More... | |
| void | CreateMenus () |
| Create menus. More... | |
| void | SetLSystemMenuChecks () |
| Set L-system menu checkmarks. More... | |
| void | EnableGenerateMenuEntry () |
Enable Generate in File menu. More... | |
| int | GetRuleStrWidth (Gdiplus::Graphics &graphics) |
| Get rule string width. More... | |
Private Attributes | |
| HWND | m_hWnd = nullptr |
| Window handle. | |
| HMENU | m_hFileMenu = nullptr |
Handle to the File menu. | |
| HMENU | m_hLSMenu = nullptr |
Handle to the L-System menu. | |
| HMENU | m_hViewMenu = nullptr |
Handle to the View menu. | |
| ULONG_PTR | m_gdiplusToken = 0 |
| GDI+ token. | |
| Gdiplus::Bitmap * | m_pBitmap = nullptr |
| Pointer to a bitmap image. | |
| LSystem | m_cLSystem |
| The L-system. | |
| UINT | m_nType = IDM_LSYS_PLANT_A |
| Current L-system type. | |
| bool | m_bThickLines = false |
| Line thickness flag. | |
| bool | m_bShowRules = true |
| Whether to show the rules. | |
| Gdiplus::FontFamily * | m_pFontFamily = nullptr |
| Font family. | |
| Gdiplus::Font * | m_pFont = nullptr |
| Font. | |
The interface between I/O from Windows (input from the drop-down menus, output to the client area of the window), the L-system string generator, turtle graphics, and the GDI+ graphics interface.
| CMain::CMain | ( | const HWND | hwnd | ) |
Initialize GDI+, create a font for drawing text, create the menus, initialize the check marks on the various menu entries, gray out the Generate entry in the File menu if necessary, create the initial L-system rules, generate the initial string from those rules, then draw the corresponding line drawing to the bitmap.
| hwnd | Window handle. |
| CMain::~CMain | ( | ) |
|
private |
|
private |
Use turtle graphics to draw the shape corresponding to the generated string to m_pBitmap, which gets resized to the smallest rectangle containing all of the non-transparent pixels. This is done by doing turtle graphics twice, the first time without drawing but measuring the extents of the rectangle that gets drawn on. After measuring, the bitmap is resized and then a second iteration of turtle graphics is performed to draw the image.
| d | Turtle graphics descriptor. |
| void CMain::Draw | ( | ) |
Use turtle graphics to draw the shape corresponding to the generated string to m_pBitmap, which gets resized to the smallest rectangle containing all of the non-transparent pixels. This function constructs a hard-coded turtle graphics descriptor appropriate to the current type stored in m_nType and then calls Draw(const TurtleDesc&) to do the actual work.
|
private |
|
private |
| void CMain::Generate | ( | ) |
| Gdiplus::Bitmap * CMain::GetBitmap | ( | ) |
|
private |
Get the width of the widest line of the rule string in pixels. This will, of course, depend on the font. Finding the pixel width of a character in any given font is a black art, so the developers of GDI+ should (in my humble opinion) be given a back-pat for making this process so easy, not withstanding the number of new GDI+ concepts and functions that I had to grok before I could get this to work.
| graphics | Reference to a GDI+ graphics object. |
| const bool CMain::IsStochastic | ( | ) | const |
| void CMain::OnPaint | ( | ) |
|
private |
Set the L-System menu checkmarks. Uncheck them all, then check the one corresponding to the current L-system type. This assumes the correct ordering of defines in WindowsHelpers.h (see the comments there).
|
private |
Set rules for the current L-system type. The rules are hard-coded from ABOP using a long switch statement. Exercise for the reader: add your favorite L-system rules from ABOP. More difficult exercise: add the ability to read custom rules from a text or XML file.
| void CMain::SetType | ( | UINT | t | ) |
Set the L-system type, set the checkmarks on the L-System menu to indicate the new type, enable the Generate entry in the File menu if the new type is stochastic, create the rules for the new type, generate a string and draw the image from that string. Does nothing if the new type is the same as the previous one.
| t | New L-system type. |
| void CMain::ToggleLineThickness | ( | ) |
1.8.14