Jump to content
  • Advertisement
Sign in to follow this  

Issues with Direct3D (c++)

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

I want to start out by saying that this is a completely scratched program. The program was previously coded using IDirect3DSurface9 for sprites. So I understand there is a much better way of coding this, with modules and such ;) I just took 1 sprite to render to get the system to work before I implemented it into the other program. That said, I'm trying to use the Draw() function and it seems to be having some problems. If someone could point me in a direction here, it would be greatly appreciated. It seems to be a problem in rendering the sprite/texture, in my main loop. I think I have most of the other stuff correctly in place, but I may be COMPLETELY off all together. Thanks in advance. #include <windows.h> #include <d3d9.h> #include <d3dx9tex.h> #include <string> using namespace std; HINSTANCE hInst; // holds the instance for this app HWND wndHandle; // global window handle LPDIRECT3D9 pD3D; // Object LPDIRECT3DDEVICE9 pd3dDevice; //Device LPDIRECT3DTEXTURE9 g_ManTexture; LPD3DXSPRITE g_ManSprite; ////////////////////////////////////////////// forward declarations bool initWindow(HINSTANCE hInstance); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // direct3D functions bool initDirect3D(HWND hwnd); bool setupSprites(void); void shutdown(void); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { // call our function to init and create our window if (!initWindow(hInstance)) { MessageBox(NULL, "Unable to create window", "ERROR", MB_OK); return false; } // init direct3d initDirect3D(wndHandle); // Main message loop: // Enter the message loop MSG msg; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { // check for messages if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } // this is called when no messages are pending else { RECT manRect; manRect.top = 0; manRect.bottom = manRect.top + 41; manRect.left = 0; manRect.right = manRect.left + 39; D3DXVECTOR3 vec(100.0, 100.0, 0.0); D3DXVECTOR3 center(0.0, 0.0, 0.0); pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_COLORVALUE(0.35f, 0.53f, 0.7, 1.0f), 1.0f, 0 ); pd3dDevice->BeginScene(); pd3dDevice->SetTexture( 0, g_ManTexture); g_ManSprite->Begin(D3DXSPRITE_ALPHABLEND); g_ManSprite->Draw(g_ManTexture,&manRect,¢er,&vec,0xFFFFFFFF); g_ManSprite->End(); pd3dDevice->EndScene(); pd3dDevice->Present( NULL, NULL, NULL, NULL );; } } // shutdown the directx manager shutdown(); return (int) msg.wParam; } bool initDirect3D(HWND hwnd) { HRESULT hr; if( NULL == ( pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { return false; } D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; d3dpp.BackBufferCount = 1; d3dpp.BackBufferHeight = 480; d3dpp.BackBufferWidth = 640; d3dpp.hDeviceWindow = hwnd; hr = pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice ); if FAILED (hr) { return false; } return true; } void shutdown(void) { if( g_ManSprite != NULL ) g_ManSprite->Release(); if( pd3dDevice != NULL) { pd3dDevice->Release(); pd3dDevice = NULL; } if( pD3D != NULL) { pD3D->Release(); pD3D = NULL; } } bool initWindow(HINSTANCE hInstance) { WNDCLASSEX wcex; wcex.cbSize = sizeof(WNDCLASSEX); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = (WNDPROC)WndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.hInstance = hInstance; wcex.hIcon = 0; wcex.hCursor = LoadCursor(NULL, IDC_ARROW); wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); wcex.lpszMenuName = NULL; wcex.lpszClassName = "DirectXExample"; wcex.hIconSm = 0; RegisterClassEx(&wcex); wndHandle = CreateWindow("DirectXExample", "DirectXExample", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL, hInstance, NULL); if (!wndHandle) return false; ShowWindow(wndHandle, SW_SHOW); UpdateWindow(wndHandle); return true; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { switch (message) { case WM_DESTROY: PostQuitMessage(0); break; } return DefWindowProc(hWnd, message, wParam, lParam); } bool setupSprites(void) { D3DXCreateTextureFromFileA(pd3dDevice, "littledude.bmp", &g_ManTexture); if (g_ManTexture == NULL) { MessageBox(NULL, "Unable to ManTexture", "ERROR", MB_OK); return false; } }

Share this post

Link to post
Share on other sites
Are you checking to see if you've successfully created your Direct3D device? :

// init direct3d
if ( initDirect3D(wndHandle) )
MessageBox( 0, "initDirect3D() failure.", "Error!1", MB_OK );
return -1;

Also, surround your code with [source][/source] to make your code legible.

Share this post

Link to post
Share on other sites
Thanks guys, I actually solved my issue, I am now able to have my sprites draw correctly and so on.

The issue I'm seeing now though, is that when I call a function to draw multiple of the same sprite, they are flashing around the screen, as if it is drawing them correctly, but constantly refreshing them. Like it is clearing them out and redrawing them constantly, which is why it seems like they are flashing.

Here is my code for drawing the sprites

void render(int whichBalloon)
//Balloon[whichBalloon].srcRect.top = 0;
//Balloon[whichBalloon].srcRect.bottom = Balloon[whichBalloon].srcRect.top + 95;
//Balloon[whichBalloon].srcRect.left = 0;
//Balloon[whichBalloon].srcRect.right = Balloon[whichBalloon].srcRect.left + 54;

FallingMan.srcRect.top = 0;
FallingMan.srcRect.bottom = FallingMan.srcRect.top + 41;
FallingMan.srcRect.left = 0;
FallingMan.srcRect.right = FallingMan.srcRect.left + 39;

if (Balloon[whichBalloon].Y < 0)
Balloon[whichBalloon].X = int(rand()%600);
Balloon[whichBalloon].Y = 370;

D3DXVECTOR3 balloonVec(Balloon[whichBalloon].X, Balloon[whichBalloon].Y, 0.0);

D3DXVECTOR3 vec(FallingMan.x, FallingMan.y, 0.0);

D3DXVECTOR3 backgroundPosition(0.0, 0.0, 0.0);

D3DCOLOR_COLORVALUE(0.35f, 0.53f, 0.7, 1.0f), 1.0f, 0 );
pd3dDevice->Present( NULL, NULL, NULL, NULL );

In this function, I send it whichBalloon which is what balloon it is, out of an area of them (I send the function the element). In My initSprites area I start each of the balloons off with a random starting position, this one just draws them, and updates them if they hit the top of the screen. I'm looking it over, but would appreciate any insight you guys have as to why I get that "Flashing" effect.

Share this post

Link to post
Share on other sites
On an update, I've went through the code to try and cut out anything that may clear the screen, but it seems to be only the balloon sprite that is having this issue. Could it possibly that I'm drawing more than one of it, and there is a better way to handle that?

Share this post

Link to post
Share on other sites
Sign in to follow this  

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