Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CMain Class Reference

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.
 

Detailed Description

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.

Definition at line 40 of file CMain.h.

Constructor & Destructor Documentation

◆ CMain()

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.

Parameters
hwndWindow handle.

Definition at line 41 of file CMain.cpp.

◆ ~CMain()

CMain::~CMain ( )

Delete all GDI+ objects, then shut down GDI+.

Definition at line 72 of file CMain.cpp.

Member Function Documentation

◆ CreateMenus()

void CMain::CreateMenus ( )
private

Add menus to the menu bar and store the menu handles, which will be needed later to set checkmarks and such.

Definition at line 285 of file CMain.cpp.

◆ Draw() [1/2]

void CMain::Draw ( const TurtleDesc d)
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.

Parameters
dTurtle graphics descriptor.

Definition at line 158 of file CMain.cpp.

◆ Draw() [2/2]

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.

Definition at line 254 of file CMain.cpp.

◆ DrawRules()

void CMain::DrawRules ( Gdiplus::Graphics &  graphics,
Gdiplus::PointF  p 
)
private

Draw the L-system rules text to a GDI+ graphics object.

Parameters
graphicsReference to a GDI+ graphics object.
pPoint at which to draw the rules (the top left pixel).

Definition at line 141 of file CMain.cpp.

◆ EnableGenerateMenuEntry()

void CMain::EnableGenerateMenuEntry ( )
private

Enable the Generate item in the File menu if stochastic, otherwise gray it out so it can't be used.

Definition at line 351 of file CMain.cpp.

◆ Generate()

void CMain::Generate ( )

Generate an L-system string for a hard-coded number of generations.

Definition at line 467 of file CMain.cpp.

◆ GetBitmap()

Gdiplus::Bitmap * CMain::GetBitmap ( )

Reader function for the bitmap pointer m_pBitmap which, it is assumed, contains a bitmap drawn by turtle graphics from a string generated by an L-system.

Returns
The bitmap pointer m_pBitmap.

Definition at line 489 of file CMain.cpp.

◆ GetRuleStrWidth()

int CMain::GetRuleStrWidth ( Gdiplus::Graphics &  graphics)
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.

Parameters
graphicsReference to a GDI+ graphics object.
Returns
The width of the rule string in pixels.

Definition at line 509 of file CMain.cpp.

◆ IsStochastic()

const bool CMain::IsStochastic ( ) const

Stochasticity test.

Returns
true if the L-system is stochastic.

Definition at line 496 of file CMain.cpp.

◆ OnPaint()

void CMain::OnPaint ( )

Draw the bitmap to the window client area, scaled down if necessary. This function should only be called in response to a WM_PAINT message.

Definition at line 90 of file CMain.cpp.

◆ SetLSystemMenuChecks()

void CMain::SetLSystemMenuChecks ( )
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).

Definition at line 341 of file CMain.cpp.

◆ SetRules()

void CMain::SetRules ( )
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.

Definition at line 369 of file CMain.cpp.

◆ SetType()

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.

Parameters
tNew L-system type.

Definition at line 428 of file CMain.cpp.

◆ ToggleLineThickness()

void CMain::ToggleLineThickness ( )

Toggle the line thickness flag. Set the checkmark on the menu entry and ask for a redraw of the window.

Definition at line 443 of file CMain.cpp.

◆ ToggleShowRules()

void CMain::ToggleShowRules ( )

Toggle the show rules flag. Set the checkmark on the menu entry and ask for a refresh of the window.

Definition at line 453 of file CMain.cpp.