Creating a Window with Raftel Engine

In this tutorial, you will learn how to create a simple window using Raftel Engine. This is the first step in building any graphical application with the engine.

Prerequisites

Before you begin, make sure you have:

Creating a Window

Step 1: Include Required Headers

Start by including the Raftel window system header in your program:

Including Required Headers
#include "raftel/window.hpp"

Step 2: Initialize the Window System

Create an instance of the window system:

Initializing Window System
auto windowSystemOpt = Raftel::WindowSystem::make();

The Window System

The WindowSystem class manages the platform-specific window functionality. It must be created before any windows can be created.

Step 3: Create a Window

Using the window system, create a new window with a title:

Creating a Window
auto windowOpt = Raftel::Window::make("WindowTest", *windowSystemOpt);
if (!windowOpt) {
  std::cerr << "Can't create window\n";
  return -1;
}

Error Handling

Always check if the window was created successfully. The make method returns an optional that contains the window instance if successful.

Step 4: Set the Window Context

Before rendering anything, set the window as the current OpenGL context:

Setting Context
windowOpt->MakeContextCurrent();

Step 5: Create the Main Render Loop

Create a loop that keeps the window open until the user closes it:

Main Render Loop
while (!windowOpt->ShouldClose()) {
  windowOpt->clear();
  windowOpt->swapBuffers();
}

The Render Loop

The render loop performs the following operations:

  • ShouldClose(): Checks if the window should close (e.g., if the user clicked the close button)
  • clear(): Clears the window's color and depth buffers
  • swapBuffers(): Swaps the front and back buffers to display the rendered frame

Complete Example

Here's the complete code for creating a window with Raftel Engine:

Complete Code
#include "raftel/window.hpp"

int main(void) {
  auto windowSystemOpt = Raftel::WindowSystem::make();
  auto windowOpt = Raftel::Window::make("WindowTest", *windowSystemOpt);
  if (!windowOpt) {
      std::cerr << "Can't create window\n";
      return -1;
  }

  windowOpt->MakeContextCurrent();
  while (!windowOpt->ShouldClose()) {
      windowOpt->clear();
      windowOpt->swapBuffers();
  }

  return 0;
}

Next Steps

Now that you've created a window, you can proceed to the next tutorial to learn how to render basic shapes:

Drawing a Triangle

Learn how to render a simple triangle using vertices and shaders.

Handling Input

Discover how to capture and respond to user input through the keyboard and mouse.

For complete API details, see the Window Class Reference in the API documentation.