From 5de218d6044730183b06886fb50478f2cf07fc2e Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Sat, 5 Sep 2020 14:32:03 +0300 Subject: [PATCH 01/12] feat: render the window using WinAPI --- Lab_1/Lab_1.sln | 31 +++++++ Lab_1/Lab_1/Lab_1.cpp | 67 ++++++++++++++ Lab_1/Lab_1/Lab_1.vcxproj | 147 ++++++++++++++++++++++++++++++ Lab_1/Lab_1/Lab_1.vcxproj.filters | 22 +++++ 4 files changed, 267 insertions(+) create mode 100644 Lab_1/Lab_1.sln create mode 100644 Lab_1/Lab_1/Lab_1.cpp create mode 100644 Lab_1/Lab_1/Lab_1.vcxproj create mode 100644 Lab_1/Lab_1/Lab_1.vcxproj.filters diff --git a/Lab_1/Lab_1.sln b/Lab_1/Lab_1.sln new file mode 100644 index 0000000..9b2ea89 --- /dev/null +++ b/Lab_1/Lab_1.sln @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30309.148 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab_1", "Lab_1\Lab_1.vcxproj", "{1D32933E-0C89-4FAA-8183-F729ABF26DDF}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x64.ActiveCfg = Debug|x64 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x64.Build.0 = Debug|x64 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x86.ActiveCfg = Debug|Win32 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x86.Build.0 = Debug|Win32 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x64.ActiveCfg = Release|x64 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x64.Build.0 = Release|x64 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x86.ActiveCfg = Release|Win32 + {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {F5EF02C5-75A8-4FCC-86CC-6E324B1EAE71} + EndGlobalSection +EndGlobal diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp new file mode 100644 index 0000000..adf6108 --- /dev/null +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -0,0 +1,67 @@ +#include +#include + +LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +TCHAR WinName[] = _T("Main window"); + +int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) +{ + HWND hWnd; // Window descriptor. + MSG msg; // Structure for storing the message. + WNDCLASS wc; // Window class. + + wc.hInstance = This; + wc.lpszClassName = WinName; // Window name. + wc.lpfnWndProc = WndProc; // Callback window function. + wc.style = CS_HREDRAW | CS_VREDRAW; // Window style. + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Default window icon. + wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Default mouse cursor. + wc.lpszMenuName = NULL; // Window has no menu. + wc.cbClsExtra = 0; // No extra class info. + wc.cbWndExtra = 0; // No extra window info. + + // Fill window with white color. + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + + // Class registration + if (!RegisterClass(&wc)) + return 0; + + hWnd = CreateWindow(WinName, // Window name. + _T("Window"), // Window title. + WS_OVERLAPPEDWINDOW, // Window style. + CW_USEDEFAULT, // X. + CW_USEDEFAULT, // Y. + CW_USEDEFAULT, // Window width. + CW_USEDEFAULT, // Window height. + HWND_DESKTOP, // Parent window descriptor. + NULL, // Window has no menu. + This, // Application descriptor. + NULL); // No extra info. + + ShowWindow(hWnd, mode); + + // Message processing cycle. + while (GetMessage(&msg, NULL, 0, 0)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } + + return 0; +} + +// The window function is called by the operating system and +// receives messages from the queue for this application. +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ + switch (message) + { + case WM_DESTROY: + PostQuitMessage(0); + break; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } + + return 0; +} \ No newline at end of file diff --git a/Lab_1/Lab_1/Lab_1.vcxproj b/Lab_1/Lab_1/Lab_1.vcxproj new file mode 100644 index 0000000..89692c7 --- /dev/null +++ b/Lab_1/Lab_1/Lab_1.vcxproj @@ -0,0 +1,147 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 16.0 + Win32Proj + {1d32933e-0c89-4faa-8183-f729abf26ddf} + Lab1 + 10.0 + + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + Application + true + v142 + Unicode + + + Application + false + v142 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + false + + + true + + + false + + + + Level3 + true + WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level3 + true + true + true + WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + Level3 + true + _DEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + + + + + Level3 + true + true + true + NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + true + + + Windows + true + true + true + + + + + + + + + \ No newline at end of file diff --git a/Lab_1/Lab_1/Lab_1.vcxproj.filters b/Lab_1/Lab_1/Lab_1.vcxproj.filters new file mode 100644 index 0000000..2954ba3 --- /dev/null +++ b/Lab_1/Lab_1/Lab_1.vcxproj.filters @@ -0,0 +1,22 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;c++;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + Source Files + + + \ No newline at end of file From 2ce43096c14584a9d45afa5db3909276cbcb0755 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Sun, 6 Sep 2020 11:32:35 +0300 Subject: [PATCH 02/12] chore: change the name of the app window --- Lab_1/Lab_1/Lab_1.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index adf6108..e046652 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -2,7 +2,7 @@ #include LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -TCHAR WinName[] = _T("Main window"); +TCHAR WinName[] = _T("Lab_1"); int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) { @@ -21,14 +21,14 @@ int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) wc.cbWndExtra = 0; // No extra window info. // Fill window with white color. - wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + wc.hbrBackground = (HBRUSH)(WHITE_BRUSH); // Class registration if (!RegisterClass(&wc)) return 0; hWnd = CreateWindow(WinName, // Window name. - _T("Window"), // Window title. + _T("Lab_1"), // Window title. WS_OVERLAPPEDWINDOW, // Window style. CW_USEDEFAULT, // X. CW_USEDEFAULT, // Y. From f2f389b77c1e61693b1de35c4f5049ffa71a163b Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Sun, 6 Sep 2020 13:43:49 +0300 Subject: [PATCH 03/12] feat: draw a picture in bmp format in the window --- Lab_1/Lab_1/Lab_1.cpp | 32 +++++++++++++- Lab_1/Lab_1/Lab_1.rc | 69 ++++++++++++++++++++++++++++++ Lab_1/Lab_1/Lab_1.vcxproj | 9 ++++ Lab_1/Lab_1/Lab_1.vcxproj.filters | 15 +++++++ Lab_1/Lab_1/bitmap1.bmp | Bin 0 -> 8310 bytes Lab_1/Lab_1/resource.h | 16 +++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Lab_1/Lab_1/Lab_1.rc create mode 100644 Lab_1/Lab_1/bitmap1.bmp create mode 100644 Lab_1/Lab_1/resource.h diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index e046652..92ea0e9 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -1,8 +1,16 @@ #include #include +#include + +#include "resource.h"; + +typedef std::basic_string, + std::allocator > String; LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); + TCHAR WinName[] = _T("Lab_1"); +HINSTANCE hInst; int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) { @@ -21,7 +29,9 @@ int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) wc.cbWndExtra = 0; // No extra window info. // Fill window with white color. - wc.hbrBackground = (HBRUSH)(WHITE_BRUSH); + wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + + hInst = This; // Class registration if (!RegisterClass(&wc)) @@ -54,8 +64,28 @@ int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) // receives messages from the queue for this application. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + PAINTSTRUCT ps; + HDC hdc; + HBITMAP hBitmap; + + static HDC memBitMap; + static BITMAP bm; + switch (message) { + case WM_CREATE: + hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1)); + GetObject(hBitmap, sizeof(bm), &bm); + hdc = GetDC(hWnd); + memBitMap = CreateCompatibleDC(hdc); + SelectObject(memBitMap, hBitmap); + ReleaseDC(hWnd, hdc); + break; + case WM_PAINT: + hdc = BeginPaint(hWnd, &ps); + BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memBitMap, 0, 0, SRCCOPY); + EndPaint(hWnd, &ps); + break; case WM_DESTROY: PostQuitMessage(0); break; diff --git a/Lab_1/Lab_1/Lab_1.rc b/Lab_1/Lab_1/Lab_1.rc new file mode 100644 index 0000000..38d855f --- /dev/null +++ b/Lab_1/Lab_1/Lab_1.rc @@ -0,0 +1,69 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Bitmap +// + +IDB_BITMAP1 BITMAP "bitmap1.bmp" + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/Lab_1/Lab_1/Lab_1.vcxproj b/Lab_1/Lab_1/Lab_1.vcxproj index 89692c7..b848882 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj +++ b/Lab_1/Lab_1/Lab_1.vcxproj @@ -141,6 +141,15 @@ + + + + + + + + + diff --git a/Lab_1/Lab_1/Lab_1.vcxproj.filters b/Lab_1/Lab_1/Lab_1.vcxproj.filters index 2954ba3..33c5184 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj.filters +++ b/Lab_1/Lab_1/Lab_1.vcxproj.filters @@ -19,4 +19,19 @@ Source Files + + + Header Files + + + + + Resource Files + + + + + Resource Files + + \ No newline at end of file diff --git a/Lab_1/Lab_1/bitmap1.bmp b/Lab_1/Lab_1/bitmap1.bmp new file mode 100644 index 0000000000000000000000000000000000000000..6f31407fca29d355a4b12d1c358773bcc5fa34d5 GIT binary patch literal 8310 zcmeIuu?@r^5ClLhxqvUhq3dw|?Kq|lcSC1*;c4QfIk@G7AerlXA7$2^JkxdcR`yJ( zW9E!ca`dn2dR*6^oVjYP<;+m}+@<$1rL)FW;!y+$5FkK+009C72oNAZfB*pk1PBly SK!5-N0t5&UAV7e?-wV8OT{v0* literal 0 HcmV?d00001 diff --git a/Lab_1/Lab_1/resource.h b/Lab_1/Lab_1/resource.h new file mode 100644 index 0000000..9d29ca6 --- /dev/null +++ b/Lab_1/Lab_1/resource.h @@ -0,0 +1,16 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by Lab_1.rc +// +#define IDB_BITMAP1 101 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 102 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif From 292580914570b9352295dda9fea90327c91c64c3 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 17:17:59 +0300 Subject: [PATCH 04/12] docs: add the README.md file to the first lab --- Lab_1/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 Lab_1/README.md diff --git a/Lab_1/README.md b/Lab_1/README.md new file mode 100644 index 0000000..8fc1228 --- /dev/null +++ b/Lab_1/README.md @@ -0,0 +1,9 @@ +# Программа, позволяющая передвигать с помощью клавиатуры и мыши спрайт + +### Задание +Разработать программу, позволяющую передвигать с помощью клавиатуры и мыши спрайт (окрашенный прямоугольник или элип) внутри рабочей области окна. Обеспечить работу колесика мыши. Прокручивание двигает спрайт по вертикали. С удерживаемой клавишей *Shift* прокручивание колесика двигает спрайт по горизонтали. Реализовать возможность замены спрайта на картинку с непрямоугольным контуром (PNG картинка). Придать спрайту движение с откосом от границ окна. +### Важные моменты +При риализации использовались: + - Событийная архитектура Windows-приложений + - Механизм обработки сообщений + - Механизм перерисовки окна \ No newline at end of file From 7260cdeceb7b1735ffb7993f140855219b0d0310 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 19:49:59 +0300 Subject: [PATCH 05/12] fix: change the render function of the window --- Lab_1/Lab_1.sln | 20 ++--- Lab_1/Lab_1/Lab_1.cpp | 129 +++++++++++++----------------- Lab_1/Lab_1/Lab_1.rc | 69 ---------------- Lab_1/Lab_1/Lab_1.vcxproj | 27 +++---- Lab_1/Lab_1/Lab_1.vcxproj.filters | 15 ---- Lab_1/Lab_1/bitmap1.bmp | Bin 8310 -> 0 bytes Lab_1/Lab_1/resource.h | 16 ---- 7 files changed, 73 insertions(+), 203 deletions(-) delete mode 100644 Lab_1/Lab_1/Lab_1.rc delete mode 100644 Lab_1/Lab_1/bitmap1.bmp delete mode 100644 Lab_1/Lab_1/resource.h diff --git a/Lab_1/Lab_1.sln b/Lab_1/Lab_1.sln index 9b2ea89..c741e69 100644 --- a/Lab_1/Lab_1.sln +++ b/Lab_1/Lab_1.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.30309.148 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab_1", "Lab_1\Lab_1.vcxproj", "{1D32933E-0C89-4FAA-8183-F729ABF26DDF}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Lab_1", "Lab_1\Lab_1.vcxproj", "{805D143F-4925-408E-8E98-71A5B106E6DF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -13,19 +13,19 @@ Global Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x64.ActiveCfg = Debug|x64 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x64.Build.0 = Debug|x64 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x86.ActiveCfg = Debug|Win32 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Debug|x86.Build.0 = Debug|Win32 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x64.ActiveCfg = Release|x64 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x64.Build.0 = Release|x64 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x86.ActiveCfg = Release|Win32 - {1D32933E-0C89-4FAA-8183-F729ABF26DDF}.Release|x86.Build.0 = Release|Win32 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Debug|x64.ActiveCfg = Debug|x64 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Debug|x64.Build.0 = Debug|x64 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Debug|x86.ActiveCfg = Debug|Win32 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Debug|x86.Build.0 = Debug|Win32 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Release|x64.ActiveCfg = Release|x64 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Release|x64.Build.0 = Release|x64 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Release|x86.ActiveCfg = Release|Win32 + {805D143F-4925-408E-8E98-71A5B106E6DF}.Release|x86.Build.0 = Release|Win32 EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {F5EF02C5-75A8-4FCC-86CC-6E324B1EAE71} + SolutionGuid = {571BAF3D-8401-47F0-A299-8FE9B367693D} EndGlobalSection EndGlobal diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 92ea0e9..847d531 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -1,97 +1,76 @@ #include -#include -#include -#include "resource.h"; - -typedef std::basic_string, - std::allocator > String; +constexpr auto WINDOW_NAME = "Lab_1"; +ATOM RegisterWindowClass(HINSTANCE); +BOOL InitWindowInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); -TCHAR WinName[] = _T("Lab_1"); -HINSTANCE hInst; - -int APIENTRY _tWinMain(HINSTANCE This, HINSTANCE Prev, LPTSTR cmd, int mode) +int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShowMode) { - HWND hWnd; // Window descriptor. - MSG msg; // Structure for storing the message. - WNDCLASS wc; // Window class. - - wc.hInstance = This; - wc.lpszClassName = WinName; // Window name. - wc.lpfnWndProc = WndProc; // Callback window function. - wc.style = CS_HREDRAW | CS_VREDRAW; // Window style. - wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // Default window icon. - wc.hCursor = LoadCursor(NULL, IDC_ARROW); // Default mouse cursor. - wc.lpszMenuName = NULL; // Window has no menu. - wc.cbClsExtra = 0; // No extra class info. - wc.cbWndExtra = 0; // No extra window info. - - // Fill window with white color. - wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); - - hInst = This; - - // Class registration - if (!RegisterClass(&wc)) - return 0; + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(cmdLine); + + MSG msg; + + RegisterWindowClass(hInstance); - hWnd = CreateWindow(WinName, // Window name. - _T("Lab_1"), // Window title. - WS_OVERLAPPEDWINDOW, // Window style. - CW_USEDEFAULT, // X. - CW_USEDEFAULT, // Y. - CW_USEDEFAULT, // Window width. - CW_USEDEFAULT, // Window height. - HWND_DESKTOP, // Parent window descriptor. - NULL, // Window has no menu. - This, // Application descriptor. - NULL); // No extra info. - - ShowWindow(hWnd, mode); - - // Message processing cycle. - while (GetMessage(&msg, NULL, 0, 0)) { + if (!InitWindowInstance(hInstance, cmdShowMode)) + return FALSE; + + while (GetMessage(&msg, NULL, 0, 0)) + { TranslateMessage(&msg); DispatchMessage(&msg); } - return 0; + return (int)msg.wParam; } -// The window function is called by the operating system and -// receives messages from the queue for this application. -LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +ATOM RegisterWindowClass(HINSTANCE hInstance) +{ + WNDCLASSEX windowClassEx; + + windowClassEx.cbSize = sizeof(WNDCLASSEX); + windowClassEx.style = CS_HREDRAW | CS_VREDRAW; + windowClassEx.lpfnWndProc = WndProc; + windowClassEx.cbClsExtra = 0; + windowClassEx.cbWndExtra = 0; + windowClassEx.hInstance = hInstance; + windowClassEx.hIcon = LoadIcon(0, IDI_WINLOGO);; + windowClassEx.hCursor = LoadCursor(0, IDC_ARROW); + windowClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + windowClassEx.lpszMenuName = 0; + windowClassEx.lpszClassName = WINDOW_NAME; + windowClassEx.hIconSm = 0; + + return RegisterClassEx(&windowClassEx); +} + +BOOL InitWindowInstance(HINSTANCE hInstance, int cmdShowMode) { - PAINTSTRUCT ps; - HDC hdc; - HBITMAP hBitmap; + HWND hWnd; + + hWnd = CreateWindow(WINDOW_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); + + if (!hWnd) + return FALSE; - static HDC memBitMap; - static BITMAP bm; + ShowWindow(hWnd, cmdShowMode); + UpdateWindow(hWnd); + + return TRUE; +} +LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) +{ switch (message) { - case WM_CREATE: - hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_BITMAP1)); - GetObject(hBitmap, sizeof(bm), &bm); - hdc = GetDC(hWnd); - memBitMap = CreateCompatibleDC(hdc); - SelectObject(memBitMap, hBitmap); - ReleaseDC(hWnd, hdc); - break; - case WM_PAINT: - hdc = BeginPaint(hWnd, &ps); - BitBlt(hdc, 0, 0, bm.bmWidth, bm.bmHeight, memBitMap, 0, 0, SRCCOPY); - EndPaint(hWnd, &ps); - break; - case WM_DESTROY: + case WM_DESTROY: PostQuitMessage(0); - break; + return 0; default: return DefWindowProc(hWnd, message, wParam, lParam); } - - return 0; -} \ No newline at end of file +} diff --git a/Lab_1/Lab_1/Lab_1.rc b/Lab_1/Lab_1/Lab_1.rc deleted file mode 100644 index 38d855f..0000000 --- a/Lab_1/Lab_1/Lab_1.rc +++ /dev/null @@ -1,69 +0,0 @@ -// Microsoft Visual C++ generated resource script. -// -#include "resource.h" - -#define APSTUDIO_READONLY_SYMBOLS -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 2 resource. -// -#include "winres.h" - -///////////////////////////////////////////////////////////////////////////// -#undef APSTUDIO_READONLY_SYMBOLS - -///////////////////////////////////////////////////////////////////////////// -// English (United States) resources - -#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) -LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US -#pragma code_page(1252) - -#ifdef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// TEXTINCLUDE -// - -1 TEXTINCLUDE -BEGIN - "resource.h\0" -END - -2 TEXTINCLUDE -BEGIN - "#include ""winres.h""\r\n" - "\0" -END - -3 TEXTINCLUDE -BEGIN - "\r\n" - "\0" -END - -#endif // APSTUDIO_INVOKED - - -///////////////////////////////////////////////////////////////////////////// -// -// Bitmap -// - -IDB_BITMAP1 BITMAP "bitmap1.bmp" - -#endif // English (United States) resources -///////////////////////////////////////////////////////////////////////////// - - - -#ifndef APSTUDIO_INVOKED -///////////////////////////////////////////////////////////////////////////// -// -// Generated from the TEXTINCLUDE 3 resource. -// - - -///////////////////////////////////////////////////////////////////////////// -#endif // not APSTUDIO_INVOKED - diff --git a/Lab_1/Lab_1/Lab_1.vcxproj b/Lab_1/Lab_1/Lab_1.vcxproj index b848882..6bd1f26 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj +++ b/Lab_1/Lab_1/Lab_1.vcxproj @@ -21,7 +21,7 @@ 16.0 Win32Proj - {1d32933e-0c89-4faa-8183-f729abf26ddf} + {805d143f-4925-408e-8e98-71a5b106e6df} Lab1 10.0 @@ -30,7 +30,7 @@ Application true v142 - Unicode + MultiByte Application @@ -86,7 +86,7 @@ Level3 true - WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions) + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) true @@ -100,11 +100,11 @@ true true true - WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - Windows + Console true true true @@ -114,11 +114,11 @@ Level3 true - _DEBUG;_WINDOWS;%(PreprocessorDefinitions) + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) true - Windows + Console true @@ -128,11 +128,11 @@ true true true - NDEBUG;_WINDOWS;%(PreprocessorDefinitions) + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) true - Windows + Console true true true @@ -141,15 +141,6 @@ - - - - - - - - - diff --git a/Lab_1/Lab_1/Lab_1.vcxproj.filters b/Lab_1/Lab_1/Lab_1.vcxproj.filters index 33c5184..2954ba3 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj.filters +++ b/Lab_1/Lab_1/Lab_1.vcxproj.filters @@ -19,19 +19,4 @@ Source Files - - - Header Files - - - - - Resource Files - - - - - Resource Files - - \ No newline at end of file diff --git a/Lab_1/Lab_1/bitmap1.bmp b/Lab_1/Lab_1/bitmap1.bmp deleted file mode 100644 index 6f31407fca29d355a4b12d1c358773bcc5fa34d5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8310 zcmeIuu?@r^5ClLhxqvUhq3dw|?Kq|lcSC1*;c4QfIk@G7AerlXA7$2^JkxdcR`yJ( zW9E!ca`dn2dR*6^oVjYP<;+m}+@<$1rL)FW;!y+$5FkK+009C72oNAZfB*pk1PBly SK!5-N0t5&UAV7e?-wV8OT{v0* diff --git a/Lab_1/Lab_1/resource.h b/Lab_1/Lab_1/resource.h deleted file mode 100644 index 9d29ca6..0000000 --- a/Lab_1/Lab_1/resource.h +++ /dev/null @@ -1,16 +0,0 @@ -//{{NO_DEPENDENCIES}} -// Microsoft Visual C++ generated include file. -// Used by Lab_1.rc -// -#define IDB_BITMAP1 101 - -// Next default values for new objects -// -#ifdef APSTUDIO_INVOKED -#ifndef APSTUDIO_READONLY_SYMBOLS -#define _APS_NEXT_RESOURCE_VALUE 102 -#define _APS_NEXT_COMMAND_VALUE 40001 -#define _APS_NEXT_CONTROL_VALUE 1001 -#define _APS_NEXT_SYMED_VALUE 101 -#endif -#endif From 0841e88b119253e6e4da7b3e7c02ded8d9591216 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 21:21:20 +0300 Subject: [PATCH 06/12] fix: change tabs to spaces --- Lab_1/Lab_1/Lab_1.cpp | 88 +++++++++++++++++++++---------------------- 1 file changed, 44 insertions(+), 44 deletions(-) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 847d531..0431bbb 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -8,69 +8,69 @@ LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShowMode) { - UNREFERENCED_PARAMETER(hPrevInstance); - UNREFERENCED_PARAMETER(cmdLine); + UNREFERENCED_PARAMETER(hPrevInstance); + UNREFERENCED_PARAMETER(cmdLine); - MSG msg; + MSG msg; - RegisterWindowClass(hInstance); + RegisterWindowClass(hInstance); - if (!InitWindowInstance(hInstance, cmdShowMode)) - return FALSE; + if (!InitWindowInstance(hInstance, cmdShowMode)) + return FALSE; - while (GetMessage(&msg, NULL, 0, 0)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } + while (GetMessage(&msg, NULL, 0, 0)) + { + TranslateMessage(&msg); + DispatchMessage(&msg); + } - return (int)msg.wParam; + return (int)msg.wParam; } ATOM RegisterWindowClass(HINSTANCE hInstance) { - WNDCLASSEX windowClassEx; - - windowClassEx.cbSize = sizeof(WNDCLASSEX); - windowClassEx.style = CS_HREDRAW | CS_VREDRAW; - windowClassEx.lpfnWndProc = WndProc; - windowClassEx.cbClsExtra = 0; - windowClassEx.cbWndExtra = 0; - windowClassEx.hInstance = hInstance; - windowClassEx.hIcon = LoadIcon(0, IDI_WINLOGO);; - windowClassEx.hCursor = LoadCursor(0, IDC_ARROW); - windowClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); - windowClassEx.lpszMenuName = 0; - windowClassEx.lpszClassName = WINDOW_NAME; - windowClassEx.hIconSm = 0; - - return RegisterClassEx(&windowClassEx); + WNDCLASSEX windowClassEx; + + windowClassEx.cbSize = sizeof(WNDCLASSEX); + windowClassEx.style = CS_HREDRAW | CS_VREDRAW; + windowClassEx.lpfnWndProc = WndProc; + windowClassEx.cbClsExtra = 0; + windowClassEx.cbWndExtra = 0; + windowClassEx.hInstance = hInstance; + windowClassEx.hIcon = LoadIcon(0, IDI_WINLOGO);; + windowClassEx.hCursor = LoadCursor(0, IDC_ARROW); + windowClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + windowClassEx.lpszMenuName = 0; + windowClassEx.lpszClassName = WINDOW_NAME; + windowClassEx.hIconSm = 0; + + return RegisterClassEx(&windowClassEx); } BOOL InitWindowInstance(HINSTANCE hInstance, int cmdShowMode) { - HWND hWnd; + HWND hWnd; - hWnd = CreateWindow(WINDOW_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, - CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); + hWnd = CreateWindow(WINDOW_NAME, WINDOW_NAME, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, + CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); - if (!hWnd) - return FALSE; + if (!hWnd) + return FALSE; - ShowWindow(hWnd, cmdShowMode); - UpdateWindow(hWnd); + ShowWindow(hWnd, cmdShowMode); + UpdateWindow(hWnd); - return TRUE; + return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { - switch (message) - { - case WM_DESTROY: - PostQuitMessage(0); - return 0; - default: - return DefWindowProc(hWnd, message, wParam, lParam); - } + switch (message) + { + case WM_DESTROY: + PostQuitMessage(0); + return 0; + default: + return DefWindowProc(hWnd, message, wParam, lParam); + } } From 4a9f71f3a807246749b120fb81463cdefb5ba68a Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 22:06:59 +0300 Subject: [PATCH 07/12] feat: draw a picture in bmp format in the window --- Lab_1/Lab_1/Lab_1.cpp | 117 ++++++++++++++++++++++++++++++ Lab_1/Lab_1/Lab_1.rc | 69 ++++++++++++++++++ Lab_1/Lab_1/Lab_1.vcxproj | 9 +++ Lab_1/Lab_1/Lab_1.vcxproj.filters | 15 ++++ Lab_1/Lab_1/resource.h | 16 ++++ Lab_1/Lab_1/sprite.bmp | Bin 0 -> 8310 bytes 6 files changed, 226 insertions(+) create mode 100644 Lab_1/Lab_1/Lab_1.rc create mode 100644 Lab_1/Lab_1/resource.h create mode 100644 Lab_1/Lab_1/sprite.bmp diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 0431bbb..4197831 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -1,4 +1,11 @@ #include +#define _USE_MATH_DEFINES +#include + +#include "resource.h" + +#define WM_UPDATE_SPRITE (WM_USER + 1) +#define WM_LOAD_DEFAULT_SPRITE (WM_USER + 2) constexpr auto WINDOW_NAME = "Lab_1"; @@ -6,6 +13,97 @@ ATOM RegisterWindowClass(HINSTANCE); BOOL InitWindowInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); +bool PostLoadDefaultSpriteMessage(HWND hWnd, HINSTANCE hInstance) +{ + return PostMessage(hWnd, WM_LOAD_DEFAULT_SPRITE, NULL, (LPARAM)hInstance); +} + +bool PostUpdateSpriteMessage(HWND hWnd) +{ + return PostMessage(hWnd, WM_UPDATE_SPRITE, NULL, NULL); +} + +SIZE GetSpriteSize(HBITMAP hBitmap) +{ + BITMAP sprite; + GetObject(hBitmap, sizeof(BITMAP), &sprite); + + SIZE spriteSize; + spriteSize.cx = sprite.bmWidth; + spriteSize.cy = sprite.bmHeight; + + return spriteSize; +} + +XFORM GetMovementXform(COORD coordinates) +{ + XFORM xForm; + xForm.eM11 = 1; + xForm.eM12 = 0; + xForm.eM21 = 0; + xForm.eM22 = 1; + xForm.eDx = coordinates.X; + xForm.eDy = coordinates.Y; + + return xForm; +} + +XFORM GetRotationXform(short degreeAngle) +{ + XFORM xForm; + FLOAT radAngle = (FLOAT)(M_PI * degreeAngle / 180); + FLOAT angleSin = sin(radAngle); + FLOAT angleCos = cos(radAngle); + + xForm.eM11 = angleCos; + xForm.eM12 = angleSin; + xForm.eM21 = -angleSin; + xForm.eM22 = angleCos; + xForm.eDx = 0; + xForm.eDy = 0; + + return xForm; +} + +bool PutSpriteOnWindow(HWND hWnd, HBITMAP sprite, COORD coordinates, short angle) +{ + HDC windowDC = GetDC(hWnd); + HDC spriteDC = CreateCompatibleDC(windowDC); + + HGDIOBJ oldObject = SelectObject(spriteDC, sprite); + SIZE bitmapSize = GetSpriteSize(sprite); + + XFORM xForm; + int graphicsMode = SetGraphicsMode(windowDC, GM_ADVANCED); + + xForm = GetMovementXform(coordinates); + SetWorldTransform(windowDC, &xForm); + + COORD transformationCenter; + transformationCenter.X = (SHORT)(-(coordinates.X + bitmapSize.cx / 2)); + transformationCenter.Y = (SHORT)(-(coordinates.Y + bitmapSize.cy / 2)); + xForm = GetMovementXform(transformationCenter); + ModifyWorldTransform(windowDC, &xForm, MWT_RIGHTMULTIPLY); + + xForm = GetRotationXform(angle); + ModifyWorldTransform(windowDC, &xForm, MWT_RIGHTMULTIPLY); + + transformationCenter.X = -transformationCenter.X; + transformationCenter.Y = -transformationCenter.Y; + xForm = GetMovementXform(transformationCenter); + ModifyWorldTransform(windowDC, &xForm, MWT_RIGHTMULTIPLY); + + bool result = BitBlt(windowDC, 0, 0, bitmapSize.cx, bitmapSize.cy, spriteDC, 0, 0, SRCCOPY); + ModifyWorldTransform(windowDC, NULL, MWT_IDENTITY); + SetGraphicsMode(windowDC, graphicsMode); + + SelectObject(spriteDC, oldObject); + DeleteDC(spriteDC); + ReleaseDC(hWnd, windowDC); + + return result; +} + int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR cmdLine, int cmdShowMode) { UNREFERENCED_PARAMETER(hPrevInstance); @@ -60,13 +158,32 @@ BOOL InitWindowInstance(HINSTANCE hInstance, int cmdShowMode) ShowWindow(hWnd, cmdShowMode); UpdateWindow(hWnd); + // Load default sprite. + PostLoadDefaultSpriteMessage(hWnd, hInstance); + return TRUE; } LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { + static HBITMAP sprite = NULL; + static COORD spritePosition = { 0, 0 }; + static short angle = 0; + switch (message) { + case WM_LOAD_DEFAULT_SPRITE: + if ((sprite = LoadBitmap((HINSTANCE)lParam, MAKEINTRESOURCE(IDB_SPRITE))) != NULL) + { + PostUpdateSpriteMessage(hWnd); + } + break; + case WM_UPDATE_SPRITE: + PutSpriteOnWindow(hWnd, sprite, spritePosition, angle); + break; + case WM_SIZE: + PostUpdateSpriteMessage(hWnd); + return DefWindowProc(hWnd, message, wParam, lParam); case WM_DESTROY: PostQuitMessage(0); return 0; diff --git a/Lab_1/Lab_1/Lab_1.rc b/Lab_1/Lab_1/Lab_1.rc new file mode 100644 index 0000000..b0b820c --- /dev/null +++ b/Lab_1/Lab_1/Lab_1.rc @@ -0,0 +1,69 @@ +// Microsoft Visual C++ generated resource script. +// +#include "resource.h" + +#define APSTUDIO_READONLY_SYMBOLS +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 2 resource. +// +#include "winres.h" + +///////////////////////////////////////////////////////////////////////////// +#undef APSTUDIO_READONLY_SYMBOLS + +///////////////////////////////////////////////////////////////////////////// +// English (United States) resources + +#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU) +LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US +#pragma code_page(1252) + +#ifdef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// TEXTINCLUDE +// + +1 TEXTINCLUDE +BEGIN + "resource.h\0" +END + +2 TEXTINCLUDE +BEGIN + "#include ""winres.h""\r\n" + "\0" +END + +3 TEXTINCLUDE +BEGIN + "\r\n" + "\0" +END + +#endif // APSTUDIO_INVOKED + + +///////////////////////////////////////////////////////////////////////////// +// +// Bitmap +// + +IDB_SPRITE BITMAP "sprite.bmp" + +#endif // English (United States) resources +///////////////////////////////////////////////////////////////////////////// + + + +#ifndef APSTUDIO_INVOKED +///////////////////////////////////////////////////////////////////////////// +// +// Generated from the TEXTINCLUDE 3 resource. +// + + +///////////////////////////////////////////////////////////////////////////// +#endif // not APSTUDIO_INVOKED + diff --git a/Lab_1/Lab_1/Lab_1.vcxproj b/Lab_1/Lab_1/Lab_1.vcxproj index 6bd1f26..053e416 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj +++ b/Lab_1/Lab_1/Lab_1.vcxproj @@ -141,6 +141,15 @@ + + + + + + + + + diff --git a/Lab_1/Lab_1/Lab_1.vcxproj.filters b/Lab_1/Lab_1/Lab_1.vcxproj.filters index 2954ba3..2cf59e0 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj.filters +++ b/Lab_1/Lab_1/Lab_1.vcxproj.filters @@ -19,4 +19,19 @@ Source Files + + + Header Files + + + + + Resource Files + + + + + Resource Files + + \ No newline at end of file diff --git a/Lab_1/Lab_1/resource.h b/Lab_1/Lab_1/resource.h new file mode 100644 index 0000000..47f81d8 --- /dev/null +++ b/Lab_1/Lab_1/resource.h @@ -0,0 +1,16 @@ +//{{NO_DEPENDENCIES}} +// Microsoft Visual C++ generated include file. +// Used by Lab_1.rc +// +#define IDB_SPRITE 101 + +// Next default values for new objects +// +#ifdef APSTUDIO_INVOKED +#ifndef APSTUDIO_READONLY_SYMBOLS +#define _APS_NEXT_RESOURCE_VALUE 102 +#define _APS_NEXT_COMMAND_VALUE 40001 +#define _APS_NEXT_CONTROL_VALUE 1001 +#define _APS_NEXT_SYMED_VALUE 101 +#endif +#endif diff --git a/Lab_1/Lab_1/sprite.bmp b/Lab_1/Lab_1/sprite.bmp new file mode 100644 index 0000000000000000000000000000000000000000..6e6fb38d3b891e9cc6f31bf84f62dc6b97004532 GIT binary patch literal 8310 zcmeIuu?@r^5ClLhxqvUhq3dw|?Kq|$S3zfZ;c4QfIk@G7AerlXA7$2^JkxdcR`yJ( zW9E!ca`dn2dR*6^oVjYP<;+m}+@<$1rL)FW;!y+$5FkK+009C72oNAZfB*pk1PBly SK!5-N0t5&UAV7e?-wV8cidc96 literal 0 HcmV?d00001 From 9d14586f95fb1175d458d3812e3e78317fae2933 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 22:30:58 +0300 Subject: [PATCH 08/12] feat: add the ability to move the sprite using the keyboard --- Lab_1/Lab_1/Lab_1.cpp | 130 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 4197831..35b10aa 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -7,7 +7,13 @@ #define WM_UPDATE_SPRITE (WM_USER + 1) #define WM_LOAD_DEFAULT_SPRITE (WM_USER + 2) +#define VK_W 0x57 +#define VK_A 0x41 +#define VK_S 0x53 +#define VK_D 0x44 + constexpr auto WINDOW_NAME = "Lab_1"; +constexpr auto SPRITE_STEP = 10; ATOM RegisterWindowClass(HINSTANCE); BOOL InitWindowInstance(HINSTANCE, int); @@ -23,6 +29,18 @@ bool PostUpdateSpriteMessage(HWND hWnd) return PostMessage(hWnd, WM_UPDATE_SPRITE, NULL, NULL); } +SIZE GetClientWindowSize(HWND hWnd) +{ + RECT windowRectangle; + GetClientRect(hWnd, &windowRectangle); + + SIZE windowSize; + windowSize.cx = windowRectangle.right - windowRectangle.left; + windowSize.cy = windowRectangle.bottom - windowRectangle.top; + + return windowSize; +} + SIZE GetSpriteSize(HBITMAP hBitmap) { BITMAP sprite; @@ -35,6 +53,88 @@ SIZE GetSpriteSize(HBITMAP hBitmap) return spriteSize; } +int FillWindowWithColor(HWND hWnd, COLORREF color) +{ + RECT windowRectangle; + GetClientRect(hWnd, &windowRectangle); + + HDC windowDC = GetDC(hWnd); + HBRUSH hBrush = CreateSolidBrush(color); + + int result = FillRect(windowDC, &windowRectangle, hBrush); + + DeleteObject(hBrush); + ReleaseDC(hWnd, windowDC); + + return result; +} + +COORD CreateNewSpritePosition(COORD spritePosition, COORD spriteSteps, HWND hWnd, HBITMAP sprite) +{ + SIZE windowSize = GetClientWindowSize(hWnd); + SIZE spriteSize = GetSpriteSize(sprite); + + COORD newSpritePosition; + + newSpritePosition.X = spritePosition.X + spriteSteps.X; + if (newSpritePosition.X < 0) + { + newSpritePosition.X = 0; + } + else if (newSpritePosition.X + spriteSize.cx > windowSize.cx) + { + newSpritePosition.X = (SHORT)(windowSize.cx - spriteSize.cx); + } + + newSpritePosition.Y = spritePosition.Y + spriteSteps.Y; + if (newSpritePosition.Y < 0) + { + newSpritePosition.Y = 0; + } + else if (newSpritePosition.Y + spriteSize.cy > windowSize.cy) + { + newSpritePosition.Y = (SHORT)(windowSize.cy - spriteSize.cy); + } + + return newSpritePosition; +} + +COORD CreateUpSteps() +{ + COORD steps; + steps.X = 0; + steps.Y = -SPRITE_STEP; + + return steps; +} + +COORD CreateRightSteps() +{ + COORD steps; + steps.X = SPRITE_STEP; + steps.Y = 0; + + return steps; +} + +COORD CreateDownSteps() +{ + COORD steps; + steps.X = 0; + steps.Y = SPRITE_STEP; + + return steps; +} + +COORD CreateLeftSteps() +{ + COORD steps; + steps.X = -SPRITE_STEP; + steps.Y = 0; + + return steps; +} + XFORM GetMovementXform(COORD coordinates) { XFORM xForm; @@ -137,7 +237,7 @@ ATOM RegisterWindowClass(HINSTANCE hInstance) windowClassEx.hInstance = hInstance; windowClassEx.hIcon = LoadIcon(0, IDI_WINLOGO);; windowClassEx.hCursor = LoadCursor(0, IDC_ARROW); - windowClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); + windowClassEx.hbrBackground = (HBRUSH)(COLOR_WINDOW); windowClassEx.lpszMenuName = 0; windowClassEx.lpszClassName = WINDOW_NAME; windowClassEx.hIconSm = 0; @@ -179,8 +279,36 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) } break; case WM_UPDATE_SPRITE: + FillWindowWithColor(hWnd, GetSysColor(COLOR_WINDOW)); PutSpriteOnWindow(hWnd, sprite, spritePosition, angle); break; + case WM_KEYDOWN: + switch (wParam) + { + case VK_UP: + case VK_W: + spritePosition = CreateNewSpritePosition(spritePosition, CreateUpSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + break; + case VK_RIGHT: + case VK_D: + spritePosition = CreateNewSpritePosition(spritePosition, CreateRightSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + break; + case VK_DOWN: + case VK_S: + spritePosition = CreateNewSpritePosition(spritePosition, CreateDownSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + break; + case VK_LEFT: + case VK_A: + spritePosition = CreateNewSpritePosition(spritePosition, CreateLeftSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + break; + default: + break; + } + break; case WM_SIZE: PostUpdateSpriteMessage(hWnd); return DefWindowProc(hWnd, message, wParam, lParam); From f589441cbfaa5fdfb64a9972e2bce1278e5a8940 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 22:47:14 +0300 Subject: [PATCH 09/12] feat: add the ability to move the sprite using the mouse wheel --- Lab_1/Lab_1/Lab_1.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 35b10aa..78e2049 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -309,6 +309,34 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) break; } break; + case WM_MOUSEWHEEL: + if (GET_KEYSTATE_WPARAM(wParam) == MK_SHIFT) + { + if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) + { + spritePosition = CreateNewSpritePosition(spritePosition, CreateLeftSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + } + else + { + spritePosition = CreateNewSpritePosition(spritePosition, CreateRightSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + } + } + else + { + if (GET_WHEEL_DELTA_WPARAM(wParam) > 0) + { + spritePosition = CreateNewSpritePosition(spritePosition, CreateUpSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + } + else + { + spritePosition = CreateNewSpritePosition(spritePosition, CreateDownSteps(), hWnd, sprite); + PostUpdateSpriteMessage(hWnd); + } + } + break; case WM_SIZE: PostUpdateSpriteMessage(hWnd); return DefWindowProc(hWnd, message, wParam, lParam); From 3f65c557e7ba3aae1c07025bcf4ef3d93f734f22 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Tue, 15 Sep 2020 22:53:09 +0300 Subject: [PATCH 10/12] feat: add the ability to rotate the sprite around its center --- Lab_1/Lab_1/Lab_1.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 78e2049..e526565 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -12,8 +12,12 @@ #define VK_S 0x53 #define VK_D 0x44 +#define VK_Q 0x51 +#define VK_E 0x45 + constexpr auto WINDOW_NAME = "Lab_1"; constexpr auto SPRITE_STEP = 10; +constexpr auto SPRITE_DEGREE_ROTATE_STEP = 15; ATOM RegisterWindowClass(HINSTANCE); BOOL InitWindowInstance(HINSTANCE, int); @@ -305,6 +309,14 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) spritePosition = CreateNewSpritePosition(spritePosition, CreateLeftSteps(), hWnd, sprite); PostUpdateSpriteMessage(hWnd); break; + case VK_Q: + angle -= SPRITE_DEGREE_ROTATE_STEP; + PostUpdateSpriteMessage(hWnd); + break; + case VK_E: + angle += SPRITE_DEGREE_ROTATE_STEP; + PostUpdateSpriteMessage(hWnd); + break; default: break; } @@ -346,4 +358,6 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) default: return DefWindowProc(hWnd, message, wParam, lParam); } + + return 0; } From 7e118a2534965c7621e3c4716a8f5ddd34b923f3 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Wed, 16 Sep 2020 11:58:11 +0300 Subject: [PATCH 11/12] feat: add the ability to load any sprite from the PC --- Lab_1/Lab_1/Lab_1.cpp | 87 ++++++++++++++++++++++++++++++++++++++- Lab_1/Lab_1/Lab_1.vcxproj | 1 + 2 files changed, 86 insertions(+), 2 deletions(-) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index e526565..1cf4470 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -1,9 +1,11 @@ #include +#include +#include "resource.h" + #define _USE_MATH_DEFINES #include -#include "resource.h" - +#define WM_LOAD_SPRITE WM_USER #define WM_UPDATE_SPRITE (WM_USER + 1) #define WM_LOAD_DEFAULT_SPRITE (WM_USER + 2) @@ -15,6 +17,8 @@ #define VK_Q 0x51 #define VK_E 0x45 +#define VK_L 0x4c + constexpr auto WINDOW_NAME = "Lab_1"; constexpr auto SPRITE_STEP = 10; constexpr auto SPRITE_DEGREE_ROTATE_STEP = 15; @@ -28,6 +32,12 @@ bool PostLoadDefaultSpriteMessage(HWND hWnd, HINSTANCE hInstance) return PostMessage(hWnd, WM_LOAD_DEFAULT_SPRITE, NULL, (LPARAM)hInstance); } +bool PostLoadSpriteMessage(HWND hWnd) +{ + return PostMessage(hWnd, WM_LOAD_SPRITE, NULL, NULL); +} + + bool PostUpdateSpriteMessage(HWND hWnd) { return PostMessage(hWnd, WM_UPDATE_SPRITE, NULL, NULL); @@ -169,6 +179,58 @@ XFORM GetRotationXform(short degreeAngle) return xForm; } +LRESULT LoadSprite(HWND hWnd, HBITMAP& sprite) +{ + char fileName[MAX_PATH] = { NULL }; + + OPENFILENAME openFileName; + openFileName.lStructSize = sizeof(OPENFILENAME); + openFileName.hwndOwner = hWnd; + openFileName.hInstance = NULL; + openFileName.lpstrFilter = "Images\0*.bmp;*.gif;*.jpeg;*.png;\0\0"; + openFileName.lpstrCustomFilter = NULL; + openFileName.nFilterIndex = 1; + openFileName.lpstrFile = fileName; + openFileName.nMaxFile = sizeof(fileName); + openFileName.lpstrFileTitle = NULL; + openFileName.lpstrInitialDir = NULL; + openFileName.lpstrTitle = "Select sprite"; + openFileName.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; + openFileName.lpstrDefExt = NULL; + + if (GetOpenFileName(&openFileName)) + { + int fileNameLength = MultiByteToWideChar(CP_ACP, 0, fileName, -1, NULL, 0); + WCHAR* wideCharFileName = new WCHAR[MultiByteToWideChar(CP_ACP, 0, fileName, -1, NULL, 0)]; + MultiByteToWideChar(CP_ACP, 0, fileName, -1, wideCharFileName, fileNameLength); + + Gdiplus::GdiplusStartupInput gdiplusStartupInput; + ULONG_PTR gdiplusToken; + GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL); + + Gdiplus::Bitmap* sourceImage = Gdiplus::Bitmap::FromFile(wideCharFileName); + HBITMAP hBitmap; + Gdiplus::Color imageBackgroundColor; + imageBackgroundColor.SetFromCOLORREF(GetSysColor(COLOR_WINDOW)); + Gdiplus::Status bitmapStatus = sourceImage->GetHBITMAP(imageBackgroundColor, &hBitmap); + + Gdiplus::GdiplusShutdown(gdiplusToken); + + if (bitmapStatus != Gdiplus::Ok) + return -1; + + SIZE windowSize = GetClientWindowSize(hWnd), spriteSize = GetSpriteSize(hBitmap); + if ((windowSize.cx < spriteSize.cx) || (windowSize.cy < spriteSize.cy)) + return -2; + + sprite = hBitmap; + + return 0; + } + + return 1; +} + bool PutSpriteOnWindow(HWND hWnd, HBITMAP sprite, COORD coordinates, short angle) { HDC windowDC = GetDC(hWnd); @@ -278,8 +340,26 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { case WM_LOAD_DEFAULT_SPRITE: if ((sprite = LoadBitmap((HINSTANCE)lParam, MAKEINTRESOURCE(IDB_SPRITE))) != NULL) + PostUpdateSpriteMessage(hWnd); + break; + case WM_LOAD_SPRITE: + switch (LoadSprite(hWnd, sprite)) { + case 0: + spritePosition = { 0, 0 }; + angle = 0; PostUpdateSpriteMessage(hWnd); + break; + case -1: + MessageBox(hWnd, "An unknown error occurred while loading the sprite.", + "Unknown error", MB_OK | MB_ICONERROR); + break; + case -2: + MessageBox(hWnd, "The sprite does not fit into the window.", + "Large sprite size", MB_OK | MB_ICONERROR); + break; + default: + break; } break; case WM_UPDATE_SPRITE: @@ -317,6 +397,9 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) angle += SPRITE_DEGREE_ROTATE_STEP; PostUpdateSpriteMessage(hWnd); break; + case VK_L: + PostLoadSpriteMessage(hWnd); + break; default: break; } diff --git a/Lab_1/Lab_1/Lab_1.vcxproj b/Lab_1/Lab_1/Lab_1.vcxproj index 053e416..646cc84 100644 --- a/Lab_1/Lab_1/Lab_1.vcxproj +++ b/Lab_1/Lab_1/Lab_1.vcxproj @@ -92,6 +92,7 @@ Windows true + gdiplus.lib;%(AdditionalDependencies) From 33b534955a05708b16f4c4610767cd4cb696d3c6 Mon Sep 17 00:00:00 2001 From: Shulga Konstantin Date: Wed, 16 Sep 2020 13:24:07 +0300 Subject: [PATCH 12/12] chore: clear the code --- Lab_1/Lab_1/Lab_1.cpp | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/Lab_1/Lab_1/Lab_1.cpp b/Lab_1/Lab_1/Lab_1.cpp index 1cf4470..8144d60 100644 --- a/Lab_1/Lab_1/Lab_1.cpp +++ b/Lab_1/Lab_1/Lab_1.cpp @@ -9,20 +9,20 @@ #define WM_UPDATE_SPRITE (WM_USER + 1) #define WM_LOAD_DEFAULT_SPRITE (WM_USER + 2) -#define VK_W 0x57 -#define VK_A 0x41 -#define VK_S 0x53 -#define VK_D 0x44 - -#define VK_Q 0x51 -#define VK_E 0x45 - -#define VK_L 0x4c - constexpr auto WINDOW_NAME = "Lab_1"; constexpr auto SPRITE_STEP = 10; constexpr auto SPRITE_DEGREE_ROTATE_STEP = 15; +constexpr auto VK_W = 0x57; +constexpr auto VK_A = 0x41; +constexpr auto VK_S = 0x53; +constexpr auto VK_D = 0x44; + +constexpr auto VK_Q = 0x51; +constexpr auto VK_E = 0x45; + +constexpr auto VK_L = 0x4c; + ATOM RegisterWindowClass(HINSTANCE); BOOL InitWindowInstance(HINSTANCE, int); LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); @@ -432,6 +432,8 @@ LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) } } break; + case WM_ERASEBKGND: + return 1; case WM_SIZE: PostUpdateSpriteMessage(hWnd); return DefWindowProc(hWnd, message, wParam, lParam);