Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Clark Lin
StudyCpp
Commits
05405833
Commit
05405833
authored
Dec 05, 2022
by
Administrator
Browse files
add window and DX start
parent
ad5630a4
Changes
4
Hide whitespace changes
Inline
Side-by-side
05.DirectX/Direct3DStart.cpp
0 → 100644
View file @
05405833
#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#pragma comment (lib, "d3d11.lib")
// global declarations
IDXGISwapChain
*
swapchain
;
// the pointer to the swap chain interface
ID3D11Device
*
dev
;
// the pointer to our Direct3D device interface
ID3D11DeviceContext
*
devcon
;
// the pointer to our Direct3D device context
// this function initializes and prepares Direct3D for use
void
InitD3D
(
HWND
hWnd
)
{
// create a struct to hold information about the swap chain
DXGI_SWAP_CHAIN_DESC
scd
;
// clear out the struct for use
ZeroMemory
(
&
scd
,
sizeof
(
DXGI_SWAP_CHAIN_DESC
));
// fill the swap chain description struct
scd
.
BufferCount
=
1
;
// one back buffer
scd
.
BufferDesc
.
Format
=
DXGI_FORMAT_R8G8B8A8_UNORM
;
// use 32-bit color
scd
.
BufferUsage
=
DXGI_USAGE_RENDER_TARGET_OUTPUT
;
// how swap chain is to be used
scd
.
OutputWindow
=
hWnd
;
// the window to be used
scd
.
SampleDesc
.
Count
=
4
;
// how many multisamples
scd
.
Windowed
=
TRUE
;
// windowed/full-screen mode
// create a device, device context and swap chain using the information in the scd struct
D3D11CreateDeviceAndSwapChain
(
NULL
,
// IDXGIAdapter *pAdapter - Let DXGI take care of what adapter to use (default adapter)
D3D_DRIVER_TYPE_HARDWARE
,
// D3D_DRIVER_TYPE DriverType - hardware or software, or others
NULL
,
// HMODULE Software
NULL
,
// UINT Flags
NULL
,
// D3D_FEATURE_LEVEL *pFeatureLevels
NULL
,
// UINT FeatureLevels
D3D11_SDK_VERSION
,
// UINT SDKVersion
&
scd
,
// DXGI_SWAP_CHAIN_DESC *pSwapChainDesc
&
swapchain
,
// IDXGISwapChain **ppSwapChain
&
dev
,
// ID3D11Device **ppDevice
NULL
,
// D3D_FEATURE_LEVEL *FeatureLevel
&
devcon
);
// ID3D11DeviceContext **ppImmediateContext
}
// this is the function that cleans up Direct3D and COM
void
CleanD3D
()
{
// close and release all existing COM objects
swapchain
->
Release
();
dev
->
Release
();
devcon
->
Release
();
}
// the WindowProc function prototype
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
);
// the entry point for any Windows program
int
WINAPI
WinMain
(
HINSTANCE
hInstance
,
HINSTANCE
hPrevInstance
,
LPSTR
lpCmdLine
,
int
nCmdShow
)
{
// the handle for the window, filled by a function
HWND
hWnd
;
// this struct holds information for the window class
WNDCLASSEX
wc
;
// clear out the window class for use
ZeroMemory
(
&
wc
,
sizeof
(
WNDCLASSEX
));
// fill in the struct with the needed information
wc
.
cbSize
=
sizeof
(
WNDCLASSEX
);
wc
.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
wc
.
lpfnWndProc
=
WindowProc
;
wc
.
hInstance
=
hInstance
;
wc
.
hCursor
=
LoadCursor
(
NULL
,
IDC_ARROW
);
wc
.
hbrBackground
=
(
HBRUSH
)
COLOR_WINDOW
;
wc
.
lpszClassName
=
L"WindowClass1"
;
// register the window class
RegisterClassEx
(
&
wc
);
// create rectangle for client area
RECT
wr
=
{
0
,
0
,
800
,
600
};
// set the size only, but not the position
AdjustWindowRect
(
&
wr
,
WS_OVERLAPPEDWINDOW
,
FALSE
);
// adjust the size
// create the window and use the result as the handle
hWnd
=
CreateWindowEx
(
NULL
,
L"WindowClass1"
,
// name of the window class
L"Our First Windowed Program"
,
// title of the window
WS_OVERLAPPEDWINDOW
,
// window style
300
,
// x-position of the window
300
,
// y-position of the window
500
,
// width of the window
400
,
// height of the window
NULL
,
// we have no parent window, NULL
NULL
,
// we aren't using menus, NULL
hInstance
,
// application handle
NULL
);
// used with multiple windows, NULL
// display the window on the screen
ShowWindow
(
hWnd
,
nCmdShow
);
// set up and initialize Direct3D
InitD3D
(
hWnd
);
// enter the main loop:
// this struct holds Windows event messages
MSG
msg
;
// wait for the next message in the queue, store the result in 'msg'
while
(
true
)
{
if
(
PeekMessage
(
&
msg
,
NULL
,
0
,
0
,
PM_REMOVE
))
{
// translate keystroke messages into the right format
TranslateMessage
(
&
msg
);
// send the message to the WindowProc function
DispatchMessage
(
&
msg
);
// check if message to quit
if
(
msg
.
message
==
WM_QUIT
)
{
break
;
}
}
else
{
}
}
// clean up DirectX and COM
CleanD3D
();
// return this part of the WM_QUIT message to Windows
return
msg
.
wParam
;
}
// this is the main message handler for the program
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
)
{
// sort through and find what code to run for the message given
switch
(
message
)
{
// this message is read when the window is closed
case
WM_DESTROY
:
{
// close the application entirely
PostQuitMessage
(
0
);
return
0
;
}
break
;
}
// Handle any messages the switch statement didn't
return
DefWindowProc
(
hWnd
,
message
,
wParam
,
lParam
);
}
\ No newline at end of file
05.DirectX/WinMainWindowPeekMsg.cpp
0 → 100644
View file @
05405833
#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
// the WindowProc function prototype
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
);
// the entry point for any Windows program
int
WINAPI
WinMain
(
HINSTANCE
hInstance
,
HINSTANCE
hPrevInstance
,
LPSTR
lpCmdLine
,
int
nCmdShow
)
{
// the handle for the window, filled by a function
HWND
hWnd
;
// this struct holds information for the window class
WNDCLASSEX
wc
;
// clear out the window class for use
ZeroMemory
(
&
wc
,
sizeof
(
WNDCLASSEX
));
// fill in the struct with the needed information
wc
.
cbSize
=
sizeof
(
WNDCLASSEX
);
wc
.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
wc
.
lpfnWndProc
=
WindowProc
;
wc
.
hInstance
=
hInstance
;
wc
.
hCursor
=
LoadCursor
(
NULL
,
IDC_ARROW
);
wc
.
hbrBackground
=
(
HBRUSH
)
COLOR_WINDOW
;
wc
.
lpszClassName
=
L"WindowClass1"
;
// register the window class
RegisterClassEx
(
&
wc
);
// create the window and use the result as the handle
hWnd
=
CreateWindowEx
(
NULL
,
L"WindowClass1"
,
// name of the window class
L"Our First Windowed Program"
,
// title of the window
WS_OVERLAPPEDWINDOW
,
// window style
300
,
// x-position of the window
300
,
// y-position of the window
500
,
// width of the window
400
,
// height of the window
NULL
,
// we have no parent window, NULL
NULL
,
// we aren't using menus, NULL
hInstance
,
// application handle
NULL
);
// used with multiple windows, NULL
// display the window on the screen
ShowWindow
(
hWnd
,
nCmdShow
);
// enter the main loop:
// this struct holds Windows event messages
MSG
msg
;
// wait for the next message in the queue, store the result in 'msg'
/*while(GetMessage(&msg, NULL, 0, 0))
{
// translate keystroke messages into the right format
TranslateMessage(&msg);
// send the message to the WindowProc function
DispatchMessage(&msg);
}*/
while
(
true
)
{
if
(
PeekMessage
(
&
msg
,
NULL
,
0
,
0
,
PM_REMOVE
))
{
// translate keystroke messages into the right format
TranslateMessage
(
&
msg
);
// send the message to the WindowProc function
DispatchMessage
(
&
msg
);
// check if message to quit
if
(
msg
.
message
==
WM_QUIT
)
{
break
;
}
}
else
{
}
}
// return this part of the WM_QUIT message to Windows
return
msg
.
wParam
;
}
// this is the main message handler for the program
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
)
{
// sort through and find what code to run for the message given
switch
(
message
)
{
// this message is read when the window is closed
case
WM_DESTROY
:
{
// close the application entirely
PostQuitMessage
(
0
);
return
0
;
}
break
;
}
// Handle any messages the switch statement didn't
return
DefWindowProc
(
hWnd
,
message
,
wParam
,
lParam
);
}
\ No newline at end of file
05.DirectX/WinMainWindowWithRect.cpp
0 → 100644
View file @
05405833
#ifndef UNICODE
#define UNICODE
#define UNICODE_WAS_UNDEFINED
#endif
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>
// the WindowProc function prototype
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
);
// the entry point for any Windows program
int
WINAPI
WinMain
(
HINSTANCE
hInstance
,
HINSTANCE
hPrevInstance
,
LPSTR
lpCmdLine
,
int
nCmdShow
)
{
// the handle for the window, filled by a function
HWND
hWnd
;
// this struct holds information for the window class
WNDCLASSEX
wc
;
// clear out the window class for use
ZeroMemory
(
&
wc
,
sizeof
(
WNDCLASSEX
));
// fill in the struct with the needed information
wc
.
cbSize
=
sizeof
(
WNDCLASSEX
);
wc
.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
wc
.
lpfnWndProc
=
WindowProc
;
wc
.
hInstance
=
hInstance
;
wc
.
hCursor
=
LoadCursor
(
NULL
,
IDC_ARROW
);
wc
.
hbrBackground
=
(
HBRUSH
)
COLOR_WINDOW
;
wc
.
lpszClassName
=
L"WindowClass1"
;
// register the window class
RegisterClassEx
(
&
wc
);
// create rectangle for client area
RECT
wr
=
{
0
,
0
,
500
,
400
};
// set the size only, but not the position
AdjustWindowRect
(
&
wr
,
WS_OVERLAPPEDWINDOW
,
FALSE
);
// adjust the size
// create the window and use the result as the handle
hWnd
=
CreateWindowEx
(
NULL
,
L"WindowClass1"
,
// name of the window class
L"Our First Windowed Program"
,
// title of the window
WS_OVERLAPPEDWINDOW
,
// window style
300
,
// x-position of the window
300
,
// y-position of the window
wr
.
right
-
wr
.
left
,
// width of the window
wr
.
bottom
-
wr
.
top
,
// height of the window
NULL
,
// we have no parent window, NULL
NULL
,
// we aren't using menus, NULL
hInstance
,
// application handle
NULL
);
// used with multiple windows, NULL
// display the window on the screen
ShowWindow
(
hWnd
,
nCmdShow
);
// enter the main loop:
// this struct holds Windows event messages
MSG
msg
;
// wait for the next message in the queue, store the result in 'msg'
while
(
GetMessage
(
&
msg
,
NULL
,
0
,
0
))
{
// translate keystroke messages into the right format
TranslateMessage
(
&
msg
);
// send the message to the WindowProc function
DispatchMessage
(
&
msg
);
}
// return this part of the WM_QUIT message to Windows
return
msg
.
wParam
;
}
// this is the main message handler for the program
LRESULT
CALLBACK
WindowProc
(
HWND
hWnd
,
UINT
message
,
WPARAM
wParam
,
LPARAM
lParam
)
{
// sort through and find what code to run for the message given
switch
(
message
)
{
// this message is read when the window is closed
case
WM_DESTROY
:
{
// close the application entirely
PostQuitMessage
(
0
);
return
0
;
}
break
;
}
// Handle any messages the switch statement didn't
return
DefWindowProc
(
hWnd
,
message
,
wParam
,
lParam
);
}
\ No newline at end of file
05.DirectX/tempCodeRunnerFile.cpp
deleted
100644 → 0
View file @
ad5630a4
wc
.
style
=
CS_HREDRAW
|
CS_VREDRAW
;
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment