⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 window.cpp

📁 小型的3D游戏引擎
💻 CPP
字号:
#include "window.h"

//////////////////////////////////////////////////////////////////////////////////////

Win32Window::Win32Window()
{
	m_hwnd = NULL;
	m_hinst = NULL;
	m_menu = NULL;
	m_active = false;
	m_name = NULL;
	m_displayMode = WINDOW;

	m_width = DEFAULT_WIDTH;
	m_height = DEFAULT_HEIGHT;
	m_colorDepth = DEFAULT_DEPTH;
}

//////////////////////////////////////////////////////////////////////////////////////

void Win32Window::SetName( char * name )
{
	// Is a name set already?
	if( m_name )
	{
		delete [] m_name;
		m_name = NULL;
	}
	
	if( (m_name = new char[strlen(name) + 1]) != NULL )
		strcpy(m_name, name);
	
}

//////////////////////////////////////////////////////////////////////////////////////

Win32Window::~Win32Window()
{
	if( m_name )
	{
		delete [] m_name;
		m_name = NULL;
	}
}   

//////////////////////////////////////////////////////////////////////////////////////

HWND Win32Window::InitPopup( WNDPROC wndProc, HINSTANCE instance, int initShow, int width, int height, int colorDepth )
{
	WNDCLASSEX  wcx;			// The windows extended class
	DWORD		dwExStyle;		// Window extended style
	DWORD		dwStyle;		// Window style
	RECT		windowRect;

	// Set the window rect
	windowRect.left = (long)0;
	windowRect.right = (long)width;
	windowRect.top = (long)0;
	windowRect.bottom = (long)height;

	// Save the instance and the window proc
	m_hinst = instance;
	m_wndProc = wndProc;

	wcx.cbSize = sizeof(WNDCLASSEX);								// Set the size of the structure
	wcx.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;	// Class style
	wcx.lpfnWndProc = m_wndProc;									// Window procedure
	wcx.cbClsExtra = 0;												// Class extra
	wcx.cbWndExtra = 0;												// Window extra
	wcx.hInstance = m_hinst;										// Win32Window handle
	wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION);					// Icon
	wcx.hCursor = LoadCursor(NULL, IDC_ARROW);						// Cursor
	wcx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);		// Background color
	wcx.lpszMenuName = NULL;										// Menu
	wcx.lpszClassName = m_name;										// Class name
	wcx.hIconSm = NULL;												// Small icon

	// Register the window class, return 0 if not successful
	if( !RegisterClassEx(&wcx) )
		return NULL;

	// Set fullscreen if wanted


	if( m_displayMode == FULLSCREEN ) 
	{
		dwExStyle = WS_EX_APPWINDOW;
		dwStyle = WS_POPUP;

		::ShowCursor(false);
	}
	else 
	{
		dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
		dwStyle = WS_OVERLAPPEDWINDOW;

		::ShowCursor(false);

		AdjustWindowRectEx(&windowRect, dwStyle, false, dwExStyle);
	}

	// Create main window
	m_hwnd = CreateWindowEx(NULL, m_name, m_name,
							dwStyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
							0, 0,
							windowRect.right - windowRect.left,
							windowRect.bottom - windowRect.top,
							NULL, NULL,
							m_hinst,
							NULL);



	// Error check
	if(!m_hwnd) {
		return NULL;
	}


	SetForegroundWindow(m_hwnd);
	//SetFocus(m_hwnd);	
	// Set the window to visible/invisible
	//ShowWindow(m_hwnd, initShow);
	ShowWindow(m_hwnd, SW_SHOWNORMAL);
	
	UpdateWindow(m_hwnd);
	Center(width, height);

	memset(&m_original, 0, sizeof(DEVMODE));
	EnumDisplaySettings(NULL, ENUM_CURRENT_SETTINGS, &m_original);
  
	return m_hwnd;
}

//////////////////////////////////////////////////////////////////////////////////////

void Win32Window::Show()
{
	ShowWindow(m_hwnd, SW_SHOWNORMAL);
}

//////////////////////////////////////////////////////////////////////////////////////

void Win32Window::Hide()
{
	ShowWindow(m_hwnd, SW_HIDE);
}

//////////////////////////////////////////////////////////////////////////////////////

void Win32Window::Close()
{
	// Destroy the window
	if( m_hwnd && !DestroyWindow(m_hwnd) )
	{
		MessageBox(NULL, "Couldn't release window handle!", "ERROR", MB_OK | MB_ICONERROR);
		m_hwnd = NULL;
	}

	// Unreg the window class
	if( !UnregisterClass(m_name, m_hinst) )
	{
		MessageBox(NULL, "Couldn't unregister the window class!", "ERROR!", MB_OK | MB_ICONERROR);
		m_hinst = NULL;
	}

	//::SendMessage(m_hwnd, WM_CLOSE, 0, 0);// <-- Don't show the error if there is one
}

//////////////////////////////////////////////////////////////////////////////////////

void Win32Window::Center(int width, int height)
{   
	RECT rc = {0, 0, width, height};
	int centerX;
	int centerY;
  
	/* Adjust that to a size that includes the border, etc. */
	AdjustWindowRectEx(&rc, WS_OVERLAPPEDWINDOW, GetMenu (m_hwnd) != NULL, 0);  /* Extended style of the main window */
  
	centerX = (GetSystemMetrics(SM_CXFULLSCREEN) / 2) - (abs (rc.left) + rc.right) / 2; 
	centerY = (GetSystemMetrics(SM_CYFULLSCREEN) / 2) - (abs (rc.top) + rc.bottom) / 2; 
	//centerX = width
  
	MoveWindow(m_hwnd, centerX, centerY, (rc.right - rc.left), (rc.bottom - rc.top), TRUE);
	//MoveWindow(m_hwnd, 300, 300, 640, 480, TRUE);
}

//////////////////////////////////////////////////////////////////////////////////////

bool Win32Window::SetFullscreenMode( int width, int height, int bpp )
{
	// Clear the memory
	memset(&m_screen, 0, sizeof(DEVMODE));

	// Set the device mode
	m_screen.dmSize = sizeof(DEVMODE);
	m_screen.dmPelsWidth = width;
	m_screen.dmPelsHeight = height;
	m_screen.dmBitsPerPel = bpp;
	m_screen.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

	// Change the screen resolution
	if( ChangeDisplaySettings(&m_screen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL )
	{
		// Failed to switch to fullscreen mode
		MessageBox(NULL, "Display mode failed", NULL, MB_OK);

		return false;
	}

	m_displayMode = FULLSCREEN;

	return true;
}

//////////////////////////////////////////////////////////////////////////////////////

bool Win32Window::SetWindowMode( int x, int y, int bpp )
{
/*	if( !m_fullscreen )	
		return false;

	// Clear the memory
	memset(&m_screen, 0, sizeof(DEVMODE));

	// Set the device mode
	m_screen.dmSize = sizeof(DEVMODE);
	m_screen.dmPelsWidth = x;
	m_screen.dmPelsHeight = y;
	m_screen.dmBitsPerPel = bpp;
	m_screen.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;

	// Change the screen res
	if( ChangeDisplaySettings(&m_screen, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL )
	{
		// Failed to switch to fullscreen mode
		MessageBox(NULL, "Display mode failed", NULL, MB_OK);

		return false;
	}

	RECT windowRect;
	windowRect.left = (long)0;
	windowRect.right = (long)m_width;
	windowRect.top = (long)0;
	windowRect.bottom = (long)m_height;


	AdjustWindowRectEx(&windowRect, WS_OVERLAPPEDWINDOW, false, WS_EX_APPWINDOW | WS_EX_WINDOWEDGE);
	
	m_fullscreen = false;*/

	return true;
}

//////////////////////////////////////////////////////////////////////////////////////

bool Win32Window::SwitchMode()
{
	if( m_displayMode == FULLSCREEN )
	{
		if( RestoreResolution() )
		{

			Close();
			InitPopup(m_wndProc, m_hinst, true);
			return true;
		}
	}
	else
	{
		if( SetFullscreenMode(m_width, m_height, m_colorDepth) )
		{
			Close();
			InitPopup(m_wndProc, m_hinst, true);
			return true;
		}
	}

	return false;
}

//////////////////////////////////////////////////////////////////////////////////////

bool Win32Window::RestoreResolution()
{
	if( ChangeDisplaySettings(NULL, 0) != DISP_CHANGE_SUCCESSFUL )
	{
		MessageBox(NULL, "Failed restoring display mode!", NULL, MB_OK | MB_ICONERROR );

		return false;
	}

	m_displayMode = WINDOW;

	return true;
}

//////////////////////////////////////////////////////////////////////////////////////

void SetDisplay( int width, int height, int depth )
{

	
}

//////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -