Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <vector>
- #include <thread>
- #include <mutex>
- #include <atomic>
- #include <memory> // for std::unique_ptr
- // Abstract base class for shapes
- class Shape {
- public:
- virtual void draw(HDC hdc) = 0;
- virtual ~Shape() = default;
- };
- // Concrete shape classes
- class Circle : public Shape {
- public:
- Circle(int x, int y, int r) : x(x), y(y), radius(r) {}
- void draw(HDC hdc) override {
- Ellipse(hdc, x - radius, y - radius, x + radius, y + radius);
- }
- Circle(const Circle& other) : x(other.x), y(other.y), radius(other.radius) { /* Copy Constructor */ }
- Circle(Circle&& other) noexcept : x(other.x), y(other.y), radius(other.radius) { /* Move Constructor */ other.x = other.y = other.radius = 0; }
- private:
- int x, y, radius;
- };
- class RectangleShape : public Shape {
- public:
- RectangleShape(int x, int y, int w, int h) : x(x), y(y), width(w), height(h) {}
- void draw(HDC hdc) override {
- ::Rectangle(hdc, x, y, x + width, y + height); // Use :: to refer to global Rectangle function
- }
- private:
- int x, y, width, height;
- };
- // ColoredShape combines both Shape and color information
- class ColoredShape : public Shape {
- public:
- ColoredShape(std::unique_ptr<Shape> shape, COLORREF color)
- : shape_(std::move(shape)), color_(color) {}
- void draw(HDC hdc) override {
- HBRUSH brush = CreateSolidBrush(color_);
- HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
- shape_->draw(hdc); // Delegate drawing to the encapsulated shape
- SelectObject(hdc, oldBrush);
- DeleteObject(brush);
- }
- private:
- std::unique_ptr<Shape> shape_;
- COLORREF color_;
- };
- // Thread-safe drawing board
- class DrawingBoard {
- public:
- void addShape(std::unique_ptr<Shape> shape) {
- std::lock_guard<std::mutex> lock(mutex_);
- shapes_.push_back(std::move(shape));
- }
- void drawAll(HDC hdc) {
- std::lock_guard<std::mutex> lock(mutex_);
- for (auto& shape : shapes_) {
- shape->draw(hdc);
- }
- }
- private:
- std::vector<std::unique_ptr<Shape>> shapes_;
- std::mutex mutex_;
- };
- // Global variables
- DrawingBoard g_board;
- std::atomic<bool> g_shouldExit(false);
- // Window procedure callback
- LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
- switch (uMsg) {
- case WM_PAINT: {
- PAINTSTRUCT ps;
- HDC hdc = BeginPaint(hwnd, &ps);
- g_board.drawAll(hdc);
- EndPaint(hwnd, &ps);
- return 0;
- }
- case WM_DESTROY:
- g_shouldExit = true;
- PostQuitMessage(0);
- return 0;
- }
- return DefWindowProc(hwnd, uMsg, wParam, lParam);
- }
- void drawingThread(HWND hwnd) {
- while (!g_shouldExit) {
- InvalidateRect(hwnd, NULL, TRUE);
- Sleep(100); // Simulate work
- }
- }
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
- const wchar_t CLASS_NAME[] = L"Sample Window Class";
- WNDCLASS wc = {};
- wc.lpfnWndProc = WindowProc;
- wc.hInstance = hInstance;
- wc.lpszClassName = CLASS_NAME;
- RegisterClass(&wc);
- HWND hwnd = CreateWindowEx(
- 0, CLASS_NAME, L"OOP Win32 Demo", WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
- NULL, NULL, hInstance, NULL);
- if (hwnd == NULL) return 0;
- ShowWindow(hwnd, nCmdShow);
- // Add shapes to the board
- g_board.addShape(std::make_unique<Circle>(100, 100, 50));
- g_board.addShape(std::make_unique<ColoredShape>(std::make_unique<RectangleShape>(150, 150, 100, 50), RGB(255, 0, 0)));
- // Start drawing thread
- std::thread drawThread(drawingThread, hwnd);
- // Message loop
- MSG msg = {};
- while (GetMessage(&msg, NULL, 0, 0)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- drawThread.join();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement