# Internet Code Issues

10 replies to this topic

### #1gamedevnoob95

New Member

• Members
• 6 posts

Posted 21 March 2010 - 03:42 PM

So I apologize in advance for any inconveniences I may have immediately placed on whoever of you I have, but I'm having difficulties with some basic code with direct X. I'm trying to learn programming as the basic step into the world of computer game programming, I already know what I like to consider a fair amount of C++, but I'm sure I'm still high from mastry. But again, could you please see if you can tell me what the problem is with this code?

  //Every windows application needs to include this
#include "windows.h"
//Every Direct3D application this
#include "d3d9.h"

bool g_bContinue = true;

//Besides the main function, there must be a message processing function
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
g_bContinue = false;
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}

//The entry point of a windows application is the WinMain function
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
//Create a window class.
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"Direct3D Window", NULL };
//Register the window class.
RegisterClassEx( &wc );
//Create the application's window.
HWND hWnd = CreateWindow( "Direct3D Window", "DirectX Wiki - D3D9 Tutorial 1",
WS_OVERLAPPEDWINDOW, 100, 100, 400, 400,
GetDesktopWindow(), NULL, wc.hInstance, NULL );

ShowWindow(hWnd,SW_SHOW);

//Create the Direct3D Object
LPDIRECT3D9 pD3D = NULL;
if( NULL == (pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
return E_FAIL;
//Setup the device presentation parameters
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;

//The final step is to use the IDirect3D9::CreateDevice method to create the Direct3D device, as illustrated in the
//following code example.
LPDIRECT3DDEVICE9 pd3dDevice = NULL;
if( FAILED( pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &pd3dDevice ) ) )
{
pD3D->Release();
pD3D = NULL;
return E_FAIL;
}

MSG msg;
while( g_bContinue )
{
//Clear render region with blue
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
//before rendering something, you have to call this
pd3dDevice->BeginScene();
//rendering of scene objects happens here
//after the scene call
pd3dDevice->EndScene();
//Screen update
pd3dDevice->Present(NULL, NULL, NULL, NULL);

// A window has to handle its messages.
TranslateMessage( &msg );
DispatchMessage( &msg );
PeekMessage(&msg, 0, 0, 0, PM_REMOVE);
}

//Do not forget to clean up here
pd3dDevice->Release();
pd3dDevice = NULL;
pD3D->Release();
pD3D = NULL;

return 0;
}

I'm sorry again, but I may not know much, but I'm trying to learn.

### #2Reedbeta

DevMaster Staff

• 5310 posts
• LocationSanta Clara, CA

Posted 21 March 2010 - 06:11 PM

First of all, please use [code]...[/code] to post code on the forum, so it preserves its formatting.

Second, we can't read your mind. Precisely what "difficulties" are you having with this code?
reedbeta.com - developer blog, OpenGL demos, and other projects

### #3Nerd_Skywalker

Valued Member

• Members
• 215 posts

Posted 21 March 2010 - 06:40 PM

Reedbeta said:

We're working on that ;)

TheNut said:

"Hmm, yes. Strong is the force with this one"

### #4Kenneth Gorking

Senior Member

• Members
• 939 posts

Posted 21 March 2010 - 06:52 PM

My guess would be the 'd3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
' line is causing the device creation to fail. Try with D3DFMT_A8R8G8B8 instead.
"Stupid bug! You go squish now!!" - Homer Simpson

### #5gamedevnoob95

New Member

• Members
• 6 posts

Posted 21 March 2010 - 07:03 PM

Thanks I'll try that, I was just needing help on this so I could see the effects and kind of get an idea. I'm trying to figure out what everything is and how to use it to develop my game engine. I'm very new to this though.

I tried it however, and it unfortunately didn't work. Here's the error's it gave me upon trying to compile it;


1>h:\null\tweweqr\qwopeitheg.cpp(27) : error C2440: 'initializing' : cannot convert from 'const char [16]' to 'LPCWSTR'

1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

1>h:\null\tweweqr\qwopeitheg.cpp(33) : error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [16]' to 'LPCWSTR'

1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

1>Build log was saved at "file://h:\Null\Tweweqr\Debug\BuildLog.htm"

1>Tweweqr - 2 error(s), 0 warning(s)



### #6Wernaeh

Senior Member

• Members
• 368 posts

Posted 22 March 2010 - 01:36 AM

To me, this seems like an unicode-related problem:

You are providing the window class as a normal char* constant (1 byte per letter). However, for some reason, the CreateWindow #define and the WNDCLASS structure resolve to their unicode versions, and expect a wide-character input string (several bytes per letter).

Try using L"Direct3D Window" instead of "Direct3D Window" at both places - the leading L in front of a string constant tells the compiler this constant is in unicode, instead of normal 1-byte characters.

See this old flipcode article for a more detailed introduction.

I can't tell you why you are on UNICODE, though - if you are using Visual Studio, you should perhaps check with your project settings.

Hope this helps,
Cheers,
- Wernaeh
Some call me mathematician, some just call me computer guy. Yet, I prefer the term professional weirdo :)

### #7Reedbeta

DevMaster Staff

• 5310 posts
• LocationSanta Clara, CA

Posted 22 March 2010 - 01:47 AM

Newer Visual Studios default to unicode mode turned on for new projects, IIRC.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #8tuohirv

Member

• Members
• 33 posts

Posted 22 March 2010 - 05:53 AM

What Visual Studio you use? Get the newest express-version. Its free and very good.. And about your problem, Wernaeh is right. you should put L before both of those static strings.

Btw, i think unicode is the worst idea ever :)

### #9Reedbeta

DevMaster Staff

• 5310 posts
• LocationSanta Clara, CA

Posted 22 March 2010 - 06:00 AM

Well, Unicode itself isn't a bad idea...all symbols, languages, and whatnot under one roof and a few standardized encodings certainly beats the hodgepodge of encodings we had before. But I confess I don't understand why MS decided to make Windows UTF-16 native instead of UTF-8.
reedbeta.com - developer blog, OpenGL demos, and other projects

### #10tuohirv

Member

• Members
• 33 posts

Posted 22 March 2010 - 06:10 AM

Misunderstanding in terms: I have always called any 16-bit character format as "unicode" in my mind somehow. Maybe it comes from before any UTF formats when we were cursing with all the new stuff... UTF-8 is different story..:)

Sorry for offtopic

### #11.oisyn

DevMaster Staff

• Moderators
• 1842 posts

Posted 22 March 2010 - 10:58 AM

Reedbeta said:

Well, Unicode itself isn't a bad idea...all symbols, languages, and whatnot under one roof and a few standardized encodings certainly beats the hodgepodge of encodings we had before. But I confess I don't understand why MS decided to make Windows UTF-16 native instead of UTF-8.
Windows NT traditionally was UCS-2, which is just a fixed-width 16 bit character set. No encodings whatsoever, and enough to represent all the unicode characters of the time (in BMP 0). It wasn't untill 1996 that unicode 2.0 was introduced with extra characters and the UTF-16 format (which is backwards compatible with UCS-2). This was implemented in Windows 2000. So it just grew that way historically.