From 6cc15c19890dbf9f46e09f03d106599d9b96829e Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Mon, 11 Nov 2024 19:26:08 +0300 Subject: [PATCH] Second painter and bugfixes --- src/main.cpp | 7 +++++-- src/painter.cpp | 38 +++++++++++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 2f3d890..b76397b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -59,7 +59,7 @@ int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInst, LPSTR lpszArgs, ); // Настройка частей StatusBar'а - int statwidths[] = {150, 300, -1}; + int statwidths[] = {150, 300, 450, -1}; SendMessageA(hWndStatusBar, SB_SETPARTS, sizeof(statwidths) / sizeof(int), (LPARAM) statwidths); ShowWindow(hWnd, nWinMode); // Показать окно @@ -174,6 +174,8 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPara case WM_MOUSEMOVE: { char str[256]; + SendMessageA(hWndStatusBar, SB_SETTEXTA, 3, (LPARAM) "F4: Сменить рисунок"); + // Устанавливаем текст в разных частях StatusBar'а // Экранные координаты курсора мыши sprintf_s(str, "X = %d, Y = %d", LOWORD(lParam), HIWORD(lParam)); @@ -210,7 +212,7 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPara if (wParam == VK_F4) { auto newPtr = reinterpret_cast(GetWindowLongPtr(hWnd, sizeof(PainterState *))); newPtr++; - if (newPtr == nullptr) + if (*newPtr == nullptr) newPtr = &predefined_painters[0]; SetWindowLongPtr(hWnd, sizeof(PainterState *), (LONG_PTR) newPtr); @@ -229,6 +231,7 @@ LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPara break; + case WM_DESTROY: delete reinterpret_cast(GetWindowLongPtr(hWnd, 0)); SetWindowLongPtr(hWnd, 0, reinterpret_cast(nullptr)); diff --git a/src/painter.cpp b/src/painter.cpp index c019675..5499f29 100644 --- a/src/painter.cpp +++ b/src/painter.cpp @@ -35,11 +35,47 @@ public: } }; +class Variant3S1Painter : public Painter { +public: + void draw(PainterState *state, Frame *frame) const override { + int W = frame->width, H = frame->height; + // Размер рисунка возьмём меньше (7 / 8), чтобы он не касался границ экрана + float a = 7.0f / 8 * ((W < H) ? W - 1 : H - 1); + if (a < 1) return; // Если окно очень маленькое, то ничего не рисуем + float angle = state->angle; // Угол поворота + a = a / 2; + + Point center{W / 2, H / 2}; + + + + // Рисуем описанную окружность + draw_circle(frame, center, a, COLOR(0, 0, 0)); + draw_polyline( + frame, COLOR{0, 200, 0}, + center.moved(0, a).rotated_around(center, 0 + angle), + center.moved(0, a).rotated_around(center, +2.0944 + angle), + center.moved(0, a).rotated_around(center, -2.0944 + angle) + ); + draw_circle(frame, center, a / 2, COLOR(0, 200, 0)); + draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, angle), a / 6, COLOR(0, 200, 0)); + draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, +2.0944 + angle), a / 6, COLOR(0, 200, 0)); + draw_circle(frame, center.moved(0, a * 2 / 3).rotated_around(center, -2.0944 + angle), a / 6, COLOR(0, 200, 0)); + + // Рисуем пиксель, на который кликнул пользователь + if (state->clicked_pixel.x >= 0 && state->clicked_pixel.x < W && + state->clicked_pixel.y >= 0 && state->clicked_pixel.y < H) + frame->set_pixel(state->clicked_pixel.x, state->clicked_pixel.y, {34, 175, 60}); // Пиксель зелёного цвета + } +}; + static auto predefined_painters_ = std::make_tuple( - DemoPainter() + DemoPainter(), + Variant3S1Painter() ); Painter const *const predefined_painters[] = { &std::get<0>(predefined_painters_), + &std::get<1>(predefined_painters_), nullptr }; \ No newline at end of file