Lindenmayer System
A Simple L-system Image Generator Featuring Turtle Graphics
Main.cpp
Go to the documentation of this file.
1 
4 // MIT License
5 //
6 // Copyright (c) 2020 Ian Parberry
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
14 //
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
17 //
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 
26 #include "CMain.h"
27 
28 static CMain* g_pMain = nullptr;
29 
40 
41 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
42  UINT nMenuId = 0; //menu identifier for menu command messages
43  static bool bResizing = false;
44 
45  switch(message){
46  case WM_CREATE: //window has been created
47  g_pMain = new CMain(hWnd); //create the main class
48  return 0;
49 
50  case WM_DESTROY: //window has been removed from the screen
51  delete g_pMain; //delete the main class
52  PostQuitMessage(0); //ready to shut down
53  return 0;
54 
55  case WM_SIZING: //user is resizing the window
56  MinDragRect(hWnd, wParam, (RECT*)lParam, 320); //enforce minimum size
57  return 0;
58 
59  case WM_PAINT: //window needs to be redrawn
60  g_pMain->OnPaint();
61  return 0;
62 
63  case WM_COMMAND: //user has selected a command from the menu
64  nMenuId = LOWORD(wParam); //menu id
65 
66  //first the L-system types
67 
68  if(IDM_LSYS_BRANCHING <= nMenuId && nMenuId <= IDM_LSYS_HEXGOSPER)
69  g_pMain->SetType(nMenuId);
70 
71  else switch(nMenuId){ //now the other manu entries
72  case IDM_FILE_GENERATE: //generate a stochastic L-system
73  if(g_pMain->IsStochastic()){
74  g_pMain->Generate();
75  g_pMain->Draw();
76  } //if
77  break;
78 
79  case IDM_FILE_SAVE: //save bitmap to image file
80  SaveBitmap(hWnd, g_pMain->GetBitmap());
81  break;
82 
83  case IDM_VIEW_THICKLINES: //draw with thick lines
85  break;
86 
87  case IDM_VIEW_RULES: //draw or hide the rule string
89  break;
90 
91  case IDM_FILE_QUIT: //so long, farewell, auf weidersehn, goodbye!
92  SendMessage(hWnd, WM_CLOSE, 0, 0);
93  break;
94  } //switch
95 
96  return 0; //all is good
97 
98  default: return DefWindowProc(hWnd, message, wParam, lParam); //not my message
99  } //switch
100 } //WndProc
101 
110 
111 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow){
112  UNREFERENCED_PARAMETER(hPrev); //nope
113  UNREFERENCED_PARAMETER(lpStr); //nope nope
114 
115  InitWindow(hInst, nShow, WndProc); //create and show a window
116 
117  MSG msg; //current message
118 
119  while(GetMessage(&msg, nullptr, 0, 0)){ //message pump
120  TranslateMessage(&msg);
121  DispatchMessage(&msg);
122  } //while
123 
124  return (int)msg.wParam;
125 } //wWinMain
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Window procedure.
Definition: Main.cpp:41
void Draw(const TurtleDesc &d)
Draw turtle graphics.
Definition: CMain.cpp:158
void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc)
Initialize window.
#define IDM_VIEW_THICKLINES
Menu id for thick lines.
Gdiplus::Bitmap * GetBitmap()
Get pointer to bitmap.
Definition: CMain.cpp:489
#define IDM_LSYS_BRANCHING
Menu id for branching structure.
#define IDM_FILE_QUIT
Menu id for Quit.
void MinDragRect(HWND hwnd, WPARAM wParam, RECT *pRect, int n)
Enforce minimum drag rectangle.
HRESULT SaveBitmap(HWND hwnd, Gdiplus::Bitmap *pBitmap)
Save bitmap to file.
static CMain * g_pMain
Pointer to the main class.
Definition: Main.cpp:28
#define IDM_FILE_SAVE
Menu id for Save.
void Generate()
Generate L-system string.
Definition: CMain.cpp:467
void ToggleShowRules()
Toggle the show rules flag.
Definition: CMain.cpp:453
Interface for the main class CMain.
const bool IsStochastic() const
Is a stochastic L-system.
Definition: CMain.cpp:496
#define IDM_FILE_GENERATE
Menu id for Save.
#define IDM_LSYS_HEXGOSPER
Menu id for hexagonal Gosper curve.
The main class.
Definition: CMain.h:40
void ToggleLineThickness()
Toggle the line thickness flag.
Definition: CMain.cpp:443
#define IDM_VIEW_RULES
Menu id for showing rules.
void OnPaint()
Paint the client area.
Definition: CMain.cpp:90
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow)
Winmain.
Definition: Main.cpp:111
void SetType(UINT t)
Set type.
Definition: CMain.cpp:428