Results 1 to 1 of 1

Thread: C++: Help with direct3d code

Threaded View

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Posts
    149

    C++: Help with direct3d code

    Okay Im really having trouble getting this to work properly. There are no errors I could find after I made a crude message handler. I didnt include that code. Why is the texture not being drawn? did I not set a needed texture stage?
    Thanks to all those that take the time to help.

    Code:
    //Purpose: Initialize Direct3d
    //Returns: Return 1 on success 0 on error 
    int CDIRECT3D::Initialize_Direct3d(HWND &window_handle)
    {
    	HRESULT hresult;
    
    
    	//create device
    	//Make Direct3D object
        if((D3D = Direct3DCreate8(D3D_SDK_VERSION))==NULL)
    	{
    		//error
    		return 0; 
    	}
    
    	hresult=D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3d_displaymode);
    	if(hresult!=D3D_OK)
    	{
    		return 0;
    	}
    
    
    
    	d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
    	d3dpp.hDeviceWindow = window_handle;
    	d3dpp.BackBufferCount = 1;
    
    	d3dpp.Windowed = false;
        d3dpp.BackBufferWidth = SCREEN_WIDTH;
        d3dpp.BackBufferHeight = SCREEN_HEIGHT;
        d3dpp.BackBufferFormat = d3d_displaymode.Format;
    	d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
    	d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_ONE;
    	d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
        d3dpp.EnableAutoDepthStencil = 1;
    
    
    
    	if(D3D_OK!=(hresult=D3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,
    		window_handle,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp,&d3d_device)))
    	{
    		return 0;
    	}
    
    	//Create vertex buffer
    	if(D3D_OK!=(d3d_device->CreateVertexBuffer(sizeof(VERTEX) * 4, NULL,
    		D3DFVF_VERTEX, D3DPOOL_MANAGED, &vertexbuffer)))
    	{
    		//error
    		return 0;
    	}
    
    	if(D3D_OK!=(d3d_device->SetStreamSource(0, vertexbuffer,sizeof(VERTEX))))
    	{
    		//error
    		return 0;
    	}
    
    	//Set vertex shader
    	if(D3D_OK!=(d3d_device->SetVertexShader(D3DFVF_VERTEX)))
    	{
    		//error
    		return 0;
    	}
    	
    
    	d3d_device->SetRenderState(D3DRS_ZENABLE, TRUE);
        d3d_device->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
        d3d_device->SetRenderState(D3DRS_LIGHTING, FALSE);
    	
    	d3d_device->SetRenderState(D3DRS_ALPHABLENDENABLE,  TRUE);
    	d3d_device->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
    	d3d_device->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
    	errorcode=d3d_device->SetTextureStageState(0, D3DTSS_COLOROP,D3DTOP_MODULATE);	
    	errorcode=d3d_device->SetTextureStageState(0,D3DTSS_COLORARG1,D3DTA_DIFFUSE);
    	errorcode=d3d_device->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_CURRENT);
    	errorcode=d3d_device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_DISABLE );
    
    
    	d3d_device->SetVertexShader(D3DFVF_VERTEX );
    
    	return 1;
    }
    
    IDirect3DTexture8* CDIRECT3D::LoadTexture(char *fileName)
    {
      IDirect3DTexture8 *d3dTexture;
      D3DXIMAGE_INFO SrcInfo;      //Optional
    
      //Use a magenta colourkey
      D3DCOLOR colorkey = 0xffffffff;
    
      // Load image from file
      if (FAILED(D3DXCreateTextureFromFileEx (d3d_device, fileName, 0, 0, 1, 0, 
            D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT, 
            colorkey, &SrcInfo, NULL, &d3dTexture)))
      {
        return NULL;
      }
    	
      //Return the newly made texture
      return d3dTexture;
    }
    
    void CDIRECT3D::BlitD3D(RECT *rDest,
        D3DCOLOR vertexColour)
    {
      VERTEX* vertices;
    
      //Lock the vertex buffer
      vertexbuffer->Lock(0, 0,(unsigned char**) &vertices, NULL);
    
      //Setup vertices
      //A -0.5f modifier is applied to vertex coordinates to match texture
      //and screen coords. Some drivers may compensate for this
      //automatically, but on others texture alignment errors are introduced
      //More information on this can be found in the Direct3D 9 documentation
      vertices[0].acolor = vertexColour;
      vertices[0].x = (float) rDest->left - 0.5f;
      vertices[0].y = (float) rDest->top - 0.5f;
      vertices[0].z = 0.0f;
      vertices[0].rhw = 1.0f;
      vertices[0].u = 0.0f;
      vertices[0].v = 0.0f;
    
      vertices[1].acolor = vertexColour;
      vertices[1].x = (float) rDest->right - 0.5f;
      vertices[1].y = (float) rDest->top - 0.5f;
      vertices[1].z = 0.0f;
      vertices[1].rhw = 1.0f;
      vertices[1].u = 1.0f;
      vertices[1].v = 0.0f;
    
      vertices[2].acolor = vertexColour;
      vertices[2].x = (float) rDest->right - 0.5f;
      vertices[2].y = (float) rDest->bottom - 0.5f;
      vertices[2].z = 0.0f;
      vertices[2].rhw = 1.0f;
      vertices[2].u = 1.0f;
      vertices[2].v = 1.0f;
    
      vertices[3].acolor = vertexColour;
      vertices[3].x = (float) rDest->left - 0.5f;
      vertices[3].y = (float) rDest->bottom - 0.5f;
      vertices[3].z = 0.0f;
      vertices[3].rhw = 1.0f;
      vertices[3].u = 0.0f;
      vertices[3].v = 1.0f;
    
     
    
      //Unlock the vertex buffer
      vertexbuffer->Unlock();
    
      //Set texture
      d3d_device->SetTexture (0,texture);
    
      //Draw image
      d3d_device->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
    }
    
    int CDIRECT3D::Draw_Text_GDI(char *message, int x,int y,int color,HDC hdc)
    {
    	SetTextColor(hdc,color );
    	SetBkMode(hdc, TRANSPARENT);
    	TextOut(hdc,x,y,message,strlen(message));
    
    	return 1;
    } 
    
    
    
    int Game_Main(void *parms)
    {
    	if (KEY_DOWN(VK_ESCAPE))
           PostMessage(main_window_handle, WM_DESTROY,0,0);
    
    	//clear backbuffer
    	direct3d.d3d_device->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,D3DCOLOR_RGBA(0,60,128,255),1.0f,0);
    
    	if(SUCCEEDED(direct3d.d3d_device->BeginScene()))
    	{
    		direct3d.BlitD3D(&drect,0xffffffff);
    
    		direct3d.d3d_device->EndScene();
    	}
    
    	direct3d.d3d_device->Present(NULL,NULL,NULL,NULL);
    
    
    	
    	return(1);
    }
    Last edited by Electroman; Dec 8th, 2004 at 07:52 PM. Reason: Added Language to title.

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