CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2009
    Posts
    14

    Question Rendering Multiple Windows using multiple devices (DirectX)

    I tried to do some simple rendering to multiple windows using multiple devices, but when it runs it gives me an error with access violation. Can someone help me? Any help is welcomed.

    Code:
    #if !defined(MULTIPLE_RENDERING) && defined(__cplusplus)
    #define MULTIPLE_RENDERING
    
    #include <string>
    #include <vector>
    #include <windows.h>
    #include <windowsx.h>
    #include <d3d9.h>
    #include <d3dx9.h>
    
    class Cls0;
    class Cls1;
    
    void csmain(void);
    Cls0 *UploadLink(void** dat);
    signed int __stdcall WinMain(HINSTANCE__ *hInstance,
    	                                  HINSTANCE__ *hPrevInstance,
    		  	          __readableTo(sentinel(0)) char *lpCmdLine,
    			          signed int nCmdShow);
    __w64 signed long int __stdcall WindowProc(HWND__ *hWnd,
    				   	            unsigned int message,
    					            __w64 unsigned int wParam,
    			                                    __w64 signed long int lParam)
    {
       if (message==WM_DESTROY) PostQuitMessage(0);
       else if (message==WM_CLOSE) DestroyWindow(hWnd);
       return 0;
    }
    
    class Cls0
    {
    	public:
    		friend class Cls1;
    		friend Cls0 *UploadLink(void** dat);
    		friend signed int __stdcall WinMain(HINSTANCE__ *hInstance,
    			                                            HINSTANCE__ *hPrevInstance,
    				   		        __readableTo(sentinel(0)) char *lpCmdLine,
    						        signed int nCmdShow);
    
    	private:
    		unsigned long backbuffer_width;
    		unsigned long backbuffer_height;
    
    	private:
    		LPDIRECT3D9 d3d;
    		WNDCLASSEX *winClass;
    
    	private:
    		std::vector<Cls1*> frames;
    
    	private:
    		Cls0(WNDCLASSEX *wc) { winClass = wc; }
    
    	public:
    		Cls0() {}
    		~Cls0() { if (d3d != NULL) d3d->Release(); }
    };
    
    Cls0 *UploadLink(void** dat)
    {
    	static Cls0 _((WNDCLASSEX*)dat[0]);
    	return &_;
    }
    
    Cls0 *DownloadLink(void)
    {
    	void *_;
    	return UploadLink((void**)&_);
    }
    
    class Cls1
    {
    	private:
    		HWND hWnd;
    		LPDIRECT3DDEVICE9 d3ddev;
    		Cls0 *c;
    		unsigned short int i[5];
    		bool b[3];
    		std::string n;
    
    	public:
    		Cls1()
    		{
    			c=DownloadLink();
    			i[0]=(i[1]=(i[2]=(i[3]=(i[4]=0))));
    			n=""; b[0]=(b[1]=(b[2]=false));
    			i[5]=c->frames.size(); c->frames.push_back(this);
    		}
    
    		~Cls1()
    		{
    			if (d3ddev != NULL) d3ddev->Release();
    			std::vector<Cls1*> temp;
    			for (int _iter=0; _iter<c->frames.size(); ++_iter)
    				temp.push_back(c->frames.at(_iter));
    			for (int _iter=c->frames.size(); _iter>=i[5]; --_iter)
    				c->frames.pop_back();
    			for (int _iter=(i[5]+1); _iter<temp.size(); ++_iter)
    				c->frames.push_back(temp.at(_iter));
    		}
    
    	public:
    		void setPosition(unsigned short int posX, unsigned short int posY)
    		{
    			i[0]=posX; i[1]=posY; b[0]=true;
    		}
    
    		unsigned short int *getPosition(void)
    		{
    			unsigned short int *t=new unsigned short int[2];
    			t[0]=i[0]; t[1]=i[1];
    			return t;
    		}
    
    		void setSize(unsigned short int posH, unsigned short int posW)
    		{
    			i[2]=posH; i[3]=posW; b[1]=true;
    		}
    
    		unsigned short int *getSize(void)
    		{
    			unsigned short int *t=new unsigned short int[2];
    			t[0]=i[2]; t[0]=i[3];
    			return t;
    		}
    
    		void setName(std::string name)
    		{
    			n = name; b[2]=true;
    		}
    
    		std::string getName(void)
    		{
    			return n;
    		}
    
    		void drawFrame(void)
    		{
    			if (i[4]==0)
    				hWnd = CreateWindowEx( NULL, c->winClass->lpszClassName,                                                                 n.c_str(),
    								       WS_OVERLAPPEDWINDOW | WS_VISIBLE, i[0], 
    									   i[1], i[3], i[4], NULL, NULL, c->winClass->hInstance, 
    									   NULL );
    			else
    				hWnd = CreateWindowEx( NULL, c->winClass->lpszClassName, n.c_str(),
    									   WS_OVERLAPPEDWINDOW | WS_VISIBLE, i[0],
    									   i[1], i[3], i[4], (c->frames.at(0))->hWnd, NULL,
    									   c->winClass->hInstance, NULL );
    
    			ShowWindow( hWnd, SW_NORMAL );
    			UpdateWindow( hWnd );
    
    			c->d3d = Direct3DCreate9( D3D_SDK_VERSION );
    
    			D3DDISPLAYMODE d3ddm;
    
    			c->d3d->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm );
    
    			D3DPRESENT_PARAMETERS d3dpp;
    		    ZeroMemory( &d3dpp, sizeof(d3dpp) );
        
    			d3dpp.Windowed               = TRUE;
    			d3dpp.SwapEffect             = D3DSWAPEFFECT_DISCARD;
    			d3dpp.BackBufferFormat       = d3ddm.Format;
    			d3dpp.EnableAutoDepthStencil = TRUE;
    			d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    			d3dpp.PresentationInterval   = D3DPRESENT_INTERVAL_IMMEDIATE;
    
    			c->d3d->CreateDevice( D3DADAPTER_DEFAULT, 
    								  D3DDEVTYPE_HAL,
    								  hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    								  &d3dpp, &d3ddev );
    
    			LPDIRECT3DSURFACE9 pBackBuffer;
    			D3DSURFACE_DESC d3dsd;
    			d3ddev->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer );  // THE ACCESS VIOLATION APPEARS HERE
    			pBackBuffer->GetDesc( &d3dsd );
    			pBackBuffer->Release();
    			c->backbuffer_width  = d3dsd.Width;
    			c->backbuffer_height = d3dsd.Height;
    		}
    
    	public:
    		friend signed int __stdcall WinMain(HINSTANCE__ *hInstance,
    											HINSTANCE__ *hPrevInstance,
    											__readableTo(sentinel(0)) char *lpCmdLine,
    											signed int nCmdShow);
    };
    
    signed int __stdcall WinMain(HINSTANCE__ *hInstance,
    							 HINSTANCE__ *hPrevInstance,
    							 __readableTo(sentinel(0)) char *lpCmdLine,
    							 signed int nCmdShow)
    {
    	WNDCLASSEX winClass;
    
    	winClass.lpszClassName = "Global_Class";
        winClass.cbSize        = sizeof(WNDCLASSEX);
        winClass.style         = CS_HREDRAW | CS_VREDRAW;
        winClass.lpfnWndProc   = WindowProc;
        winClass.hInstance     = hInstance;
        winClass.hIcon         = LoadIcon(hInstance, (LPCTSTR)NULL);
        winClass.hIconSm       = LoadIcon(hInstance, (LPCTSTR)NULL);
        winClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
        winClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winClass.lpszMenuName  = NULL;
        winClass.cbClsExtra    = 0;
        winClass.cbWndExtra    = 0;
    
    	void **dat=new void*;
    	dat[0] = (void*)&winClass;
    	UploadLink(dat);
    	Cls0 *C=DownloadLink();
    
    	RegisterClassEx(C->winClass);
    
    	csmain();
    
    	MSG msg;
    
        while (true)
        {
            while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    
            if(msg.message == WM_QUIT)
                break;
    
    		Cls0 *C = DownloadLink();
    
    		for (int x=0; x<C->frames.size(); ++x) {
    			Cls1 *temp = C->frames.at(x);
    			temp->d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
    			temp->d3ddev->BeginScene();
    			temp->d3ddev->EndScene();
    			temp->d3ddev->Present(NULL, NULL, NULL, NULL);
    		}
        }
    
    	return msg.wParam;
    }
    
    #endif

  2. #2
    Join Date
    Apr 1999
    Posts
    3,585

    Re: Rendering Multiple Windows using multiple devices (DirectX)

    An access violation typically results from a bad pointer or array index. Where does the code assert? Have you tried running with the DirectX debugger on?
    Gort...Klaatu, Barada Nikto!

  3. #3
    Join Date
    Sep 2009
    Posts
    14

    Re: Rendering Multiple Windows using multiple devices (DirectX)

    thank you.... i observed that one of my arrays, i from cls1, has 5 elements and i (like a "genius" as i am ) i havent noticed i used i[5], witch doesnt exists and created the access violation error.... problem solved

  4. #4
    Join Date
    Sep 2009
    Posts
    14

    Re: Rendering Multiple Windows using multiple devices (DirectX)

    thank you.... i observed that one of my arrays, i from cls1, has 5 elements and i (like a "genius" as i am ) i havent noticed i used i[5], witch doesnt exists and created one access violation error.... but still i keep getting another access violation error....

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured