Smooth 2D Noise Viewer
Perlin and Value Noise
Main.cpp
Go to the documentation of this file.
1
3
4// MIT License
5//
6// Copyright (c) 2022 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#include "resource.h"
28
29static CMain* g_pMain = nullptr;
30
31static const int g_nWidth = 600;
32static const int g_nHeight = 600;
33
42
43LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){
44 UINT nMenuId = 0; //menu identifier for menu command messages
45 static bool bResizing = false;
46
47 switch(message){
48 case WM_CREATE: //window has been created
49 g_pMain = new CMain(hWnd); //create the main class
51 return 0;
52
53 case WM_DESTROY: //window has been removed from the screen
54 delete g_pMain; //delete the main class
55 PostQuitMessage(0); //ready to shut down
56 return 0;
57
58 case WM_PAINT: //window needs to be redrawn
60 return 0;
61
62 //menu bar ---------------------------------------------------
63
64 case WM_COMMAND: //user has selected a command from the menu
65 nMenuId = LOWORD(wParam); //menu id
66
67 switch(nMenuId){
68
69 //file menu ---------------------------------------------------
70
71 case IDM_FILE_SAVE: //save bitmap to image file
73 break;
74
75 case IDM_FILE_PROPS: //display noise properties
76 MessageBox(nullptr, g_pMain->GetNoiseDescription().c_str(),
77 L"Properties", MB_ICONINFORMATION | MB_OK);
78 break;
79
80 case IDM_FILE_QUIT: //so long, farewell, auf weidersehn, goodbye!
81 SendMessage(hWnd, WM_CLOSE, 0, 0);
82 break;
83
84 //generate menu ---------------------------------------------------
85
87 g_pMain->GenerateNoiseBitmap(eNoise::Perlin);
88 InvalidateRect(hWnd, nullptr, FALSE);
89 break;
90
92 g_pMain->GenerateNoiseBitmap(eNoise::Value);
93 InvalidateRect(hWnd, nullptr, FALSE);
94 break;
95
98 InvalidateRect(hWnd, nullptr, FALSE);
99 break;
100
102 g_pMain->Jump();
103 InvalidateRect(hWnd, nullptr, FALSE);
104 break;
105
107 g_pMain->Jump(0.0f, 0.0f);
108 InvalidateRect(hWnd, nullptr, FALSE);
109 break;
110
111 //view menu ---------------------------------------------------
112
113 case IDM_VIEW_COORDS:
115 InvalidateRect(hWnd, nullptr, FALSE);
116 break;
117
118 case IDM_VIEW_GRID:
120 InvalidateRect(hWnd, nullptr, FALSE);
121 break;
122
123 //distribution menu ---------------------------------------------------
124
126 if(g_pMain->SetDistribution(eDistribution::Uniform))
127 InvalidateRect(hWnd, nullptr, FALSE);
128 break;
129
131 if(g_pMain->SetDistribution(eDistribution::Maximal))
132 InvalidateRect(hWnd, nullptr, FALSE);
133 break;
134
136 if(g_pMain->SetDistribution(eDistribution::Cosine))
137 InvalidateRect(hWnd, nullptr, FALSE);
138 break;
139
141 if(g_pMain->SetDistribution(eDistribution::Normal))
142 InvalidateRect(hWnd, nullptr, FALSE);
143 break;
144
146 if(g_pMain->SetDistribution(eDistribution::Exponential))
147 InvalidateRect(hWnd, nullptr, FALSE);
148 break;
149
151 if(g_pMain->SetDistribution(eDistribution::Midpoint))
152 InvalidateRect(hWnd, nullptr, FALSE);
153 break;
154
155 //hash function menu --------------------------------------------------
156
157 case IDM_HASH_PERM:
158 g_pMain->SetHash(eHash::Permutation);
159 InvalidateRect(hWnd, nullptr, FALSE);
160 break;
161
162 case IDM_HASH_LCON:
163 g_pMain->SetHash(eHash::LinearCongruential);
164 InvalidateRect(hWnd, nullptr, FALSE);
165 break;
166
167 case IDM_HASH_STD:
168 g_pMain->SetHash(eHash::Std);
169 InvalidateRect(hWnd, nullptr, FALSE);
170 break;
171
172 //spline function menu ------------------------------------------------
173
174 case IDM_SPLINE_NONE:
175 g_pMain->SetSpline(eSpline::None);
176 InvalidateRect(hWnd, nullptr, FALSE);
177 break;
178
179 case IDM_SPLINE_CUBIC:
180 g_pMain->SetSpline(eSpline::Cubic);
181 InvalidateRect(hWnd, nullptr, FALSE);
182 break;
183
185 g_pMain->SetSpline(eSpline::Quintic);
186 InvalidateRect(hWnd, nullptr, FALSE);
187 break;
188
189 //settings menu ---------------------------------------------------
190
193 InvalidateRect(hWnd, nullptr, FALSE);
194 break;
195
198 InvalidateRect(hWnd, nullptr, FALSE);
199 break;
200
203 InvalidateRect(hWnd, nullptr, FALSE);
204 break;
205
208 InvalidateRect(hWnd, nullptr, FALSE);
209 break;
210
213 InvalidateRect(hWnd, nullptr, FALSE);
214 break;
215
218 InvalidateRect(hWnd, nullptr, FALSE);
219 break;
220
222 g_pMain->Reset();
223 InvalidateRect(hWnd, nullptr, FALSE);
224 break;
225
226 //help menu ---------------------------------------------------
227
228 case IDM_HELP_HELP:
229 ShellExecute(0, 0,
230 L"https://ian-parberry.github.io/smooth2dnoiseviewer/html/",
231 0, 0, SW_SHOW);
232 break;
233
234 case IDM_HELP_ABOUT:
235 MessageBox(nullptr,
236 L"Copyright © Ian Parberry, 2022.\nSource code available under the MIT License from https://github.com/Ian-Parberry/smooth2dnoiseviewer.",
237 L"About", MB_ICONINFORMATION | MB_OK);
238 break;
239 } //switch
240
241 return 0; //all is good
242
243 default:
244 return DefWindowProc(hWnd, message, wParam, lParam); //not my message
245 } //switch
246} //WndProc
247
252
253void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc){
254 const LPCWSTR appname = L"2D Noise Generator";
255
256 WNDCLASSEX wndClass = {0}; //extended window class structure
257
258 wndClass.cbSize = sizeof(WNDCLASSEX);
259 wndClass.style = CS_HREDRAW | CS_VREDRAW;
260 wndClass.lpfnWndProc = WndProc;
261 wndClass.cbClsExtra = 0;
262 wndClass.cbWndExtra = 0;
263 wndClass.hInstance = hInst;
264 wndClass.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
265 wndClass.hCursor = LoadCursor(nullptr, IDC_ARROW);
266 wndClass.hbrBackground = nullptr;
267 wndClass.lpszMenuName = nullptr;
268 wndClass.lpszClassName = appname;
269
270 RegisterClassEx(&wndClass);
271
272 const DWORD dwStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
273 const DWORD dwStyleEx = WS_EX_APPWINDOW | WS_EX_DLGMODALFRAME;
274
275 RECT r{0};
276 r.right = g_nWidth;
277 r.bottom = g_nHeight + GetSystemMetrics(SM_CYMENU);
278 AdjustWindowRectEx(&r, dwStyle, FALSE, dwStyleEx);
279
280 const HWND hwnd = CreateWindowEx(dwStyleEx, appname, appname, dwStyle,
281 CW_USEDEFAULT, CW_USEDEFAULT, r.right - r.left, r.bottom - r.top,
282 nullptr, nullptr, hInst, nullptr);
283
284 ShowWindow(hwnd, nShow);
285 UpdateWindow(hwnd);
286} //InitWindow
287
296
297int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow){
298 UNREFERENCED_PARAMETER(hPrev); //nope
299 UNREFERENCED_PARAMETER(lpStr); //nope nope
300
301 InitWindow(hInst, nShow, WndProc); //create and show a window
302
303 MSG msg; //current message
304
305 while(GetMessage(&msg, nullptr, 0, 0)){ //message pump
306 TranslateMessage(&msg);
307 DispatchMessage(&msg);
308 } //while
309
310 return (int)msg.wParam;
311} //wWinMain
Interface for the main class CMain.
static const int g_nHeight
Client area height in pixels.
Definition: Main.cpp:32
static const int g_nWidth
Client area width in pixels.
Definition: Main.cpp:31
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
Window procedure.
Definition: Main.cpp:43
static CMain * g_pMain
Pointer to the main class.
Definition: Main.cpp:29
void InitWindow(HINSTANCE hInst, INT nShow, WNDPROC WndProc)
Definition: Main.cpp:253
int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR lpStr, int nShow)
Winmain.
Definition: Main.cpp:297
HRESULT SaveBitmap(HWND hwnd, const std::wstring &wstrName, Gdiplus::Bitmap *pBitmap)
Save bitmap to file.
#define IDM_SETTINGS_TSIZE_UP
Menu id for table size up.
#define IDM_VIEW_GRID
Menu id for view grid.
#define IDM_GENERATE_VALUENOISE
Menu id for Value Noise.
#define IDM_DISTRIBUTION_EXPONENTIAL
Menu id for exponential distribution.
#define IDM_DISTRIBUTION_UNIFORM
Menu id for uniform distribution.
#define IDM_HASH_LCON
Menu id for linear congruential hash.
#define IDM_DISTRIBUTION_NORMAL
Menu id for normal distribution.
#define IDM_GENERATE_PERLINNOISE
Menu id for Perlin Noise.
#define IDM_FILE_SAVE
Menu id for Save.
#define IDM_SPLINE_QUINTIC
Menu id for quintic spline.
#define IDM_SPLINE_NONE
Menu id for cubic spline.
#define IDM_HASH_STD
Menu id for std::hash.
#define IDM_SETTINGS_SCALE_UP
Menu id for scale up.
#define IDM_SETTINGS_SCALE_DN
Menu id for scale down.
#define IDM_GENERATE_JUMP
Menu id for jump.
#define IDM_FILE_QUIT
Menu id for Quit.
#define IDM_HASH_PERM
Menu id for permutation hash.
#define IDM_SETTINGS_RESET
Menu id for reset settings.
#define IDM_HELP_HELP
Menu id for display help.
#define IDM_SETTINGS_TSIZE_DN
Menu id for table size down.
#define IDM_SETTINGS_OCTAVE_UP
Menu id for octave up.
#define IDM_SPLINE_CUBIC
Menu id for no spline.
#define IDM_SETTINGS_OCTAVE_DN
Menu id for octave down.
#define IDM_DISTRIBUTION_COSINE
Menu id for cosine distribution.
#define IDM_GENERATE_RANDOMIZE
Menu id for regenerate Noise.
#define IDM_DISTRIBUTION_MAXIMAL
Menu id for midpoint displacement.
#define IDM_DISTRIBUTION_MIDPOINT
Menu id for midpoint displacement.
#define IDM_HELP_ABOUT
Menu id for display About info.
#define IDM_VIEW_COORDS
Menu id for view coordinates.
#define IDM_GENERATE_RESETORIGIN
Menu id for reset origin.
#define IDM_FILE_PROPS
Menu id for Properties.
The main class.
Definition: CMain.h:41
const std::wstring GetNoiseDescription() const
Get noise description.
Definition: CMain.cpp:598
void DecreaseScale()
Decrease scale.
Definition: CMain.cpp:520
const std::wstring GetFileName() const
Get noise file name.
Definition: CMain.cpp:559
void CreateBitmap(int w, int h)
Create bitmap.
Definition: CMain.cpp:175
void Randomize()
Randomize PRNG.
Definition: CMain.cpp:406
void Reset()
Reset number of octaves, scale, table size.
Definition: CMain.cpp:542
bool SetDistribution(eDistribution)
Set probability distribution.
Definition: CMain.cpp:417
void DecreaseTableSize()
Decrease table size.
Definition: CMain.cpp:534
void GenerateNoiseBitmap(Gdiplus::PointF, Gdiplus::RectF)
Generate bitmap rectangle.
Definition: CMain.cpp:273
void ToggleViewCoords()
Toggle View Coordinates flag.
Definition: CMain.cpp:449
void IncreaseTableSize()
Increase table size.
Definition: CMain.cpp:527
Gdiplus::Bitmap * GetBitmap() const
Get pointer to bitmap.
Definition: CMain.cpp:695
void SetHash(eHash)
Set hash function.
Definition: CMain.cpp:440
void DecreaseOctaves()
Decrease number of octaves.
Definition: CMain.cpp:504
void Jump()
Change origin coordinates.
Definition: CMain.cpp:467
void ToggleViewGrid()
Toggle View Grid flag.
Definition: CMain.cpp:458
void SetSpline(eSpline)
Set spline function.
Definition: CMain.cpp:431
void IncreaseOctaves()
Increase number of octaves.
Definition: CMain.cpp:496
void OnPaint()
Paint the client area of the window.
Definition: CMain.cpp:67
void IncreaseScale()
Increase scale.
Definition: CMain.cpp:512