Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.



This topic is 5618 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

This has been really bothering me... Ive been programming for about 2 years now, and during that time I've gotten relatively good with DirectDraw. The past couple months Ive been trying to learn D3D9, but thats been plauged with problems ever since I got it to Initialize. Anyway, Here lately Ive been writing out a story line and design doc for a simple 2d space strategy game, nothing big or flashy, just something ive wanted to make. Now my problem. I know this sounds really newb and i think i might flame myself for it when I get done writing it, but whats your opinion? Should I go ahead and make the game with DDraw and wait for more tutorials to come out and everything so i could learn it better, or keep struggeling to make a triangle spin in D3D9? Id just like some people's suggestions, like what would u do in my situation. I know how Id do it in DDraw, the only really hard part would be just typing it all out. Well theres my question, I hope i can get some help. thanks yall. Hibiki Konzaki Wheres the any key? [edited by - Hibiki_KonzakiXIII on June 4, 2003 12:58:04 AM]

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
I remember before I learned directdraw i was like no thats too complicated i dont understand and i was like direct3d thats impossible
then I learned directdraw and I was like omg this is amazing and I still was like direct3d is way to hard
then direct3d8 came out and i started looking at the tutorials in the sdk and i was like wow this is easy
ill make another post and show you my d3d9 class
maybe it will help you

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

// direct3d.h

#pragma once

class CDirect3D


IDirect3D9* m_pD3D;
IDirect3DDevice9* m_pd3dDevice;


~CDirect3D() { Shutdown(); }

IDirect3DDevice9* GetDevice()
{ return m_pd3dDevice; }

bool Init(HWND hWnd, int width, int height, int bpp);
void Shutdown();

void BeginScene(DWORD dwColor);
void EndScene();
void ResetWorldMatrix();
void Begin3D();
void Begin2D();

void SaveScreenShot(LPCTSTR pDestFile);


// direct3d.cpp

#include <d3dx9.h>
#include "direct3d.h"

m_pD3D = NULL;
m_pd3dDevice = NULL;

bool CDirect3D::Init(HWND hWnd, int width, int height, int bpp)

// Convert Bpp to a D3D format

D3DFORMAT d3dFormat = D3DFMT_R5G6B5;
if( bpp == 32 )
d3dFormat = D3DFMT_X8R8G8B8;

// Initialise Direct3D

m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);
if( !m_pD3D )
return false;

// Make sure the D3D format is valid

if( FAILED(m_pD3D->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, d3dFormat, d3dFormat, FALSE)) )
return false;

// Get the adapters capabilities

D3DCAPS9 d3dCaps;
m_pD3D->GetDeviceCaps(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, &d3dCaps);

// Test for hardware TnL


// Setup the device

d3dpp.BackBufferWidth = width;
d3dpp.BackBufferHeight = height;
d3dpp.BackBufferFormat = d3dFormat;
d3dpp.BackBufferCount = 1;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;
d3dpp.MultiSampleQuality = 0;
d3dpp.hDeviceWindow = hWnd;
d3dpp.Windowed = FALSE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.Flags = 0;
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;

// Create the device

m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, BehaviorFlags, &d3dpp, &m_pd3dDevice);
if( !m_pd3dDevice )
return false;

// Set the device viewport

D3DVIEWPORT9 d3dViewport = { 0, 0, width, height, 0.0f, 1.0f };

// Setup default lighting

m_pd3dDevice->SetRenderState(D3DRS_AMBIENT, 0x00202020);

return true;

void CDirect3D::Shutdown()
if( m_pD3D )
if( m_pd3dDevice )
m_pd3dDevice = NULL;

m_pD3D = NULL;

void CDirect3D::BeginScene(DWORD dwColor)
m_pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, dwColor, 1.0f, 0);

void CDirect3D::EndScene()
m_pd3dDevice->Present(NULL, NULL, NULL, NULL);

void CDirect3D::ResetWorldMatrix()
D3DXMATRIX matWorld; D3DXMatrixIdentity(&matWorld);
m_pd3dDevice->SetTransform(D3DTS_WORLD, &matWorld);

void CDirect3D::Begin3D()
//D3DXMATRIX matView;


/* Set the camera position, look at position, and up direction
D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(8.0f, 6.0f, -18.0f),
&D3DXVECTOR3(0.0f, 4.0f, 0.0f), &D3DXVECTOR3(0.0f, 1.0f, 0.0f));
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matView);

// Get the current viewport

D3DVIEWPORT9 d3dViewport;

// Set the fov, aspect ratio, and near and far clipping planes

D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, float(d3dViewport.Width/d3dViewport.Height), 1.0f, 1000.0f);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matProj);

// Make sure lighting and the z-buffer are enabled

m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);

void CDirect3D::Begin2D()
D3DXMATRIX matIdentity;
D3DXMATRIX matOrtho;

// Reset the world and view matrices

m_pd3dDevice->SetTransform(D3DTS_WORLD, &matIdentity);
m_pd3dDevice->SetTransform(D3DTS_VIEW, &matIdentity);

// Get the current viewport

D3DVIEWPORT9 d3dViewport;

// Setup the orthogonal projection matrix

D3DXMatrixOrthoLH(&matOrtho, (float)d3dViewport.Width, (float)d3dViewport.Height, 0.0f, 1.0f);
m_pd3dDevice->SetTransform(D3DTS_PROJECTION, &matOrtho);

// Make sure the z-buffer and lighting are disabled

m_pd3dDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
m_pd3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

void CDirect3D::SaveScreenShot(LPCTSTR pDestFile)
// Pointer to the memory location containing a copy of the front buffer

IDirect3DSurface9* pd3dsFront = NULL;

// Get the current viewport

D3DVIEWPORT9 d3dViewport;

// Create the surface that the screen shot will be copied into

m_pd3dDevice->CreateOffscreenPlainSurface(d3dViewport.Width, d3dViewport.Height, D3DFMT_A8R8G8B8, D3DPOOL_SYSTEMMEM, &pd3dsFront, NULL);

// Copy the front buffer to the image surface

if( FAILED( m_pd3dDevice->GetFrontBufferData(0, pd3dsFront) ) )

// Save screen shot to a bitmap file

D3DXSaveSurfaceToFile(pDestFile, D3DXIFF_BMP, pd3dsFront, NULL, NULL);

// Release the surface so there is no memory leak


Share this post

Link to post
Share on other sites

  • 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!