All pastes #1880497 Raw Edit

Untitled

public text v1 · immutable
#1880497 ·published 2010-06-10 16:24 UTC
rendered paste body
#include "InterfaceWin32.h"
#include "Renderer.h"
#include "OpenGLRenderer.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "Modules.h"
#include <iostream>
#include <list>

struct WinData
{
	HWND hWnd;
	due::InterfaceWin32* dueInterface;
};

WinData a;

namespace due
{
	InterfaceWin32::InterfaceWin32(const CreationParameters& params)
	{
		WindowTitle = params.WindowTitle;
		WindowWidth = params.WindowWidth;
		WindowHeight = params.WindowHeight;
		Frequency = params.Frequency;
		Fullscreen = false;
		Active = false;
		a.dueInterface = this;
	}

	bool InterfaceWin32::Init()
	{
		int		PixelFormat;			// Holds The Results After Searching For A Match
		WNDCLASS	wc;						// Windows Class Structure
		DWORD		dwExStyle;				// Window Extended Style
		DWORD		dwStyle;				// Window Style
		RECT		WindowRect;				// Grabs Rectangle Upper Left / Lower Right Values
		WindowRect.left=(long)0;			// Set Left Value To 0
		WindowRect.right=(long)WindowWidth;		// Set Right Value To Requested Width
		WindowRect.top=(long)0;				// Set Top Value To 0
		WindowRect.bottom=(long)WindowHeight;		// Set Bottom Value To Requested Height

		hInstance	= GetModuleHandle(NULL);				// Grab An Instance For Our Window
		wc.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC;	// Redraw On Size, And Own DC For Window.
		wc.lpfnWndProc		= (WNDPROC) WndProc;					// WndProc Handles Messages
		wc.cbClsExtra		= 0;									// No Extra Window Data
		wc.cbWndExtra		= 0;									// No Extra Window Data
		wc.hInstance		= hInstance;							// Set The Instance
		wc.hIcon			= LoadIcon(NULL, IDI_WINLOGO);			// Load The Default Icon
		wc.hCursor			= LoadCursor(NULL, IDC_ARROW);			// Load The Arrow Pointer
		wc.hbrBackground	= NULL;									// No Background Required For GL
		wc.lpszMenuName		= NULL;									// We Don't Want A Menu
		wc.lpszClassName	= "DUEApp";								// Set The Class Name

		if (!RegisterClass(&wc))									// Attempt To Register The Window Class
		{
			MessageBox(NULL,"Failed To Register The Window Class.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;											// Return false
		}
		
		if (Fullscreen)												// Attempt Fullscreen Mode?
		{
			DEVMODE dmScreenSettings;								// Device Mode
			memset(&dmScreenSettings,0,sizeof(dmScreenSettings));	// Makes Sure Memory's Cleared
			dmScreenSettings.dmSize=sizeof(dmScreenSettings);		// Size Of The Devmode Structure
			dmScreenSettings.dmPelsWidth	= WindowWidth;				// Selected Screen Width
			dmScreenSettings.dmPelsHeight	= WindowHeight;				// Selected Screen Height
			dmScreenSettings.dmBitsPerPel	= 16;					// Selected Bits Per Pixel
			dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

			// Try To Set Selected Mode And Get Results.  NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
			if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCCESSFUL)
			{
				// If The Mode Fails, Offer Two Options.  Quit Or Use Windowed Mode.
				if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","NeHe GL",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
				{
					Fullscreen=false;		// Windowed Mode Selected.  Fullscreen = false
				}
				else
				{
					// Pop Up A Message Box Letting User Know The Program Is Closing.
					MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
					return false;									// Return false
				}
			}
		}

		if (Fullscreen)												// Are We Still In Fullscreen Mode?
		{
			dwExStyle=WS_EX_APPWINDOW;								// Window Extended Style
			dwStyle=WS_POPUP;										// Windows Style
			ShowCursor(false);										// Hide Mouse Pointer
		}
		else
		{
			dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;			// Window Extended Style
			dwStyle=WS_OVERLAPPEDWINDOW;							// Windows Style
		}

		AdjustWindowRectEx(&WindowRect, dwStyle, false, dwExStyle);		// Adjust Window To True Requested Size

		// Create The Window
		if (!(hWnd = CreateWindowEx(dwExStyle,							// Extended Style For The Window
									wc.lpszClassName,					// Class Name
									(LPCSTR)WindowTitle.c_str(),		// Window Title
									dwStyle |							// Defined Window Style
									WS_CLIPSIBLINGS |					// Required Window Style
									WS_CLIPCHILDREN,					// Required Window Style
									0, 0,								// Window Position
									WindowRect.right-WindowRect.left,	// Calculate Window Width
									WindowRect.bottom-WindowRect.top,	// Calculate Window Height
									NULL,								// No Parent Window
									NULL,								// No Menu
									hInstance,							// Instance
									NULL)))								// Dont Pass Anything To WM_CREATE
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		a.hWnd = hWnd;

		static	PIXELFORMATDESCRIPTOR pfd=				// pfd Tells Windows How We Want Things To Be
		{
			sizeof(PIXELFORMATDESCRIPTOR),				// Size Of This Pixel Format Descriptor
			1,											// Version Number
			PFD_DRAW_TO_WINDOW |						// Format Must Support Window
			PFD_SUPPORT_OPENGL |						// Format Must Support OpenGL
			PFD_DOUBLEBUFFER,							// Must Support Double Buffering
			PFD_TYPE_RGBA,								// Request An RGBA Format
			16,										// Select Our Color Depth
			0, 0, 0, 0, 0, 0,							// Color Bits Ignored
			0,											// No Alpha Buffer
			0,											// Shift Bit Ignored
			0,											// No Accumulation Buffer
			0, 0, 0, 0,									// Accumulation Bits Ignored
			16,											// 16Bit Z-Buffer (Depth Buffer)  
			0,											// No Stencil Buffer
			0,											// No Auxiliary Buffer
			PFD_MAIN_PLANE,								// Main Drawing Layer
			0,											// Reserved
			0, 0, 0										// Layer Masks Ignored
		};
		
		if (!(hDC=GetDC(hWnd)))							// Did We Get A Device Context?
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Can't Create A GL Device Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd)))	// Did Windows Find A Matching Pixel Format?
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Can't Find A Suitable PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		if(!SetPixelFormat(hDC,PixelFormat,&pfd))		// Are We Able To Set The Pixel Format?
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Can't Set The PixelFormat.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		if (!(hRC=wglCreateContext(hDC)))				// Are We Able To Get A Rendering Context?
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Can't Create A GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		if(!wglMakeCurrent(hDC, hRC))					// Try To Activate The Rendering Context
		{
			//KillGLWindow();								// Reset The Display
			MessageBox(NULL,"Can't Activate The GL Rendering Context.","ERROR",MB_OK|MB_ICONEXCLAMATION);
			return false;								// Return false
		}

		ShowWindow(hWnd,SW_SHOW);						// Show The Window
		SetForegroundWindow(hWnd);						// Slightly Higher Priority
		SetFocus(hWnd);									// Sets Keyboard Focus To The Window

		a.dueInterface = this;
		a.hWnd = hWnd;		

		mRenderer->Resize(WindowWidth, WindowHeight);

		Active = true;
		return true;
	}

	void InterfaceWin32::SetRenderer(gfx::Renderer* renderer)
	{
		mRenderer = renderer;
		mRenderer->Resize(WindowWidth, WindowHeight);
	}

	bool InterfaceWin32::Run()
	{
		MSG msg;

		bool Quit = false;

		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{	
			if(msg.message == WM_QUIT)
			{
				Quit = true;
			}	

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		/* Update mouse coordinates */
		if(a.dueInterface->GetMouse()->Move == true)
		{
			POINT point;

			point.x = a.dueInterface->GetMouse()->GetNewX();
			point.y = a.dueInterface->GetMouse()->GetNewY();
			
			ClientToScreen(hWnd, &point);
			SetCursorPos(point.x, point.y);

			a.dueInterface->GetMouse()->Move = false;
		}
		
		SwapBuffers(hDC);

		return !Quit;
	}

	void InterfaceWin32::ShowConsole()
	{
		HWND hWnd = GetConsoleWindow();
		ShowWindow( hWnd, SW_SHOW );
	}

	void InterfaceWin32::HideConsole()
	{
		HWND hWnd = GetConsoleWindow();
		ShowWindow( hWnd, SW_HIDE );
	}

	void InterfaceWin32::SetWindowTitle(string title)
	{
		SetWindowText(a.dueInterface->hWnd, title.c_str());
	}

	void InterfaceWin32::SetKeyboard(io::Keyboard* keyboard)
	{
		k = keyboard;
	}

	void InterfaceWin32::SetMouse(io::Mouse* mouse)
	{
		mMouse = mouse;
	}

	gfx::Renderer* InterfaceWin32::GetRenderer()
	{
		return mRenderer;
	}

	io::Keyboard* InterfaceWin32::GetKeyboard()
	{
		return k;
	}

	io::Mouse* InterfaceWin32::GetMouse()
	{
		return mMouse;
	}
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    switch(msg)
    {
    case WM_CREATE:
        //Beep(200, 1000);
        return 0;
        break;

    case WM_PAINT:
        {
            HDC hDC;
            PAINTSTRUCT ps;
            hDC = BeginPaint( hWnd, &ps );
                // don't draw here.  draw in the draw() function.
            EndPaint( hWnd, &ps );			
        }
        return 0;
        break;

	case WM_SIZE:
	{
		if(a.dueInterface->Active == true)
		{			
			a.dueInterface->GetRenderer()->Resize(LOWORD(lParam), HIWORD(lParam));
			a.dueInterface->GetRenderer()->EndScene();		
			SwapBuffers(a.dueInterface->hDC);
		}
		return 0;
		break;
	}

    case WM_KEYDOWN:
		a.dueInterface->GetKeyboard()->AddKeyHit((int)wParam);
        switch(wParam)
        {
        case VK_ESCAPE:
			PostQuitMessage(0);
            break;
        default:			
            break;
        }
        return 0;

	case WM_KEYUP:
		a.dueInterface->GetKeyboard()->AddKeyUp((int)wParam);
        break;
        return 0;
	
	case WM_SETCURSOR:
		//ShowCursor(Globals.EngineBase->Mouse.IsVisible());
		break;
		return 0;

	case WM_LBUTTONDOWN:
		//Globals
		return 0;
		break;

	case WM_MOUSEMOVE:
		POINT Coords;
		GetCursorPos(&Coords); 
		ScreenToClient(hWnd, &Coords);
		a.dueInterface->GetMouse()->UpdateCoordinates(Coords.x, Coords.y);
		return 0;
		break;

    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
        break;
    }
 
    return DefWindowProc( hWnd, msg, wParam, lParam );
}