Sign in to follow this  
GotenRulezU

DirectX compiler complaint when locking the buffer

Recommended Posts

GotenRulezU    100
well not really when locking the buffer but inside the lock when i use memcpy. heres my source(skip for short version)
#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<string>
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"d3d9.lib")
using namespace std;

bool bAppDone = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam);
struct points{
	int x,y,z;
	DWORD color;
};
const DWORD point_fvf=D3DFVF_XYZRHW|D3DFVF_DIFFUSE;
class Program{
private:
	IDirect3D9 *pD3D;
	IDirect3DDevice9 *pDevice;
	IDirect3DVertexBuffer9 *pVB;
	D3DCOLOR bgColor;
	points point1;
public:
	HRESULT InitD3D(bool bWindowed, HWND hwnd){
		pD3D = Direct3DCreate9(D3D_SDK_VERSION);
		if(!pD3D){
			CatchErrors("Error initializing D3D");
			return E_FAIL;
		}
		D3DPRESENT_PARAMETERS d3dpp;
		D3DDISPLAYMODE d3ddm;
		ZeroMemory(&d3ddm,sizeof(D3DDISPLAYMODE));
		pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
		ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
		d3dpp.BackBufferCount = 1;
		d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
		d3dpp.MultiSampleQuality = 0;
		d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
		d3dpp.hDeviceWindow = hwnd;
		d3dpp.Flags = 0;
		d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
		d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
		d3dpp.BackBufferFormat = d3ddm.Format;
		d3dpp.EnableAutoDepthStencil = FALSE;

		if(bWindowed){
			d3dpp.Windowed = TRUE;
		}
		else{
			RECT rect;
			GetWindowRect(hwnd,&rect);
			int bbHeight = rect.bottom - rect.top;
			int bbWidth = rect.right - rect.left;
			d3dpp.BackBufferHeight = bbHeight;
			d3dpp.BackBufferWidth = bbWidth;
			d3dpp.Windowed = FALSE;
		}
		if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,
				D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pDevice))){
			CatchErrors("Error creating device!");
			return E_FAIL;
		}
		bgColor = D3DCOLOR_COLORVALUE(1,0,0,0);
		point1.color = D3DCOLOR_COLORVALUE(0,0,1,0);
		point1.x = 50;
		point1.y = 50;
		point1.z = 1;
		return S_OK;
	}
	HRESULT ShutdownD3D(){
		if(pD3D != NULL){
			if(FAILED(pD3D->Release())){
				CatchErrors("Error shutting down D3D");
				return E_FAIL;
			}
		}
		if(pDevice != NULL){
			if(FAILED(pDevice->Release())){
				CatchErrors("Error shutting down D3D Device");
				return E_FAIL;
			}
		}
		return S_OK;
	}
	HRESULT RenderD3D(){
		pDevice->SetFVF(point_fvf);
		void* data;
		pDevice->CreateVertexBuffer(sizeof(points),D3DUSAGE_WRITEONLY,point_fvf,D3DPOOL_MANAGED,
			&pVB,NULL);
		pVB->Lock(0,sizeof(point1),&data,0);
		memcpy(data,point1,sizeof(points));
		pVB->Unlock();
		if(FAILED(pDevice->Clear(0,NULL,D3DCLEAR_TARGET,bgColor,1.0f,0))){
			CatchErrors("Failed to clear!");
			return E_FAIL;
		}
		pDevice->BeginScene();
		pDevice->DrawPrimitiveUP(D3DPT_POINTLIST,1,data,sizeof(points));
		pDevice->EndScene();
		pDevice->Present(NULL,NULL,NULL,NULL);
		return S_OK;
	}
	void CatchErrors(string error){
		MessageBox(NULL,error.c_str(),"Error",MB_OK);
		ShutdownD3D();
	}
	void SetBGColor(D3DCOLOR iColor){
		bgColor = iColor;
	}

}program;
	
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCmd){
	WNDCLASS wc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.hCursor = LoadCursor(NULL,IDC_ARROW);
	wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wc.hInstance = hInstance;
	wc.lpfnWndProc = WndProc;
	wc.lpszClassName = "class";
	wc.lpszMenuName = NULL;
	wc.style = CS_VREDRAW|CS_HREDRAW;
	RegisterClass(&wc);
	HWND hwnd = CreateWindow("class","window",WS_OVERLAPPEDWINDOW,0,0,
		GetSystemMetrics(0),GetSystemMetrics(1),NULL,NULL,hInstance,NULL);
	program.InitD3D(false,hwnd);
	MSG msg;
	UpdateWindow(hwnd);
	ShowWindow(hwnd, iCmd);
	while(!bAppDone){
		if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else{
			program.RenderD3D();
		}
	}
	program.ShutdownD3D();
	UnregisterClass("class",hInstance);
	return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam){
	switch(iMsg){
		case WM_KEYDOWN:
			switch(LOWORD(wParam)){
				case VK_RETURN:
					break;
				case VK_ESCAPE:
					bAppDone = true;
					break;
			}
			break;
		case WM_DESTROY:
			bAppDone = true;
			break;
	}
	return DefWindowProc(hwnd,iMsg,wParam,lParam);
}

heres the part where i'm having trouble:
HRESULT RenderD3D(){
		pDevice->SetFVF(point_fvf);
		void* data;
		pDevice->CreateVertexBuffer(sizeof(points),D3DUSAGE_WRITEONLY,point_fvf,D3DPOOL_MANAGED,
			&pVB,NULL);
		pVB->Lock(0,sizeof(point1),&data,0);
		memcpy(data,point1,sizeof(points));
		pVB->Unlock();
		if(FAILED(pDevice->Clear(0,NULL,D3DCLEAR_TARGET,bgColor,1.0f,0))){
			CatchErrors("Failed to clear!");
			return E_FAIL;
		}
		pDevice->BeginScene();
		pDevice->DrawPrimitiveUP(D3DPT_POINTLIST,1,data,sizeof(points));
		pDevice->EndScene();
		pDevice->Present(NULL,NULL,NULL,NULL);
		return S_OK;
	}

where i call memcpy(data,point1,sizeof(points)) i get a compiler error:
c:\documents and settings\cory fisher\my documents\visual studio 2005\projects\directx\directx\main.cpp(91) : error C2664: 'memcpy' : cannot convert parameter 2 from 'points' to 'const void *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

any ideas on how to fix this? thx, Cory

Share this post


Link to post
Share on other sites
GotenRulezU    100
thx dude, its bad when the directx tutorial in the docs doesnt work lol
edit: after changing it it compiles but when i run it i get this:
Direct3D9: (ERROR) :Vertex buffer size needs to enough to hold one vertex
Direct3D9: (ERROR) :Failure trying to create Vertex Buffer
where have i gone wrong...?
-Cory

Share this post


Link to post
Share on other sites
GotenRulezU    100
Getting frustrated XD
Now i've tried trianglelist and its running but doesnt draw anything lol.

#include<windows.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<string>
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"d3d9.lib")
using namespace std;

D3DCOLOR current = D3DCOLOR_COLORVALUE(1,0,0,0);
bool bAppDone = false;
LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam);
struct tri{
int x,y,z;
DWORD color;
};
const DWORD point_fvf=(D3DFVF_XYZ|D3DFVF_DIFFUSE);
class Program{
private:
IDirect3D9 *pD3D;
IDirect3DDevice9 *pDevice;
IDirect3DVertexBuffer9 *pVB;
D3DCOLOR bgColor;
tri tri1[3];
public:
HRESULT InitD3D(bool bWindowed, HWND hwnd){
pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!pD3D){
CatchErrors("Error initializing D3D");
return E_FAIL;
}
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;
ZeroMemory(&d3ddm,sizeof(D3DDISPLAYMODE));
pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm);
ZeroMemory(&d3dpp,sizeof(D3DPRESENT_PARAMETERS));
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
d3dpp.BackBufferFormat = d3ddm.Format;
d3dpp.EnableAutoDepthStencil = FALSE;
if(bWindowed){
d3dpp.Windowed = TRUE;
}
else{
RECT rect;
GetWindowRect(hwnd,&rect);
int bbHeight = rect.bottom - rect.top;
int bbWidth = rect.right - rect.left;
d3dpp.BackBufferHeight = bbHeight;
d3dpp.BackBufferWidth = bbWidth;
d3dpp.Windowed = FALSE;
}
if(FAILED(pD3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&pDevice))){
CatchErrors("Error creating device!");
return E_FAIL;
}
bgColor = D3DCOLOR_COLORVALUE(1,0,0,0);
tri1[0].color = 0xFFFFFFFF;
tri1[0].x = 50;
tri1[0].y = 50;
tri1[0].z = 1;
tri1[1].color = 0xFFFFFFFF;
tri1[1].x = 100;
tri1[1].y = 50;
tri1[1].z = 1;
tri1[2].color = 0xFFFFFFFF;
tri1[2].x = 0;
tri1[2].y = 100;
tri1[2].z = 1;
pDevice->SetFVF(point_fvf);
pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_SOLID);
pDevice->CreateVertexBuffer(sizeof(tri)*3,D3DUSAGE_WRITEONLY,D3DFVF_XYZ|D3DFVF_DIFFUSE,
D3DPOOL_MANAGED,&pVB,NULL);
return S_OK;
}
HRESULT ShutdownD3D(){
if(pD3D != NULL){
if(FAILED(pD3D->Release())){
CatchErrors("Error shutting down D3D");
return E_FAIL;
}
}
if(pDevice != NULL){
if(FAILED(pDevice->Release())){
CatchErrors("Error shutting down D3D Device");
return E_FAIL;
}
}
if(pVB != NULL){
if(FAILED(pVB->Release())){
CatchErrors("Error shutting down D3D VB");
return E_FAIL;
}
}
return S_OK;
}
HRESULT RenderD3D(){
bgColor = current;
void *data;
pVB->Lock(0,0,&data,0);
memcpy(data,&tri1,sizeof(tri1));
pVB->Unlock();
if(FAILED(pDevice->Clear(0,NULL,D3DCLEAR_TARGET,bgColor,1.0f,0))){
CatchErrors("Failed to clear!");
return E_FAIL;
}
pDevice->BeginScene();
if(FAILED(pDevice->DrawPrimitiveUP(D3DPT_TRIANGLELIST,1,tri1,sizeof(tri)))){
CatchErrors("failed to draw");
return E_FAIL;
}
pDevice->EndScene();
pDevice->Present(NULL,NULL,NULL,NULL);
pVB->Release();
return S_OK;
}
void CatchErrors(string error){
MessageBox(NULL,error.c_str(),"Error",MB_OK);
ShutdownD3D();
}
void SetBGColor(D3DCOLOR iColor){
bgColor = iColor;
}

}program;

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, PSTR cmdLine, int iCmd){
WNDCLASS wc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.lpszClassName = "class";
wc.lpszMenuName = NULL;
wc.style = CS_VREDRAW|CS_HREDRAW;
RegisterClass(&wc);
HWND hwnd = CreateWindow("class","window",WS_OVERLAPPEDWINDOW,0,0,
GetSystemMetrics(0),GetSystemMetrics(1),NULL,NULL,hInstance,NULL);
program.InitD3D(true,hwnd);
MSG msg;
UpdateWindow(hwnd);
ShowWindow(hwnd, iCmd);
while(!bAppDone){
DWORD start = GetTickCount();
if(PeekMessage(&msg,NULL,0,0,PM_REMOVE)){
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
//if(GetTickCount() - start <= 40)
if(E_FAIL == program.RenderD3D()){
return 0;
}
}
}
program.ShutdownD3D();
UnregisterClass("class",hInstance);
return 0;
}

LRESULT CALLBACK WndProc(HWND hwnd,UINT iMsg, WPARAM wParam, LPARAM lParam){
switch(iMsg){
case WM_KEYDOWN:
switch(LOWORD(wParam)){
case VK_RETURN:
switch(current){
case D3DCOLOR_COLORVALUE(1,0,0,0):{
current = D3DCOLOR_COLORVALUE(0,1,0,0);
break;
}
case D3DCOLOR_COLORVALUE(0,1,0,0):{
current = D3DCOLOR_COLORVALUE(0,0,1,0);
break;
}
case D3DCOLOR_COLORVALUE(0,0,1,0):{
current = D3DCOLOR_COLORVALUE(1,0,0,0);
break;
}
}
break;
case VK_ESCAPE:
PostQuitMessage(0);
bAppDone = true;
break;
}
break;
case WM_DESTROY:
bAppDone = true;
break;
}
return DefWindowProc(hwnd,iMsg,wParam,lParam);
}


i hate not knowing wtf i'm doin wrong. i hope once i get the basics of this i can remember them.
-Cory

Share this post


Link to post
Share on other sites
GotenRulezU    100
omfg all because i used int instead of float in my fvf struct. omfg works now lol
3 hours all wasted and all i had to do was change int -> float lol
thx guys,
Cory

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