Hi,
I''ve got a simple 3 part program (main, h, cpp) that does nothing more than create a window, initialize directx, begin the scene, clear the viewport, and sit there.
Everything seems to be ok in most places. The problems I''m having are an Assert failing in win_main.cpp, and it appears to be running really slow. When I got to move the window, it hesitates. It never does clear the viewport and turn the background black or any other color. I''ve been looking at this comparing it to various tutorials on sites and some sample code I have in my book and from another web site. Can someone point me in the right direction of:
1. How to fix the viewport problem.
2. Fixing the assert in win_main.cpp.
Here is my source:
win_main.cpp
#include <windows.h>
#include "init_d3d.h"
#pragma comment( lib, "d3d9.lib" )
#define class_name "SpaceInvaders"
const int iWinWidth = 800;
const int iWinHeight = 800;
LRESULT CALLBACK WinProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam );
int WINAPI WinMain( HINSTANCE hinstance, HINSTANCE hprev, PSTR cmdline, int ishow )
{
HWND hwnd;
MSG msg;
WNDCLASSEX wndclassex = {0};
wndclassex.cbSize = sizeof( WNDCLASSEX );
wndclassex.style = CS_HREDRAW | CS_VREDRAW;
wndclassex.lpfnWndProc = WinProc;
wndclassex.hInstance = hinstance;
wndclassex.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndclassex.lpszClassName = class_name;
wndclassex.hCursor = (HCURSOR)LoadImage( NULL, MAKEINTRESOURCE(IDC_ARROW), IMAGE_CURSOR, 0, 0, LR_SHARED );
RegisterClassEx( &wndclassex );
RECT rect = { 0, 0, iWinWidth, iWinHeight };
DWORD winStyleEx = WS_EX_CLIENTEDGE;
DWORD winStyle = WS_CAPTION | WS_SYSMENU;
AdjustWindowRectEx( &rect, winStyle, false, winStyleEx );
hwnd = CreateWindowEx( winStyleEx,
class_name,
"Space Invaders Clone Build 0.01.06092004 [alpha]",
winStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
iWinWidth,
iWinHeight,
NULL,
NULL,
hinstance,
NULL );
if ( g_D3D->init( hwnd ) == false )
return EXIT_FAILURE;
GetClientRect( hwnd, &rect );
//assert(rect.right == iWinWidth && rect.bottom == rect.bottom);
ShowWindow( hwnd, ishow );
UpdateWindow( hwnd );
while ( 1 )
{
if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
{
if ( msg.message == WM_QUIT )
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
{
g_D3D->beginScene();
g_D3D->clearViewPort( 0xFF000000 ); // D3DCOLOR_ARGB( 255, 125, 155, 255 )
g_D3D->endScene();
}
}
UnregisterClass( class_name, hinstance );
return EXIT_SUCCESS;
}
LRESULT CALLBACK WinProc( HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam )
{
switch ( message )
{
/*case WM_RBUTTONDOWN:
MessageBox( 0, "Hello DirectX 9!", "Hello", MB_OK );
return 0;*/
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;
case WM_KEYDOWN:
if( wparam == VK_ESCAPE )
SendMessage( hwnd, WM_CLOSE, 0, 0 );
return 0;
}
return DefWindowProc( hwnd, message, wparam, lparam );
}
init_d3d.cpp
#include "init_d3d.h"
CD3D_obj::CD3D_obj()
{
d3d_interface = NULL;
d3d_device = NULL;
}
void CD3D_obj::beginScene()
{
HRESULT bs_result = d3d_device->BeginScene();
assert( bs_result == D3D_OK );
}
void CD3D_obj::endScene()
{
HRESULT es_result = d3d_device->EndScene();
assert( es_result == D3D_OK );
}
bool CD3D_obj::init( HWND hwnd )
{
HRESULT d3d_result;
D3DPRESENT_PARAMETERS d3d_params = {0};
d3d_interface = Direct3DCreate9( D3D_SDK_VERSION );
if ( d3d_interface == NULL )
return false;
d3d_params.Windowed = true; // Windowed mode
d3d_params.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3d_params.BackBufferFormat = D3DFMT_UNKNOWN;
/*
This is some code I found. It would be more ideal
for our app to eventually have a configurations
window and save the settings for resultion, and
windowed mode (y/n). This is good education though.
if( is_app_fullscreen )
{
pp.Windowed = FALSE;
pp.BackBufferWidth = 640;
pp.BackBufferHeight = 480;
}
else
{
pp.Windowed = TRUE;
}
*/
d3d_result = d3d_interface->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, // There is also SOFTWARE processing...
&d3d_params,
&d3d_device );
if ( d3d_result != D3D_OK )
{
d3d_result = d3d_interface->CreateDevice( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, // There is also SOFTWARE processing...
&d3d_params,
&d3d_device );
if ( d3d_result != D3D_OK )
return false;
}
return true;
}
bool CD3D_obj::clearViewPort( int iVPColor )
{
HRESULT cvp_result = d3d_device->Clear( 0, NULL, D3DCLEAR_TARGET, iVPColor, 1.0f, 0 );
return ( cvp_result == D3D_OK );
}
CD3D_obj::~CD3D_obj()
{
if( d3d_device != NULL )
d3d_device->Release();
if( d3d_interface != NULL )
d3d_interface->Release();
d3d_device = NULL;
d3d_interface = NULL;
}
CD3D_obj theD3D_obj;
CD3D_obj *g_D3D = &theD3D_obj;
init_d3d.h
#ifndef INIT_D3D_H
#define INIT_D3D_H
#include <d3d9.h>
#include <assert.h>
class CD3D_obj
{
public:
CD3D_obj(); // constructor
~CD3D_obj(); // deconstructor
void beginScene();
void endScene();
bool init( HWND hwnd );
bool clearViewPort( int iVPColor );
private:
IDirect3D9 *d3d_interface;
IDirect3DDevice9 *d3d_device;
CD3D_obj( const CD3D_obj &obj ) {}
CD3D_obj& operator =(CD3D_obj &obj) { return *this; }
};
extern CD3D_obj *g_D3D;
#endif