• Advertisement
Sign in to follow this  
  • entries
    67
  • comments
    82
  • views
    36962

Helloworld version of D3D :)

Sign in to follow this  

117 views

Here is some C++ Code I wrote I added some comments so people understand the buildup of the program,what this shows is nothing more then a blank screen but this is one of the many buildups people can use:



#include

//adds the library files required for compiling
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS "Wijnand"
#define WINDOW_NAME "Blank D3D Window"


// Function Prototypes here you create your "functions/Methods(java) in C++
bool InitializeD3D(HWND hWnd, bool fullscreen);
void RenderScene();
void Shutdown();


// Direct3D object and device.
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;


// Windows API Message loop that enables the developer to assign specific keys to a command.
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
break;

case WM_KEYUP:
if(wParam == VK_ESCAPE) PostQuitMessage(0);
break;
}

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


// Declare the Winmain (main in normal C++)
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
{
// Register the window class see WINAPI
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
WINDOW_CLASS, NULL };
RegisterClassEx(&wc);

// Create the application's window
HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, 640, 480, GetDesktopWindow(), NULL,
wc.hInstance, NULL);

// Initialize Direct3D
if(InitializeD3D(hWnd, false)) //check if D3D is initialized
{
// Show the window
ShowWindow(hWnd, SW_SHOWDEFAULT);
UpdateWindow(hWnd);

// Enter the message loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));

while(msg.message != WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderScene();
}
}

// Release any and all resources.
Shutdown();

// Unregister our window.
UnregisterClass(WINDOW_CLASS, wc.hInstance);
return 0;
}


bool InitializeD3D(HWND hWnd, bool fullscreen)
{
D3DDISPLAYMODE displayMode;

// Create the D3D object.
g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
if(g_D3D == NULL) return false;

// Get the desktop display mode.
if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
return false;

// Set up the structure used to create the D3DDevice
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));

if(fullscreen)
{
d3dpp.Windowed = FALSE;
d3dpp.BackBufferWidth = 640;
d3dpp.BackBufferHeight = 480;
}
else
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = displayMode.Format;

// Create the D3DDevice
if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_D3DDevice)))
{
return false;
}

return true;
}


void RenderScene()
{
// Clear the backbuffer.
g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

// Begin the scene. Start rendering.
g_D3DDevice->BeginScene();

// End the scene. Stop rendering.
g_D3DDevice->EndScene();

// Display the scene.
g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}


void Shutdown()
{
if(g_D3DDevice != NULL) g_D3DDevice->Release();
if(g_D3D != NULL) g_D3D->Release();
}



Sign in to follow this  


2 Comments


Recommended Comments

You can put code between [source][/source] tags, makes it a lot easier to read.

Also if you want the backbuffer to actually be 640x480 you might want to call AdjustWindowRect. From my days of Win32 I think setting the window size to 640x480 means the whole damn window, borders plus menus and title bar. You want the client area to be 640x480. This could of course be wrong since I haven't touched Win32 in years, take a screenshot and check the size.

Share this comment


Link to comment
Quote:
Original post by Scet
Also if you want the backbuffer to actually be 640x480 you might want to call AdjustWindowRect. From my days of Win32 I think setting the window size to 640x480 means the whole damn window, borders plus menus and title bar. You want the client area to be 640x480. This could of course be wrong since I haven't touched Win32 in years, take a screenshot and check the size.


This is correct, the actual function I use is AdjustWindowRectEx, and is used like so:


DWORD dwExStyle;
DWORD dwStyle;
RECT WindowRect;
WindowRect.left = (long)0;
WindowRect.right = (long)640;
WindowRect.top = (long)0;
WindowRect.bottom=(long)480;

dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
dwStyle = WS_OVERLAPPEDWINDOW;

AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);





It is called just after you call RegisterClass(...). You can then replace your

HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
100, 100, 640, 480, GetDesktopWindow(), NULL,
wc.hInstance, NULL);

with

HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
WindowRect.left, WindowRect.top, WindowRect.right, WindowRect.bottom, GetDesktopWindow(), NULL,
wc.hInstance, NULL);


I think [smile].

Share this comment


Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement