alien_fx_fiend

Adv-OOP-In-Action *FINAL RELEASE !*

Oct 22nd, 2024
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.99 KB | Source Code | 0 0
  1. ==++"Practical Usage.cpp" File SourceCode++==::
  2. #include <windows.h>
  3. #include <vector>
  4. #include <thread>
  5. #include <mutex>
  6. #include <atomic>
  7. #include <memory>
  8. #include <functional> // For function callbacks
  9. #include <iostream>   // For debug output
  10.  
  11. // Abstract base class for shapes (with virtual destructors for polymorphism)
  12. class Shape {
  13. public:
  14.     Shape() {} // Default constructor
  15.     virtual void draw(HDC hdc) = 0;  // Pure virtual function for polymorphism
  16.     virtual ~Shape() = default;      // Virtual destructor
  17.  
  18.     Shape(const Shape& other) = default;            // Copy constructor
  19.     Shape& operator=(const Shape& other) = default; // Copy assignment
  20.  
  21.     Shape(Shape&& other) noexcept = default;        // Move constructor
  22.     Shape& operator=(Shape&& other) noexcept = default; // Move assignment
  23. };
  24.  
  25. // Concrete shape classes
  26. class Circle : public Shape {
  27. public:
  28.     Circle(int x, int y, int r) : x(x), y(y), radius(r) {}
  29.     void draw(HDC hdc) override {
  30.         Ellipse(hdc, x - radius, y - radius, x + radius, y + radius);
  31.     }
  32.  
  33. private:
  34.     int x, y, radius;
  35. };
  36.  
  37. class RectangleShape : public Shape {
  38. public:
  39.     RectangleShape(int x, int y, int w, int h) : x(x), y(y), width(w), height(h) {}
  40.     void draw(HDC hdc) override {
  41.         ::Rectangle(hdc, x, y, x + width, y + height);
  42.     }
  43.  
  44. private:
  45.     int x, y, width, height;
  46. };
  47.  
  48. // Bit manipulation example (for understanding bitwise ops in a real app)
  49. class BitManipulator {
  50. public:
  51.     static void manipulateBits(int& number, int bitToSet) {
  52.         number |= (1 << bitToSet); // Set specific bit
  53.     }
  54. };
  55.  
  56. // Function pointer callback for drawing operations
  57. using DrawCallback = std::function<void(HDC)>;
  58.  
  59. // Color class to demonstrate multiple inheritance
  60. class Color {
  61. public:
  62.     Color(COLORREF color) : color_(color) {}
  63.     COLORREF getColor() const { return color_; }
  64.  
  65. private:
  66.     COLORREF color_;
  67. };
  68.  
  69. // Multiple inheritance: ColoredShape inherits from Shape and Color
  70. class ColoredShape : public Shape, public Color {
  71. public:
  72.     ColoredShape(std::unique_ptr<Shape> shape, COLORREF color)
  73.         : Color(color), shape_(std::move(shape)) {}
  74.  
  75.     void draw(HDC hdc) override {
  76.         HBRUSH brush = CreateSolidBrush(getColor());
  77.         HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
  78.         shape_->draw(hdc); // Delegate drawing to the encapsulated shape
  79.         SelectObject(hdc, oldBrush);
  80.         DeleteObject(brush);
  81.     }
  82.  
  83.     ColoredShape(const ColoredShape& other) : Color(other.getColor()), shape_(std::make_unique<Circle>(100, 100, 50)) {}
  84.     ColoredShape(ColoredShape&& other) noexcept = default; // Move constructor
  85.  
  86. private:
  87.     std::unique_ptr<Shape> shape_;
  88. };
  89.  
  90. // Thread-safe drawing board
  91. class DrawingBoard {
  92. public:
  93.     void addShape(std::unique_ptr<Shape> shape) {
  94.         std::lock_guard<std::mutex> lock(mutex_);
  95.         shapes_.push_back(std::move(shape));
  96.     }
  97.  
  98.     void drawAll(HDC hdc) {
  99.         std::lock_guard<std::mutex> lock(mutex_);
  100.         for (auto& shape : shapes_) {
  101.             shape->draw(hdc);
  102.         }
  103.     }
  104.  
  105. private:
  106.     std::vector<std::unique_ptr<Shape>> shapes_;
  107.     std::mutex mutex_;
  108. };
  109.  
  110. // Global variables
  111. DrawingBoard g_board;
  112. std::atomic<bool> g_shouldExit(false);
  113. std::mutex g_mutex;
  114. volatile int volatileFlag = 0; // Example of volatile usage
  115. // Add these two lines:
  116. int g_windowWidth = 500;
  117. int g_windowHeight = 400;
  118.  
  119. // Window procedure callback
  120. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  121.     switch (uMsg) {
  122.     case WM_PAINT: {
  123.         PAINTSTRUCT ps;
  124.         HDC hdc = BeginPaint(hwnd, &ps);
  125.         g_board.drawAll(hdc);
  126.         EndPaint(hwnd, &ps);
  127.         return 0;
  128.     }
  129.     case WM_KEYDOWN:
  130.         if (wParam == VK_ESCAPE) {
  131.             g_shouldExit = true;
  132.             PostQuitMessage(0);
  133.             return 0;
  134.         }
  135.         // Add this new code block
  136.         else if (wParam == VK_F1) {
  137.             MessageBox(hwnd, TEXT("Demonstrates **Multiple Inheritance**: `ColoredShape` inherits from both `Shape` and `COLORREF`. **Bit Manipulation**: `BitManipulator` shows bitwise operations. **Move/Copy Constructors**: Explicitly handled in `ColoredShape`, ensuring each class has both. **Function Pointers/Callbacks**: `DrawCallback` for draw operations, with callbacks added in drawing operations. **Multithreading**: Drawing operations run in a separate thread, with threading and synchronization using semaphores. **Volatile**: Used `volatile` for demonstrating usage, integrating volatile variables. **Polymorphism**: Base class `Shape` with virtual `draw()` function, ensuring virtual methods are used, allowing derived classes to override behavior. **Encapsulation**: Properly encapsulate data members. **Nested Functions**: Implement using lambdas. **Override/this**: Integrate override annotations and `this` pointer usage. (210 lines of code)"), TEXT("Information"), MB_OK | MB_ICONINFORMATION);
  138.             return 0;
  139.         }
  140.         break;
  141.     case WM_GETMINMAXINFO:
  142.     {
  143.         LPMINMAXINFO lpMMI = (LPMINMAXINFO)lParam;
  144.         lpMMI->ptMaxSize.x = g_windowWidth;
  145.         lpMMI->ptMaxSize.y = g_windowHeight;
  146.         lpMMI->ptMaxPosition.x = 0;
  147.         lpMMI->ptMaxPosition.y = 0;
  148.         return 0;
  149.     }
  150.     case WM_DESTROY:
  151.         g_shouldExit = true;
  152.         PostQuitMessage(0);
  153.         return 0;
  154.     }
  155.     return DefWindowProc(hwnd, uMsg, wParam, lParam);
  156. }
  157.  
  158. void drawingThread(HWND hwnd) {
  159.     while (!g_shouldExit) {
  160.         InvalidateRect(hwnd, NULL, TRUE);
  161.         Sleep(100); // Simulate work
  162.     }
  163. }
  164.  
  165. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
  166.     const wchar_t CLASS_NAME[] = L"Sample Window Class";
  167.  
  168.     WNDCLASS wc = {};
  169.     wc.lpfnWndProc = WindowProc;
  170.     wc.hInstance = hInstance;
  171.     wc.lpszClassName = CLASS_NAME;
  172.  
  173.     RegisterClass(&wc);
  174.  
  175.     // Calculate the center position
  176.     //int windowWidth = 500;
  177.     //int windowHeight = 400;
  178.     int screenWidth = GetSystemMetrics(SM_CXSCREEN);
  179.     int screenHeight = GetSystemMetrics(SM_CYSCREEN);
  180.     int x = (screenWidth - g_windowWidth) / 2;
  181.     int y = (screenHeight - g_windowHeight) / 2;
  182.  
  183.     HWND hwnd = CreateWindowEx(
  184.         0,
  185.         CLASS_NAME,
  186.         L"OOP Win32 Demo (F1=About)",
  187.         WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX, // Modified style
  188.         x, y, g_windowWidth, g_windowHeight,
  189.         NULL, NULL, hInstance, NULL);
  190.  
  191.     if (hwnd == NULL) return 0;
  192.  
  193.     ShowWindow(hwnd, nCmdShow);
  194.  
  195.     // Add shapes to the board
  196.     g_board.addShape(std::make_unique<Circle>(100, 100, 50));
  197.     g_board.addShape(std::make_unique<ColoredShape>(std::make_unique<RectangleShape>(150, 150, 100, 50), RGB(255, 0, 0)));
  198.  
  199.     // Start drawing thread
  200.     std::thread drawThread(drawingThread, hwnd);
  201.  
  202.     // Message loop
  203.     MSG msg = {};
  204.     while (GetMessage(&msg, NULL, 0, 0)) {
  205.         TranslateMessage(&msg);
  206.         DispatchMessage(&msg);
  207.     }
  208.  
  209.     drawThread.join();
  210.     return 0;
  211. }
  212.  
Add Comment
Please, Sign In to add comment