Skip to content

Commit 2179ca2

Browse files
author
Kong Wei Hang
committed
Add windows platform support.\n Add debug tool to show more infomations in creating and registing window function
1 parent 4a27314 commit 2179ca2

12 files changed

+307
-29
lines changed

Algorithms/PriorityQueue.h

+128
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#pragma once
2+
3+
template<typename T>
4+
class PriorityQueue
5+
{
6+
public:
7+
PriorityQueue();
8+
~PriorityQueue();
9+
10+
void Insert(T* key);
11+
const T* Max()const;
12+
T* DeleteMax();
13+
bool IsEmpty();
14+
int Size();
15+
16+
private:
17+
bool IsLess(T* lhs, T* rhs);
18+
void Swap(T* lhs, T* rhs);
19+
void Swin(int index);
20+
void Sink(int index);
21+
22+
T* mContent;
23+
int mCount;
24+
};
25+
26+
template<typename T>
27+
PriorityQueue<T>::PriorityQueue()
28+
{
29+
mContent = new T[100];
30+
mCount = 1;
31+
}
32+
33+
template<typename T>
34+
PriorityQueue<T>::~PriorityQueue()
35+
{
36+
delete[] mContent;
37+
mCount = 1;
38+
}
39+
40+
template<typename T>
41+
bool PriorityQueue<T>::IsLess(T* lhs, T* rhs)
42+
{
43+
return *lhs = *rhs;
44+
}
45+
46+
template<typename T>
47+
void PriorityQueue<T>::Swap(T* lhs, T* rhs)
48+
{
49+
T tmp = *lhs;
50+
*lhs = *rhs;
51+
*rhs = tmp;
52+
}
53+
54+
template<typename T>
55+
void PriorityQueue<T>::Swin(int index)
56+
{
57+
while (index > 1)
58+
{
59+
int j = index / 2;
60+
if (IsLess(mContent[index], mContent[j]))
61+
{
62+
break;
63+
}
64+
65+
Swap(mContent[index], mContent[j]);
66+
67+
index = j;
68+
}
69+
}
70+
71+
template<typename T>
72+
void PriorityQueue<T>::Sink(int index)
73+
{
74+
while (index * 2 <= mCount)
75+
{
76+
int j = index * 2;
77+
78+
if (j < mCount && IsLess(mContent[j], mContent[j + 1]))
79+
{
80+
j = j + 1;
81+
}
82+
83+
if (isless(mContent[index), mContent[j])
84+
{
85+
Swap(mContent[index], mContent[j]);
86+
}
87+
88+
index = j;
89+
}
90+
}
91+
92+
template<typename T>
93+
void PriorityQueue<T>::Insert(T* key)
94+
{
95+
mCount++;
96+
mContent[mCount] = key;
97+
Swin(mCount);
98+
}
99+
100+
template<typename T>
101+
const T* PriorityQueue<T>::Max() const
102+
{
103+
return mContent[mCount];
104+
}
105+
106+
template<typename T>
107+
T* PriorityQueue<T>::DeleteMax()
108+
{
109+
T* max = mContent[1];
110+
Swap(mContent[1], mContent[mCount]);
111+
mContent[mCount] = nullptr;
112+
mCount--;
113+
Sink(1);
114+
return max;
115+
}
116+
117+
template<typename T>
118+
bool PriorityQueue<T>::IsEmpty()
119+
{
120+
return mCount == 1;
121+
}
122+
123+
template<typename T>
124+
int PriorityQueue<T>::Size()
125+
{
126+
return mCount;
127+
}
128+

MacWindow.vcxproj

+5
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
</Link>
101101
</ItemDefinitionGroup>
102102
<ItemGroup>
103+
<ClCompile Include="Windows\DebugTool\DebugConsole.cpp" />
103104
<ClCompile Include="Windows\MainWindow.cpp" />
104105
<ClCompile Include="mathlab\MathLib.cpp" />
105106
<ClCompile Include="mathlab\Vector2.cpp" />
@@ -117,6 +118,7 @@
117118
<ClInclude Include="macos\MacDrawer.h" />
118119
<ClInclude Include="macos\UICanvas.h" />
119120
<ClInclude Include="macos\ViewController.h" />
121+
<ClInclude Include="Windows\DebugTool\DebugConsole.h" />
120122
<ClInclude Include="Windows\MainWindow.h" />
121123
<ClInclude Include="mathlab\MathLib.h" />
122124
<ClInclude Include="mathlab\Vector2.h" />
@@ -126,6 +128,9 @@
126128
<ItemGroup>
127129
<None Include="README.md" />
128130
</ItemGroup>
131+
<ItemGroup>
132+
<Image Include="Windows\Resources\scaffolding.ico" />
133+
</ItemGroup>
129134
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
130135
<ImportGroup Label="ExtensionTargets">
131136
</ImportGroup>

MacWindow.vcxproj.filters

+13-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
<ClCompile Include="Windows\MainWindow.cpp">
3131
<Filter>Source Files</Filter>
3232
</ClCompile>
33+
<ClCompile Include="Windows\DebugTool\DebugConsole.cpp">
34+
<Filter>Source Files</Filter>
35+
</ClCompile>
3336
</ItemGroup>
3437
<ItemGroup>
3538
<ClInclude Include="library\lua\lua.h">
@@ -71,14 +74,22 @@
7174
<ClInclude Include="Algorithms\Vector.h">
7275
<Filter>Header Files</Filter>
7376
</ClInclude>
74-
<ClInclude Include="Algorithms\SortSelection.h">
77+
<ClInclude Include="Algorithms\Iterator.h">
7578
<Filter>Header Files</Filter>
7679
</ClInclude>
77-
<ClInclude Include="Algorithms\Iterator.h">
80+
<ClInclude Include="Algorithms\Sort.h">
81+
<Filter>Header Files</Filter>
82+
</ClInclude>
83+
<ClInclude Include="Windows\DebugTool\DebugConsole.h">
7884
<Filter>Header Files</Filter>
7985
</ClInclude>
8086
</ItemGroup>
8187
<ItemGroup>
8288
<None Include="README.md" />
8389
</ItemGroup>
90+
<ItemGroup>
91+
<Image Include="Windows\Resources\scaffolding.ico">
92+
<Filter>Resource Files</Filter>
93+
</Image>
94+
</ItemGroup>
8495
</Project>

Windows/BaseWindow.h

+48-12
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,30 @@
1010
template <class T>
1111
class BaseWindow
1212
{
13+
1314
public:
15+
enum StatusCode {
16+
Null,
17+
Initializing,
18+
InitiailizationFailed,
19+
Running,
20+
Closing
21+
};
22+
1423
static LRESULT CALLBACK sWindowProc(HWND, UINT, WPARAM, LPARAM);
1524
static const bool FULL_SCREEN;
1625
static const bool VSYNC_ENABLED;
1726
static const float SCREEN_DEPTH;
1827
static const float SCREEN_NEAR;
1928

20-
BaseWindow() : mWnd(NULL) {}
29+
BaseWindow(HINSTANCE hInstance)
30+
: mWnd(NULL)
31+
, mhInstance(hInstance)
32+
, mStatusCode(StatusCode::Null)
33+
{}
34+
virtual ~BaseWindow() {}
2135
bool Create(
22-
PCWSTR lpWindowName,
36+
LPCWSTR lpWindowName,
2337
DWORD dwStyle,
2438
int& width,
2539
int& height,
@@ -31,18 +45,16 @@ class BaseWindow
3145
);
3246
HWND getHwnd()const { return mWnd; }
3347

34-
35-
36-
3748
protected:
3849
void ShutdownWindows();
3950
virtual LRESULT HandMessage(UINT uMSG, WPARAM wParam, LPARAM lParam) = 0;
40-
virtual PCWSTR ClassName() const = 0;
51+
virtual LPCWSTR ClassName() const = 0;
4152

4253
HWND mWnd;
54+
HINSTANCE mhInstance;
55+
StatusCode mStatusCode;
4356

4457
private:
45-
4658
};
4759

4860

@@ -62,7 +74,7 @@ const float BaseWindow<T>::SCREEN_NEAR = 0.1f;
6274

6375
template<class T>
6476
bool BaseWindow<T>::Create(
65-
PCWSTR lpWindowName,
77+
LPCWSTR lpWindowName,
6678
DWORD dwStyle,
6779
int& width,
6880
int& height,
@@ -72,14 +84,36 @@ bool BaseWindow<T>::Create(
7284
HWND hWndParent /*= 0*/,
7385
HMENU hMenu /*= 0*/)
7486
{
87+
mStatusCode = Initializing;
7588
DEVMODE dmScreenSetting;
7689

77-
WNDCLASS wc = { 0 };
90+
// This will hold the class we created
91+
WNDCLASSEX wc = { 0 };
92+
93+
// First fill in the window class structure
94+
wc.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
95+
// The size, in bytes, of this structure. Set this member to sizeof(WNDCLASSEX). Be sure to set this member before calling the GetClassInfoEx function.
96+
// The parameter MUST be correct, if not will due window class class cannot be created because the parameter is invalid.
97+
wc.cbSize = sizeof(WNDCLASSEX);
7898
wc.lpfnWndProc = T::sWindowProc;
7999
wc.hInstance = GetModuleHandle(NULL);
80100
wc.lpszClassName = ClassName();
81-
82-
RegisterClass(&wc);
101+
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
102+
//wc.hIcon = LoadIcon(mhInstance, L"ScaffoldLogo");
103+
//wc.hIcon = LoadIcon(nullptr, IDI_APPLICATION);
104+
//wc.hIconSm = LoadIcon(mhInstance, L"ScaffoldLogo");
105+
//wc.hIconSm = LoadIcon(nullptr, IDI_APPLICATION);
106+
107+
//If the function succeeds, the return value is a class atom that uniquely identifies the class being registered.
108+
//This atom can only be used by the CreateWindow, CreateWindowEx, GetClassInfo, GetClassInfoEx, FindWindow, FindWindowEx,
109+
//and UnregisterClass functions and the IActiveIMMap::FilterClientWindows method.
110+
//If the function fails, the return value is zero.
111+
ATOM classAtom = RegisterClassEx(&wc);
112+
if (classAtom == 0)
113+
{
114+
mStatusCode = StatusCode::InitiailizationFailed;
115+
return false;
116+
}
83117

84118
width = GetSystemMetrics(SM_CXSCREEN);
85119
height = GetSystemMetrics(SM_CYSCREEN);
@@ -121,7 +155,8 @@ bool BaseWindow<T>::Create(
121155
this
122156
);
123157
if (!mWnd)
124-
{
158+
{
159+
mStatusCode = StatusCode::InitiailizationFailed;
125160
return false;
126161
}
127162
ShowWindow(mWnd, SW_SHOW);
@@ -156,6 +191,7 @@ void BaseWindow<T>::ShutdownWindows()
156191
template<class T>
157192
LRESULT BaseWindow<T>::sWindowProc(HWND hwnd, UINT uMSG, WPARAM wParam, LPARAM lParam)
158193
{
194+
159195
static T* pThis = NULL;
160196

161197
if (uMSG == WM_NCCREATE)

Windows/DebugTool/DebugConsole.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include "DebugConsole.h"
2+
3+
#include <Windows.h> //AllocConsole and FreeConsole
4+
#include <strsafe.h>
5+
//#include <afxwin.h>
6+
7+
namespace DebugTool {
8+
9+
bool DebugConsole::Initialize() {
10+
if (!AllocConsole()) {
11+
//AfxMessageBox("Failed to create the console!", MB_ICONEXCLAMATION);
12+
return false;
13+
}
14+
return true;
15+
}
16+
17+
bool DebugConsole::Shutdown() {
18+
if (!FreeConsole()) {
19+
//AfxMessageBox("Could not free the console!");
20+
return false;
21+
}
22+
return true;
23+
}
24+
25+
void DebugConsole::RetrieveError(LPTSTR lpszFunction) {
26+
// Retrieve the system error message for the last-error code
27+
28+
LPVOID lpMsgBuf;
29+
LPVOID lpDisplayBuf;
30+
DWORD dw = GetLastError();
31+
32+
FormatMessage(
33+
FORMAT_MESSAGE_ALLOCATE_BUFFER |
34+
FORMAT_MESSAGE_FROM_SYSTEM |
35+
FORMAT_MESSAGE_IGNORE_INSERTS,
36+
NULL,
37+
dw,
38+
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
39+
(LPTSTR)&lpMsgBuf,
40+
0, NULL);
41+
42+
// Display the error message and exit the process
43+
44+
lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
45+
(lstrlen((LPCTSTR)lpMsgBuf) + lstrlen((LPCTSTR)lpszFunction) + 40) * sizeof(TCHAR));
46+
StringCchPrintf((LPTSTR)lpDisplayBuf,
47+
LocalSize(lpDisplayBuf) / sizeof(TCHAR),
48+
TEXT("%s failed with error %d: %s"),
49+
lpszFunction, dw, lpMsgBuf);
50+
MessageBox(NULL, (LPCTSTR)lpDisplayBuf, TEXT("Error"), MB_OK);
51+
52+
LocalFree(lpMsgBuf);
53+
LocalFree(lpDisplayBuf);
54+
ExitProcess(dw);
55+
}
56+
}

Windows/DebugTool/DebugConsole.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include <wtypes.h>
4+
5+
namespace DebugTool {
6+
class DebugConsole
7+
{
8+
public:
9+
DebugConsole() = default;
10+
~DebugConsole() = default;
11+
12+
bool Initialize();
13+
bool Shutdown();
14+
15+
void RetrieveError(LPTSTR lpszFunction);
16+
protected:
17+
private:
18+
};
19+
}

0 commit comments

Comments
 (0)