Jump to content
  • Advertisement
Sign in to follow this  
walnut100

Is there something wrong with my window code?

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

Hey guys. I've been learning (slowly but surely) how to work with C++ and Direct3D9. I had a nice little 2D engine going with crude animation. Everything drew nicely at a smooth framerate. Then I decided to try to encapsulate my code to make it more reusable, and from there I've hit problems. I wrote a wrapper for Win32 using one of the articles on this site and some broken code from an almost finished wrapper posted in a blog on here. I fixed it up and got it to run just how I wanted it to. Then I started writing a wrapper around the trivial Direct3D setup, with my main goal for the time being to display a texture like on my old code. I got the device to set up, and I could clear the screen to a color of my choice, but I couldn't get the vertices or the texture to show up on screen for some reason. I quadruple checked my code against the old code and it looked like, Direct3D-wise, I was doing everything right. Which leads me to believe that I might have something wrong with my window code. Again, I can't get textures to display for some reason, and I can't for the life of me figure out why. The only code I have on this computer is my window code, so I'll post that, and if you guys need to see what else I had I'll be more than happy to put it up later on. Also, this code gives me some errors with longs with the W64 switch on. Thank you very much, I'm hoping I didn't confuse any of you with how wordy I am. Window.h
#ifndef WINDOW_H
#define WINDOW_H

#include <map>

class Window;

typedef long (* tyMessageHandler)(Window &, HWND, long, long);
typedef std::map<long, tyMessageHandler> tyMessageMap;
typedef tyMessageMap::iterator tyMessageIterator;

class Window
{
public:
	Window(HINSTANCE hInstance);
	~Window();

	HWND Create(DWORD style, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, int dimScreenWidth, int dimScreenHeight);
	long Show(int CmdShow) const;

	tyMessageHandler GetMessageHandler(long message);
	tyMessageHandler RegisterMessageHandler(long message, tyMessageHandler handler);

	void SetExit(bool exitState);
	bool IsExit() const;

	void SetHWND(HWND hwnd);
	HWND GetHWND() const;

	void SetHINSTANCE(HINSTANCE hInstance);
	HINSTANCE GetHINSTANCE() const;

	bool HandleMessages();

	static LRESULT CALLBACK StaticWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
	static long OnClose(Window &wnd, HWND hwnd, long param0, long param1);
	static long OnDestroy(Window &wnd, HWND hwnd, long param0, long param1);

private:
	tyMessageMap m_MsgHandlers;
	
	HWND m_hwnd;
	HINSTANCE m_hInstance;
	bool m_Exit;
};

#endif // WINDOW_H
window.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <cassert>
#include "Window.h"

Window::Window(HINSTANCE hInstance) : m_hInstance(hInstance), m_Exit(false), m_hwnd(NULL)
{
	RegisterMessageHandler(WM_CLOSE, OnClose);
	RegisterMessageHandler(WM_DESTROY, OnDestroy);
}

Window::~Window()
{
}

HWND Window::Create(DWORD style, LPCTSTR lpszClassName, LPCTSTR lpszWindowName, int dimScreenWidth, int dimScreenHeight)
{
	WNDCLASSEX  wcx;
	HWND window = NULL;
	ZeroMemory(&wcx, sizeof(wcx));

	wcx.cbSize = sizeof(WNDCLASSEX);
	wcx.style = CS_HREDRAW | CS_VREDRAW;
	wcx.lpfnWndProc = StaticWindowProc;
	wcx.cbClsExtra = 0;
    wcx.cbWndExtra = 0;
	wcx.hInstance = GetHINSTANCE();
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wcx.lpszMenuName = NULL;
	wcx.lpszClassName = lpszClassName;
	wcx.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

	if (!RegisterClassEx(&wcx)) { return false; }

	window = CreateWindowEx(
		0,	// Extended style
		lpszClassName,
		lpszWindowName,
		style,	// window style
		(GetSystemMetrics(SM_CXSCREEN) / 4), (GetSystemMetrics(SM_CYSCREEN) / 4), // x, y
		dimScreenWidth, dimScreenHeight,	// width, height
		NULL,	// Parent window
		NULL,	// Menu
		GetHINSTANCE(),
		(LPVOID)this);

	return window;
}

long Window::Show(int CmdShow) const
{
	return ShowWindow(GetHWND(), CmdShow);
}

tyMessageHandler Window::GetMessageHandler(long message)
{
	tyMessageIterator iter = m_MsgHandlers.find(message);
	if (iter == m_MsgHandlers.end())
	{
		return NULL;
	}
	return iter->second;
}

tyMessageHandler Window::RegisterMessageHandler(long message, tyMessageHandler handler)
{
	tyMessageHandler m = NULL;
	tyMessageIterator iter = m_MsgHandlers.find(message);

	if (iter != m_MsgHandlers.end())
	{
		m = iter->second;
	}

	m_MsgHandlers.insert(std::pair<long, tyMessageHandler>(message, handler));
	return m;
}

void Window::SetExit(bool exitState)
{
	m_Exit = exitState;
}
bool Window::IsExit() const
{
	return !m_Exit;
}

void Window::SetHWND(HWND hwnd)
{
	m_hwnd = hwnd;
}

HWND Window::GetHWND() const
{
	return m_hwnd;
}

void Window::SetHINSTANCE(HINSTANCE hInstance)
{
	m_hInstance = hInstance;
}

HINSTANCE Window::GetHINSTANCE() const
{
	return m_hInstance;
}

bool Window::HandleMessages()
{
	static MSG msg;

	if (!GetHWND())
	{
		throw std::runtime_error(std::string("Window not yet created"));
	}
	if (PeekMessage(&msg, GetHWND(), 0, 0, PM_REMOVE))
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	else
	{
		return IsExit();
	}

	return true;
}

LRESULT CALLBACK Window::StaticWindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	Window *wnd  = 0;

	if (message == WM_NCCREATE)
	{
		CREATESTRUCT *cs = reinterpret_cast<CREATESTRUCT *>(lParam);
		wnd = reinterpret_cast<Window *>(cs->lpCreateParams);

		::SetWindowLong(hwnd, GWLP_USERDATA, reinterpret_cast<long>(wnd));

		wnd->SetHWND(hwnd);
	}
	else
	{
		wnd = reinterpret_cast<Window *>(::GetWindowLong(hwnd, GWL_USERDATA));
	}

	if (wnd)
	{
		tyMessageHandler handler = wnd->GetMessageHandler(message);
		if(handler != NULL)
		{
			return handler(*wnd, hwnd, (long)wParam, (long)lParam);
		}
	}

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

long Window::OnClose(Window &wnd, HWND hwnd, long param0, long param1)
{
	(void)wnd; (void)param0; (void)param1;
	DestroyWindow(hwnd);

	return 0;
}

long Window::OnDestroy(Window &wnd, HWND hwnd, long param0, long param1)
{
	(void)hwnd; (void)param0; (void)param1;
	PostQuitMessage(0);
	wnd.SetExit(true);

	return 0;
}
main.cpp
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include "Window.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	Window *wnd = new Window(hInstance);

	wnd->Create(WS_OVERLAPPEDWINDOW|WS_VISIBLE, "TreeClass", "*tree*", 640,480);
	wnd->Show(nCmdShow);
	while (true)
	{
		if (!wnd->HandleMessages())
		{
			break;
		}
	}

	return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by walnut100
..I got the device to set up, and I could clear the screen to a color of my choice, but I couldn't get the vertices or the texture to show up on screen for some reason...


To me that strongly suggests that your Window code is fine and that something is amiss in your Direct3D code. It seems very unlikely to me that IDirect3DDevice9::Clear() would work and primitive drawing calls fail due to problems with the Windows code.

Could be wrong though.

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!