Optimizing the use of memory. how to do?

Started by
4 comments, last by snoddas14 17 years, 1 month ago
hey, i got this awsom problem, a bit funny :P my test is 2 obejtks and the use around 650 K of my 1 gig ram. how da hell do i optimize it?
Advertisement
Can we have a bit more information please...
What are the objects?
What language are you using?
What is this 'test'?

Degra
Oh sorry, i got one palm ( texturd ).
and the language is c++

and the test is just for optimizing ram usage.


#include <windowsx.h> //windows addon!#include <windows.h> //windows!#include <d3d9.h> //D3D basics!#include <d3dx9.h> //D3DX Viruel miljö och bra saker!#include <mesh.h>#include <input.h>#include <atlbase.h>HINSTANCE hInst;HWND wndHandle;input Input;LoadMesh Mesh1;LPDIRECT3D9 pD3D; //devecein som skapar D3DLPDIRECT3DDEVICE9 pd3dDevice; // den riktiga D3DDevicen!/*************************Komihåg. sätt utt kamera ovanför "playern"när playern flyttar sig skall kameran flytta med.och man ska ha samma position som gubben har.**************************/long float PlayerX = 0.0f;long float PlayerY = 0.0f;long float PlayerZ = -10.0f;long float CameraLAX = 0.0f;long float CameraLAY = 2.0f;long float CameraLAZ = -10.0f;long float CameraPSX = 0.0f;long float CameraPSY = 10.0f;long float CameraPSZ = -15.0f;/**************WINDOWS GREJEN!**************/HRESULT hr;bool initWindow(HINSTANCE hInstance);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);bool InitD3D();void RenderD3D();void Exit();int WINAPI WinMain(HINSTANCE hInstance, 				   HINSTANCE hPrevInstance, 				   LPTSTR lpCmdLine, int nCmdShow){	if (!initWindow(hInstance))	{		MessageBox(NULL,"Error cant InitWnd","Error",MB_OK);		return false;	}	if(!InitD3D())	{		MessageBox(NULL,"Error Cant Init D3D","Error",MB_OK);		return false;	}	// Main message loop:    MSG msg;     ZeroMemory( &msg, sizeof(msg) );    while( msg.message!=WM_QUIT )    {		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )        {			TranslateMessage( &msg );            DispatchMessage( &msg );        }		else		{			Input.inputDET();			RenderD3D();					}    }	Exit();		return (int) msg.wParam;}bool initWindow(HINSTANCE hInstance){	WNDCLASSEX wcex;	wcex.cbSize = sizeof(WNDCLASSEX);	wcex.style			= CS_HREDRAW | CS_VREDRAW;	wcex.lpfnWndProc	= (WNDPROC)WndProc;	wcex.cbClsExtra		= 0;	wcex.cbWndExtra		= 0;	wcex.hInstance		= hInstance;	wcex.hIcon			= 0;	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);	wcex.lpszMenuName	= NULL;	wcex.lpszClassName	= "DirectXExample";	wcex.hIconSm		= 0;	RegisterClassEx(&wcex);	// create the window	wndHandle = CreateWindow("DirectXExample", 							 "DirectXExample", 							 WS_OVERLAPPEDWINDOW,							 CW_USEDEFAULT, 							 CW_USEDEFAULT, 							 640, 							 480, 							 NULL, 							 NULL, 							 hInstance, 							 NULL);   if (!wndHandle)      return false;      ShowWindow(wndHandle, SW_SHOW);   UpdateWindow(wndHandle);   return true;}LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam){	switch (message) 	{		case WM_DESTROY:			PostQuitMessage(0);			break;	}	return DefWindowProc(hWnd, message, wParam, lParam);}bool InitD3D(){	if(NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION) ) )	{		MessageBox(NULL,"Error - Wrong SDK","Error",MB_OK);		return FALSE;	}	D3DPRESENT_PARAMETERS d3dpp;	ZeroMemory(&d3dpp,sizeof(d3dpp));	d3dpp.BackBufferCount = 1;	d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;	d3dpp.BackBufferHeight = 480;	d3dpp.BackBufferWidth = 640;	d3dpp.hDeviceWindow = wndHandle;	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;	d3dpp.Windowed = TRUE;	d3dpp.EnableAutoDepthStencil = TRUE;	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;	if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, wndHandle,D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))	{		MessageBox(NULL,"Error - Creating Device","Error",MB_OK);		return FALSE;	}	Input.inputDEV(hInst, wndHandle);	pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);	pd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);  	pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE); 	return TRUE;}void input::inputDEV(HINSTANCE hInst, HWND wndHandle){	DirectInput8Create(GetModuleHandle(NULL), DIRECTINPUT_VERSION, 		IID_IDirectInput8,(void**)&din, NULL);	din->CreateDevice(GUID_SysKeyboard, &dinkey, NULL);	dinkey->SetDataFormat(&c_dfDIKeyboard);	dinkey->SetCooperativeLevel(wndHandle, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND);	return;}void input::inputDET(){#define KEYDOWN(name, key) (name[key] &0x80)	static char keystate[256];	dinkey->Acquire();	dinkey->GetDeviceState(sizeof(keystate),(LPVOID)keystate);	if(KEYDOWN(keystate, DIK_A))	{		MessageBox(NULL,"Din Motor kan bli bra!","Du Aeger",MB_OK);	}	return;}void input::kill(){	if(din !=NULL)		din->Release();	if(dinkey !=NULL)		dinkey->Release();}void LoadMesh::D3DXLoadMeshFromDriver(){	D3DXLoadMeshFromX("Palm.x", D3DXMESH_SYSTEMMEM, pd3dDevice, NULL,								 &BuffMat, NULL, &NumMaterial, &Mesh);	D3DXMATERIAL* tempMaterials = (D3DXMATERIAL*)BuffMat->GetBufferPointer();	BuffMaterial = new D3DMATERIAL9[NumMaterial];	BuffTexture = new LPDIRECT3DTEXTURE9[NumMaterial];	for(DWORD i = 0; i < NumMaterial; i++)	{		BuffMaterial = tempMaterials.MatD3D;		BuffMaterial.Ambient = BuffMaterial.Diffuse;		USES_CONVERSION;		if(FAILED(D3DXCreateTextureFromFile(pd3dDevice,			(tempMaterials.pTextureFilename), &BuffTexture)))        BuffTexture = NULL; 	}	return;}void LoadMesh::Show(){	D3DXMATRIX Translate;	for(DWORD i=0; i < NumMaterial; i++)	{			D3DXMatrixTranslation(&Translate, PlayerX, PlayerY, PlayerZ);		pd3dDevice->SetMaterial(&BuffMaterial);        pd3dDevice->SetTexture(0, BuffTexture);		Mesh->DrawSubset(i);	}	return;}void LoadMesh::kill(){		if(Mesh !=NULL)        Mesh->Release();	if(BuffMat !=NULL)		BuffMat->Release();}void RenderD3D(void){	D3DXMATRIX matView;	D3DXMATRIX matProj;	Mesh1.D3DXLoadMeshFromDriver();	pd3dDevice->Clear(0,NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f,0);	pd3dDevice->Clear(0,NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0), 1.0f,0);	pd3dDevice->BeginScene();	Mesh1.Show();	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3 (CameraPSX, CameraPSY, CameraPSZ), &D3DXVECTOR3 (CameraLAX, CameraLAY, CameraLAZ), &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));	pd3dDevice->SetTransform(D3DTS_VIEW, &matView);	D3DXMatrixPerspectiveFovLH(&matProj, D3DXToRadian(45), (FLOAT)640/(FLOAT)480, 1.0f, 1000.0f);	pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);	pd3dDevice->EndScene();	pd3dDevice->Present(NULL,NULL,NULL,NULL);}void Exit(){	if(pD3D !=NULL)		pD3D->Release();	if(pd3dDevice !=NULL)        pd3dDevice->Release();	Input.kill();	Mesh1.kill();}
I am surprised it takes so little. I guess you know that the OS really does alot of behind-the-scenes juggling right?
Where is the point in optimizing this stuff?

Previously "Krohm"

Quote:Original post by snoddas14
hey, i got this awsom problem, a bit funny :P

my test is 2 obejtks and the use around 650 K of my 1 gig ram.
how da hell do i optimize it?


I'm sorry, but I don't make any sense out of that all.
I'm guessing that what you actually mean is that it uses 650Mb of RAM and not 650Kb, and thus you are wondering how you can lower it (Stämmer det?). Since if it actually uses 650Kb of RAM then it's already unrealistically optimized.
Best regards, Omid
yes of coruse.

härligt med lite svenskar =)

This topic is closed to new replies.

Advertisement