graphics

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit a045e29861904b41873ca611beb9130b540a2da4
parent eb4d535914124a58d4686c7e33ccbe8885145265
Author: Brian Swetland <swetland@frotz.net>
Date:   Sat,  2 Feb 2013 19:07:35 -0800

dxapp: start using directinput, fix some bugs

- acquire a keyboard device
- treat ESC as a quit now button
- exit fullscreen mode on teardown to avoid a DXGI crash

Diffstat:
Mcommon/app.h | 11++++++++++-
Mcommon/dxapp.cc | 74+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------
Mhello/hello.vcxproj | 7+++----
3 files changed, 76 insertions(+), 16 deletions(-)

diff --git a/common/app.h b/common/app.h @@ -22,6 +22,7 @@ #pragma warning ( disable : 4005 ) #include <d3d10.h> +#include <dinput.h> #ifdef _DEBUG #define DEVICE_DEBUG_FLAGS D3D10_CREATE_DEVICE_DEBUG @@ -41,10 +42,12 @@ public: virtual void release(void) {}; virtual void mouse(int x, int y, int buttons) {}; + /* glue - do not use */ int start(HINSTANCE hInstance, int nCmdShow); + void eventloop(void); void stop(void); int reconfigure(int init); - + void setActive(int a) { active = a; }; protected: int width; int height; @@ -71,10 +74,16 @@ protected: } int compileVertexShader(const char *fn, ID3D10VertexShader **vs, ID3D10Blob **data); int compilePixelShader(const char *fn, ID3D10PixelShader **ps, ID3D10Blob **data); + private: + LPDIRECTINPUT8 dinput; + LPDIRECTINPUTDEVICE8 dkeyboard; + unsigned char keystate[256]; int initD3D(void); + int initDirectInput(void); HWND hwnd; HINSTANCE hinstance; + int active; }; int compileShader(const char *fn, const char *profile, ID3D10Blob **shader); diff --git a/common/dxapp.cc b/common/dxapp.cc @@ -21,7 +21,7 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -App::App() : width(800), height(600), +App::App() : width(800), height(600), active(0), device(NULL), targetView(NULL), depthView(NULL), swapchain(NULL), rasterizerState(NULL) { } @@ -30,7 +30,12 @@ App::~App() { } void App::stop(void) { - if (device) device->ClearState(); + if (swapchain) { + // teardown during fullscreen will crash + // ensure we're not fullscreen first... + swapchain->SetFullscreenState(false, NULL); + } + if (device) device->Release(); release(); @@ -39,7 +44,7 @@ void App::stop(void) { if (depthBuffer) depthBuffer->Release(); if (targetView) targetView->Release(); if (swapchain) swapchain->Release(); - if (device) device->Release(); + if (device) device->ClearState(); printx("-- goodbye --\n"); } @@ -47,6 +52,7 @@ void App::stop(void) { static App *app; static int moving = 0; static int frame = 0; + static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { PAINTSTRUCT ps; HDC hdc; @@ -96,6 +102,7 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l } case WM_ACTIVATEAPP: printx("win: activate: %d\n", wParam); + app->setActive(wParam); break; default: return DefWindowProc(hWnd, message, wParam, lParam); @@ -105,23 +112,44 @@ static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM l int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow) { app = createApp(); - if (app->start(hInstance, nCmdShow)) return 0; + app->eventloop(); + app->stop(); + delete app; + return 0; +} +void App::eventloop(void) { + HRESULT hr; MSG msg = {0}; while (msg.message != WM_QUIT) { - if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + if (active) { + if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + continue; + } + } else { + if (GetMessage(&msg, 0, 0, 0) == -1) + return; TranslateMessage(&msg); DispatchMessage(&msg); + continue; + } + int n; + hr = dkeyboard->GetDeviceState(sizeof(keystate), (void*) keystate); + if (FAILED(hr)) { + // We can lose the keyboard if we lose focus + // Try to reacquire it + dkeyboard->Acquire(); } else { - app->render(); - frame++; + if (keystate[DIK_ESCAPE] & 0x80) + break; } + app->render(); + frame++; } - app->stop(); - delete app; - return (int) msg.wParam; } int App::start(HINSTANCE hInstance, int nCmdShow) { @@ -153,11 +181,35 @@ int App::start(HINSTANCE hInstance, int nCmdShow) { if (FAILED(initD3D())) return -1; + if (initDirectInput()) + return -1; if (init()) return -1; return 0; } +int App::initDirectInput(void) { + HRESULT hr; + hr = DirectInput8Create(hinstance, + DIRECTINPUT_VERSION, IID_IDirectInput8, + (void**) &dinput, NULL); + if (hr) + return -1; + hr = dinput->CreateDevice(GUID_SysKeyboard, &dkeyboard, NULL); + if (hr) + return -1; + hr = dkeyboard->SetDataFormat(&c_dfDIKeyboard); + if (hr) + return -1; + hr = dkeyboard->SetCooperativeLevel(hwnd, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE); + if (hr) + return -1; + hr = dkeyboard->Acquire(); + if (hr) + return -1; + return 0; +} + int App::reconfigure(int init) { HRESULT hr; int w, h; @@ -379,7 +431,7 @@ int compileShader(const char *fn, const char *profile, ID3D10Blob **shader) { hr = D3D10CompileShader((char*) data, dsz, fn, NULL, NULL, "main", profile, - D3D10_SHADER_ENABLE_STRICTNESS, // | D3D10_SHADER_DEBUG, + D3D10_SHADER_ENABLE_STRICTNESS, shader, &errors); free(data); diff --git a/hello/hello.vcxproj b/hello/hello.vcxproj @@ -57,7 +57,7 @@ </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> - <AdditionalDependencies>d3d10.lib;dxerr.lib;dxguid.lib;winmm.lib;d3dcompiler.lib;libpng-1.2.50.lib;zlib-1.2.7.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalDependencies>d3d10.lib;dxerr.lib;dxguid.lib;winmm.lib;d3dcompiler.lib;dinput8.lib;libpng-1.2.50.lib;zlib-1.2.7.lib;%(AdditionalDependencies)</AdditionalDependencies> <SubSystem>Windows</SubSystem> <AdditionalLibraryDirectories>../Debug;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> @@ -73,7 +73,7 @@ </ClCompile> <Link> <GenerateDebugInformation>true</GenerateDebugInformation> - <AdditionalDependencies>d3d10.lib;dxerr.lib;dxguid.lib;winmm.lib;d3dcompiler.lib;libpng-1.2.50.lib;zlib-1.2.7.lib;%(AdditionalDependencies)</AdditionalDependencies> + <AdditionalDependencies>d3d10.lib;dxerr.lib;dxguid.lib;winmm.lib;d3dcompiler.lib;dinput8.lib;libpng-1.2.50.lib;zlib-1.2.7.lib;%(AdditionalDependencies)</AdditionalDependencies> <SubSystem>Windows</SubSystem> <AdditionalLibraryDirectories>../Release;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <EnableCOMDATFolding>true</EnableCOMDATFolding> @@ -113,4 +113,4 @@ <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> -</Project> -\ No newline at end of file +</Project>