Jump to content
  • Advertisement
cambalinho

C++: my 1st DirectX 11: why the memory used is increasing?

Recommended Posts

i'm doing my 1st DirectX 11 program:

//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:
	HRESULT DirectXResult=0;

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);
		DirectXResult=Device->CreateRenderTargetView(pBackBuffer, NULL, &backbuffer);
		if (backbuffer == NULL|| DirectXResult!=S_OK)
			MessageBox(NULL, "error", "error", MB_OK);
		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;
	}

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";
		WNDCLASSEX wndClassName;
		ZeroMemory(&wndClassName, sizeof(WNDCLASSEX));
		wndClassName.cbSize = sizeof(WNDCLASSEX);
		wndClassName.cbClsExtra = 0;
		wndClassName.cbWndExtra = 0;
		wndClassName.hbrBackground = (HBRUSH)(COLOR_WINDOW);
		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()
	{
		;
	}
	
	operator HWND()
	{
		return MainWindow;
	}

	static std::function<void()>DirectXRender;

	static WPARAM MessageLoop()
	{
		MSG msgMessage;
		ZeroMemory(&msgMessage, sizeof(MSG));
		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;
  
//maic.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;
	ZeroMemory(&msgMessage, sizeof(MSG));
	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);
}

i'm very new with Game Loop programming. i'm reading several tutorials for see the problem and they are more or less the same. but why the memory used is increasing?(i see that on Task Manager)

Share this post


Link to post
Share on other sites
Advertisement

A) Task Manager is not reliable.  I've watched my program use more and more memory while idling then drop 15mb in usage.  And I was literally away from the computer for 10 minutes while I ran.

B) Why do you have two message loops?  One static in the header and one in the main.cpp file.

C) I don't know D3D11 but I don't see where you're allocating anything repeatedly.

Share this post


Link to post
Share on other sites

now i have the answer. my graphics card is for DirectX10... that's why i can inicializate the DirectX 11(it's an update of 10).

thank you so much for all

Share this post


Link to post
Share on other sites

Some general advice:

- enable/ use the d3d/dx11 debug layer, it will tell you if you forgot to release (COM) DX objects
- I personally also use VLD (visual leak detector) which checks for memory leaks in C++ in general and outputs them in the console/debugger output
- always release what you create :)

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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!