Skip to content

Commit 7e4ed61

Browse files
committed
Compiles and runs and works and everything!
git-svn-id: http://zigserv/svn/Projects/trunk/wcdx@268 41632dc1-7a16-0410-8441-a1f66f767317
1 parent 368cb42 commit 7e4ed61

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

include/iwcdx.idl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import "unknwn.idl";
22
import "wtypes.idl";
33

4+
cpp_quote("#ifdef WCDX_EXPORTS")
5+
cpp_quote("#define WCDXAPI __declspec(dllexport)")
6+
cpp_quote("#else")
7+
cpp_quote("#define WCDXAPI __declspec(dllimport)")
8+
cpp_quote("#endif")
9+
410
interface IWcdx;
11+
12+
cpp_quote("WCDXAPI")
513
IWcdx* CreateWcdx(HWND window);
614

715
[

src/wcdx.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
using namespace std;
99

10-
IWcdx* CreateWcdx(HWND window)
10+
WCDXAPI IWcdx* CreateWcdx(HWND window)
1111
{
1212
try
1313
{
@@ -19,7 +19,7 @@ IWcdx* CreateWcdx(HWND window)
1919
}
2020
}
2121

22-
Wcdx::Wcdx(HWND window) : d3d(::Direct3DCreate9(D3D_SDK_VERSION)), dirty(false)
22+
Wcdx::Wcdx(HWND window) : ref_count(1), d3d(::Direct3DCreate9(D3D_SDK_VERSION)), dirty(false)
2323
{
2424
D3DPRESENT_PARAMETERS params =
2525
{
@@ -42,17 +42,39 @@ Wcdx::Wcdx(HWND window) : d3d(::Direct3DCreate9(D3D_SDK_VERSION)), dirty(false)
4242

4343
HRESULT STDMETHODCALLTYPE Wcdx::QueryInterface(REFIID riid, void** ppvObject)
4444
{
45+
if (ppvObject == nullptr)
46+
return E_POINTER;
47+
48+
if (IsEqualIID(riid, IID_IUnknown))
49+
{
50+
*reinterpret_cast<IUnknown**>(ppvObject) = this;
51+
++ref_count;
52+
return S_OK;
53+
}
54+
if (IsEqualIID(riid, IID_IWcdx))
55+
{
56+
*reinterpret_cast<IWcdx**>(ppvObject) = this;
57+
++ref_count;
58+
return S_OK;
59+
}
60+
4561
return E_NOINTERFACE;
4662
}
4763

4864
ULONG STDMETHODCALLTYPE Wcdx::AddRef()
4965
{
50-
return 0;
66+
return ++ref_count;
5167
}
5268

5369
ULONG STDMETHODCALLTYPE Wcdx::Release()
5470
{
55-
return 0;
71+
if (--ref_count == 0)
72+
{
73+
delete this;
74+
return 0;
75+
}
76+
77+
return ref_count;
5678
}
5779

5880
HRESULT STDMETHODCALLTYPE Wcdx::SetPalette(const PALETTEENTRY entries[256])

src/wcdx.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,7 @@
88
#include <d3d9.h>
99

1010

11-
#ifdef WCDX_EXPORTS
12-
#define WCDXAPI __declspec(dllexport)
13-
#else
14-
#define WCDXAPI __declspec(dllimport)
15-
#endif
16-
17-
18-
class WCDXAPI Wcdx : public IWcdx
11+
class Wcdx : public IWcdx
1912
{
2013
public:
2114
Wcdx(HWND window);
@@ -36,6 +29,8 @@ class WCDXAPI Wcdx : public IWcdx
3629
HRESULT STDMETHODCALLTYPE Present() override;
3730

3831
private:
32+
ULONG ref_count;
33+
3934
// Suppress warnings about IDirect3D9Ptr not being exported -- they're not
4035
// directly usable by clients, so it doesn't matter.
4136
#pragma warning(push)

test/src/main.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#include <iwcdx.h>
55

6+
#include <d3d9.h>
67
#include <DxErr.h>
78
#include <Shlwapi.h>
9+
#include <comdef.h>
810

911
#include <algorithm>
1012
#include <memory>
@@ -27,7 +29,7 @@ enum
2729

2830
struct WindowData
2931
{
30-
Wcdx& wcdx;
32+
IWcdx* wcdx;
3133
vector<unique_ptr<Bitmap>> images;
3234
size_t imageIndex;
3335
};
@@ -76,7 +78,7 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpComma
7678
L"Wcdx Test", WS_OVERLAPPEDWINDOW & ~(WS_THICKFRAME | WS_MAXIMIZEBOX),
7779
CW_USEDEFAULT, CW_USEDEFAULT, 1024, 768,
7880
nullptr, nullptr, hInstance, nullptr);
79-
Wcdx wcdx(window);
81+
IWcdx* wcdx = CreateWcdx(window);
8082
WindowData windowData = { wcdx };
8183
::SetWindowLongPtr(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(&windowData));
8284

@@ -132,15 +134,15 @@ void ShowImage(HWND window, Bitmap& image)
132134
image.GetPalette(&palette, paletteData.size());
133135

134136
assert(palette.Count == 256);
135-
windowData.wcdx.SetPalette(reinterpret_cast<PALETTEENTRY*>(palette.Entries));
137+
windowData.wcdx->SetPalette(reinterpret_cast<PALETTEENTRY*>(palette.Entries));
136138

137139
Rect imageRect(0, 0, image.GetWidth(), image.GetHeight());
138140
BitmapData bits;
139141
image.LockBits(&imageRect, 0, image.GetPixelFormat(), &bits);
140142
at_scope_exit([&]{ image.UnlockBits(&bits); });
141143

142144
RECT updateRect = { 0, 0, image.GetWidth(), image.GetHeight() };
143-
windowData.wcdx.UpdateFrame(bits.Scan0, updateRect, bits.Stride);
145+
windowData.wcdx->UpdateFrame(updateRect.left, updateRect.top, updateRect.right - updateRect.left, updateRect.bottom - updateRect.top, bits.Stride, reinterpret_cast<byte*>(bits.Scan0));
144146
}
145147

146148
bool OnCreate(HWND window, const CREATESTRUCT& create)
@@ -152,6 +154,8 @@ bool OnCreate(HWND window, const CREATESTRUCT& create)
152154

153155
void OnDestroy(HWND window)
154156
{
157+
WindowData& windowData = *reinterpret_cast<WindowData*>(::GetWindowLongPtr(window, GWLP_USERDATA));
158+
windowData.wcdx->Release();
155159
::CoUninitialize();
156160
::PostQuitMessage(EXIT_SUCCESS);
157161
}
@@ -178,7 +182,7 @@ void OnRender(HWND window)
178182
return;
179183

180184
WindowData& windowData = *reinterpret_cast<WindowData*>(::GetWindowLongPtr(window, GWLP_USERDATA));
181-
windowData.wcdx.Present();
185+
windowData.wcdx->Present();
182186

183187
// ::PostMessage(window, WM_APP_RENDER, 0, 0);
184188
}

test/test.vcxproj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
<Link>
5858
<SubSystem>Windows</SubSystem>
5959
<GenerateDebugInformation>true</GenerateDebugInformation>
60-
<AdditionalDependencies>wcdx.lib;DxErr.lib;Shlwapi.lib;Gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
60+
<AdditionalDependencies>DxErr.lib;Shlwapi.lib;Gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
6161
<AdditionalLibraryDirectories>$(OutDir);$(DXSDK_DIR)Lib\$(TargetedSDKArchitecture);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
6262
</Link>
6363
</ItemDefinitionGroup>
@@ -77,7 +77,7 @@
7777
<GenerateDebugInformation>true</GenerateDebugInformation>
7878
<EnableCOMDATFolding>true</EnableCOMDATFolding>
7979
<OptimizeReferences>true</OptimizeReferences>
80-
<AdditionalDependencies>wcdx.lib;DxErr.lib;Shlwapi.lib;Gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
80+
<AdditionalDependencies>DxErr.lib;Shlwapi.lib;Gdiplus.lib;%(AdditionalDependencies)</AdditionalDependencies>
8181
<AdditionalLibraryDirectories>$(OutDir);$(DXSDK_DIR)Lib\$(TargetedSDKArchitecture);%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
8282
</Link>
8383
</ItemDefinitionGroup>
@@ -107,6 +107,11 @@
107107
<Image Include="res\images\wc_008.png" />
108108
<Image Include="res\images\wc_009.png" />
109109
</ItemGroup>
110+
<ItemGroup>
111+
<ProjectReference Include="..\wcdx.vcxproj">
112+
<Project>{9ec19d93-a821-411f-abfb-c60b657d5839}</Project>
113+
</ProjectReference>
114+
</ItemGroup>
110115
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
111116
<ImportGroup Label="ExtensionTargets">
112117
</ImportGroup>

0 commit comments

Comments
 (0)