-
Notifications
You must be signed in to change notification settings - Fork 0
/
MandelbrotCpp.cpp
897 lines (761 loc) · 20.6 KB
/
MandelbrotCpp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
// MandelbrotCpp.cpp : Defines the entry point for the application.
//
#include "framework.h"
#include "MandelbrotCpp.h"
#include "Point.h"
#include "colorConvert.h"
#include "resource.h"
#include <vector>
#include <windowsx.h>
#include <thread>
#include <wingdi.h>
#include <WinUser.h>
#include <string>
#include <ObjIdl.h>
#include <commdlg.h>
#pragma warning( push )
#pragma warning( disable : 4458 )
#include "gdiplus.h"
#pragma warning( pop )
#pragma comment(lib, "Gdiplus.lib")
#define MAX_LOADSTRING 100
//void distributeCalculation(HWND hWnd);
void recalculate(HWND hWnd);
void resetZoom(HWND hWnd);
void updateMenuChecks(HWND hWnd);
void updateZoomMenuText(HWND hWnd);
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid); // helper function
// Global Variables:
HINSTANCE hInst; // current instance
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
bool saveBitmap(const HWND& hWnd);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_MANDELBROTCPP, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MANDELBROTCPP));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MANDELBROTCPP));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_MANDELBROTCPP);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
//Run resetZoom to set the xMin/xMax and yMin/yMax for the correct fractal algorithm
resetZoom(hWnd);
//recalculate will run the correct algorithm based on useMandelbrotMath
recalculate(hWnd);
//Set the initial algorithm menu check boxes
updateMenuChecks(hWnd);
//Run the code at the end from the windows gui handler to draw the bitmap on the screen.
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
Gdiplus::ImageCodecInfo* pImageCodecInfo = NULL;
Gdiplus::GetImageEncodersSize(&num, &size);
if (size == 0)
return -1; // Failure
pImageCodecInfo = (Gdiplus::ImageCodecInfo*)(malloc(size));
if (pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for (UINT j = 0; j < num; ++j)
{
if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0)
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
BOOL IsWindowMode = TRUE;
WINDOWPLACEMENT wpc;
LONG HWNDStyle = 0;
LONG HWNDStyleEx = 0;
void FullScreenSwitch(HWND hWnd)
{
if (IsWindowMode)
{
IsWindowMode = FALSE;
GetWindowPlacement(hWnd, &wpc);
if (HWNDStyle == 0)
HWNDStyle = GetWindowLong(hWnd, GWL_STYLE);
if (HWNDStyleEx == 0)
HWNDStyleEx = GetWindowLong(hWnd, GWL_EXSTYLE);
LONG NewHWNDStyle = HWNDStyle;
NewHWNDStyle &= ~WS_BORDER;
NewHWNDStyle &= ~WS_DLGFRAME;
NewHWNDStyle &= ~WS_THICKFRAME;
LONG NewHWNDStyleEx = HWNDStyleEx;
NewHWNDStyleEx &= ~WS_EX_WINDOWEDGE;
SetWindowLong(hWnd, GWL_STYLE, NewHWNDStyle | WS_POPUP);
SetWindowLong(hWnd, GWL_EXSTYLE, NewHWNDStyleEx | WS_EX_TOPMOST);
ShowWindow(hWnd, SW_SHOWMAXIMIZED);
}
else
{
IsWindowMode = TRUE;
SetWindowLong(hWnd, GWL_STYLE, HWNDStyle);
SetWindowLong(hWnd, GWL_EXSTYLE, HWNDStyleEx);
ShowWindow(hWnd, SW_SHOWNORMAL);
SetWindowPlacement(hWnd, &wpc);
}
}
//Array to hold data points that need to be drawn - DEPRECATED
//std::vector<Point> dataPoints;
//The bitmap object that holds the pixel data that is shown on in the main window
HBITMAP fractalBitmap = nullptr;
unsigned char* fractalColorData = nullptr;
double const mandelbrotDefaultWindowSize = 6;
double const juliaDefaultWindowSize = 400;
//Position of the Zoom Level menu bar indicator from the left side of the menu bar.
int const zoomLevelMenuPosition = 4;
//xMin, xMax, yMin, yMax are used in the calculation to determine the range of values the algorithm is applied to
double xMin;
double xMax;
double yMin;
double yMax;
//How many times through the loop to determine if a data point fits into the mandelbrot data set
int maxIteration = 255;
double widthScale = 0;
double heightScale = 0;
//How far to zoom when clicking on the screen. This value is squared in the zoom code
int zoomLevel = 3;
double scaleFactor = 1;
//If this is true then the program will draw the mandelbot data set. If it is false then it will draw the julia fractal data set.
bool useMandelbrotMath = true;
void resetZoom(HWND hWnd)
{
if (useMandelbrotMath)
{
xMin = -(mandelbrotDefaultWindowSize / 2);
xMax = (mandelbrotDefaultWindowSize / 2);
yMin = -(mandelbrotDefaultWindowSize / 2);
yMax = (mandelbrotDefaultWindowSize / 2);
}
else
{
xMin = -(juliaDefaultWindowSize / 2);
xMax = (juliaDefaultWindowSize / 2);
yMin = -(juliaDefaultWindowSize / 2);
yMax = (juliaDefaultWindowSize / 2);
}
updateZoomMenuText(hWnd);
}
HMENU oldMenu;
void menuSwitch(HWND hWnd)
{
if (oldMenu == nullptr)
{
oldMenu = GetMenu(hWnd);
SetMenu(hWnd, nullptr);
}
else
{
SetMenu(hWnd, oldMenu);
oldMenu = nullptr;
}
}
void updateMenuChecks(HWND hWnd)
{
if (hWnd == nullptr)
return; // No Window?
HMENU hMenu = GetMenu(hWnd);
if (hMenu == nullptr)
return; // No menu?
if (useMandelbrotMath)
{
CheckMenuItem(hMenu, ID_ALGORITHM_MANDELBROT, MF_BYCOMMAND | MF_CHECKED);
CheckMenuItem(hMenu, ID_ALGORITHM_JULIA, MF_BYCOMMAND | MF_UNCHECKED);
}
else
{
CheckMenuItem(hMenu, ID_ALGORITHM_MANDELBROT, MF_BYCOMMAND | MF_UNCHECKED);
CheckMenuItem(hMenu, ID_ALGORITHM_JULIA, MF_BYCOMMAND | MF_CHECKED);
}
}
void updateZoomMenuText(HWND hWnd)
{
if (useMandelbrotMath)
{
scaleFactor = mandelbrotDefaultWindowSize / (xMax - xMin);
}
else
{
scaleFactor = juliaDefaultWindowSize / (xMax - xMin);
}
wchar_t menuText[30];
//wsprintf(menuText, L"Zoom Level: %S", std::to_wstring(scaleFactor));
wsprintf(menuText, L"Zoom Level: %d", (int)scaleFactor);
HMENU hMenu = GetMenu(hWnd);
MENUITEMINFOW MI = { sizeof(MENUITEMINFO) , MIIM_STRING };
MI.dwTypeData = menuText;
SetMenuItemInfo(hMenu, zoomLevelMenuPosition, MF_BYPOSITION, &MI);
DrawMenuBar(hWnd);
}
//Function used for calculating the bitmap memory allocation size
int getStride(int width)
{
int bpp = 32;
int stride = (width * (bpp / 8));
return stride;
}
//Function to easily get the client window size
std::pair<int, int> getClientSize(HWND hWnd)
{
RECT clientRect;
GetClientRect(hWnd, &clientRect);
int width = clientRect.right;
int height = clientRect.bottom;
return std::make_pair(width, height );
}
//Function to map a value over a range of other values
double linearMap(double value, double low, double high, double newLow, double newHigh)
{
return newLow + ((value - low) / (high - low)) * (newHigh - newLow);
}
/*
int linearMap(double value, double low, double high, int newLow, int newHigh)
{
return (int) (newLow + ((value - low) / (high - low)) * ((double) newHigh - (double) newLow));
}
*/
void calculateMandelbrot(HWND hWnd, double xMin, double yMin, int maxIteration, double widthScale, double heightScale)
{
if (fractalColorData == nullptr)
return;
auto [width, height] = getClientSize(hWnd);
int stride = getStride(width);
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
double w = xMin + (c * widthScale);
double h = yMin + (r * heightScale);
double x = 0;
double y = 0;
int iteration = 0;
while ((x * x + y * y <= 4) && (iteration < maxIteration))
{
double xtemp = (x * x - y * y) + w;
y = ((2 * x) * y) + h;
x = xtemp;
iteration++;
}
//int newW = (int)linearMap(w, xMin, xMax, 0, width - 1);
//int newH = (int)linearMap(h, yMin, yMax, height - 1, 0);
unsigned char* pixData = fractalColorData + (stride * (height - r - 1)) + (c * 4);
if (iteration != maxIteration)
{
HsvColor hsvColor{ (unsigned char) linearMap(iteration, 0, maxIteration, 0, 255), 255, 255 };
RgbColor newColor = HsvToRgb(hsvColor);
pixData[0] = newColor.b;
pixData[1] = newColor.g;
pixData[2] = newColor.r;
pixData[3] = 255;
}
else
{
pixData[0] = 0;
pixData[1] = 0;
pixData[2] = 0;
pixData[3] = 255;
}
}
}
}
void calculateJulia(HWND hWnd, double xMin, double yMin, int maxIteration, double widthScale, double heightScale)
{
if (fractalColorData == nullptr)
return;
auto [width, height] = getClientSize(hWnd);
int stride = getStride(width);
//Julia specific calculation variables
double C = -0.8;
double D = 0.156;
for (int r = 0; r < height; r++)
{
for (int c = 0; c < width; c++)
{
double w = xMin + (c * widthScale);
double h = yMin + (r * heightScale);
double x = (h - 32) / 100;
double y = (w - 32) / 100;
int iteration = 0;
while (iteration < maxIteration)
{
double R = ((x * x) - (y * y)) + C;
double I = (2 * (x * y)) + D;
if (R * R > maxIteration * 2)
{
break;
}
else
{
x = R;
y = I;
}
iteration++;
}
//int newW = (int)linearMap(w, xMin, xMax, 0, width - 1);
//int newH = (int)linearMap(h, yMin, yMax, height - 1, 0);
unsigned char* pixData = fractalColorData + (stride * (height - r - 1)) + (c * 4);
if (iteration != maxIteration)
{
HsvColor hsvColor{ (unsigned char) linearMap(iteration, 0, maxIteration, 0, 255), 255, 255 };
RgbColor newColor = HsvToRgb(hsvColor);
pixData[0] = newColor.b;
pixData[1] = newColor.g;
pixData[2] = newColor.r;
pixData[3] = 255;
}
else
{
pixData[0] = 0;
pixData[1] = 0;
pixData[2] = 0;
pixData[3] = 255;
}
}
}
}
void recalculate(HWND hWnd)
{
auto [width, height] = getClientSize(hWnd);
widthScale = (xMax - xMin) / width;
heightScale = (yMax - yMin) / height;
//dataPoints.clear();
if (useMandelbrotMath)
{
calculateMandelbrot(hWnd, xMin, yMin, maxIteration, widthScale, heightScale);
//distributeCalculation(hWnd);
}
else
{
calculateJulia(hWnd, xMin, yMin, maxIteration, widthScale, heightScale);
//distributeCalculation(hWnd);
}
InvalidateRect(hWnd, nullptr, true);
}
HBITMAP createMandelbrotBitmap(int width, int height, unsigned char*& bits)
{
int stride = getStride(width);
int byteCount = (stride * height);
HANDLE hMem = CreateFileMapping(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE, 0, byteCount, nullptr);
BITMAPV5HEADER bmh = { sizeof(BITMAPV5HEADER), width, -height, 1, 32, BI_RGB, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF };
HDC hdcMem = ::CreateCompatibleDC(nullptr);
HBITMAP hbitmap = CreateDIBSection(hdcMem, reinterpret_cast<BITMAPINFO*>(&bmh), DIB_RGB_COLORS, (void**)&bits, NULL, 0);
DeleteObject(hdcMem);
return hbitmap;
}
void drawMandelbrot(HDC hdc, HWND hWnd)
{
//Get the size of the window
auto [width, height] = getClientSize(hWnd);
/*
int stride = getStride(width);
if (!dataPoints.empty())
{
for (Point& p : dataPoints)
{
int newW = (int)linearMap(p.x, xMin, xMax, 0, width - 1);
int newH = (int)linearMap(p.y, yMin, yMax, height - 1, 0);
unsigned char* pixData = mandelbrotColorData + (stride * newH) + (newW * 4);
if (p.color != -1)
{
HsvColor hsvColor{ linearMap(p.color, 0, maxIteration, 0, 255), 255, 255 };
RgbColor newColor = HsvToRgb(hsvColor);
pixData[0] = newColor.b;
pixData[1] = newColor.g;
pixData[2] = newColor.r;
pixData[3] = 255;
}
else
{
pixData[0] = 0;
pixData[1] = 0;
pixData[2] = 0;
pixData[3] = 255;
}
}
}
*/
HDC hdcMem = ::CreateCompatibleDC(nullptr);
auto oldBitmap = SelectObject(hdcMem, fractalBitmap);
BitBlt(hdc, 0, 0, width, height, hdcMem, 0, 0, SRCCOPY);
SelectObject(hdcMem, oldBitmap);
DeleteObject(hdcMem);
}
/*
int numberOfProcessors()
{
SYSTEM_INFO si;
GetSystemInfo(&si);
return si.dwNumberOfProcessors;
}
*/
//NOTICE - I'm not using the multiprocess code. It doesn't seem to be faster than using 1 thread.
/*
void distributeCalculation(HWND hWnd)
{
//How many processes of the algorithm to run.
int numProcesses = 1;
auto [width, height] = getClientSize(hWnd);
double widthScale = (xMax - xMin) / width;
double heightScale = (yMax - yMin) / height;
double totalLength = abs(xMin) + abs(xMax);
double pieceLength = totalLength / numProcesses;
for (int i = 0; i < numProcesses; i++)
{
double xMinNew = xMin + (pieceLength * i);
double xMaxNew = xMin + (pieceLength * (i + 1));
if (useMandelbrotMath)
{
std::thread threadObject(calculateMandelbrot, hWnd, xMinNew, xMaxNew, yMin, yMax, maxIteration, widthScale, heightScale);
threadObject.join();
}
else
{
std::thread threadObject(calculateJulia, hWnd, xMinNew, xMaxNew, yMin, yMax, maxIteration, widthScale, heightScale);
threadObject.join();
}
}
InvalidateRect(hWnd, nullptr, false);
}
*/
void zoomIn(HWND hWnd, int x, int y, int zoomLevel)
{
auto [width, height] = getClientSize(hWnd);
double xMouse = linearMap(x, 0, width, xMin, xMax);
double yMouse = linearMap(y, 0, height, yMax, yMin);
double xMinTemp = xMouse - ((xMax - xMin) / (zoomLevel * zoomLevel));
double xMaxTemp = xMouse + ((xMax - xMin) / (zoomLevel * zoomLevel));
double yMinTemp = yMouse - ((yMax - yMin) / (zoomLevel * zoomLevel));
double yMaxTemp = yMouse + ((yMax - yMin) / (zoomLevel * zoomLevel));
xMin = xMinTemp;
xMax = xMaxTemp;
yMin = yMinTemp;
yMax = yMaxTemp;
updateZoomMenuText(hWnd);
recalculate(hWnd);
}
bool saveBitmap(const HWND& hWnd)
{
wchar_t fileName[MAX_PATH];
wcscpy_s(fileName, MAX_PATH, L"ouput.png");
OPENFILENAME ofn = { sizeof(OPENFILENAME) };
ofn.hwndOwner = hWnd;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = L"Save image";
ofn.lpstrDefExt = L"png";
if (!GetSaveFileName(&ofn))
{
return false;
}
// Initialize GDI+.
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
Gdiplus::Bitmap* image = Gdiplus::Bitmap::FromHBITMAP(fractalBitmap, nullptr);
// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/png", &encoderClsid);
image->Save(fileName, &encoderClsid, NULL);
delete image;
Gdiplus::GdiplusShutdown(gdiplusToken);
return true;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
{
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
}
break;
case ID_ALGORITHM_MANDELBROT:
{
useMandelbrotMath = true;
resetZoom(hWnd);
recalculate(hWnd);
updateMenuChecks(hWnd);
}
break;
case ID_ALGORITHM_JULIA:
{
useMandelbrotMath = false;
resetZoom(hWnd);
recalculate(hWnd);
updateMenuChecks(hWnd);
}
break;
case ID_FILE_RESETZOOM:
{
resetZoom(hWnd);
recalculate(hWnd);
}
break;
case ID_FILE_SAVEIMAGE:
{
saveBitmap(hWnd);
}
break;
case IDM_EXIT:
{
DestroyWindow(hWnd);
}
break;
case ID_MAXITERATIONS_3:
{
maxIteration = 3;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_5:
{
maxIteration = 5;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_10:
{
maxIteration = 10;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_15:
{
maxIteration = 15;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_25:
{
maxIteration = 25;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_50:
{
maxIteration = 50;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_255:
{
maxIteration = 255;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_1000:
{
maxIteration = 1000;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_5000:
{
maxIteration = 5000;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_10000:
{
maxIteration = 10000;
recalculate(hWnd);
}
break;
case ID_MAXITERATIONS_1000000:
{
maxIteration = 1000000;
recalculate(hWnd);
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_KEYDOWN:
{
if (GetKeyState('M') & 0x8000)
{
bool calculating = false;
if (useMandelbrotMath && !calculating)
{
calculating = true;
useMandelbrotMath = false;
}
else if (!useMandelbrotMath && !calculating)
{
calculating = true;
useMandelbrotMath = true;
}
resetZoom(hWnd);
recalculate(hWnd);
updateMenuChecks(hWnd);
calculating = false;
}
if (GetKeyState('R') & 0x8000)
{
resetZoom(hWnd);
recalculate(hWnd);
}
if (GetKeyState('F') & 0x8000 || GetKeyState(VK_F11) & 0x8000)
{
FullScreenSwitch(hWnd);
}
if (GetKeyState('H') & 0x8000)
{
menuSwitch(hWnd);
}
if (GetKeyState('S') & 0x8000)
{
saveBitmap(hWnd);
}
}
break;
case WM_LBUTTONDOWN:
{
int xPos = GET_X_LPARAM(lParam);
int yPos = GET_Y_LPARAM(lParam);
zoomIn(hWnd, xPos, yPos, zoomLevel);
}
break;
case WM_SIZE:
{
auto [width, height] = getClientSize(hWnd);
if (fractalBitmap != nullptr)
{
DeleteObject(fractalBitmap);
}
fractalBitmap = createMandelbrotBitmap(width, height, fractalColorData);
recalculate(hWnd);
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
//Draw Mandelbrot
drawMandelbrot(hdc, hWnd);
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}