Barret
March 14th, 2005, 12:24 PM
Im trying to learn directx but quickly stumbled across alot of linking problems. after some googeling I have downloaded the new libfiles for vc++ 6 but I still get alot of linking errors. can someone try to compile this on another platform and test if it works?
// Include the Windows header file that’s needed for all Windows applications
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "d3d9.lib")
#include <windows.h> // Header File For Windows
#include <d3dx9.h> // Header File For DirectX 3D
#include <d3d9.h> // Header File For DirectX 3D
#include <d3dx9tex.h>
#include <iostream.h>
#include <string>
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device
IDirect3DSurface9* srcSurface; //pointer to surface
// forward declarations
bool initWindow( HINSTANCE hInstance );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
bool initDirect3D( void);
void render( void);
IDirect3DSurface9* getSurfaceFromBitmap( std::string filename);
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
// Initialize the window
if ( !initWindow( hInstance ) )
return false;
// called after creating the window
if ( !initDirect3D( ) )
return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
//laddar bild till surface
getSurfaceFromBitmap("gubbe.bmp");
while( msg.message!= WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
else
{
render();
}
}
return (int) msg.wParam;
}
/******************************************************************************
* bool initWindow( HINSTANCE hInstance )
* initWindow registers the window class for the application, creates the window
******************************************************************************/
bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure.This describes how the window
// will look to the system
wcex.cbSize = sizeof( WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC) WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor( NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)( COLOR_WINDOW+ 1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = "DirectXExample"; // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(& wcex);
// Create the window
wndHandle = CreateWindow(
"DirectXExample", // the window class to use
"DirectXExample", // the title bar text
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
// Display the window on the screen
ShowWindow( wndHandle, SW_SHOW);
UpdateWindow( wndHandle);
return true;
}
/******************************************************************************
* LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)
* * The window procedure
******************************************************************************/
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
case WM_DESTROY:
PostQuitMessage( 0);
break;
}
// Always return the message to the default window
// procedure for further processing
return DefWindowProc( hWnd, message, wParam, lParam);
}
/*********************************************************************
* initDirect3D
*********************************************************************/
bool initDirect3D( void)
{
pD3D = NULL;
pd3dDevice = NULL;
// Create the DirectX object
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
// Fill the presentation parameters structure
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferWidth = 800;
d3dpp.hDeviceWindow = wndHandle;
// Create a default DirectX device
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice ) ) )
{
return false;
}
return true;
}
/*********************************************************************
* Render
*********************************************************************/
void Render( void)
{
// This will hold the back buffer
IDirect3DSurface9* backbuffer = NULL;
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;
// Clear the back buffer to a blue color
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 0,0,255), 1.0f, 0 );
// Get the back buffer
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
pd3dDevice->StretchRect( srcSurface,
NULL,
backbuffer,
NULL,
D3DTEXF_NONE );
// Present the back buffer contents to the display
pd3dDevice->Present ( NULL, NULL, NULL, NULL );
}
void cleanUp(void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
}
/**********************************************************
* getSurfaceFromBitmap
**********************************************************/
IDirect3DSurface9* getSurfaceFromBitmap( std:: string filename)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo; // holds details concerning this bitmap
// Get the width and height info from this bitmap
hResult = D3DXGetImageInfoFromFile( filename.c_str(), &imageInfo);
// Make sure that the call to D3DXGetImageInfoFromFile succeeded
if FAILED (hResult)
return NULL;
// Create the offscreen surface that will hold the bitmap
hResult = pd3dDevice->CreateOffscreenPlainSurface( 800,
600,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL );
// Make sure that this function call did not fail; if it did,
// exit this function
if ( FAILED( hResult ) )
return NULL;
// Load the bitmap into the surface that was created earlier
hResult = D3DXLoadSurfaceFromFile( surface,
NULL,
NULL,
filename.c_str( ),
NULL,
D3DX_DEFAULT,
0,
NULL );
if ( FAILED( hResult ) )
return NULL;
return surface;
}
// Include the Windows header file that’s needed for all Windows applications
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "d3d9.lib")
#include <windows.h> // Header File For Windows
#include <d3dx9.h> // Header File For DirectX 3D
#include <d3d9.h> // Header File For DirectX 3D
#include <d3dx9tex.h>
#include <iostream.h>
#include <string>
HINSTANCE hInst; // global handle to hold the application instance
HWND wndHandle; // global variable to hold the window handle
LPDIRECT3D9 pD3D; // the Direct3D object
LPDIRECT3DDEVICE9 pd3dDevice; // the Direct3D device
IDirect3DSurface9* srcSurface; //pointer to surface
// forward declarations
bool initWindow( HINSTANCE hInstance );
LRESULT CALLBACK WndProc( HWND, UINT, WPARAM, LPARAM );
bool initDirect3D( void);
void render( void);
IDirect3DSurface9* getSurfaceFromBitmap( std::string filename);
// This is winmain, the main entry point for Windows applications
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR lpCmdLine, int nCmdShow )
{
// Initialize the window
if ( !initWindow( hInstance ) )
return false;
// called after creating the window
if ( !initDirect3D( ) )
return false;
// main message loop:
MSG msg;
ZeroMemory( &msg, sizeof( msg ) );
//laddar bild till surface
getSurfaceFromBitmap("gubbe.bmp");
while( msg.message!= WM_QUIT )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage ( &msg );
DispatchMessage ( &msg );
}
else
{
render();
}
}
return (int) msg.wParam;
}
/******************************************************************************
* bool initWindow( HINSTANCE hInstance )
* initWindow registers the window class for the application, creates the window
******************************************************************************/
bool initWindow( HINSTANCE hInstance )
{
WNDCLASSEX wcex;
// Fill in the WNDCLASSEX structure.This describes how the window
// will look to the system
wcex.cbSize = sizeof( WNDCLASSEX); // the size of the structure
wcex.style = CS_HREDRAW | CS_VREDRAW; // the class style
wcex.lpfnWndProc = (WNDPROC) WndProc; // the window procedure callback
wcex.cbClsExtra = 0; // extra bytes to allocate for this class
wcex.cbWndExtra = 0; // extra bytes to allocate for this instance
wcex.hInstance = hInstance; // handle to the application instance
wcex.hIcon = 0; // icon to associate with the application
wcex.hCursor = LoadCursor( NULL, IDC_ARROW);// the default cursor
wcex.hbrBackground = (HBRUSH)( COLOR_WINDOW+ 1); // the background color
wcex.lpszMenuName = NULL; // the resource name for the menu
wcex.lpszClassName = "DirectXExample"; // the class name being created
wcex.hIconSm = 0; // the handle to the small icon
RegisterClassEx(& wcex);
// Create the window
wndHandle = CreateWindow(
"DirectXExample", // the window class to use
"DirectXExample", // the title bar text
WS_EX_TOPMOST | WS_POPUP | WS_VISIBLE,
CW_USEDEFAULT, // the starting x coordinate
CW_USEDEFAULT, // the starting y coordinate
640, // the pixel width of the window
480, // the pixel height of the window
NULL, // the parent window; NULL for desktop
NULL, // the menu for the application; NULL for none
hInstance, // the handle to the application instance
NULL); // no values passed to the window
// Make sure that the window handle that is created is valid
if (!wndHandle)
return false;
// Display the window on the screen
ShowWindow( wndHandle, SW_SHOW);
UpdateWindow( wndHandle);
return true;
}
/******************************************************************************
* LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam,LPARAM lParam)
* * The window procedure
******************************************************************************/
LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// Check any available messages from the queue
switch (message)
{
case WM_DESTROY:
PostQuitMessage( 0);
break;
}
// Always return the message to the default window
// procedure for further processing
return DefWindowProc( hWnd, message, wParam, lParam);
}
/*********************************************************************
* initDirect3D
*********************************************************************/
bool initDirect3D( void)
{
pD3D = NULL;
pd3dDevice = NULL;
// Create the DirectX object
if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
{
return false;
}
// Fill the presentation parameters structure
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = FALSE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferWidth = 800;
d3dpp.hDeviceWindow = wndHandle;
// Create a default DirectX device
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF,
wndHandle,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&pd3dDevice ) ) )
{
return false;
}
return true;
}
/*********************************************************************
* Render
*********************************************************************/
void Render( void)
{
// This will hold the back buffer
IDirect3DSurface9* backbuffer = NULL;
// Check to make sure you have a valid Direct3D device
if( NULL == pd3dDevice )
return;
// Clear the back buffer to a blue color
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB( 0,0,255), 1.0f, 0 );
// Get the back buffer
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
pd3dDevice->StretchRect( srcSurface,
NULL,
backbuffer,
NULL,
D3DTEXF_NONE );
// Present the back buffer contents to the display
pd3dDevice->Present ( NULL, NULL, NULL, NULL );
}
void cleanUp(void)
{
// Release the device and the Direct3D object
if( pd3dDevice != NULL )
pd3dDevice->Release( );
if( pD3D != NULL )
pD3D->Release( );
}
/**********************************************************
* getSurfaceFromBitmap
**********************************************************/
IDirect3DSurface9* getSurfaceFromBitmap( std:: string filename)
{
HRESULT hResult;
IDirect3DSurface9* surface = NULL;
D3DXIMAGE_INFO imageInfo; // holds details concerning this bitmap
// Get the width and height info from this bitmap
hResult = D3DXGetImageInfoFromFile( filename.c_str(), &imageInfo);
// Make sure that the call to D3DXGetImageInfoFromFile succeeded
if FAILED (hResult)
return NULL;
// Create the offscreen surface that will hold the bitmap
hResult = pd3dDevice->CreateOffscreenPlainSurface( 800,
600,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL );
// Make sure that this function call did not fail; if it did,
// exit this function
if ( FAILED( hResult ) )
return NULL;
// Load the bitmap into the surface that was created earlier
hResult = D3DXLoadSurfaceFromFile( surface,
NULL,
NULL,
filename.c_str( ),
NULL,
D3DX_DEFAULT,
0,
NULL );
if ( FAILED( hResult ) )
return NULL;
return surface;
}