EDN Admin
Well-known member
Here are the code:
Core.h
<pre lang="x-cpp #include <Windows.h>
#include <atlbase.h>
#include <D3D10.h>
#include <D3DX10.h>
#ifndef MsgBox(error)
# define MsgBox(error) MessageBox(NULL,TEXT(##error),TEXT(__FUNCTION__),MB_OK)
#endif
class Window
{
private:
WNDCLASSEX wc;
WNDPROC WindowProc;
protected:
HWND hWnd;
HINSTANCE hInstance;
const int WIDTH;
const int HEIGHT;
public:
Window();
~Window();
HWND Register(int, int);
};
class Program : public Window
{
private:
CComPtr<ID3D10Device> pD3DDevice;
CComPtr<IDXGISwapChain> pSwapChain;
CComPtr<ID3D10RenderTargetView> pRenderTargetView;
public:
Program();
~Program();
bool Initialize();
bool Render();
};[/code]
Core.cpp
<pre lang="x-cpp #include "Core.h"
#include "resource.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//
// WINDOW CLASS
//
Window::Window() : WIDTH(240), HEIGHT(160)
{
Window::WindowProc = WndProc;
}
Window::~Window()
{
}
HWND Window::Register(int x, int y)
{
Window::wc.cbClsExtra = 0;
Window::wc.cbSize = sizeof(WNDCLASSEX);
Window::wc.cbWndExtra = 0;
Window::wc.hbrBackground = CreateSolidBrush(RGB(30, 30, 30));
Window::wc.hCursor = LoadCursor(Window::hInstance, IDC_ARROW);
Window::wc.hIcon = LoadIcon(Window::hInstance, MAKEINTRESOURCE(IDI_ICON1));
Window::wc.hIconSm = LoadIcon(Window::hInstance, MAKEINTRESOURCE(IDI_ICON2_SM));
Window::wc.hInstance = Window::hInstance;
Window::wc.lpfnWndProc = WindowProc;
Window::wc.lpszClassName = TEXT("Practice");
Window::wc.lpszMenuName = NULL;
Window::wc.style = WS_OVERLAPPED | WS_SYSMENU;
if (!RegisterClassEx(&(Window::wc)))
{
MsgBox("Error in RegisterClassEx(), check to see if its correctly giving the address of WNDCLASSEX.");
return NULL;
}
RECT ClientArea = {0, 0, Window::WIDTH, Window::HEIGHT};
AdjustWindowRect(&ClientArea, WS_OVERLAPPED | WS_SYSMENU, FALSE);
HWND hWnd = CreateWindow( TEXT("Practice"),
TEXT("DX"),
WS_OVERLAPPED | WS_SYSMENU,
x,
y,
ClientArea.right - ClientArea.left,
ClientArea.bottom - ClientArea.top,
NULL,
NULL,
Window::hInstance,
NULL);
if (hWnd == NULL)
{
MsgBox("Error in Window::Register(), check for the errors.");
return NULL;
}
Window::hWnd = hWnd;
return Window::hWnd;
}
//
// PROGRAM CLASS
//
Program:rogram()
{
Program:D3DDevice = NULL;
Program:SwapChain = NULL;
Program:RenderTargetView = NULL;
Program::hWnd = Window::hWnd;
}
Program::~Program()
{
//Order is important.
//Program:RenderTargetView = NULL;
//Program:SwapChain = NULL;
//Program:D3DDevice = NULL;
}
bool Program::Initialize()
{
HRESULT hResult;
DXGI_SWAP_CHAIN_DESC SwapChain;
ZeroMemory(&SwapChain, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChain.BufferCount = 1;
SwapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SwapChain.BufferDesc.Height = Program::HEIGHT;
SwapChain.BufferDesc.RefreshRate.Denominator = 1;
SwapChain.BufferDesc.RefreshRate.Numerator = 60;
SwapChain.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
SwapChain.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
SwapChain.BufferDesc.Width = Program::WIDTH;
SwapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChain.Flags = NULL;
SwapChain.OutputWindow = Program::hWnd;
SwapChain.SampleDesc.Count = 1;
SwapChain.SampleDesc.Quality = 0;
SwapChain.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
SwapChain.Windowed = TRUE;
hResult = D3D10CreateDeviceAndSwapChain( NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
0,
D3D10_SDK_VERSION,
&SwapChain,
&(Program:SwapChain),
&(Program:D3DDevice));
if (FAILED(hResult))
{
MsgBox("Error on creating Swap Chain and Device. Check for errors.");
return false;
}
CComPtr<ID3D10Texture2D> pBackBuffer;
hResult = (Program:SwapChain)->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hResult))
{
MsgBox("Error on GetBuffer(). Buffer is empty");
return false;
}
hResult = (Program:D3DDevice)->CreateRenderTargetView(pBackBuffer, NULL, &(Program:RenderTargetView));
if (FAILED(hResult))
{
MsgBox("Error on CreateRenderTargetView(). Something is wrong...");
return false;
}
Program:D3DDevice->OMSetRenderTargets(1, &(Program:RenderTargetView), NULL); //<-----THIS GIVES THE ERROR------
D3D10_VIEWPORT Viewport;
Viewport.Height = Program::HEIGHT;
Viewport.MaxDepth = 1.0;
Viewport.MinDepth = 0.0;
Viewport.TopLeftX = 0;
Viewport.TopLeftY = 0;
Viewport.Width = Program::WIDTH;
Program:D3DDevice->RSSetViewports(1, &Viewport);
return true;
}
bool Program::Render()
{
Program:D3DDevice->ClearRenderTargetView(Program:RenderTargetView, D3DXCOLOR(0.255, 0.244, 0.6, 1.0));
HRESULT hResult = Program:SwapChain->Present(1, DXGI_PRESENT_TEST);
if (FAILED(hResult))
{
MsgBox("Error in pSwapChain->Present(), requires checking for any signs of possible errors.");
return false;
}
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE NotUsed, LPSTR CommandLine, int ShowCommand)
{
Program MyWindow;
HWND hWnd = MyWindow.Register(CW_USEDEFAULT, CW_USEDEFAULT);
if (!MyWindow.Initialize())
{
MsgBox("Error in Initialize().");
return 1;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(msg.message != WM_QUIT)
{
while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!MyWindow.Render())
{
MsgBox("Error in Render().");
return 2;
}
UpdateWindow(hWnd);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
[/code]
<pre lang="x-cpp [/code]
I dont know why it tells me the Program:D3DDevice fails. There are no NULL-pointing pointers anywhere. Or is it because I forgot to call Release() then NULL it, before continuing along? That doesnt sound right either...
Thanks in advance to those who can tell me why I get the errors.
View the full article
Core.h
<pre lang="x-cpp #include <Windows.h>
#include <atlbase.h>
#include <D3D10.h>
#include <D3DX10.h>
#ifndef MsgBox(error)
# define MsgBox(error) MessageBox(NULL,TEXT(##error),TEXT(__FUNCTION__),MB_OK)
#endif
class Window
{
private:
WNDCLASSEX wc;
WNDPROC WindowProc;
protected:
HWND hWnd;
HINSTANCE hInstance;
const int WIDTH;
const int HEIGHT;
public:
Window();
~Window();
HWND Register(int, int);
};
class Program : public Window
{
private:
CComPtr<ID3D10Device> pD3DDevice;
CComPtr<IDXGISwapChain> pSwapChain;
CComPtr<ID3D10RenderTargetView> pRenderTargetView;
public:
Program();
~Program();
bool Initialize();
bool Render();
};[/code]
Core.cpp
<pre lang="x-cpp #include "Core.h"
#include "resource.h"
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
//
// WINDOW CLASS
//
Window::Window() : WIDTH(240), HEIGHT(160)
{
Window::WindowProc = WndProc;
}
Window::~Window()
{
}
HWND Window::Register(int x, int y)
{
Window::wc.cbClsExtra = 0;
Window::wc.cbSize = sizeof(WNDCLASSEX);
Window::wc.cbWndExtra = 0;
Window::wc.hbrBackground = CreateSolidBrush(RGB(30, 30, 30));
Window::wc.hCursor = LoadCursor(Window::hInstance, IDC_ARROW);
Window::wc.hIcon = LoadIcon(Window::hInstance, MAKEINTRESOURCE(IDI_ICON1));
Window::wc.hIconSm = LoadIcon(Window::hInstance, MAKEINTRESOURCE(IDI_ICON2_SM));
Window::wc.hInstance = Window::hInstance;
Window::wc.lpfnWndProc = WindowProc;
Window::wc.lpszClassName = TEXT("Practice");
Window::wc.lpszMenuName = NULL;
Window::wc.style = WS_OVERLAPPED | WS_SYSMENU;
if (!RegisterClassEx(&(Window::wc)))
{
MsgBox("Error in RegisterClassEx(), check to see if its correctly giving the address of WNDCLASSEX.");
return NULL;
}
RECT ClientArea = {0, 0, Window::WIDTH, Window::HEIGHT};
AdjustWindowRect(&ClientArea, WS_OVERLAPPED | WS_SYSMENU, FALSE);
HWND hWnd = CreateWindow( TEXT("Practice"),
TEXT("DX"),
WS_OVERLAPPED | WS_SYSMENU,
x,
y,
ClientArea.right - ClientArea.left,
ClientArea.bottom - ClientArea.top,
NULL,
NULL,
Window::hInstance,
NULL);
if (hWnd == NULL)
{
MsgBox("Error in Window::Register(), check for the errors.");
return NULL;
}
Window::hWnd = hWnd;
return Window::hWnd;
}
//
// PROGRAM CLASS
//
Program:rogram()
{
Program:D3DDevice = NULL;
Program:SwapChain = NULL;
Program:RenderTargetView = NULL;
Program::hWnd = Window::hWnd;
}
Program::~Program()
{
//Order is important.
//Program:RenderTargetView = NULL;
//Program:SwapChain = NULL;
//Program:D3DDevice = NULL;
}
bool Program::Initialize()
{
HRESULT hResult;
DXGI_SWAP_CHAIN_DESC SwapChain;
ZeroMemory(&SwapChain, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChain.BufferCount = 1;
SwapChain.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SwapChain.BufferDesc.Height = Program::HEIGHT;
SwapChain.BufferDesc.RefreshRate.Denominator = 1;
SwapChain.BufferDesc.RefreshRate.Numerator = 60;
SwapChain.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
SwapChain.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
SwapChain.BufferDesc.Width = Program::WIDTH;
SwapChain.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChain.Flags = NULL;
SwapChain.OutputWindow = Program::hWnd;
SwapChain.SampleDesc.Count = 1;
SwapChain.SampleDesc.Quality = 0;
SwapChain.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
SwapChain.Windowed = TRUE;
hResult = D3D10CreateDeviceAndSwapChain( NULL,
D3D10_DRIVER_TYPE_HARDWARE,
NULL,
0,
D3D10_SDK_VERSION,
&SwapChain,
&(Program:SwapChain),
&(Program:D3DDevice));
if (FAILED(hResult))
{
MsgBox("Error on creating Swap Chain and Device. Check for errors.");
return false;
}
CComPtr<ID3D10Texture2D> pBackBuffer;
hResult = (Program:SwapChain)->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
if (FAILED(hResult))
{
MsgBox("Error on GetBuffer(). Buffer is empty");
return false;
}
hResult = (Program:D3DDevice)->CreateRenderTargetView(pBackBuffer, NULL, &(Program:RenderTargetView));
if (FAILED(hResult))
{
MsgBox("Error on CreateRenderTargetView(). Something is wrong...");
return false;
}
Program:D3DDevice->OMSetRenderTargets(1, &(Program:RenderTargetView), NULL); //<-----THIS GIVES THE ERROR------
D3D10_VIEWPORT Viewport;
Viewport.Height = Program::HEIGHT;
Viewport.MaxDepth = 1.0;
Viewport.MinDepth = 0.0;
Viewport.TopLeftX = 0;
Viewport.TopLeftY = 0;
Viewport.Width = Program::WIDTH;
Program:D3DDevice->RSSetViewports(1, &Viewport);
return true;
}
bool Program::Render()
{
Program:D3DDevice->ClearRenderTargetView(Program:RenderTargetView, D3DXCOLOR(0.255, 0.244, 0.6, 1.0));
HRESULT hResult = Program:SwapChain->Present(1, DXGI_PRESENT_TEST);
if (FAILED(hResult))
{
MsgBox("Error in pSwapChain->Present(), requires checking for any signs of possible errors.");
return false;
}
return true;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE NotUsed, LPSTR CommandLine, int ShowCommand)
{
Program MyWindow;
HWND hWnd = MyWindow.Register(CW_USEDEFAULT, CW_USEDEFAULT);
if (!MyWindow.Initialize())
{
MsgBox("Error in Initialize().");
return 1;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
MSG msg;
ZeroMemory(&msg, sizeof(MSG));
while(msg.message != WM_QUIT)
{
while(PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (!MyWindow.Render())
{
MsgBox("Error in Render().");
return 2;
}
UpdateWindow(hWnd);
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
[/code]
<pre lang="x-cpp [/code]
I dont know why it tells me the Program:D3DDevice fails. There are no NULL-pointing pointers anywhere. Or is it because I forgot to call Release() then NULL it, before continuing along? That doesnt sound right either...
Thanks in advance to those who can tell me why I get the errors.
View the full article