ایجاد یک کتابخانه گرافیکی از صفر به شکل ماژولار و کلاسبندی شده یک پروژه بزرگ است. برای راهاندازی مرحله اول، ابتدا باید ساختار پایهای کتابخانه را طراحی کنیم. در این مرحله، ایجاد پنجره (Window Creation) را به عنوان اولین ماژول پیادهسازی میکنیم. این ماژول شامل کلاسهای زیر خواهد بود:
مرحله ۱: طراحی و پیادهسازی ماژول ایجاد پنجره
اهداف:
ایجاد پنجره با قابلیت تنظیم عرض، ارتفاع و عنوان.
مدیریت دوره حیات پنجره (ساخت، نابودی، بهروزرسانی).
پشتیبانی از سیستمعاملهای مختلف (Windows, Linux, macOS).
ساختار پروژه
code Copy code
MyGLFW/
├── include/
│ ├── MyGLFW.hpp // هدر اصلی
│ ├── Window.hpp // تعریف کلاس Window
├── src/
│ ├── Window.cpp // پیادهسازی Window
│ ├── platform/
│ ├── win32/ // کدهای مخصوص Windows
│ ├── x11/ // کدهای مخصوص Linux (X11)
│ ├── cocoa/ // کدهای مخصوص macOS
├── examples/
│ ├── main.cpp // تست کتابخانه
۱. کلاس Window (include/Window.hpp)
cpp Copy code
#pragma once
#include <string>
namespace MyGLFW {
class Window {
public:
Window(int width, int height, const std::string& title);
~Window();
bool create();
void destroy();
void pollEvents();
void swapBuffers();
bool shouldClose() const;
private:
struct Impl; // تعریف مخفی (Pimpl Idiom)
Impl* pImpl;
};
} // namespace MyGLFW
۲. پیادهسازی Window (src/Window.cpp)
از روش Pimpl (Pointer to Implementation) استفاده میکنیم تا جزئیات پلتفرم را از کاربر پنهان کنیم:
cpp Copy code
#include "Window.hpp"
#include <memory>
// تعریف ساختار داخلی برای پیادهسازی مخصوص پلتفرم
struct Window::Impl {
void* nativeWindow; // مثلاً HWND در Windows یا Window در X11
bool closed;
Impl(int width, int height, const std::string& title) : closed(false) {
// فراخوانی توابع مخصوص پلتفرم
#ifdef _WIN32
createWin32Window(width, height, title);
#elif __linux__
createX11Window(width, height, title);
#elif __APPLE__
createCocoaWindow(width, height, title);
#endif
}
~Impl() { destroy(); }
void createWin32Window(int width, int height, const std::string& title);
void createX11Window(int width, int height, const std::string& title);
void createCocoaWindow(int width, int height, const std::string& title);
void destroy();
};
// توابع عمومی Window
Window::Window(int width, int height, const std::string& title) {
pImpl = new Impl(width, height, title);
}
Window::~Window() {
delete pImpl;
}
bool Window::create() {
// پیادهسازی مخصوص پلتفرم در Impl
return pImpl != nullptr;
}
void Window::destroy() {
pImpl->destroy();
}
void Window::pollEvents() {
// مدیریت رویدادهای پلتفرم
}
void Window::swapBuffers() {
// تبادل بافر برای رندرینگ دوگانه (Double Buffering)
}
bool Window::shouldClose() const {
return pImpl->closed;
}
۳. پیادهسازی مخصوص پلتفرم (src/platform/win32/Win32Window.cpp)
مثال برای Windows با استفاده از Win32 API:
cpp Copy code
#include "../Window.hpp"
#include <windows.h>
void Window::Impl::createWin32Window(int width, int height, const std::string& title) {
// ثبت کلاس پنجره
WNDCLASS wc = {};
wc.lpfnWndProc = DefWindowProc;
wc.hInstance = GetModuleHandle(nullptr);
wc.lpszClassName = "MyGLFWWindowClass";
RegisterClass(&wc);
// ایجاد پنجره
HWND hwnd = CreateWindow(
"MyGLFWWindowClass", title.c_str(),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
width, height,
nullptr, nullptr, GetModuleHandle(nullptr), nullptr
);
if (hwnd) {
ShowWindow(hwnd, SW_SHOW);
nativeWindow = hwnd;
} else {
closed = true;
}
}
void Window::Impl::destroy() {
HWND hwnd = static_cast<HWND>(nativeWindow);
if (hwnd) {
DestroyWindow(hwnd);
nativeWindow = nullptr;
}
}
۴. تست کتابخانه (examples/main.cpp)
cpp Copy code
#include "MyGLFW/Window.hpp"
#include <iostream>
int main() {
MyGLFW::Window window(800, 600, "MyGLFW Window");
if (!window.create()) {
std::cerr << "Failed to create window!" << std::endl;
return -1;
}
while (!window.shouldClose()) {
window.pollEvents();
// کد رندرینگ OpenGL را اینجا اضافه کنید
window.swapBuffers();
}
window.destroy();
return 0;
}
نکات مهم برای توسعه
جدا کردن مسئولیتها: هر ماژول (مثلاً Input، Time، Context) را در فایلهای جداگانه پیادهسازی کنید.
پشتیبانی چندپلتفرمی: از دایرکتیوهای پیشپردازنده (#ifdef _WIN32, #elif __linux__) برای مدیریت کد مخصوص پلتفرم استفاده کنید.
مدیریت منابع: از قراردادهای RAII برای مدیریت منابع (مثل unique_ptr یا shared_ptr) استفاده کنید.
توثیق: مستنداتی برای هر کلاس و تابع بنویسید (مثلاً Doxygen).
مرحله بعدی
در مرحله بعد، ماژول مدیریت رویدادهای ورودی (Input Handling) را اضافه خواهیم کرد (کلید، موس، لمس). این ماژول شامل کلاسهای زیر خواهد بود:
هر مرحله را میتوانید به صورت مستقل تست کنید و سپس به کتابخانه اصلی متصل کنید.