Re: Mouse Position

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Callum,

Once again thank you very much for you support and help, but I tried
this and to still no success.

So what I have done is created a very simple application, one so simple
I can past it here in its full glory, so you can try and let me know
that I am doing wrong. (please dont say it worked correctly, as this
will make me pull my hair out, becuase it must be a pc/card/something
else issue.

//////////////////////////////////////////////////////////////////
// CODE FOR ENTIRE TEST PROGRAM

// MouseIssue.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <windows.h>
#include <d3d9.h>
#include <d3dx9math.h>
#include <string>

struct CUSTOMVERTEX
{
FLOAT x, y, z;//, rhw; // The transformed position for the vertex
DWORD color; // The vertex color
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

std::string sWindowName;
HWND _hWnd;
LPDIRECT3D9 _pD3D;
LPDIRECT3DDEVICE9 _pd3dDevice;
D3DPRESENT_PARAMETERS _d3dpp;
LPDIRECT3DVERTEXBUFFER9 _pVB = NULL;

unsigned _cubesize;

int g_iMouseX;
int g_iMouseY;

// new code from callum
bool m_bWindowed; // Is Windowed
RECT m_rcWindowBounds; // Saved window bounds for mode switches
RECT m_rcWindowClient; // Saved client area size for mode switches
UINT m_uWidth; // Device Width
UINT m_uHeight; // Device Height
D3DVIEWPORT9 m_Viewport; // The viewport into the device
// end of new code

void CreateCube(IDirect3DDevice9 *pDevice) {

if( _pVB != NULL ) {
return;
}

CUSTOMVERTEX cube[] = { 0.0, 0.0, 0.0, 0xffffffff, 100.0, 0.0, 0.0,
0xffffffff,
100.0, 0.0, 0.0, 0xffffffff, 100.0, 100.0, 0.0, 0xffffffff,
100.0, 100.0, 0.0, 0xffffffff, 0.0, 100.0, 0.0, 0xffffffff,
0.0, 100.0, 0.0, 0xffffffff, 0.0, 0.0, 0.0, 0xffffffff,

100.0, 0.0, 0.0, 0xffffffff, 100.0, 0.0, 100.0, 0xffffffff,
100.0, 0.0, 100.0, 0xffffffff, 100.0, 100.0, 100.0, 0xffffffff,
100.0, 100.0, 100.0, 0xffffffff, 100.0, 100.0, 0.0, 0xffffffff,
100.0, 100.0, 0.0, 0xffffffff, 100.0, 0.0, 0.0, 0xffffffff,

0.0, 0.0, 0.0, 0xffffffff, 0.0, 0.0, 100.0, 0xffffffff,
0.0, 0.0, 100.0, 0xffffffff, 0.0, 100.0, 100.0, 0xffffffff,
0.0, 100.0, 100.0, 0xffffffff, 0.0, 100.0, 0.0, 0xffffffff,
0.0, 100.0, 0.0, 0xffffffff, 0.0, 0.0, 0.0, 0xffffffff,

0.0, 0.0, 100.0, 0xffffffff, 100.0, 0.0, 100.0, 0xffffffff,
100.0, 0.0, 100.0, 0xffffffff, 100.0, 100.0, 100.0, 0xffffffff,
100.0, 100.0, 100.0, 0xffffffff, 0.0, 100.0, 100.0, 0xffffffff,
0.0, 100.0, 100.0, 0xffffffff, 0.0, 0.0, 100.0, 0xffffffff,

0.0, 100.0, 0.0, 0xffffffff, 0.0, 100.0, 100.0, 0xffffffff,
0.0, 100.0, 100.0, 0xffffffff, 100.0, 100.0, 100.0, 0xffffffff,
100.0, 100.0, 100.0, 0xffffffff, 100.0, 100.0, 0.0, 0xffffffff,
100.0, 100.0, 0.0, 0xffffffff, 0.0, 100.0, 0.0, 0xffffffff };

_cubesize = sizeof(cube) / sizeof(CUSTOMVERTEX);
if( FAILED( pDevice->CreateVertexBuffer( sizeof(cube),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &_pVB, NULL ) ) ) {
return;
}

// fill the vertex buffer
VOID* pVertices;
if( FAILED( _pVB->Lock( 0, sizeof(cube), (void**)&pVertices, 0 ) ) )
return;

memcpy( pVertices, cube, sizeof(cube) );
_pVB->Unlock();

}

void DrawCube(IDirect3DDevice9 *pDevice) {
if( _pVB == NULL ) {
return;
}
// draw the vertices
pDevice->SetStreamSource( 0, _pVB, 0, sizeof(CUSTOMVERTEX) );
pDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
pDevice->DrawPrimitive( D3DPT_LINELIST, 0, (_cubesize / 2) );
}

void RenderPosition() {

LPDIRECT3DSURFACE9 pBB = NULL;
_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBB);
D3DSURFACE_DESC sd;
pBB->GetDesc(&sd);
pBB->Release();

D3DXMATRIXA16 matProj;
_pd3dDevice->GetTransform( D3DTS_PROJECTION, &matProj );

// Compute the vector of the pick ray in screen space
D3DXVECTOR3 v;
v.x = ( ( ( 2.0f * g_iMouseX ) / sd.Width ) - 1 ) / matProj._11;
v.y = -( ( ( 2.0f * g_iMouseY ) / sd.Height ) - 1 ) / matProj._22;
v.z = 1.0f;

// Get the inverse view matrix
D3DXMATRIX m, matView;
_pd3dDevice->GetTransform( D3DTS_VIEW, &matView );
D3DXMatrixInverse( &m, NULL, &matView );

D3DXVECTOR3 vPickRayDir, vPickRayOrig;
// Transform the screen space pick ray into 3D space
vPickRayDir.x = v.x*m._11 + v.y*m._21 + v.z*m._31;
vPickRayDir.y = v.x*m._12 + v.y*m._22 + v.z*m._32;
vPickRayDir.z = v.x*m._13 + v.y*m._23 + v.z*m._33;
D3DXVec3Normalize(&vPickRayDir,&vPickRayDir);
vPickRayOrig.x = m._41;
vPickRayOrig.y = m._42;
vPickRayOrig.z = m._43;


float NEAR_Z = 100.0f;
vPickRayOrig+=vPickRayDir*NEAR_Z;


// next draw a line from 0,0,0 to this point
CUSTOMVERTEX vertices[2];
vertices[0].x = (float) 0.0;
vertices[0].y = (float) 0.0;
vertices[0].z = (float) 0.0;
vertices[0].color = 0xffffffff;

vertices[1].x = vPickRayOrig.x;
vertices[1].y = vPickRayOrig.y;
vertices[1].z = vPickRayOrig.z;
vertices[1].color = 0xffffffff;

LPDIRECT3DVERTEXBUFFER9 pVB = NULL;
if( FAILED( _pd3dDevice->CreateVertexBuffer( 2 *
sizeof(CUSTOMVERTEX),
0,
D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT,
&pVB, NULL ) ) ) {
return;
}

// fill the vertex buffer
VOID* pVertices;
if( FAILED( pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 )
))
return;

memcpy( pVertices, vertices, sizeof(vertices) );
pVB->Unlock();

if( SUCCEEDED( _pd3dDevice->BeginScene() ) ) {

// draw the vertices
_pd3dDevice->SetStreamSource( 0, pVB, 0,
sizeof(CUSTOMVERTEX) );
_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
_pd3dDevice->DrawPrimitive( D3DPT_LINELIST, 0, 1 );
_pd3dDevice->EndScene();
}

}


void SetupCamera() {

D3DXMATRIX IdtyMtrx;
D3DXMatrixIdentity(&IdtyMtrx);
_pd3dDevice->SetTransform(D3DTS_WORLD, &IdtyMtrx);
_pd3dDevice->SetTransform(D3DTS_PROJECTION, &IdtyMtrx);

D3DXVECTOR3 vEyePt( 0.0f, 0.0f, -400.0f );
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
D3DXMATRIXA16 matView;
D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

D3DXMATRIX matProj;
FLOAT fAspect = ((FLOAT)500) / 500; // hard coded from the _tmain
paramters
D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f,
1000.0f );
_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

}

void RenderX() {

_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0),
1.0f, 0 );

if( SUCCEEDED( _pd3dDevice->BeginScene() ) ) {
DrawCube(_pd3dDevice);

_pd3dDevice->EndScene();

}

RenderPosition();

_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

LONG WINAPI wndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM
lParam ) {

switch( uMsg ) {

case WM_MOUSEMOVE:
{
g_iMouseX = LOWORD(lParam);
g_iMouseY = HIWORD(lParam);
InvalidateRect(_hWnd, NULL, false);
}
break;
case WM_PAINT :
{
PAINTSTRUCT ps;
BeginPaint(_hWnd, &ps);
RenderX();
EndPaint(_hWnd, &ps);
}
break;
case WM_KEYDOWN:
{
PostQuitMessage(0);
}
}
return DefWindowProc( hWnd, uMsg, wParam, lParam );
}

HWND CrteWnd( const char *pszName, int x, int y, int w, int h) {
// Register the window class
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, wndProc, 0L, 0L,
GetModuleHandle(NULL), LoadCursor(NULL,
IDC_ARROW), NULL, NULL, NULL,
pszName, NULL };

ATOM a = RegisterClassEx( &wc );

// Create the application's window
HWND hWnd = CreateWindow( pszName, pszName,
WS_OVERLAPPEDWINDOW, x, y, w, h,
GetDesktopWindow(), NULL, wc.hInstance,
NULL );

sWindowName = pszName;

return hWnd;

}

void Process() {

MSG msg;
SetCursor(LoadCursor(NULL, IDC_ARROW));
ShowWindow(_hWnd, SW_NORMAL );
UpdateWindow(_hWnd );
SetForegroundWindow(_hWnd);
SetFocus(_hWnd);
while( GetMessage( &msg, NULL, 0, 0) > 0) {

TranslateMessage( &msg );
DispatchMessage( &msg );

}
}

void setupX() {

_pD3D = Direct3DCreate9( D3D_SDK_VERSION );

// Set up the structure used to create the D3DDevice
ZeroMemory( &_d3dpp, sizeof(_d3dpp) );
_d3dpp.Windowed = TRUE;
_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
_d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

RECT rect;
GetWindowRect(_hWnd, &rect);
_d3dpp.BackBufferWidth = rect.right - rect.left;
_d3dpp.BackBufferHeight = rect.bottom - rect.top; // remember good old
upside down

// Create the D3DDevice
if( FAILED( _pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &_d3dpp, &_pd3dDevice ) ) ) {
_pD3D->Release();
return;
}

// Turn on the zbuffer
_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

}

bool SetupDevice(bool bWindowed, UINT uWidth, UINT uHeight) {

// Store if windowed or fullscreen localy
m_bWindowed = bWindowed;

// Store current window properties
GetWindowRect(_hWnd, &m_rcWindowBounds);
GetClientRect(_hWnd, &m_rcWindowClient);

// If no width specified
if (!uWidth) {
// If fullscreen, Default to 800
if (!bWindowed) {
m_uWidth = 800;
}
// If windowed, Default to client width
else {
m_uWidth = m_rcWindowClient.right - m_rcWindowClient.left;
}
}
// If width specified
else {
m_uWidth = uWidth;
}

// If no height specified
if (!uHeight) {
// If fullscreen, Default to 600
if (!bWindowed) {
m_uHeight = 600;
}
// If windowed, Default to client height
else {
m_uHeight = m_rcWindowClient.bottom - m_rcWindowClient.top;
}
}
// If height specified
else {
m_uHeight = uHeight;
}
return true;
};

int _tmain(int argc, _TCHAR* argv[])
{
_hWnd = CrteWnd("TEST DEMO", 100, 100, 500, 500);
setupX();
SetupDevice(true, 500, 500);

// Set Default Viewport Parameters
m_Viewport.X = 0;
m_Viewport.Y = 0;
m_Viewport.Width = m_uWidth;
m_Viewport.Height = m_uHeight;
m_Viewport.MinZ = 0.0f;
m_Viewport.MaxZ = 1.0f;
_pd3dDevice->SetViewport(&m_Viewport);

CreateCube(_pd3dDevice);
SetupCamera();
Process();
return 0;
}

//// END OF CODE FOR ENTIRE TEST PROGRAM
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

As you can see, I have made the changes pretty much as a cut and paste
from your posts. I have pretty much made the a skin and bones
application, error checking was a moot point, as I really dont care in
this particular case as I am posting this to a news group, (so kiddies
reading this, do not follow this code as a good example of what you
should do :-) ).

Anyhow, thanx again in advance, and please show me the light on what I
am doing wrong. To run this example, simply create a new win32 console
app, past the entire above code into the main cpp file. Add the direct
x libraries to the project, and voila, all should be operational (well
I hope so anyways.)

Cheers
Jason

.



Relevant Pages

  • Re: How add buton onto title bar of any external active window?
    ... I want to add buttons ontoany active window. ... int sm_CXSIZE; ... static int WindowsFeaturesWidth(HWND hwnd, DWORD style) ... LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM ...
    (microsoft.public.win32.programmer.gdi)
  • Re: PolyLine and autoscrolling canvas
    ... Seems odd to call a window class a DLL... ... CMyDLL(CWnd *parent, CWnd *parentcontrol, CRect parentrect, ... void set_MinMax(int minvalue, int maxvalue, int maxsizearray); ...
    (microsoft.public.vc.mfc)
  • Re: POOM Notifications
    ... Outlook notifications, ... HWND hWnd; ... BOOL InitInstance(HINSTANCE, int); ... // PURPOSE: Registers the window class. ...
    (microsoft.public.pocketpc.developer)
  • Re: POOM Notifications
    ... Outlook notifications, ... HWND hWnd; ... BOOL InitInstance(HINSTANCE, int); ... // PURPOSE: Registers the window class. ...
    (microsoft.public.pocketpc.developer)
  • How add buton onto title bar of any external active window?
    ... I want to add buttons ontoany active window. ... int sm_CXSIZE; ... static int WindowsFeaturesWidth(HWND hwnd, DWORD style) ... LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM ...
    (microsoft.public.win32.programmer.gdi)