Sign in to follow this  
JoshuaGreen

Direct3D with C++ Help

Recommended Posts

JoshuaGreen    100
Im sorry if i put this in the wrong place anyways i was trying to make a window for D3D in Visual C++ and i kept gettin errors, ok so this i s what i did

1. i opened Windows visual C++ 2010 Express and created a empty project.
2. i added a new Item called C++ file and named it Main.cpp then added it to the source files.
3.i went to [url="http://www.directxtutorial.com/tutorial9/b-direct3dbasics/dx9B1.aspx#still"]http://www.directxtutorial.com/tutorial9/b-direct3dbasics/dx9B1.aspx#still[/url] and when to the bottom of the page and copied the source code.
4.pasted the copied source code into the main.cpp then clicked Start Debugging and waited.
5. it had errors and didnt work.

i dont know why it didnt work because i copied it from the website.
here's the source code containing the stuff ment for a blank window below.

// include the basic windows header files[b] and the Direct3D header file[/b]
#include <windows.h>
#include <windowsx.h>
[b]#include <d3d9.h>[/b]

[b]// include the Direct3D Library file
#pragma comment (lib, "d3d9.lib")[/b]

[b]// global declarations
LPDIRECT3D9 d3d; // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev; // the pointer to the device class[/b]

[b]// function prototypes
void initD3D(HWND hWnd); // sets up and initializes Direct3D
void render_frame(void); // renders a single frame
void cleanD3D(void); // closes Direct3D and releases memory[/b]

// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);


// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
L"WindowClass",
[b]L"Our First Direct3D Program",[/b]
WS_OVERLAPPEDWINDOW,
300, 300,
[b]800, 600,[/b]
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);

// set up and initialize Direct3D
[b]initD3D(hWnd);[/b]

// enter the main loop:

MSG msg;

while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

[b]render_frame();[/b]
}

// clean up DirectX and COM
[b]cleanD3D();[/b]

return msg.wParam;
}


// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

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


[b]// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION); // create the Direct3D interface

D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use
d3dpp.Windowed = TRUE; // program windowed, not fullscreen
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames
d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D
[/b]

[b] // create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
}[/b]


[b]// this is the function used to render a single frame
void render_frame(void)
{
// clear the window to a deep blue
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 40, 100), 1.0f, 0);

d3ddev->BeginScene(); // begins the 3D scene

// do 3D rendering on the back buffer here

d3ddev->EndScene(); // ends the 3D scene

d3ddev->Present(NULL, NULL, NULL, NULL); // displays the created frame on the screen
}[/b]


[b]// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
{
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}



ok and here's the errors i get when i try to build it.


[font="Consolas"][size="1"][font="Consolas"][size="1"]

[size="2"]1>------ Build started: Project: joshua, Configuration: Debug Win32 ------[/size]

[size="2"]1> main.cpp[/size]

[size="2"]1>c:\users\rudy\desktop\iinet invoice\howzer studios\game engine project\joshua\joshua\main.cpp(39): error C2440: '=' : cannot convert from 'const wchar_t [12]' to 'LPCSTR'[/size]

[size="2"]1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast[/size]

[size="2"]1>c:\users\rudy\desktop\iinet invoice\howzer studios\game engine project\joshua\joshua\main.cpp(52): error C2664: 'CreateWindowExA' : cannot convert parameter 2 from 'const wchar_t [12]' to 'LPCSTR'[/size]

[size="2"]1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast[/size]

[size="2"]========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


why is it saying that when i just copied and pasted it and is there a way to fix it??


many Thanks -josh :) :) [/size]

[/size][/font][/size][/font][/b]

Share this post


Link to post
Share on other sites
jyk    2094
It looks like the code you're using was intended for use in a project where Unicode is enabled. I can't remember exactly where it is, but somewhere in the project properties you can specify which character set is used; try changing this to 'Unicode' and see if that resolves the problem.

Share this post


Link to post
Share on other sites
ryan20fun    2635
[quote name='DJG2011' timestamp='1306611471' post='4816875']

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = L"WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
L"WindowClass",
[b]L"Our First Direct3D Program",[/b]
WS_OVERLAPPEDWINDOW,
300, 300,
[b]800, 600,[/b]
NULL,
NULL,
hInstance,
NULL);[b][font="Consolas"][size="1"][font="Consolas"][size="1"]

[/size][/font][/size][/font][/b]
[/quote]

try changeing that to CreateWindowExA / CreateWindowExW
or the unicode or multibyte version of that call

Share this post


Link to post
Share on other sites
Texus    248
You can also work with TCHAR instead of wchar_t or char.
Instead of writing L"something" or just "something" you write _T("something").

With doing this it doesn't matter whether you are working with UNICODE or not.
(You may have to include tchar.h or define _T(x) by yourself.)

Share this post


Link to post
Share on other sites
JoshuaGreen    100
[quote name='jyk' timestamp='1306615838' post='4816901']
It looks like the code you're using was intended for use in a project where Unicode is enabled. I can't remember exactly where it is, but somewhere in the project properties you can specify which character set is used; try changing this to 'Unicode' and see if that resolves the problem.
[/quote]


thanks it works now

Share this post


Link to post
Share on other sites

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

Sign in to follow this