Sign in to follow this  
brandonman

UGPDX Demo 2

Recommended Posts

I'm working on the lines demo in DirectX, with Dev C++. I have the linker set up and all the code put in there. It compiles fine but when i run it, a console window, the one's that C++ programs run in normally, and no regular window with my lines comes up. It can't be my source code because I even took out mine and put in the book's source code from the cd. It still happened so i ctrl-Z'd back to mine. Here's my linker stuff:
-ld3d9 -ld3dx9
and here's the code:
#include<d3d9.h> /*this includes d3d9.h, it appears hat the forums can't take the less than and greater than symbols*/

#pragma comment(lib, "d3d(.lib")
#pragma comment(lib, "d3dx9.lib")

#define WINDOW_CLASS "UGPDX"
#define WINDOW_NAME "Drawing Lines"

//function prototypes
bool InitializeD3D(HWND hWnd, bool fullscreen);
bool InitializeObjects();
void RenderScene();
void Shutdown();

//direct3d object and device
LPDIRECT3D9 g_D3D = NULL;
LPDIRECT3DDEVICE9 g_D3DDevice = NULL;

//Vertex Buffer to hold the geometry
LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

//a structure for vertex type
struct stD3DVertex
{
   float x, y, z;
   unsigned long color;
};

//custom FVF, which describes custom vertex structure
#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)

LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
   switch(msg)
   {
      case WM_DESTROY:
           PostQuitMessage(0);
           return 0;
           break;
           
      case WM_KEYUP:
           if(wp == VK_ESCAPE) PostQuitMessage(0);
           break;
           }
   return DefWindowProc(hWnd, msg, wp, lp);
}

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst,
                   LPSTR cmdLine, int show)
                   
{
   //register window class
   WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc,
                     0, 0, GetModuleHandle(NULL), NULL, NULL,
                     NULL, NULL, WINDOW_CLASS, NULL };
   RegisterClassEx(&wc);
   
//create 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))
{
   //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 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_HARDWARE_VERTEXPROCESSING,
   &d3dpp, &g_D3DDevice))) return false;
   
//Initialize any objects we'll be displaying
if(!InitializeObjects()) return false;

return true;
}

bool InitializeObjects()
{
   unsigned long col = D3DCOLOR_XRGB(255, 255, 255);
   
   //fill in our structure to draw an object
   //x, y, z, color
   stD3DVertex objData[] =
   {
      { 420.0f, 150.0f, 0.5f, col, },
      { 420.0f, 350.0f, 0.5f, col, },
      { 220.0f, 150.0f, 0.5f, col, },
      { 220.0f, 350.0f, 0.5f, col, },
      };
      
//create the vertex buffer
if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 0,
   D3DFVF_VERTEX, D3DPOOL_DEFAULT, &g_VertexBuffer,
   NULL))) return false;
          
//fill the vertex buffer
void *ptr;

if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData),
   (void**)&ptr, 0))) return false;
   
   memcpy(ptr, objData, sizeof(objData));
   
   g_VertexBuffer->Unlock();
   
   return true;
}

void RenderScene()
{
   //clear backbuffer
   g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
                      D3DCOLOR_XRGB(0,0,0), 1.0f, 0);
                      
   //begin the scene and start rendering
   g_D3DDevice->BeginScene();
   
   //render object
   g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0,
                                sizeof(stD3DVertex));
   g_D3DDevice->SetFVF(D3DFVF_VERTEX);
   g_D3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, 2);
   //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();
   if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
   
   g_D3DDevice = NULL;
   g_D3D = NULL;
   g_VertexBuffer = NULL;
}
[/code]

I was thinking it's with the linker, but I don't get any linker errors. Also, here's the compile log:

Compiler: Default compiler
Building Makefile: "C:\Dev-Cpp\UGPDX\Makefile.win"
Executing  make...
make.exe -f "C:\Dev-Cpp\UGPDX\Makefile.win" all
make.exe: Nothing to be done for `all'.

Execution terminated
Compilation successful



[Edited by - brandonman on July 10, 2007 8:06:53 AM]

Share this post


Link to post
Share on other sites
a console or whatever it's called like what normal C++ programs run in pops up and dissapears, and the actual window with the lines and such, like most DirectX programs run in, doesn't come up. Say you had a simple C++ program that just used cout to print hi. No extra code except the main and includes. You would have a console window, or command prompt, forgot what they're called. That's what I get, and it closes as soon as it opens.

Share this post


Link to post
Share on other sites
Does that happen when you run the generated exe, or only when you run it through the IDE. There's nothing in your code that would create a console box, the only things I can think of is that it's your IDE doing something just before your app is launched (executing the app, probably), or it's some other software you have installed doing something (Like a debug viewer or D3D hooking software or whatever).

In eitehr case, if it doesn't happen when you run the app from the generated exe, it's nothing to worry about.

Share this post


Link to post
Share on other sites
Try running you app in the debugger and step through it to see where the console window opens. If it opens before you enter WinMain, then it's some setting in your IDE.

Share this post


Link to post
Share on other sites
Both of mine open up the console window at the LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp) part. The blank window from before keeps going after the return DefWindowProc(hWnd, msg, wParam, lParam); while the lines don't. Any ideas?
Edit: Just realized I said I was in Opengl in the post, I'm in DirectX, my mistake. So used to opengl.

Share this post


Link to post
Share on other sites
Can you zip up the project and post it anywhere perhaps? The only thing I can think of offhand is if either something else is tracking the launch of the application, or if your compiler settings are off. It may be a console application wrapped around the windows one (strange, but can happen, though I don't know of any compilers that would ever do this standard).

Share this post


Link to post
Share on other sites
Hey man!
In the create device call, the behavior flags parameter should be "D3DCREATE_SOFTWARE_VERTEXPROCESSING" instead of "D3DCREATE_HARDWARE_VERTEXPROCESSING"
I think that should help.
Anton

Share this post


Link to post
Share on other sites
That shouldn't affect the creation of the window though.

Are you sure you've created a Windows project? You may have clicked console and somehow the window gets created anyway, albeit briefly.
It's been awhile since I've used Dev-C++ (at least with Windows apps anyway) so I'm not 100% on that one.

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