Window System
The Window System is a fundamental component of Raftel Engine that handles window creation, management, and OpenGL context setup. Built on top of GLFW, it provides a high-level abstraction for creating and controlling application windows for your games and applications.
Key Features
- Simplified window creation and management
- Automatic OpenGL context setup
- Input event handling
- Window resizing and fullscreen toggling
- Debug message callbacks for OpenGL errors
Architecture Overview
The Window System in Raftel Engine consists of two main classes:
WindowSystem Class
The WindowSystem
class handles the initialization and termination of the GLFW library.
It's designed to be created once at the beginning of your application and destroyed when your
application terminates.
Window Class
The Window
class represents an individual window in your application. It provides methods
for controlling the window, handling input, and managing the OpenGL context. Each window can have its
own title, size, and properties.
+----------------+ creates +----------------+
| WindowSystem | -----------------> | Window |
+----------------+ +----------------+
| - destroy_ | | - window_ |
+----------------+ | - input |
| + make() | | - width/height |
| + ~WindowSystem| | - isFullscreen |
+----------------+ +----------------+
| + make() |
| + clear() |
| + swapBuffers()|
| + toggleFS() |
+----------------+
Basic Usage
Here's a basic example of how to use the Window System to create a window and run a simple rendering loop:
#include "raftel/window.hpp"
int main() {
// Initialize window system
auto windowSystemOpt = Raftel::WindowSystem::make();
if (!windowSystemOpt) {
std::cerr << "Failed to initialize window system.\n";
return -1;
}
// Create a window
auto windowOpt = Raftel::Window::make("My Raftel Application", *windowSystemOpt);
if (!windowOpt) {
std::cerr << "Failed to create window.\n";
return -1;
}
// Make the window's OpenGL context current
windowOpt->MakeContextCurrent();
// Main loop
while (!windowOpt->ShouldClose()) {
// Clear the window
windowOpt->clear();
// Render your scene here
// ...
// Swap buffers and poll events
windowOpt->swapBuffers();
}
return 0;
}
WindowSystem Class Reference
Static Methods
Creates and initializes a new WindowSystem instance. This method initializes the GLFW library.
Returns: A unique pointer to the created WindowSystem object, or nullptr if initialization fails.
Constructor and Destructor
Default constructor for WindowSystem. This constructor is called by the make() method.
Destructor that terminates the GLFW library if necessary. This ensures proper cleanup of GLFW resources.
Move Semantics
Move constructor for WindowSystem. This transfers ownership of resources from another WindowSystem object.
Window Class Reference
Static Methods
Creates a new window with the specified properties.
Parameters:
title
- The title of the windowws
- The window system to associate the window withwidth
- The width of the window (default 0 for fullscreen resolution)height
- The height of the window (default 0 for fullscreen resolution)fullscreen
- Whether the window should be fullscreen or not
Returns: A unique pointer to the created Window object, or nullptr if creation fails.
Window Control Methods
Makes the window context current for OpenGL operations. This method also initializes GLEW and sets up the OpenGL debug message callback.
Checks if the window should close, optionally checking for the Escape key.
Parameters:
allowEscapeKey
- Whether pressing the Escape key should cause the window to close
Returns: true if the window should close, false otherwise.
Clears the window's color and depth buffers.
Swaps the front and back buffers, displaying the rendered content. This method also polls for window events.
Gets the current size of the primary monitor.
Returns: A glm::ivec2 representing the width and height of the screen.
Toggles between fullscreen and windowed modes.
Parameters:
window
- The GLFW window object to toggle
Handles window resize events, adjusting the OpenGL viewport accordingly.
Parameters:
newWidth
- The new width of the windownewHeight
- The new height of the window
Debug Message Callback
The Window System sets up a debug message callback for OpenGL that prints detailed information about errors, warnings, and notifications. This is particularly useful during development to catch OpenGL issues early.
// Sample debug output:
API, ERROR, HIGH, 1282: Invalid operation
SHADER COMPILER, PERFORMANCE, MEDIUM, 7: Shader compilation took longer than expected
Best Practices
- Create only one WindowSystem instance for your entire application.
- Always check if window creation was successful before using the window.
- Use the window's swapBuffers() method at the end of your render loop to display your rendered content.
- Handle window resize events appropriately to ensure your application looks good at different resolutions.
- Consider disabling the escape key functionality for ShouldClose() if you want to implement your own exit confirmation dialog.
Related Topics
Creating a Window Tutorial
Step-by-step guide to creating and configuring windows in Raftel Engine.
Input System
Learn how to handle keyboard, mouse, and other input devices in your applications.
Performance Optimization
Tips for optimizing window and rendering performance.