Jump to content
  • Advertisement
Sign in to follow this  
Squigglyo

Need help with buttons and directx

This topic is 2614 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 have spent an entire day trying to figure this stuff out.

I am simply trying to create a window and have a button on it.

The purpose of this form will be to have some directx objects drawn on it.

All that i end up with is a white screen that is going over the top of my button, the button is still there (you can see it when you click its location) but the damn white screen is covering it.

If someone could help me you'll save me another day of frustration.



#include "Renderer.h"


#include <d3d9.h>
#include <d3dx9.h>

//
// Constructor
//
Renderer::Renderer(IDirect3DDevice9* a_pDevice)
: m_debugLineCount(0)
, m_pDebugLineData(NULL)
, m_pDebugLineVertexBuf(NULL)
{
// Create a data buffer for debug line vertices
const int debugLineVertCount = MAX_DEBUG_LINES * 2; // *2 = two points per line
m_pDebugLineData = new DebugLineVertexData[debugLineVertCount];

// Create a vertex buffer for debug lines
const int vertexBufferSizeBytes = sizeof(DebugLineVertexData) * debugLineVertCount;
a_pDevice->CreateVertexBuffer(vertexBufferSizeBytes, 0, DEBUG_LINE_FVF,
D3DPOOL_DEFAULT, &m_pDebugLineVertexBuf, NULL);
// Assume it works; if it fails, m_pDebugLineVertexBuf will just be NULL
}

//
// Destructor
//
Renderer::~Renderer()
{
if (m_pDebugLineData)
{
delete[] m_pDebugLineData;
m_pDebugLineData = NULL;
}

if (m_pDebugLineVertexBuf)
{
m_pDebugLineVertexBuf->Release();
m_pDebugLineVertexBuf = NULL;
}
}



//
// Renders all objects
// Returns true if successful
//
bool Renderer::Render(float a_dt, IDirect3DDevice9* a_pDevice)
{
if (!a_pDevice)
{
return false;
}

// Clear backbuffer
a_pDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);

// Create our camera (view-space) transform for this frame
D3DXMATRIX worldToView;
D3DXVECTOR3 lookFromPos(0, 0, -10);
D3DXVECTOR3 lookAtPos(0, 0, 0);
D3DXVECTOR3 upVec(0, 1, 0);
D3DXMatrixLookAtLH(&worldToView, &lookFromPos, &lookAtPos, &upVec);
a_pDevice->SetTransform(D3DTS_VIEW, &worldToView);

// Create our projection matrix for this frame
D3DXMATRIX viewToScreen;
D3DXMatrixPerspectiveFovLH(&viewToScreen, 3.14f / 4, 1, 1, 5000);
a_pDevice->SetTransform(D3DTS_PROJECTION, &viewToScreen);

// Set up common render states
a_pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
a_pDevice->SetRenderState(D3DRS_ZFUNC, D3DCMP_LESS);
a_pDevice->SetRenderState(D3DRS_LIGHTING, TRUE);
a_pDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);
a_pDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(0,0,0));

// Begin rendering
a_pDevice->BeginScene();

// Finish rendering
a_pDevice->EndScene();

// Display the backbuffer
a_pDevice->Present(NULL, NULL, NULL, NULL);

return true;
}





#include "DXMgr.h"

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <d3d9.h>
#include <d3dx9.h>
#pragma comment(lib, "d3d9")
#pragma comment(lib, "d3dx9")

// Singleton instance
static DXMgr* s_pInst = NULL;

//
// Gets the singleton instance
//
DXMgr& DXMgr::GetInstance()
{
if (!s_pInst)
{
s_pInst = new DXMgr();
}
return *s_pInst;
}

//
// Constructor
//
DXMgr::DXMgr()
: m_pD3D(NULL)
, m_pDevice(NULL)
{
}

//
// Destructor
//
DXMgr::~DXMgr()
{
}

//
// Initialisation
//
bool DXMgr::Init(HWND a_hWnd)
{
// Initialise Direct3D

m_pD3D = Direct3DCreate9(D3D_SDK_VERSION);

// Initialise presentation parameters for our new device
D3DPRESENT_PARAMETERS presentParams;
ZeroMemory(&presentParams, sizeof(presentParams));
presentParams.Windowed = TRUE;
presentParams.SwapEffect = D3DSWAPEFFECT_FLIP;
presentParams.hDeviceWindow = a_hWnd;
presentParams.EnableAutoDepthStencil = TRUE;
presentParams.AutoDepthStencilFormat = D3DFMT_D24S8;

// Create a device
HRESULT result = m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, a_hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED, &presentParams, &m_pDevice);
if (FAILED(result))
{
// Failed to create device, undo initialisation of D3D
m_pD3D->Release();
m_pD3D = NULL;
return false;
}

return true;
}

//
// Shutdown
//
void DXMgr::Shutdown()
{
// Destroy device
if (m_pDevice)
{
m_pDevice->Release();
m_pDevice = NULL;
}

// Destroy Direct3D
if (m_pD3D)
{
m_pD3D->Release();
m_pD3D = NULL;
}
}

//
// Returns the Direct3D interface
//
IDirect3D9* DXMgr::GetD3D() const
{
return m_pD3D;
}

//
// Returns the Direct3D device
//
IDirect3DDevice9* DXMgr::GetDevice() const
{
return m_pDevice;
}





#include "Renderer.h"
#include "DXMgr.h"
#include <time.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <iostream>
#include <sstream>
#include <string>
#include <stdlib.h>
#include <tchar.h>
#include <vector>
using namespace std;

#define WIN32_LEAN_AND_MEAN //changes how windows.h compiles
#include <windows.h>
#include <windowsx.h>


//////////////////////////////////////////////////////////////////////////
// Static variables
//////////////////////////////////////////////////////////////////////////
const char g_className[] = "myWindowClass";

//////////////////////////////////////////////////////////////////////////
// Forward declarations
//////////////////////////////////////////////////////////////////////////
LRESULT CALLBACK WndProc(HWND f_hwnd, UINT f_msg, WPARAM f_wParam, LPARAM f_lParam);


//////////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////////

//
// Program entry point
//

HWND hWnd2;

int WINAPI WinMain(HINSTANCE f_hInstance, HINSTANCE f_prevHInstance, LPSTR f_cmdLine, int f_cmdShow)
{
const char* WINDOW_CLASS_NAME = "Assignment 1 - By Scott";
Renderer *scene;

HWND hWnd;
//Register the Window Class
WNDCLASS wndClass;
wndClass.cbClsExtra = NULL;
wndClass.cbWndExtra = NULL;
wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndClass.hInstance = f_hInstance;
wndClass.lpfnWndProc = WndProc;
wndClass.lpszClassName = WINDOW_CLASS_NAME;
wndClass.lpszMenuName = NULL;
wndClass.style = CS_CLASSDC;


if (!RegisterClass(&wndClass))
{
//The register failed.
MessageBox(NULL, "Failed to Register!", "Failed to Launch!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}

// Create a window using the window class
hWnd = CreateWindow(
WINDOW_CLASS_NAME,
"Assignment 1: Scott",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU | WS_VISIBLE,
CW_USEDEFAULT,
CW_USEDEFAULT,
750,
750,
NULL,
NULL,
f_hInstance,
0);

if (hWnd == NULL)
{
//The window failed to be created
MessageBox(NULL, "Failed to Create Window!", "Failed To Launch!", MB_OK);
return 0;
}

ShowWindow(hWnd, f_cmdShow); // Set window as visible
UpdateWindow(hWnd); // Draw the window

DXMgr::GetInstance().Init(hWnd);
scene = new Renderer(DXMgr::GetInstance().GetDevice());



bool hasQuit = false;
float lastTime = 0;


bool showing = false;
while (!hasQuit)
{
// Process all messages queued up for our window
MSG msg;
while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg); // Let Windows perform some processing on our msg
DispatchMessage(&msg); // Send the message to our window procedure

if (msg.message == WM_QUIT)
{
hasQuit = true;
}
}


//a timer class, simply calculates a delta time
float curTime = timer->GetTime();
float dt = curTime - lastTime;

scene->Render(dt, DXMgr::GetInstance().GetDevice());

lastTime = curTime;

}

// Clean up Direct3D

DXMgr::GetInstance().Shutdown();

return 0;
}





//
// Window procedure (handles window messages)
//
LRESULT CALLBACK WndProc(HWND f_hWnd, UINT f_msg, WPARAM f_wParam, LPARAM f_lParam)
{
int x = 0;
switch (f_msg)
{
case WM_LBUTTONDOWN:
//MessageBox(f_hWnd,"Hello","Hello",0);
break;
case WM_CREATE:
CreateWindow( "button", "BUTTON!",
WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON,
10,10,100,30, f_hWnd,
(HMENU) 1,
NULL, NULL );
break;

case WM_CLOSE:
case WM_DESTROY:
PostQuitMessage(0);
return 0;

default:
break;
}

return DefWindowProc(f_hWnd, f_msg, f_wParam, f_lParam);
}



That is the trimmed down version of my code, i have simply removed all of the stuff unnecessary to read.
It looks like the problem may lie in the
a_pDevice->Present(NULL, NULL, NULL, NULL);
located in the renderer. If i comment it out the button shows, but the program is extremely slow if i add any of the objects i am going to be adding such as LPD3DXFONT text.

Share this post


Link to post
Share on other sites
Advertisement
What were you expecting the behavior to be? When you create a Direct3D device with a window handle to a window, it will render over the top of the form's airspace. In this case, you are rendering the cleared (to white) back buffer to your window space. Were you hoping to have your button on top of the white space? If so, you are going to have to be sure the button is always painted last in the paint order, and you will not be able to use your render loop for rendering the scene. My personal suggestion is that you do not try and overlay Direct3D render areas with windows GUI elements.

Share this post


Link to post
Share on other sites
I agree with the above post, if you want GUI type elements such as a button. You can either make your own in a variety of ways or use a GUI system such as CEGUI depending on the scope of the project.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!