commit a8c44e6dc461b61635b1292021a7fbbba43ae4e5
parent 10d7b6d55d0abb95d04a34bf13318fc7ce167160
Author: Brian Swetland <swetland@frotz.net>
Date: Sat, 2 Feb 2013 01:54:04 -0800
dxapp: handle window resizing
Diffstat:
3 files changed, 121 insertions(+), 48 deletions(-)
diff --git a/common/app.h b/common/app.h
@@ -42,15 +42,17 @@ public:
int start(HINSTANCE hInstance, int nCmdShow);
void stop(void);
+ int reconfigure(int init);
+
protected:
int width;
int height;
ID3D10Device *device;
- IDXGISwapChain *swap;
- ID3D10RenderTargetView *target;
- ID3D10Texture2D *depthStencil;
- ID3D10DepthStencilView *depthStencilView;
+ IDXGISwapChain *swapchain;
+ ID3D10RenderTargetView *targetView;
+ ID3D10Texture2D *depthBuffer;
+ ID3D10DepthStencilView *depthView;
ID3D10RasterizerState *rasterizerState;
int createTextureRGBA(void *data, int tw, int th,
diff --git a/common/dxapp.cc b/common/dxapp.cc
@@ -21,7 +21,9 @@
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
-App::App() : width(800), height(600), device(NULL), target(NULL), swap(NULL) {
+App::App() : width(800), height(600),
+ device(NULL), targetView(NULL), depthView(NULL),
+ swapchain(NULL), rasterizerState(NULL) {
}
App::~App() {
@@ -33,15 +35,18 @@ void App::stop(void) {
release();
if (rasterizerState) rasterizerState->Release();
- if (depthStencilView) depthStencilView->Release();
- if (depthStencil) depthStencil->Release();
- if (target) target->Release();
- if (swap) swap->Release();
+ if (depthView) depthView->Release();
+ if (depthBuffer) depthBuffer->Release();
+ if (targetView) targetView->Release();
+ if (swapchain) swapchain->Release();
if (device) device->Release();
printx("-- goodbye --\n");
}
+static App *app;
+static int moving = 0;
+
static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
PAINTSTRUCT ps;
HDC hdc;
@@ -53,6 +58,29 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
case WM_DESTROY:
PostQuitMessage( 0 );
break;
+ case WM_SIZE:
+#if 0
+ if (wParam == SIZE_MINIMIZED) printx("win: minimized\n");
+ else if (wParam == SIZE_MAXIMIZED) printx("win: maximized\n");
+ else if (wParam == SIZE_RESTORED) printx("win: restored\n");
+ else printx("win: sized %d??\n", wParam);
+#endif
+ if (!moving)
+ app->reconfigure(0);
+ break;
+ case WM_ENTERSIZEMOVE:
+ moving = 1;
+ break;
+ case WM_EXITSIZEMOVE:
+ moving = 0;
+ app->reconfigure(0);
+ break;
+ case WM_MOUSEMOVE:
+// printx("win: mouse!\n");
+ break;
+ case WM_ACTIVATEAPP:
+ printx("win: activate: %d\n", wParam);
+ break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
@@ -60,7 +88,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) {
- App *app = createApp();
+ app = createApp();
if (app->start(hInstance, nCmdShow))
return 0;
@@ -74,7 +102,6 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
app->render();
}
}
-
app->stop();
delete app;
return (int) msg.wParam;
@@ -114,42 +141,52 @@ int App::start(HINSTANCE hInstance, int nCmdShow) {
return 0;
}
-int App::initD3D(void) {
+int App::reconfigure(int init) {
HRESULT hr;
- RECT rc;
- GetClientRect(hwnd, &rc);
-
- unsigned width = rc.right - rc.left;
- unsigned height = rc.bottom - rc.top;
-
- DXGI_SWAP_CHAIN_DESC sc;
- ZeroMemory(&sc, sizeof(sc));
- sc.BufferCount = 1;
- sc.BufferDesc.Width = width;
- sc.BufferDesc.Height = height;
- sc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
- sc.BufferDesc.RefreshRate.Numerator = 60;
- sc.BufferDesc.RefreshRate.Denominator = 1;
- sc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
- sc.OutputWindow = hwnd;
- sc.SampleDesc.Count = 1;
- sc.SampleDesc.Quality = 0;
- sc.Windowed = TRUE;
+ int w, h;
+
+ if (init) {
+ printx("reconfigure: init\n");
+ } else {
+ RECT r;
+ GetClientRect(hwnd, &r);
+ w = r.right - r.left;
+ h = r.bottom - r.top;
+
+ // TODO: handle minimized state
+ if (w < 320) w = 320;
+ if (h < 240) h = 240;
+
+ if ((width == w) && (height == h))
+ return 0;
+
+ width = w;
+ height = h;
+ if (targetView) {
+ targetView->Release();
+ targetView = NULL;
+ }
+ if (depthView) {
+ depthView->Release();
+ depthView = NULL;
+ }
+ if (depthBuffer) {
+ depthBuffer->Release();
+ depthBuffer = NULL;
+ }
- hr = D3D10CreateDeviceAndSwapChain(NULL,
- D3D10_DRIVER_TYPE_HARDWARE, NULL, DEVICE_DEBUG_FLAGS,
- D3D10_SDK_VERSION, &sc, &swap, &device);
- if (FAILED(hr))
- return -1;
+ hr = swapchain->ResizeBuffers(1, width, height, DXGI_FORMAT_R8G8B8A8_UNORM, 0);
+ if (FAILED(hr))
+ return -1;
+ }
ID3D10Texture2D *buffer;
- hr = swap->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &buffer);
- if (FAILED(hr))
- return hr;
- hr = device->CreateRenderTargetView(buffer, NULL, &target);
+ hr = swapchain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &buffer);
+ if (FAILED(hr)) printx("OOPS\n");
+ hr = device->CreateRenderTargetView(buffer, NULL, &targetView);
buffer->Release();
if (FAILED(hr))
- return hr;
+ return -1;
D3D10_TEXTURE2D_DESC td;
td.Width = width;
@@ -163,7 +200,7 @@ int App::initD3D(void) {
td.BindFlags = D3D10_BIND_DEPTH_STENCIL;
td.CPUAccessFlags = 0;
td.MiscFlags = 0;
- hr = device->CreateTexture2D(&td, NULL, &depthStencil);
+ hr = device->CreateTexture2D(&td, NULL, &depthBuffer);
if (FAILED(hr))
return -1;
@@ -171,7 +208,7 @@ int App::initD3D(void) {
dsvd.Format = td.Format;
dsvd.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
dsvd.Texture2D.MipSlice = 0;
- hr = device->CreateDepthStencilView(depthStencil, &dsvd, &depthStencilView);
+ hr = device->CreateDepthStencilView(depthBuffer, &dsvd, &depthView);
if (FAILED(hr))
return -1;
@@ -184,6 +221,42 @@ int App::initD3D(void) {
vp.TopLeftY = 0;
device->RSSetViewports(1, &vp);
+ device->OMSetRenderTargets(1, &targetView, depthView);
+
+ return 0;
+}
+
+int App::initD3D(void) {
+ HRESULT hr;
+ RECT rc;
+ GetClientRect(hwnd, &rc);
+
+ unsigned width = rc.right - rc.left;
+ unsigned height = rc.bottom - rc.top;
+
+ DXGI_SWAP_CHAIN_DESC sc;
+ ZeroMemory(&sc, sizeof(sc));
+ sc.BufferCount = 1;
+ sc.BufferDesc.Width = width;
+ sc.BufferDesc.Height = height;
+ sc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
+ sc.BufferDesc.RefreshRate.Numerator = 60;
+ sc.BufferDesc.RefreshRate.Denominator = 1;
+ sc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
+ sc.OutputWindow = hwnd;
+ sc.SampleDesc.Count = 1;
+ sc.SampleDesc.Quality = 0;
+ sc.Windowed = TRUE;
+
+ hr = D3D10CreateDeviceAndSwapChain(NULL,
+ D3D10_DRIVER_TYPE_HARDWARE, NULL, DEVICE_DEBUG_FLAGS,
+ D3D10_SDK_VERSION, &sc, &swapchain, &device);
+ if (FAILED(hr))
+ return -1;
+
+ if (reconfigure(1))
+ return -1;
+
D3D10_RASTERIZER_DESC rd;
rd.FillMode = D3D10_FILL_SOLID;
rd.CullMode = D3D10_CULL_BACK;
@@ -200,8 +273,6 @@ int App::initD3D(void) {
return -1;
device->RSSetState(rasterizerState);
- device->OMSetRenderTargets(1, &target, depthStencilView);
-
return S_OK;
}
diff --git a/hello/hello.cc b/hello/hello.cc
@@ -150,8 +150,8 @@ void TestApp::render(void) {
t = (timeCur - timeStart) / 1000.0f;
float rgba[4] = { 0.0f, 0.125f, 0.3f, 1.0f };
- device->ClearRenderTargetView(target, rgba);
- device->ClearDepthStencilView(depthStencilView, D3D10_CLEAR_DEPTH, 1.0f, 0 );
+ device->ClearRenderTargetView(targetView, rgba);
+ device->ClearDepthStencilView(depthView, D3D10_CLEAR_DEPTH, 1.0f, 0 );
UINT stride = 32;
UINT offset = 0;
@@ -177,7 +177,7 @@ void TestApp::render(void) {
device->UpdateSubresource(cbuf, 0, NULL, &cb0, 0, 0);
device->DrawIndexedInstanced(m->icount, 8*8*8, 0, 0, 0);
- swap->Present(0, 0);
+ swapchain->Present(0, 0);
}
App *createApp(void) {