-
January 14th, 2018, 10:13 AM
#1
DirectX 11 - why the memory used is increasing?
i'm starting the DirectX 11(several people advice me).
i did 2 class's for help me create the window and inicializate the DirectX 11:
Code:
//Cambalinho.h
#include <Windows.h>
#include <string>
#include <functional>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#include <DxErr.h>
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")
#pragma comment (lib, "DxErr.lib")
using namespace std;
class DirectX
{
private:
public:
function<void()> Render;
IDXGISwapChain * DXSwap;
ID3D11Device *Device;
ID3D11DeviceContext *DeviceContext;
DXGI_SWAP_CHAIN_DESC DXSwapdesc;
ID3D11RenderTargetView *backbuffer;
ID3D11Texture2D *pBackBuffer;
D3D11_VIEWPORT ViewPort;
DirectX(HWND window)
{
RECT WindowRect = { 0 };
GetClientRect(window, &WindowRect);
//inicializate Directx:
ZeroMemory(&DXSwapdesc, sizeof(DXGI_SWAP_CHAIN_DESC));
DXSwapdesc.BufferCount = 1;
DXSwapdesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
DXSwapdesc.BufferDesc.Width = WindowRect.right - WindowRect.left;
DXSwapdesc.BufferDesc.Height = WindowRect.bottom - WindowRect.top;
DXSwapdesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
DXSwapdesc.OutputWindow = window;
DXSwapdesc.SampleDesc.Count = 1;
DXSwapdesc.SampleDesc.Quality = 0;
DXSwapdesc.Windowed = true;
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL,
D3D11_SDK_VERSION, &DXSwapdesc, &DXSwap, &Device, NULL, &DeviceContext);
//Prepare the View Port:
DXSwap->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&pBackBuffer);
Device->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
DeviceContext->OMSetRenderTargets(1, &backbuffer, NULL);
//Create the View Port:
ZeroMemory(&ViewPort, sizeof(D3D11_VIEWPORT));
ViewPort.TopLeftX = 0;
ViewPort.TopLeftY = 0;
ViewPort.Width = static_cast<float>( WindowRect.right- WindowRect.left);
ViewPort.Height = static_cast< float>(WindowRect.bottom- WindowRect.top);
ViewPort.MinDepth = 0.0f;
ViewPort.MaxDepth = 1.0f;
ViewPort.TopLeftX = 0.0f;
ViewPort.TopLeftY = 0.0f;
DeviceContext->RSSetViewports(1, &ViewPort);
}
void Clear(float color[4])
{
DeviceContext->ClearRenderTargetView(backbuffer, color);
}
void Update()
{
DXSwap->Present(0, 0);
}
~DirectX()
{
//DirectX Clean Up:
pBackBuffer->Release();
if(DXSwap)
DXSwap->Release();
if(backbuffer)
backbuffer->Release();
if(Device)
Device->Release();
if(DeviceContext)
DeviceContext->Release();
}
};
class Form
{
private:
HWND MainWindow = NULL;
static HRESULT CALLBACK WndProc(HWND window, UINT message, WPARAM WParam, LPARAM LParam)
{
Form *inst =(Form*) GetWindowLongPtr(window, GWLP_USERDATA);
switch (message)
{
case WM_CREATE:
{
LPCREATESTRUCT lpCreate = (LPCREATESTRUCT)LParam;
inst =(Form*) lpCreate->lpCreateParams;
SetWindowLongPtr(window, GWLP_USERDATA, (LONG_PTR)inst);
}
break;
case WM_CLOSE:
{
DestroyWindow(window);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
}
break;
default:
return DefWindowProc(window, message, WParam, LParam);
}
return 0;
}
HBRUSH ClassBackColor;
public:
Form(string Title = "hello World", int PosX = CW_USEDEFAULT, int PosY=CW_USEDEFAULT, int Width=CW_USEDEFAULT, int Height=CW_USEDEFAULT)
{
string strWindowClassName = "WindowClassname";
ClassBackColor = CreateSolidBrush(RGB(0, 0, 255));
WNDCLASSEX wndClassName = { 0 };
wndClassName.cbSize = sizeof(WNDCLASSEX);
wndClassName.cbClsExtra = 0;
wndClassName.cbWndExtra = 0;
wndClassName.hbrBackground = ClassBackColor;
wndClassName.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClassName.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClassName.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wndClassName.hInstance = GetModuleHandle(NULL);
wndClassName.lpfnWndProc = (WNDPROC)WndProc;
wndClassName.lpszClassName = strWindowClassName.c_str();
wndClassName.lpszMenuName = 0;
wndClassName.style = CS_HREDRAW | CS_VREDRAW;
if (!RegisterClassEx(&wndClassName))
MessageBox(NULL, "Not Registed", "error", MB_OK);
RECT windrect = { 0,0,Width,Height };
AdjustWindowRect(&windrect, WS_OVERLAPPEDWINDOW, FALSE);
MainWindow = CreateWindowEx(WS_EX_CLIENTEDGE, strWindowClassName.c_str(), Title.c_str(), WS_OVERLAPPEDWINDOW, PosX, PosY, windrect.right- windrect.left, windrect.bottom- windrect.top, NULL, NULL, GetModuleHandle(NULL),reinterpret_cast<LPVOID> (this));
if(MainWindow==NULL)
MessageBox(NULL, "Not created", "error", MB_OK);
ShowWindow(MainWindow, SW_SHOWNORMAL);
UpdateWindow(MainWindow);
}
~Form()
{
DeleteObject(ClassBackColor);
}
operator HWND()
{
return MainWindow;
}
static std::function<void()>DirectXRender;
static WPARAM MessageLoop()
{
MSG msgMessage = { 0 };
while (msgMessage.message != WM_QUIT)
{
if (PeekMessage(&msgMessage, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
if (DirectXRender)
DirectXRender();
}
return static_cast<int>(msgMessage.wParam);
}
};
function<void()>Form::DirectXRender = nullptr;
and heres how i use them:
Code:
//main.cpp:
#include "cambalinho.h"
Form frmMain("hello World!!!",CW_USEDEFAULT,CW_USEDEFAULT,800,500);
DirectX DirecX(frmMain);
int WINAPI WinMain(HINSTANCE ActualInstance, HINSTANCE NullInstance, LPSTR CommandLine, int WindowShow)
{
DirecX.Render=[]
{
float clearColor[4] = { 0.0f, 0.0f, 0.25f, 1.0f };
DirecX.Clear(clearColor);
DirecX.Update();
};
//Message Loop:
MSG msgMessage = { 0 };
while (msgMessage.message != WM_QUIT)
{
if (PeekMessage(&msgMessage, NULL, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msgMessage);
DispatchMessage(&msgMessage);
}
DirecX.Render();
}
return static_cast<int>(msgMessage.wParam);
}
these code works fine and no errors.
on Task Manager i see that the used memory is increasing... why these happens?
if i take off "DirecX.Render();" the problem don't happen
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|