Sign in to follow this  
NLScotty

Direct2D, background becomes black when redraw

Recommended Posts

Hello,

I'm trying to create some sort of simple game. I've never used Direct X before so maybe there are some stupid things.

Anyways, the problem is that, whenever I minimalize or make it smaller/bigger instead of the yellow background I gave it, it becomes black and it stays, well I think you need my code for it.

Start of the program, win32 things
[code]#include "Main.h"
#include "Dmain.h"

int WINAPI WinMain(
HINSTANCE /* hInstance */,
HINSTANCE /* hPrevInstance */,
LPSTR /* lpCmdLine */,
int /* nCmdShow */
)
{
HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
if (SUCCEEDED(CoInitialize(NULL)))
{
DemoApp app;
if (SUCCEEDED(app.Initialize()))
{
app.RunMessageLoop();
}
CoUninitialize();
}
return 0;
}

DemoApp::DemoApp()
{
m_hwnd = 0;
}

DemoApp::~DemoApp()
{

}

HRESULT DemoApp::Initialize()
{
HRESULT hr = S_OK;
DirectStart clDirectX;

hr = clDirectX.InitializeDirectX();

if(hr == S_FALSE )
return hr;

WNDCLASSEX wcex = { sizeof(wcex) };
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
wcex.cbWndExtra = sizeof(LONG_PTR);
wcex.hInstance = HINST_THISCOMPONENT;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.lpszClassName = L"D2DDemoApp";

RegisterClassEx(&wcex);

m_hwnd = CreateWindow(L"D2DDemoApp",L"NazStory",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,
300,300,
NULL,NULL,
HINST_THISCOMPONENT,this);

hr = m_hwnd ? S_OK : S_FALSE;

if (SUCCEEDED(hr))
{
ShowWindow(m_hwnd, SW_SHOWNORMAL);
UpdateWindow(m_hwnd);
}
hr = clDirectX.StartDirectX(m_hwnd);
return hr;
}

void DemoApp::RunMessageLoop()
{
MSG msg;

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

LRESULT CALLBACK DemoApp::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_PAINT:
{
break;
}
case WM_DESTROY:
{
PostQuitMessage(0);
break;
}
default:
DefWindowProc(hwnd, message, wParam, lParam);
}
return true;
}[/code]

Direct X class I wrote
[code]#include "DMain.h"

DirectStart::DirectStart() :
pFactory(NULL),
pRT(NULL),
pBlackBrush(NULL)
{

}

DirectStart::~DirectStart()
{

}

/* Start from DirectX 2D */
HRESULT DirectStart::InitializeDirectX()
{
HRESULT hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,&pFactory);
return hr;
}

HRESULT DirectStart::StartDirectX(HWND hwnd)
{
if(!hwnd)
return false;

RECT rc;
GetClientRect(hwnd, &rc);

HRESULT hr = pFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top)),
&pRT
);

if(SUCCEEDED(hr))
{
hr = pRT->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Yellow),
&pBlackBrush);
}
/*to make sure if it works or not, testing this */
pRT->BeginDraw();
pRT->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->Clear(D2D1::ColorF(D2D1::ColorF::Yellow));
pRT->EndDraw();

return hr;
}

[/code]

Off course commens on my code are always welcome, I've read some SDK documentation and coded most myself, except some unintrested things.

Thanks for reading,

Share this post


Link to post
Share on other sites
i dont see a "normal" DirectX Graphics Device there, i use the [i]IDirect3D9[/i] and [i]IDirect3DDevice9[/i] Interface and then [i]D3DXSPRITE[/i] Interface to do 2D.
but your Code, i Dont Know maybe someone with experiance with that can help
:) :) :)

Share this post


Link to post
Share on other sites
I don't see the includes you make, but because you are using D2D1CreateFactory I assume that u are working with Direct2D / DirectDraw.
DirectDraw is deprecated, so I suggest you learn Direct3D.
If you use Direct3D9 then you must use IDirect3D9 and IDirect3DDevice9 like ryan20fun already said.

Share this post


Link to post
Share on other sites
It looks like you're only rendering once, put your render code under WM_PAINT. Whenever you need to redraw (e.g WM_MOUSEMOVE ) call InvalidateRect(hwnd, 0, 0);
You'll also need to add code to resize the render target when the window size changes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this