File: main.cpp

package info (click to toggle)
devil 1.6.7-5%2Betch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 11,536 kB
  • ctags: 7,441
  • sloc: ansic: 35,573; sh: 8,075; cpp: 7,465; pascal: 792; makefile: 399; python: 47
file content (809 lines) | stat: -rw-r--r-- 21,661 bytes parent folder | download | duplicates (4)
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
//============================================================//
// File:		main.cpp
// 
// Date:		11-21-2000
//===========================================================//
#include "../header/Render2DCore.h"
#include "resource.h"
#include <math.h>
#include <il/ilut.h>


HWND						m_MainHWND;
HINSTANCE					hInst;
beScreen					m_MainDDScreen;
beSurface*					m_pMainSurface;
beSurface*					m_pMouseCursorSurface;
beSurface*					m_pBackgroundSurface;
DDBLTFX						ddbltfx;

int							m_iWindowWidth	= 640;
int							m_iWindowHeight = 480;
int							m_iCurrMouseX;
int							m_iCurrMouseY;
beScreenParams				InitDisplayStruct;

#define BORDER_W	8
#define MENU_H		46
#define MIN_W		205  // Accomodate the menu bar.
#define	MAX_W		400
#define	MAX_H		300
#define	TITLE		"OpenIL Direct Draw Test"

ILuint	NumUndosAllowed = 4, UndoSize = 0;
ILuint	Undos[11] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
ILuint	Width, Height, Depth;  // Main image
ILuint	MainImage = 0;  // Main image
HBITMAP	hBitmap;  // Main bitmap

ILuint	FilterType;
ILuint	FilterParamInt;
ILfloat	FilterParamFloat;


BOOL APIENTRY AboutDlgProc (HWND hDlg, UINT message, UINT wParam, LONG lParam);
BOOL APIENTRY FilterDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
BOOL APIENTRY ResizeDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
void GenFilterString(char *Out, char **Strings);

//===========================================================//
// WindowProc()
//===========================================================//
long FAR PASCAL WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	static int x, y;
	static HDC hDC;

	static char NewTitle[2048];
	static char OpenFileName[2048];
	static char OpenFilter[2048];
	static char SaveFileName[2048];
	static char SaveFilter[2048];
	static char *OFilter[] = {
		"All Files (*.*)", "*.*",
		"Half-Life Model Files (*.mdl)", "*.mdl",
		"Homeworld Image Files (*.lif)", "*.lif",
		"Image Files (All Supported Types)", "*.jpe;*.jpg;*.jpeg;*.lif;*.bmp;*.ico;*.pbm;*.pgm;*.pnm;*.ppm;*.png;*.bw;*.rgb;*.rgba;*.sgi;*.tga;*.tif;*.tiff;*.pcx",
		"Jpeg Files (*.jpe, *.jpg, *.jpeg)", "*.jpe;*.jpg;*.jpeg",
		"Microsoft Bitmap Files (*.bmp)", "*.bmp",
		"Microsoft Icon Files (*.ico)", "*.ico",
		"OpenIL Files (*.oil)", "*.oil",
		"Portable AnyMap Files (*.pbm, *.pgm, *.pnm, *.ppm)", "*.pbm;*.pgm;*.pnm;*.ppm",
		"Portable Network Graphics Files (*.png)", "*.png",
		"Sgi Files (*.sgi)", "*.bw;*.rgb;*.rgba;*.sgi",
		"Targa Files (*.tga)", "*.tga",
		"Tiff Files (*.tif)", "*.tif;*.tiff",
		"Quake Wal Files (*.wal)", "*.wal",
		"ZSoft Pcx Files (*.pcx)", "*.pcx",
		"\0\0"
	};
	static char *SFilter[] = {
		"All Files (*.*)", "*.*",
		"C-Style Header (*.h)", "*.h",
		"Jpeg Files (*.jpe, *.jpg, *.jpeg)", "*.jpe;*.jpg;*.jpeg",
		"Microsoft Bitmap Files (*.bmp)", "*.bmp",
		"OpenIL Files (*.oil)", "*.oil",
		"Portable AnyMap Files (*.pbm, *.pgm, *.ppm)", "*.pbm;*.pgm;*.ppm",
		"Portable Network Graphics Files (*.png)", "*.png",
		"Sgi Files (*.sgi)", "*.bw;*.rgb;*.rgba;*.sgi",
		"Targa Files (*.tga)", "*.tga",
		"ZSoft Pcx Files (*.pcx)", "*.pcx",
		"\0\0"
	};

	static OPENFILENAME Ofn = {
		sizeof(OPENFILENAME),
		hWnd,
		NULL,
		OpenFilter,
		NULL,
		0,
		0,
		OpenFileName,
		2048,
		NULL,
		0,
		NULL,
		NULL,
		OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST,
		0,
		0,
		NULL,
		NULL,
		NULL,
		NULL
	};

    switch (message)
    {
        case WM_ACTIVATE:
            return 0L;

		case WM_CREATE:
			GenFilterString(OpenFilter, OFilter);
			GenFilterString(SaveFilter, SFilter);

			// Store the device context
			hDC = GetDC(hWnd);
			return 0L;

        case WM_DESTROY:
            // Clean up and close the app
			m_MainDDScreen.Delete();
            PostQuitMessage(0);
            return 0L;

        case WM_KEYDOWN:
            // Handle any non-accelerated key commands
            switch (wParam)
            {
                case VK_ESCAPE:
                case VK_F12:
                    PostMessage(hWnd, WM_CLOSE, 0, 0);
                    return 0L;
            }
            break;

		case WM_MOVE:
			m_MainDDScreen.Move();
			break;

		case WM_SIZE:
            m_MainDDScreen.Move();
			break;

		case WM_MOUSEMOVE:
			m_iCurrMouseX = LOWORD(lParam); 
			m_iCurrMouseY = HIWORD(lParam); 

		case WM_PAINT:
			if (!m_pBackgroundSurface)
				break;

			// we don't need a colorfill...cause we're using a bitmap to clear the surface
			m_pMainSurface->GetSurface()->Blt(NULL, NULL, NULL, DDBLT_COLORFILL, &ddbltfx);
			m_pMainSurface->BltFast(0, 0, m_pBackgroundSurface, NULL, DDBLTFAST_NOCOLORKEY);

			/*RECT rcRect;
			rcRect.left = 0;
			rcRect.right = 128;
			rcRect.top = 0;
			rcRect.bottom = 128;*/

			DDSURFACEDESC2 ddsd;
			if (!m_pMouseCursorSurface->Lock(ddsd))
				return -1;

			// pixel-plotting test
			for (x = 0; x < 128; x++) {
				for (y = 0; y < 128; y++) {	
					m_pMouseCursorSurface->PlotPixel(x, y, x*2%255, 0, y*2%255, &ddsd);
				}
			}
			if (!m_pMouseCursorSurface->Unlock())
				return -1;

			//m_pMainSurface->BltFast(0, 0, m_pMouseCursorSurface,
			//                   &rcRect, DDBLTFAST_NOCOLORKEY);

			m_MainDDScreen.GetBackBufferSurface()->BltFast(0,0,m_pMainSurface->GetSurface(),NULL,DDBLTFAST_NOCOLORKEY);
			m_MainDDScreen.Flip();

			ValidateRect(hWnd, NULL);
			break;

		case WM_COMMAND:
			FilterType = LOWORD(wParam);

			switch (LOWORD(wParam))
			{
				case ID_FILE_EXIT:
					DestroyWindow(hWnd);
					return (0L);

				case ID_HELP_ABOUT:
					DialogBox (hInst,
						MAKEINTRESOURCE(IDD_DIALOG_ABOUT),
						hWnd,
						AboutDlgProc);
					return (0L);

				case ID_EFFECTS_COUNTCOLORS:
					int Colours;
					Colours = iluColoursUsed();
					char ColourString[255];
					sprintf(ColourString, "The number of colours in this image is:  %d", Colours);
					MessageBox(NULL, ColourString, "Colour Count", MB_OK);
					return (0L);

				case ID_EDIT_COPY:
					ilutSetWinClipboard();
					return (0L);

				case ID_EDIT_PASTE:
					ILuint Test;
					ilGenImages(1, &Test);
					ilBindImage(Test);

					// Check if there's anything in the clipboard.
					if (!ilutGetWinClipboard()) {
						ilDeleteImages(1, &Test);
						return (0L);
					}
					ilDeleteImages(1, &Test);

					ilDeleteImages(UndoSize, Undos);
					UndoSize = 0;

					ilGenImages(1, &MainImage);
					ilBindImage(MainImage);
					Undos[0] = MainImage;
					ilutGetWinClipboard();

					ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
					ilutRenderer(ILUT_WIN32);

					Width = ilGetInteger(IL_IMAGE_WIDTH);
					Height = ilGetInteger(IL_IMAGE_HEIGHT);
					Depth = ilGetInteger(IL_IMAGE_DEPTH);

					hBitmap = ilutConvertToHBitmap(hDC);
					m_pBackgroundSurface->LoadImage(hBitmap, false);
					DeleteObject(hBitmap);

					RECT Rect;
					GetWindowRect(hWnd, &Rect);
					SetWindowPos(hWnd, HWND_TOP, Rect.left, Rect.top,
						Width < MIN_W ? MIN_W + BORDER_W : Width + BORDER_W,
						Height + MENU_H, SWP_SHOWWINDOW);
					return (0L);

				case ID_FILE_LOAD:
					sprintf(OpenFileName, "*.*");
					Ofn.lpstrFilter = OpenFilter;
					Ofn.lpstrFile = OpenFileName;
					Ofn.lpstrTitle = "Open File";
					Ofn.Flags = OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST;

					if (!GetOpenFileName(&Ofn))
						return (0L);

					ilDeleteImages(UndoSize, Undos);
					UndoSize = 0;

					ilGenImages(1, &MainImage);
					ilBindImage(MainImage);
					Undos[0] = MainImage;
					if (!ilLoadImage(OpenFileName))
						return (0L);
					ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
					ilutRenderer(ILUT_WIN32);

					Width = ilGetInteger(IL_IMAGE_WIDTH);
					Height = ilGetInteger(IL_IMAGE_HEIGHT);
					Depth = ilGetInteger(IL_IMAGE_DEPTH);

					hBitmap = ilutConvertToHBitmap(hDC);
					m_pBackgroundSurface->LoadImage(hBitmap, false);
					DeleteObject(hBitmap);

					GetWindowRect(hWnd, &Rect);
					SetWindowPos(hWnd, HWND_TOP, Rect.left, Rect.top,
						Width < MIN_W ? MIN_W + BORDER_W : Width + BORDER_W,
						Height + MENU_H, SWP_SHOWWINDOW);

					sprintf(NewTitle, "%s - %s", TITLE, OpenFileName);
					SetWindowText(hWnd, NewTitle);

					return (0L);

				case ID_FILE_SAVE:
					sprintf(SaveFileName, "monkey.tga");
					Ofn.lpstrFilter = SaveFilter;
					Ofn.lpstrFile = SaveFileName;
					Ofn.lpstrTitle = "Save File";
					Ofn.Flags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;

					if (!GetSaveFileName(&Ofn))
						return (0L);

					ilEnable(IL_FILE_OVERWRITE);
					ilSaveImage(SaveFileName);

					sprintf(NewTitle, "%s - %s", TITLE, SaveFileName);
					SetWindowText(hWnd, NewTitle);

					return (0L);

				case ID_EDIT_UNDOLEVEL:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						NumUndosAllowed = FilterParamInt <= 10 ? FilterParamInt : 10;
					}
					return (0L);

				case ID_EDIT_UNDO:
					if (UndoSize && NumUndosAllowed) {
						ilDeleteImages(1, &Undos[UndoSize]);
						ilBindImage(Undos[--UndoSize]);
						hBitmap = ilutConvertToHBitmap(hDC);
						m_pBackgroundSurface->LoadImage(hBitmap, false);
						DeleteObject(hBitmap);

						Width = ilGetInteger(IL_IMAGE_WIDTH);
						Height = ilGetInteger(IL_IMAGE_HEIGHT);
						Depth = ilGetInteger(IL_IMAGE_DEPTH);

						GetWindowRect(hWnd, &Rect);
						SetWindowPos(hWnd, HWND_TOP, Rect.left, Rect.top,
							Width < MIN_W ? MIN_W + BORDER_W : Width + BORDER_W,
							Height + MENU_H, SWP_SHOWWINDOW);


						InvalidateRect(hWnd, NULL, FALSE);
					}
					return (0L);
			}


			if (++UndoSize > NumUndosAllowed) {
				if (NumUndosAllowed > 0) {
					UndoSize = NumUndosAllowed;
					ilDeleteImages(1, &Undos[0]);
					memcpy(Undos, Undos+1, NumUndosAllowed * sizeof(ILuint));
					ilBindImage(Undos[UndoSize]);
				}
			}

			if (NumUndosAllowed > 0) {
				ilGenImages(1, &Undos[UndoSize]);
				ilBindImage(Undos[UndoSize]);
				ilCopyImage(Undos[UndoSize-1]);
				MainImage = Undos[UndoSize];  // ???
			}


			
			switch (LOWORD(wParam))
			{
				case ID_CONVERT_RGB:
					ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
					break;

				case ID_CONVERT_RGBA:
					ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
					break;

				case ID_CONVERT_BGR:
					ilConvertImage(IL_BGR, IL_UNSIGNED_BYTE);
					break;

				case ID_CONVERT_BGRA:
					ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
					break;

				case ID_CONVERT_LUMINANCE:
					ilConvertImage(IL_LUMINANCE, IL_UNSIGNED_BYTE);
					break;

				case ID_EFFECTS_FLIP:
					iluFlipImage();
					break;

				case ID_EFFECTS_MIRROR:
					iluMirror();
					break;

				case ID_FILTER_EMBOSS:
					iluEmboss();
					break;

				case ID_FILTER_ALIENIFY:
					iluAlienify();
					break;

				case ID_FILTER_NEGATIVE:
					iluNegative();
					break;

				case ID_EFFECTS_FILTERS_EDGEDETECT_EMBOSS:
					iluEdgeDetectE();
					break;

				case ID_EFFECTS_FILTERS_EDGEDETECT_SOBEL:
					iluEdgeDetectS();
					break;

				case ID_EFFECTS_FILTERS_EDGEDETECT_PREWITT:
					iluEdgeDetectP();
					break;

				case ID_FILTER_NOISE:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluNoisify(FilterParamFloat);
					}
					break;

				case ID_FILTER_PIXELIZE:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluPixelize(FilterParamInt);
					}
					break;

				case ID_FILTERS_BLUR_AVERAGE:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluBlurAvg(FilterParamInt);
					}
					break;

				case ID_FILTERS_BLUR_GAUSSIAN:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluBlurGaussian(FilterParamInt);
					}
					break;

				case ID_FILTER_GAMMACORRECT:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluGammaCorrect(FilterParamFloat);
					}
					break;

				case ID_FILTER_SHARPEN:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluSharpen(FilterParamFloat, 1);
					}
					break;

				case ID_EFFECTS_FILTERS_ROTATE:
					if (DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_FILTER),
						hWnd, FilterDlgProc) == TRUE) {
						iluRotate(FilterParamFloat);

						Width = ilGetInteger(IL_IMAGE_WIDTH);
						Height = ilGetInteger(IL_IMAGE_HEIGHT);
						RECT Rect;
						GetWindowRect(hWnd, &Rect);
						SetWindowPos(hWnd, HWND_TOP, Rect.left, Rect.top,
							Width < MIN_W ? MIN_W + BORDER_W : Width + BORDER_W,
							Height + MENU_H, SWP_SHOWWINDOW);
					}
					break;

				case ID_EFFECTS_FILTERS_SCALE:
					iluImageParameter(ILU_FILTER, ILU_BILINEAR);
					DialogBox(hInst, MAKEINTRESOURCE(IDD_DIALOG_RESIZE), hWnd, ResizeDlgProc);
					break;
			}

			ilConvertImage(IL_BGRA, IL_UNSIGNED_BYTE);
			hBitmap = ilutConvertToHBitmap(hDC);
			m_pBackgroundSurface->LoadImage(hBitmap, false);
			DeleteObject(hBitmap);
			InvalidateRect(hWnd, NULL, FALSE);
			break;

		default:
			return DefWindowProc(hWnd, message, wParam, lParam);

    }


    return 0L;
}


//===========================================================//
// InitApp()
//===========================================================//
static HRESULT InitApp(HINSTANCE hInstance, int nCmdShow)
{
    HWND		hWnd;
    WNDCLASSEX	wcex;

	wcex.cbSize = sizeof(WNDCLASSEX); 
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= (LPCSTR)IDR_MENU1;
	wcex.lpszClassName	= TITLE;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON10);

	RegisterClassEx(&wcex);


    // Create a window
    hWnd = CreateWindow(TITLE, TITLE, WS_OVERLAPPEDWINDOW, 50, 50, 400, 300,
						NULL, NULL, hInstance, NULL);
    if (!hWnd)
        return FALSE;
    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);
    SetFocus(hWnd);

	m_MainHWND = hWnd;

	InitDisplayStruct.m_Flags = 0;
	InitDisplayStruct.m_Flags |= BE_SCREENFLAGS_WINDOWED;
	InitDisplayStruct.m_OwnerWnd = hWnd;

	InitDisplayStruct.m_iScreenWidth = m_iWindowWidth;
	InitDisplayStruct.m_iScreenHeight = m_iWindowHeight;
	InitDisplayStruct.m_iScreenBitCount = 16;

	if (!m_MainDDScreen.Initialize(InitDisplayStruct))
		MessageBox(NULL,"ah!","ahhhh",MB_OK);
	
	m_pMouseCursorSurface	= new beSurface(&m_MainDDScreen);
	m_pBackgroundSurface	= new beSurface(&m_MainDDScreen);
	m_pMainSurface			= new beSurface(&m_MainDDScreen);
	m_pMainSurface->CreateSurface(InitDisplayStruct.m_iScreenWidth,InitDisplayStruct.m_iScreenHeight,false);

	ilEnable(IL_FORMAT_SET);
	ilEnable(IL_ORIGIN_SET);
	ilEnable(IL_TYPE_SET);

	ilFormatFunc(IL_BGRA);
	ilOriginFunc(IL_ORIGIN_LOWER_LEFT);
	ilTypeFunc(IL_UNSIGNED_BYTE);

	hBitmap = ilutConvertToHBitmap(GetDC(hWnd));
	m_pBackgroundSurface->LoadImage(hBitmap, false);

	m_pMouseCursorSurface->CreateSurface(128, 128, true);

	InvalidateRect(hWnd, NULL, FALSE);

	return TRUE;
}

//===========================================================//
// WinMain()
//===========================================================//
int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    MSG		msg;
	HACCEL	hAccelTable;

	hInst = hInstance;

    InitApp(hInstance, nCmdShow);
	ZeroMemory(&ddbltfx, sizeof(ddbltfx));
	ddbltfx.dwSize = sizeof(ddbltfx);
	ddbltfx.dwFillColor = RGB(128, 128, 128);

	hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDR_MENU1);
	while (GetMessage(&msg, NULL, 0, 0)) {
		if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
	}

	delete m_pMainSurface;
	delete m_pMouseCursorSurface;
	delete m_pBackgroundSurface;

    return msg.wParam;
}


BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
    switch (message)
	{
		// Initialize the dialog box
	    case WM_INITDIALOG:
		{
			int i;
			ILenum ilError;
			char VersionNum[256];

			sprintf(VersionNum, "Num:  %d", ilGetInteger(IL_VERSION_NUM));

			// ilGetString demo
			SetDlgItemText(hDlg, IDC_ABOUT_VENDOR, ilGetString(IL_VENDOR));
			SetDlgItemText(hDlg, IDC_ABOUT_VER_STRING, ilGetString(IL_VERSION));
			SetDlgItemText(hDlg, IDC_ABOUT_VER_NUM, VersionNum);

			// Display any recent error messages
			for (i = 0; i < 6; i++) {
				ilError = ilGetError();
				if (ilError == IL_NO_ERROR)
					break;
				SetDlgItemText(hDlg, IDC_ERROR1+i, iluErrorString(ilError));
			}

			return (TRUE);
		}
		break;

		// Process command messages
	    case WM_COMMAND:      
		{
			// Validate and Make the changes
			if (LOWORD(wParam) == IDOK)
				EndDialog(hDlg, TRUE);
			if (LOWORD(wParam) == IDCANCEL)
				EndDialog(hDlg, FALSE);
	    }
		break;

		// Closed from sysbox
		case WM_CLOSE:
			EndDialog(hDlg,TRUE);
		break;
	}

	return FALSE;
}


// Dialog procedure.
BOOL APIENTRY FilterDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
	char EditString[255];

    switch (message)
	{
		// Initialize the dialog box
	    case WM_INITDIALOG:
		{
			switch (FilterType)
			{
				case ID_FILTER_PIXELIZE:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Width of pixelized block:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1");
					break;
				case ID_FILTER_NOISE:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Amount of noise threshold:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1");
					break;
				case ID_FILTERS_BLUR_AVERAGE:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Number of iterations:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1");
					break;
				case ID_FILTERS_BLUR_GAUSSIAN:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Number of iterations:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1");
					break;
				case ID_FILTER_GAMMACORRECT:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Amount of gamma correction:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1.0");
					break;
				case ID_FILTER_SHARPEN:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Sharpening factor:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "1.0");
					break;
				case ID_EFFECTS_FILTERS_ROTATE:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Number of degress to rotate:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "0.0");
					break;

				case ID_EDIT_UNDOLEVEL:
					SetDlgItemText(hDlg, IDC_FILTER_DESC_TEXT, "Set level of undo:");
					SetDlgItemText(hDlg, IDC_FILTER_EDIT, "4");
					break;

			}

			return TRUE;
		}
		break;

		// Process command messages
	    case WM_COMMAND:
		{
			// Validate and Make the changes
			if (LOWORD(wParam) == IDOK) {
				GetDlgItemText(hDlg, IDC_FILTER_EDIT, EditString, 255);
				FilterParamInt = atoi(EditString);
				FilterParamFloat = (float)atof(EditString);
				EndDialog(hDlg, TRUE);
			}
			if (LOWORD(wParam) == IDCANCEL) {
				EndDialog(hDlg, FALSE);
			}
	    }
		break;

		// Closed from sysbox
		case WM_CLOSE:
			EndDialog(hDlg,TRUE);
		break;
	}

	return FALSE;
}


// Dialog procedure.
BOOL APIENTRY ResizeDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
	static char x[255], y[255], z[255];
	static ILuint xsize, ysize, zsize;
	static RECT Rect;

    switch (message)
	{
		// Initialize the dialog box
	    case WM_INITDIALOG:
		{
			sprintf(x, "%d", Width);
			sprintf(y, "%d", Height);
			sprintf(z, "%d", Depth);
			SetDlgItemText(hDlg, IDC_EDIT_RESIZE_X, x);
			SetDlgItemText(hDlg, IDC_EDIT_RESIZE_Y, y);
			SetDlgItemText(hDlg, IDC_EDIT_RESIZE_Z, z);
			return TRUE;
		}
		break;

		// Process command messages
	    case WM_COMMAND:
		{
			// Validate and Make the changes
			if (LOWORD(wParam) == IDOK) {
				GetDlgItemText(hDlg, IDC_EDIT_RESIZE_X, x, 255);
				GetDlgItemText(hDlg, IDC_EDIT_RESIZE_Y, y, 255);
				GetDlgItemText(hDlg, IDC_EDIT_RESIZE_Z, z, 255);
				xsize = atoi(x);
				ysize = atoi(y);
				zsize = atoi(z);
				if (xsize && ysize && zsize) {
					iluScale(xsize, ysize, zsize);

					Width = ilGetInteger(IL_IMAGE_WIDTH);
					Height = ilGetInteger(IL_IMAGE_HEIGHT);
					Depth = ilGetInteger(IL_IMAGE_DEPTH);

					GetWindowRect(m_MainHWND, &Rect);
					SetWindowPos(m_MainHWND, HWND_TOP, Rect.left, Rect.top,
						Width < MIN_W ? MIN_W + BORDER_W : Width + BORDER_W,
						Height + MENU_H, SWP_SHOWWINDOW);


					InvalidateRect(m_MainHWND, NULL, FALSE);
				}
				EndDialog(hDlg, TRUE);
			}
			if (LOWORD(wParam) == IDCANCEL) {
				EndDialog(hDlg, FALSE);
			}
	    }
		break;

		// Closed from sysbox
		case WM_CLOSE:
			EndDialog(hDlg,TRUE);
		break;
	}

	return FALSE;
}


void GenFilterString(char *Out, char **Strings)
{
	int OutPos = 0, StringPos = 0;

	while (Strings[StringPos][0] != 0) {
		sprintf(Out + OutPos, Strings[StringPos]);
		OutPos += strlen(Strings[StringPos++]) + 1;
	}

	Out[OutPos++] = 0;
	Out[OutPos] = 0;

	return;
}