alien_fx_fiend

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

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