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.

Class Relationship Diagram

+----------------+       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:

Basic Window Creation
#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

static std::unique_ptr<WindowSystem> make()

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

WindowSystem()

Default constructor for WindowSystem. This constructor is called by the make() method.

~WindowSystem() noexcept

Destructor that terminates the GLFW library if necessary. This ensures proper cleanup of GLFW resources.

Move Semantics

WindowSystem(WindowSystem&& other) noexcept

Move constructor for WindowSystem. This transfers ownership of resources from another WindowSystem object.

Window Class Reference

Static Methods

static std::unique_ptr<Window> make(const char* title, WindowSystem& ws, int width = 0, int height = 0, bool fullscreen = false)

Creates a new window with the specified properties.

Parameters:

  • title - The title of the window
  • ws - The window system to associate the window with
  • width - 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

void MakeContextCurrent()

Makes the window context current for OpenGL operations. This method also initializes GLEW and sets up the OpenGL debug message callback.

bool ShouldClose(bool allowEscapeKey = true)

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.

void clear()

Clears the window's color and depth buffers.

void swapBuffers()

Swaps the front and back buffers, displaying the rendered content. This method also polls for window events.

glm::ivec2 getScreenSize()

Gets the current size of the primary monitor.

Returns: A glm::ivec2 representing the width and height of the screen.

void toggleFullscreen(GLFWwindow* window)

Toggles between fullscreen and windowed modes.

Parameters:

  • window - The GLFW window object to toggle
void onResize(int newWidth, int newHeight)

Handles window resize events, adjusting the OpenGL viewport accordingly.

Parameters:

  • newWidth - The new width of the window
  • newHeight - 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.

Debug Output Example
// Sample debug output:
API, ERROR, HIGH, 1282: Invalid operation
SHADER COMPILER, PERFORMANCE, MEDIUM, 7: Shader compilation took longer than expected

Best Practices

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.