나와야 하는 결과물
오른쪽 버튼을 누를 때 마다 크로스헤어를 오른쪽으로 이동시켜야 합니다.
MainWindow.h
여기서 kbd가 정의되어 있습니다.
/******************************************************************************************
* Chili DirectX Framework Version 16.07.20 *
* MainWindow.h *
* Copyright 2016 PlanetChili *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework 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 DirectX Framework 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 DirectX Framework. If not, see . *
******************************************************************************************/
#pragma once
#include "ChiliWin.h"
#include "Graphics.h"
#include "Keyboard.h"
#include "Mouse.h"
#include "ChiliException.h"
#include
// for granting special access to hWnd only for Graphics constructor
class HWNDKey
{
friend Graphics::Graphics( HWNDKey& );
public:
HWNDKey( const HWNDKey& ) = delete;
HWNDKey& operator=( HWNDKey& ) = delete;
protected:
HWNDKey() = default;
protected:
HWND hWnd = nullptr;
};
class MainWindow : public HWNDKey
{
public:
class Exception : public ChiliException
{
public:
using ChiliException::ChiliException;
virtual std::wstring GetFullMessage() const override { return GetNote() + L"\nAt: " + GetLocation(); }
virtual std::wstring GetExceptionType() const override { return L"Windows Exception"; }
};
public:
MainWindow( HINSTANCE hInst,wchar_t* pArgs );
MainWindow( const MainWindow& ) = delete;
MainWindow& operator=( const MainWindow& ) = delete;
~MainWindow();
bool IsActive() const;
bool IsMinimized() const;
void ShowMessageBox( const std::wstring& title,const std::wstring& message,UINT type = MB_OK ) const;
void Kill()
{
PostQuitMessage( 0 );
}
// returns false if quitting
bool ProcessMessage();
const std::wstring& GetArgs() const
{
return args;
}
private:
static LRESULT WINAPI _HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam );
static LRESULT WINAPI _HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam );
LRESULT HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam );
public:
Keyboard kbd;
Mouse mouse;
private:
static constexpr wchar_t* wndClassName = L"Chili DirectX Framework Window";
HINSTANCE hInst = nullptr;
std::wstring args;
};
MainWindow.cpp
여기서 kbd를 사용 중 입니다.
/******************************************************************************************
* Chili DirectX Framework Version 16.07.20 *
* MainWindow.cpp *
* Copyright 2016 PlanetChili.net *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework 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 DirectX Framework 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 DirectX Framework. If not, see . *
******************************************************************************************/
#include "MainWindow.h"
#include "Resource.h"
#include "Graphics.h"
#include "ChiliException.h"
#include "Game.h"
#include
MainWindow::MainWindow( HINSTANCE hInst,wchar_t * pArgs )
:
args( pArgs ),
hInst( hInst )
{
// register window class
WNDCLASSEX wc = { sizeof( WNDCLASSEX ),CS_CLASSDC,_HandleMsgSetup,0,0,
hInst,nullptr,nullptr,nullptr,nullptr,
wndClassName,nullptr };
wc.hIconSm = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON ),IMAGE_ICON,16,16,0 );
wc.hIcon = (HICON)LoadImage( hInst,MAKEINTRESOURCE( IDI_APPICON ),IMAGE_ICON,32,32,0 );
wc.hCursor = LoadCursor( nullptr,IDC_ARROW );
RegisterClassEx( &wc );
// create window & get hWnd
RECT wr;
wr.left = 350;
wr.right = Graphics::ScreenWidth + wr.left;
wr.top = 100;
wr.bottom = Graphics::ScreenHeight + wr.top;
AdjustWindowRect( ≀,WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,FALSE );
hWnd = CreateWindow( wndClassName,L"Chili DirectX Framework",
WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
wr.left,wr.top,wr.right - wr.left,wr.bottom - wr.top,
nullptr,nullptr,hInst,this );
// throw exception if something went terribly wrong
if( hWnd == nullptr )
{
throw Exception( _CRT_WIDE( __FILE__ ),__LINE__,
L"Failed to get valid window handle." );
}
// show and update
ShowWindow( hWnd,SW_SHOWDEFAULT );
UpdateWindow( hWnd );
}
MainWindow::~MainWindow()
{
// unregister window class
UnregisterClass( wndClassName,hInst );
}
bool MainWindow::IsActive() const
{
return GetActiveWindow() == hWnd;
}
bool MainWindow::IsMinimized() const
{
return IsIconic( hWnd ) != 0;
}
void MainWindow::ShowMessageBox( const std::wstring& title,const std::wstring& message,UINT type ) const
{
MessageBox( hWnd,message.c_str(),title.c_str(),type );
}
bool MainWindow::ProcessMessage()
{
MSG msg;
while( PeekMessage( &msg,nullptr,0,0,PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
if( msg.message == WM_QUIT )
{
return false;
}
}
return true;
}
LRESULT WINAPI MainWindow::_HandleMsgSetup( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
// 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( lParam );
MainWindow* const pWnd = reinterpret_cast( pCreate->lpCreateParams );
// sanity check
assert( pWnd != nullptr );
// set WinAPI-managed user data to store ptr to window class
SetWindowLongPtr( hWnd,GWLP_USERDATA,reinterpret_cast( pWnd ) );
// set message proc to normal (non-setup) handler now that setup is finished
SetWindowLongPtr( hWnd,GWLP_WNDPROC,reinterpret_cast( &MainWindow::_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 MainWindow::_HandleMsgThunk( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
// retrieve ptr to window class
MainWindow* const pWnd = reinterpret_cast( GetWindowLongPtr( hWnd,GWLP_USERDATA ) );
// forward message to window class handler
return pWnd->HandleMsg( hWnd,msg,wParam,lParam );
}
LRESULT MainWindow::HandleMsg( HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
break;
case WM_KILLFOCUS:
kbd.ClearState();
break;
// ************ KEYBOARD MESSAGES ************ //
case WM_KEYDOWN:
if( !(lParam & 0x40000000) || kbd.AutorepeatIsEnabled() ) // no thank you on the autorepeat
{
kbd.OnKeyPressed( static_cast(wParam) );
}
break;
case WM_KEYUP:
kbd.OnKeyReleased( static_cast(wParam) );
break;
case WM_CHAR:
kbd.OnChar( static_cast(wParam) );
break;
// ************ END KEYBOARD MESSAGES ************ //
// ************ MOUSE MESSAGES ************ //
case WM_MOUSEMOVE:
{
POINTS pt = MAKEPOINTS( lParam );
if( pt.x > 0 && pt.x 0 && pt.y
{
mouse.OnMouseMove( pt.x,pt.y );
if( !mouse.IsInWindow() )
{
SetCapture( hWnd );
mouse.OnMouseEnter();
}
}
else
{
if( wParam & (MK_LBUTTON | MK_RBUTTON) )
{
pt.x = std::max( short( 0 ),pt.x );
pt.x = std::min( short( Graphics::ScreenWidth - 1 ),pt.x );
pt.y = std::max( short( 0 ),pt.y );
pt.y = std::min( short( Graphics::ScreenHeight - 1 ),pt.y );
mouse.OnMouseMove( pt.x,pt.y );
}
else
{
ReleaseCapture();
mouse.OnMouseLeave();
mouse.OnLeftReleased( pt.x,pt.y );
mouse.OnRightReleased( pt.x,pt.y );
}
}
break;
}
case WM_LBUTTONDOWN:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnLeftPressed( pt.x,pt.y );
SetForegroundWindow( hWnd );
break;
}
case WM_RBUTTONDOWN:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnRightPressed( pt.x,pt.y );
break;
}
case WM_LBUTTONUP:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnLeftReleased( pt.x,pt.y );
break;
}
case WM_RBUTTONUP:
{
const POINTS pt = MAKEPOINTS( lParam );
mouse.OnRightReleased( pt.x,pt.y );
break;
}
case WM_MOUSEWHEEL:
{
const POINTS pt = MAKEPOINTS( lParam );
if( GET_WHEEL_DELTA_WPARAM( wParam ) > 0 )
{
mouse.OnWheelUp( pt.x,pt.y );
}
else if( GET_WHEEL_DELTA_WPARAM( wParam )
{
mouse.OnWheelDown( pt.x,pt.y );
}
break;
}
// ************ END MOUSE MESSAGES ************ //
}
return DefWindowProc( hWnd,msg,wParam,lParam );
}
Game.cpp
저는 여기서 kbd를 사용하고자 합니다.
/******************************************************************************************
* Chili DirectX Framework Version 16.07.20 *
* Game.cpp *
* Copyright 2016 PlanetChili.net *
* *
* This file is part of The Chili DirectX Framework. *
* *
* The Chili DirectX Framework 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 DirectX Framework 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 DirectX Framework. If not, see . *
******************************************************************************************/
#include "MainWindow.h"
#include "Game.h"
Game::Game( MainWindow& wnd )
:
wnd( wnd ),
gfx( wnd )
{
}
void Game::Go()
{
gfx.BeginFrame();
UpdateModel();
ComposeFrame();
gfx.EndFrame();
}
void Game::UpdateModel()
{
}
void Game::ComposeFrame()
{
int x,y;
x = 400;
y = 300;
if (kbd.RightIsPressed()) {
x = 500;
}
gfx.PutPixel( x, y, 255, 255, 255);
gfx.PutPixel(-5 + x, y, 255, 255, 255);
gfx.PutPixel(-4 + x, y, 255, 255, 255);
gfx.PutPixel(-3 + x, y, 255, 255, 255);
gfx.PutPixel( 3 + x, y, 255, 255, 255);
gfx.PutPixel( 4 + x, y, 255, 255, 255);
gfx.PutPixel( 5 + x, y, 255, 255, 255);
gfx.PutPixel( x, -5 + y, 255, 255, 255);
gfx.PutPixel( x, -4 + y, 255, 255, 255);
gfx.PutPixel( x, -3 + y, 255, 255, 255);
gfx.PutPixel( x, 3 + y, 255, 255, 255);
gfx.PutPixel( x, 4 + y, 255, 255, 255);
gfx.PutPixel( x, 5 + y, 255, 255, 255);
<span style="font-
제가 학교에서 수업을 듣느라 답이 늦었습니다만.. 이건 수업을 안들었어도 답이 불가능했을 것 같네요. 만약 구글링을 하셨어도 (하셨겠지만) 못찾으셨다면 아무래도 강의를 제공하고있는 유튜버에게 직접 메일을 보내보시는게 좋을 것 같습니다. 다른 스크립트에서 멀쩡히 kbd 클래스를 쓰고있는게 분명한데 안되는거라면 잘 이해를 못하겠는데, 한번 괄호안에 true 를 넣어보시고 그래도 안된다면 메일을 보내보시거나 댓글을 남겨봅시다.
wnd.kbd. 아닐까요
제가 할때는 그랬던거같은데
오오... 이렇게 해결되면 좋겠습니다
wnd.kbd로 해도안되네요 아쉽게도
다시 읽는중...
kbd는 wnd의 퍼블릭멤버가 맞으니까 wnd.kbd로 하면 될거같은데...흠 안 되신다니
동영상에는 wnd.kbd.KeyIsPress(...)를 사용중인데 말이죠