Skip to main content

GLFWFactory.ixx File

Factory functions for creating GLFW-based Application and Window instances. More...

Included Headers

Namespaces Index

namespacehelios
namespaceext

Platform-specific extensions and backend implementations. More...

namespaceglfw

GLFW-specific implementations. More...

namespaceapp

GLFW application infrastructure. More...

Classes Index

classGLFWFactory

Factory class for creating pre-configured GLFW-specific application components. More...

Description

Factory functions for creating GLFW-based Application and Window instances.

File Listing

The file content with the documentation metadata removed is:

1/**
2 * @file GLFWFactory.ixx
3 * @brief Factory functions for creating GLFW-based Application and Window instances.
4 */
5module;
6
7#include <GLFW/glfw3.h>
8#include <format>
9#include <memory>
10#include <string>
11
12export module helios.ext.glfw.app.GLFWFactory;
13
14import helios.window;
15import helios.input;
16import helios.event;
17import helios.app.controller.BasicWindowRenderingController;
18import helios.util.Guid;
19import helios.event.EventManager;
20
21import helios.ext.glfw.app.GLFWApplication;
22import helios.ext.glfw.window.GLFWWindowConfig;
23import helios.ext.glfw.window.GLFWWindowUserPointer;
24import helios.ext.glfw.window.GLFWWindow;
25import helios.ext.glfw.input.GLFWInputAdapter;
26
27import helios.rendering.material.Material;
28import helios.rendering.RenderTarget;
29
30import helios.ext.opengl.rendering;
31
32export namespace helios::ext::glfw::app {
33
34
35 /**
36 * @brief Factory class for creating pre-configured GLFW-specific application components.
37 *
38 * Eases setup of examples and default applications by reducing required boilerplate code.
39 */
40 class GLFWFactory {
41
42 public:
43
44 /**
45 * @brief Creates a pre-configured GLFWApplication instance for OpenGL rendering.
46 *
47 * @param title The default title used with the application's main window.
48 * @param width The width for the main window.
49 * @param height The height for the main window.
50 * @param aspectRatioNumer Aspect ratio numerator.
51 * @param aspectRatioDenom Aspect ratio denominator.
52 *
53 * @return A unique_ptr to the newly created GLFWApplication.
54 */
55 static std::unique_ptr<GLFWApplication> makeOpenGLApp(
56 std::string title,
57 int width = 800, int height = 600,
58 int aspectRatioNumer = 0, int aspectRatioDenom = 0
59 ) {
60 auto openGLDevice = std::make_unique<helios::ext::opengl::rendering::OpenGLDevice>(
61 std::make_unique<helios::ext::opengl::rendering::OpenGLMeshRenderer>(),
62 std::make_unique<helios::ext::opengl::rendering::OpenGLGlyphTextRenderer>(),
63 std::make_unique<helios::ext::opengl::rendering::FreeTypeFontResourceManager>()
64 );
65 auto deadzoneStrategy = std::make_unique<helios::input::gamepad::RadialDeadzoneStrategy>();
66 auto inputManager = std::make_unique<helios::input::InputManager>(
67 std::make_unique<helios::ext::glfw::input::GLFWInputAdapter>(std::move(deadzoneStrategy))
68 );
69 auto eventManager = std::make_unique<helios::event::BasicEventManager>(
70 std::make_unique<helios::event::DequeEventQueue>(),
71 std::make_unique<helios::event::Dispatcher>()
72 );
73
74 std::unique_ptr<GLFWApplication> app = std::make_unique<GLFWApplication>(
75 std::move(openGLDevice),
76 std::move(inputManager),
77 std::move(eventManager)
78 );
79
80 app->init();
81 auto cfg = makeWindowCfg(
82 std::move(title),
83 width,
84 height,
85 aspectRatioNumer,
86 aspectRatioDenom
87 );
88 auto renderTarget = std::make_unique<helios::rendering::RenderTarget>();
89
90 helios::ext::glfw::window::GLFWWindow& win = app->createWindow(std::move(renderTarget), cfg);
91 app->addController(std::make_unique<helios::app::controller::BasicWindowRenderingController>(win));
92
93 // set the window user pointer so the frameBufferSizeCallback does not break
94 win.setWindowUserPointer(std::make_unique<helios::ext::glfw::window::GLFWWindowUserPointer>(
95 app.get(),
96 &win
97 ));
98
99 app->setCurrent(win);
100
101 return std::move(app);
102 }
103
104
105 /**
106 * @brief Creates a default `GLFWWindowConfig` with the specific title.
107 *
108 * This method also makes sure that a proper frameBufferSizeCallback is configured,
109 * assuming this config is used with a Window created by an application.
110 *
111 * @param title The title for the window configuration.
112 * @param width The width for the main window.
113 * @param height The height for the main window.
114 * @param aspectRatioNumer Aspect ratio numerator.
115 * @param aspectRatioDenom Aspect ratio denominator.
116 *
117 * @return A `GLFWWindowConfig` object with default properties like height and width.
118 */
120 std::string title, int width = 800, int height = 600,
121 int aspectRatioNumer = 0, int aspectRatioDenom = 0
122 ) {
124 cfg.title = std::move(title);
125 cfg.width = width;
126 cfg.height = height;
127 cfg.aspectRatioNumer = aspectRatioNumer;
128 cfg.aspectRatioDenom = aspectRatioDenom;
129
130 cfg.frameBufferSizeCallback = [] (GLFWwindow* nativeWin, const int width, const int height) {
131 static const auto evtGuid = helios::util::Guid::generate();
132
133 if (const auto* ptr = static_cast<helios::ext::glfw::window::GLFWWindowUserPointer*>(glfwGetWindowUserPointer(nativeWin))) {
134 auto event = std::make_unique<helios::window::event::FrameBufferResizeEvent>(
135 ptr->window->guid(), width, height,
136 // we will use the window's guid as the tag
137 ptr->window->guid().value()
138 );
139 ptr->application->eventManager().post(
140 std::move(event),
142 );
143 }
144 };
145
146 return cfg;
147 }
148 };
149}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.