Jump to content
  • Advertisement
Sign in to follow this  
congacon

Load Surface!!!!

This topic is 4833 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi !!! I'm trying to load a surface from file but it didn't work Can you help me . My code here:
//-----------------------------------------------------------------------------
#include <Windows.h>//Add them vo Winmm.lib ,d3dx9.lib
#include <mmsystem.h>
#include <d3dx9.h>
//#pragma warning( disable : 4996 ) // disable deprecated warning 
//#include <strsafe.h>
//#pragma warning( default : 4996 ) 




//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D			= NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice	= NULL; // Our rendering device
LPDIRECT3DSURFACE9		g_pSurface		= NULL;	//Our surface 
POINT point;





//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        return E_FAIL;

    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( &d3dpp, sizeof(d3dpp) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;

    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }

    

    return S_OK;
}




//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
    //g_pd3dDevice->CreateOffscreenPlainSurface(	640, // the width of the surface to create
	//								480, // the height of the surface to create
	//								D3DFMT_X8R8G8B8, // the surface format
	//								D3DPOOL_DEFAULT, // the memory pool to use
	//								&g_pSurface, // holds the resulting surface
	//								NULL); // reserved; should be NULL
	D3DXIMAGE_INFO Info;
	D3DXGetImageInfoFromFile("E:\\FZH62.jpg", &Info);
	g_pd3dDevice->CreateOffscreenPlainSurface(	Info.Width,
												Info.Height,
												Info.Format,
												D3DPOOL_SYSTEMMEM,
												&g_pSurface, 
												NULL);
	if(FAILED(D3DXLoadSurfaceFromFile(	g_pSurface,
										NULL,
										NULL,
										"E:\\FZH62.jpg",
										NULL,
										D3DX_DEFAULT,
										0,
										NULL )))
	{
		MessageBox(NULL,"Khong load surface duoc","Load Surface",MB_OK);
	}
	
	return S_OK;
}




//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    //if( g_pVB != NULL )
        //g_pVB->Release();

    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();

    if( g_pD3D != NULL )
        g_pD3D->Release();
	if( g_pSurface !=NULL)
		g_pSurface->Release();
}





//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
	
    
    // Begin the scene
	if(SUCCEEDED(g_pd3dDevice->BeginScene()))
	{
		//Draw stuff here
		IDirect3DSurface9 *BackBuffer = NULL;
		if(FAILED(g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, & BackBuffer)))
		{
			MessageBox(NULL,"Khong load backbuffer duoc","Load Surface",MB_OK);
		}
		
		g_pd3dDevice->UpdateSurface(g_pSurface, NULL, BackBuffer, NULL);
		//g_pd3dDevice->StretchRect(	g_pSurface,
		//						NULL,
		//						BackBuffer,
		//						NULL,
		//						D3DTEXF_NONE );
		// End the scene
		g_pd3dDevice->EndScene();
	}
    
//{
	// Check to make sure you have a valid Direct3D device
	//if( NULL == g_pd3dDevice )
	//return;
	// Clear the back buffer to a blue color
	//g_pd3dDevice->Clear(	0, 
	//						NULL, 
	//						D3DCLEAR_TARGET,
	//						D3DCOLOR_XRGB(0,255,0), 
	//						1.0f, 
	//						0 );
		//IDirect3DSurface9* backbuffer = NULL;
		// Get the back buffer
		//g_pd3dDevice->GetBackBuffer(	0,
		//							0,
		//							D3DBACKBUFFER_TYPE_MONO,
		//							&backbuffer );
		// Copy the offscreen surface to the back buffer
		// Note the use of NULL values for the source and destination RECTs
		// This ensures a copy of the entire surface to the back buffer
		//g_pd3dDevice->StretchRect(	g_pSurface,
		//						NULL,
		//						backbuffer,
		//						NULL,
		//						D3DTEXF_NONE );
	//	g_pd3dDevice->EndScene();
	//}
    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
        case WM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return 0;
		case WM_KEYDOWN:
			switch(wParam)
			{
			case 'Q':
				Cleanup();
				PostQuitMessage( 0 );
				return 0;
			}
			
		case WM_LBUTTONDOWN:
			MessageBox(NULL,"Ban vua nhan chuot trai","Thong diep",MB_OK);
			
			//Cleanup();
			//PostQuitMessage( 0 );
			//return 0;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      "D3D Tutorial", NULL };
    RegisterClassEx( &wc );

    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 04: Lights",
                              WS_OVERLAPPEDWINDOW, 100, 100, 640, 480,
                              NULL, NULL, wc.hInstance, NULL );

    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                msg.pt.x=point.x;
				msg.pt.y=point.y;
				if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}

Thanks a lot [Edited by - Coder on August 23, 2005 3:25:04 AM]

Share this post


Link to post
Share on other sites
Advertisement
I just want to load a bitmap to surface .
Another question :
when i load a texture to a sprite .Its image scale a bit . Why???
How can i fix it??
Thanks

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!