Advertisement
alien_fx_fiend

Advanced-OOP-In-Action (Practical Usage)

Sep 15th, 2024
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #include <windows.h>
  2. #include <vector>
  3. #include <thread>
  4. #include <mutex>
  5. #include <atomic>
  6. #include <memory> // for std::unique_ptr
  7.  
  8. // Abstract base class for shapes
  9. class Shape {
  10. public:
  11.     virtual void draw(HDC hdc) = 0;
  12.     virtual ~Shape() = default;
  13. };
  14.  
  15. // Concrete shape classes
  16. class Circle : public Shape {
  17. public:
  18.     Circle(int x, int y, int r) : x(x), y(y), radius(r) {}
  19.     void draw(HDC hdc) override {
  20.         Ellipse(hdc, x - radius, y - radius, x + radius, y + radius);
  21.     }
  22.     Circle(const Circle& other) : x(other.x), y(other.y), radius(other.radius) { /* Copy Constructor */ }
  23.     Circle(Circle&& other) noexcept : x(other.x), y(other.y), radius(other.radius) { /* Move Constructor */ other.x = other.y = other.radius = 0; }
  24.  
  25. private:
  26.     int x, y, radius;
  27. };
  28.  
  29. class RectangleShape : public Shape {
  30. public:
  31.     RectangleShape(int x, int y, int w, int h) : x(x), y(y), width(w), height(h) {}
  32.     void draw(HDC hdc) override {
  33.         ::Rectangle(hdc, x, y, x + width, y + height); // Use :: to refer to global Rectangle function
  34.     }
  35.  
  36. private:
  37.     int x, y, width, height;
  38. };
  39.  
  40. // ColoredShape combines both Shape and color information
  41. class ColoredShape : public Shape {
  42. public:
  43.     ColoredShape(std::unique_ptr<Shape> shape, COLORREF color)
  44.         : shape_(std::move(shape)), color_(color) {}
  45.  
  46.     void draw(HDC hdc) override {
  47.         HBRUSH brush = CreateSolidBrush(color_);
  48.         HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
  49.         shape_->draw(hdc); // Delegate drawing to the encapsulated shape
  50.         SelectObject(hdc, oldBrush);
  51.         DeleteObject(brush);
  52.     }
  53.  
  54. private:
  55.     std::unique_ptr<Shape> shape_;
  56.     COLORREF color_;
  57. };
  58.  
  59. // Thread-safe drawing board
  60. class DrawingBoard {
  61. public:
  62.     void addShape(std::unique_ptr<Shape> shape) {
  63.         std::lock_guard<std::mutex> lock(mutex_);
  64.         shapes_.push_back(std::move(shape));
  65.     }
  66.  
  67.     void drawAll(HDC hdc) {
  68.         std::lock_guard<std::mutex> lock(mutex_);
  69.         for (auto& shape : shapes_) {
  70.             shape->draw(hdc);
  71.         }
  72.     }
  73.  
  74. private:
  75.     std::vector<std::unique_ptr<Shape>> shapes_;
  76.     std::mutex mutex_;
  77. };
  78.  
  79. // Global variables
  80. DrawingBoard g_board;
  81. std::atomic<bool> g_shouldExit(false);
  82.  
  83. // Window procedure callback
  84. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  85.     switch (uMsg) {
  86.     case WM_PAINT: {
  87.         PAINTSTRUCT ps;
  88.         HDC hdc = BeginPaint(hwnd, &ps);
  89.         g_board.drawAll(hdc);
  90.         EndPaint(hwnd, &ps);
  91.         return 0;
  92.     }
  93.     case WM_DESTROY:
  94.         g_shouldExit = true;
  95.         PostQuitMessage(0);
  96.         return 0;
  97.     }
  98.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  99. }
  100.  
  101. void drawingThread(HWND hwnd) {
  102.     while (!g_shouldExit) {
  103.         InvalidateRect(hwnd, NULL, TRUE);
  104.         Sleep(100); // Simulate work
  105.     }
  106. }
  107.  
  108. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  109.     const wchar_t CLASS_NAME[] = L"Sample Window Class";
  110.  
  111.     WNDCLASS wc = {};
  112.     wc.lpfnWndProc = WindowProc;
  113.     wc.hInstance = hInstance;
  114.     wc.lpszClassName = CLASS_NAME;
  115.  
  116.     RegisterClass(&wc);
  117.  
  118.     HWND hwnd = CreateWindowEx(
  119.         0, CLASS_NAME, L"OOP Win32 Demo", WS_OVERLAPPEDWINDOW,
  120.         CW_USEDEFAULT, CW_USEDEFAULT, 500, 400,
  121.         NULL, NULL, hInstance, NULL);
  122.  
  123.     if (hwnd == NULL) return 0;
  124.  
  125.     ShowWindow(hwnd, nCmdShow);
  126.  
  127.     // Add shapes to the board
  128.     g_board.addShape(std::make_unique<Circle>(100, 100, 50));
  129.     g_board.addShape(std::make_unique<ColoredShape>(std::make_unique<RectangleShape>(150, 150, 100, 50), RGB(255, 0, 0)));
  130.  
  131.     // Start drawing thread
  132.     std::thread drawThread(drawingThread, hwnd);
  133.  
  134.     // Message loop
  135.     MSG msg = {};
  136.     while (GetMessage(&msg, NULL, 0, 0)) {
  137.         TranslateMessage(&msg);
  138.         DispatchMessage(&msg);
  139.     }
  140.  
  141.     drawThread.join();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement