Commit feed6bf4 authored by chili's avatar chili
Browse files

basic Window class

parent 14fab71d
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
// target Windows 7 or later
#define _WIN32_WINNT 0x0601
#include <sdkddkver.h>
// The following #defines disable a bunch of unused windows stuff. If you
// get weird errors when trying to do some windows stuff, try removing some
// (or all) of these defines (it will increase build time though).
#define WIN32_LEAN_AND_MEAN
#define NOGDICAPMASKS
#define NOSYSMETRICS
#define NOMENUS
#define NOICONS
#define NOSYSCOMMANDS
#define NORASTEROPS
#define OEMRESOURCE
#define NOATOM
#define NOCLIPBOARD
#define NOCOLOR
#define NOCTLMGR
#define NODRAWTEXT
#define NOKERNEL
#define NONLS
#define NOMEMMGR
#define NOMETAFILE
#define NOMINMAX
#define NOOPENFILE
#define NOSCROLL
#define NOSERVICE
#define NOSOUND
#define NOTEXTMETRIC
#define NOWH
#define NOCOMM
#define NOKANJI
#define NOHELP
#define NOPROFILER
#define NODEFERWINDOWPOS
#define NOMCX
#define NORPC
#define NOPROXYSTUB
#define NOIMAGE
#define NOTAPE
#define STRICT
#include <Windows.h>
\ No newline at end of file
#include <Windows.h>
#include "WindowsMessageMap.h"
#include <sstream>
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Window.h"
LRESULT CALLBACK WndProc( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
static WindowsMessageMap mm;
OutputDebugString( mm( msg,lParam,wParam ).c_str() );
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 69 );
break;
case WM_KEYDOWN:
if( wParam == 'D' )
{
SetWindowText( hWnd,"Respects" );
}
break;
case WM_KEYUP:
if( wParam == 'F' )
{
SetWindowText( hWnd,"Dangerfield" );
}
break;
case WM_CHAR:
{
static std::string title;
title.push_back( (char)wParam );
SetWindowText( hWnd,title.c_str() );
}
break;
case WM_LBUTTONDOWN:
{
const POINTS pt = MAKEPOINTS( lParam );
std::ostringstream oss;
oss << "(" << pt.x << "," << pt.y << ")";
SetWindowText( hWnd,oss.str().c_str() );
}
break;
}
return DefWindowProc( hWnd,msg,wParam,lParam );
}
int CALLBACK WinMain(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow )
{
const auto pClassName = "hw3dbutts";
// register window class
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof( wc );
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = pClassName;
wc.hIconSm = nullptr;
RegisterClassEx( &wc );
// create window instance
HWND hWnd = CreateWindowEx(
0,pClassName,
"Happy Hard Window",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
200,200,640,480,
nullptr,nullptr,hInstance,nullptr
);
// show the damn window
ShowWindow( hWnd,SW_SHOW );
Window wnd( 800,300,"Donkey Fart Box" );
// message pump
MSG msg;
BOOL gResult;
while( (gResult = GetMessage( &msg,nullptr,0,0 )) > 0 )
......@@ -91,8 +40,6 @@ int CALLBACK WinMain(
{
return -1;
}
else
{
return msg.wParam;
}
return msg.wParam;
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "Window.h"
// Window Class Stuff
Window::WindowClass Window::WindowClass::wndClass;
Window::WindowClass::WindowClass() noexcept
:
hInst( GetModuleHandle( nullptr ) )
{
WNDCLASSEX wc = { 0 };
wc.cbSize = sizeof( wc );
wc.style = CS_OWNDC;
wc.lpfnWndProc = HandleMsgSetup;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = GetInstance();
wc.hIcon = nullptr;
wc.hCursor = nullptr;
wc.hbrBackground = nullptr;
wc.lpszMenuName = nullptr;
wc.lpszClassName = GetName();
wc.hIconSm = nullptr;
RegisterClassEx( &wc );
}
Window::WindowClass::~WindowClass()
{
UnregisterClass( wndClassName,GetInstance() );
}
const char* Window::WindowClass::GetName() noexcept
{
return wndClassName;
}
HINSTANCE Window::WindowClass::GetInstance() noexcept
{
return wndClass.hInst;
}
// Window Stuff
Window::Window( int width,int height,const char* name ) noexcept
{
// calculate window size based on desired client region size
RECT wr;
wr.left = 100;
wr.right = width + wr.left;
wr.top = 100;
wr.bottom = height + wr.top;
AdjustWindowRect( &wr,WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,FALSE );
// create window & get hWnd
hWnd = CreateWindow(
WindowClass::GetName(),name,
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
CW_USEDEFAULT,CW_USEDEFAULT,wr.right - wr.left,wr.bottom - wr.top,
nullptr,nullptr,WindowClass::GetInstance(),this
);
// show window
ShowWindow( hWnd,SW_SHOWDEFAULT );
}
Window::~Window()
{
DestroyWindow( hWnd );
}
LRESULT WINAPI Window::HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
// use create parameter passed in from CreateWindow() to store window class pointer at WinAPI side
if( msg == WM_NCCREATE )
{
// extract ptr to window class from creation data
const CREATESTRUCTW* const pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
Window* const pWnd = static_cast<Window*>(pCreate->lpCreateParams);
// set WinAPI-managed user data to store ptr to window class
SetWindowLongPtr( hWnd,GWLP_USERDATA,reinterpret_cast<LONG_PTR>(pWnd) );
// set message proc to normal (non-setup) handler now that setup is finished
SetWindowLongPtr( hWnd,GWLP_WNDPROC,reinterpret_cast<LONG_PTR>(&Window::HandleMsgThunk) );
// forward message to window class handler
return pWnd->HandleMsg( hWnd,msg,wParam,lParam );
}
// if we get a message before the WM_NCCREATE message, handle with default handler
return DefWindowProc( hWnd,msg,wParam,lParam );
}
LRESULT WINAPI Window::HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
// retrieve ptr to window class
Window* const pWnd = reinterpret_cast<Window*>(GetWindowLongPtr( hWnd,GWLP_USERDATA ));
// forward message to window class handler
return pWnd->HandleMsg( hWnd,msg,wParam,lParam );
}
LRESULT Window::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept
{
switch( msg )
{
case WM_CLOSE:
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd,msg,wParam,lParam );
}
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include "ChiliWin.h"
class Window
{
private:
// singleton manages registration/cleanup of window class
class WindowClass
{
public:
static const char* GetName() noexcept;
static HINSTANCE GetInstance() noexcept;
private:
WindowClass() noexcept;
~WindowClass();
WindowClass( const WindowClass& ) = delete;
WindowClass& operator=( const WindowClass& ) = delete;
static constexpr const char* wndClassName = "Chili Direct3D Engine Window";
static WindowClass wndClass;
HINSTANCE hInst;
};
public:
Window( int width,int height,const char* name ) noexcept;
~Window();
Window( const Window& ) = delete;
Window& operator=( const Window& ) = delete;
private:
static LRESULT CALLBACK HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
static LRESULT CALLBACK HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam ) noexcept;
private:
int width;
int height;
HWND hWnd;
};
\ No newline at end of file
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#include "WindowsMessageMap.h"
#include <Windows.h>
#include <string>
......
/******************************************************************************************
* Chili Direct3D Engine *
* Copyright 2018 PlanetChili <http://www.planetchili.net> *
* *
* This file is part of Chili Direct3D Engine. *
* *
* Chili Direct3D Engine is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* The Chili Direct3D Engine is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with The Chili Direct3D Engine. If not, see <http://www.gnu.org/licenses/>. *
******************************************************************************************/
#pragma once
#include <unordered_map>
#include <Windows.h>
......
......@@ -145,10 +145,13 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="Window.cpp" />
<ClCompile Include="WindowsMessageMap.cpp" />
<ClCompile Include="WinMain.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="ChiliWin.h" />
<ClInclude Include="Window.h" />
<ClInclude Include="WindowsMessageMap.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
......
......@@ -21,10 +21,19 @@
<ClCompile Include="WindowsMessageMap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="WindowsMessageMap.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="ChiliWin.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Window.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment