3#include "SDL3/SDL_events.h"
7#include "components/PathUtils.hpp"
8#include "components/EngineCommands.hpp"
15#include "components/backends/vulkan/PhysicsDebug.hpp"
18#include "components/UtilitySystem.hpp"
26#include "VexBuildVersion.hpp"
31 return VEX_VERSION_MAJOR;
35 return VEX_VERSION_MINOR;
39 return VEX_VERSION_PATCH;
43 return VEX_VERSION_STRING;
49 if(m_gameInfo.versionMajor == 0 && m_gameInfo.versionMinor == 0 && m_gameInfo.versionPatch == 0){
50 log(
"Project version not set!");
53 log(
"Creating window..");
54 m_window = std::make_shared<Window>(title, width, height);
55 m_resolutionManager = std::make_unique<ResolutionManager>(m_window->GetSDLWindow());
58 const char* driver = SDL_GetCurrentVideoDriver();
59 if (driver && std::string(driver) ==
"wayland") {
61 log(
"Wayland detected: Enforcing Software VSync strategy.");
65 log(
"Initializing virtual file system..");
66 m_vfs = std::make_shared<VirtualFileSystem>();
69 std::string defaultUserDataDir = std::string(SDL_GetUserFolder(SDL_Folder::SDL_FOLDER_DOCUMENTS) + gInfo.projectName);
73 m_physicsSystem = std::make_unique<PhysicsSystem>(m_registry);
74 m_physicsSystem->init();
76 m_audioSystem = std::make_unique<AudioSystem>(m_registry);
77 m_audioSystem->Init(m_vfs.get());
79 auto renderRes = m_resolutionManager->getRenderResolution();
80 log(
"Initializing Vulkan interface...");
81 m_interface = std::make_unique<Interface>(m_window->GetSDLWindow(), renderRes, m_gameInfo, m_vfs.get());
82 m_imgui = std::make_unique<VulkanImGUIWrapper>(m_window->GetSDLWindow(), *m_interface->getContext());
85 vex::DebugConsole::Get().Init();
87 log(
"Initializing engine components...");
89 m_inputSystem = std::make_unique<InputSystem>(m_registry, m_window->GetSDLWindow());
90 m_sceneManager = std::make_unique<SceneManager>();
94 RegisterEngineCommands(
this);
95 log(
"Engine initialized successfully");
99 return VEX_ENGINE_BUILD_ID;
103 return m_sceneManager.get();
107 return m_interface.get();
111 return std::make_shared<VexUI>(*m_interface->getContext(), m_vfs.get(), m_interface->getResources(), m_resolutionManager.get());
115 Uint64 lastTime = SDL_GetPerformanceCounter();
118 Uint64 frameStart = SDL_GetPerformanceCounter();
119 if (onUpdateLoop) onUpdateLoop();
122 while (SDL_PollEvent(&event)) {
124 m_imgui->processEvent(&event);
127 if(uiComp.m_vexUI->isInitialized()){
128 uiComp.m_vexUI->processEvent(event);
131 switch (event.type) {
132 case SDL_EVENT_GAMEPAD_ADDED:
133 SDL_OpenGamepad(event.gdevice.which);
134 log(
"Gamepad connected and opened.");
139 case SDL_EVENT_DID_ENTER_FOREGROUND:
140 log(
"Binding window to Vulkan...");
141 m_interface->bindWindow(m_window->GetSDLWindow());
142 m_internally_paused =
false;
144 case SDL_EVENT_WILL_ENTER_BACKGROUND:
145 m_interface->unbindWindow();
146 m_internally_paused =
true;
148 case SDL_EVENT_WINDOW_RESIZED:
149 m_resolutionManager->update();
156 if (!m_running)
break;
160 if(!transform.isReady()){
161 transform.setRegistry(m_registry);
167 float targetFps = (float)m_targetFps;
170 if (m_vsyncEnabled && m_isWayland) {
171 targetFps = m_window->getRefreshRate();
176 float targetFrameTime = 1000.0f / targetFps;
178 Uint64 frameEnd = SDL_GetPerformanceCounter();
179 float elapsedMS = (frameEnd - frameStart) / (
float)SDL_GetPerformanceFrequency() * 1000.0f;
181 if (elapsedMS < targetFrameTime) {
182 SDL_Delay(
static_cast<Uint32
>(targetFrameTime - elapsedMS));
186 Uint64 now = SDL_GetPerformanceCounter();
187 m_deltaTime = (float)((now - lastTime) / (
float)SDL_GetPerformanceFrequency());
192 log(
"Engine shutdown requested. Waiting for GPU to finish...");
193 m_interface->WaitForGPUToFinish();
199 m_interface->WaitForGPUToFinish();
208 if (m_vsyncEnabled == enabled)
return;
209 m_vsyncEnabled = enabled;
212 m_interface->setVSync(enabled);
217 return m_vsyncEnabled;
221 m_audioSystem->Shutdown();
222 m_audioSystem.reset();
223 if (m_physicsSystem) {
224 m_physicsSystem->shutdown();
225 m_physicsSystem.reset();
229 m_inputSystem.reset();
235 log(
"Engine initialization handed of to Editor");
239 m_resolutionManager->setMode(mode);
240 m_resolutionManager->update();
246 m_interface->setEnvironment(settings);
250 return m_interface->getEnvironment();
255 m_window->setFullscreen(enabled, exclusive);
260 return m_window ? m_window->isFullscreen() :
false;
268 m_inputSystem->processEvent(event, deltaTime);
272 m_inputSystem->update(deltaTime);
276 m_audioSystem->Update(cameraEntity);
283 if(!uiComp.m_vexUI->isInitialized()){
284 uiComp.m_vexUI->init();
285 uiComp.m_vexUI->update(deltaTime);
290 if (m_paused && deltaTime == 0.0f) deltaTime = 0.016f;
292 if(!(m_paused || m_internally_paused)){
293 ProcessUtilityComponents(m_registry, deltaTime, *
this);
294 m_sceneManager->scenesUpdate(deltaTime);
295 m_physicsSystem->update(deltaTime);
301 if(!uiComp.m_vexUI->isInitialized()){
302 uiComp.m_vexUI->init();
303 uiComp.m_vexUI->update(deltaTime);
310 if(!m_internally_paused){
319 auto renderRes = m_resolutionManager->getRenderResolution();
332 if (!m_interface->getRenderer().beginFrame(renderRes, renderData)) {
337 const std::vector<DebugVertex>* debugLines =
nullptr;
338 if(m_renderPhysicsDebug) {
339 auto* dbg = m_interface->getPhysicsDebug();
341 m_physicsSystem->setDebugRenderer(dbg);
342 m_physicsSystem->drawDebug();
343 debugLines = &dbg->GetLines();
345 m_interface->getRenderer().renderScene(renderData, cameraEntity, m_registry, m_frame, debugLines);
347 m_interface->getRenderer().renderScene(renderData, cameraEntity, m_registry, m_frame);
350 m_interface->getRenderer().composeFrame(renderData, *m_imgui,
false);
351 m_interface->getRenderer().endFrame(renderData);
352 }
catch (
const std::exception& e) {
353 log(LogLevel::ERROR,
"Frame render failed");
Contains basic components like transform, camera, name components..
Contains main Engine class.
This file defines interface Class for vulkan backend.
This file defines SceneManager class.
Component you add to your GameObject to add ui elements to your game. ex. hud to player.
This file defines VirtualFileSystem and VPKStream classes.
This file defines class with Imgui for vulkan backend definition.
This file defines Window class.
void run(std::function< void()> onUpdateLoop=nullptr)
Starts and runs the main game loop.
virtual void setFullscreen(bool enabled, bool exclusive=false)
Function to enable/disable fullscreen mode. with optional exclusive parameter.
static int GetVersionMinor()
Returns the minor version of the engine.
void prepareScenesForHotReload()
Prepares the engine for a hot-reload event.
SceneManager * getSceneManager()
Returns pointer to SceneManager.
static const char * GetVersionString()
Returns the version string of the engine.
virtual void beginGame()
Internal virtual function for handling stuff right at the beginning of the game but after initializat...
virtual void render()
Internal function that orchestrates the frame rendering process.
void WaitForGpu()
Waits for the GPU to finish all pending operations.
bool isFullscreen()
Function to check if fullscreen mode is enabled.
Interface * getInterface()
Returns Interface, used internally.
void setFrameLimit(int fps)
Sets the target frame rate limit.
void setResolutionMode(ResolutionMode mode)
Sets the resolution mode and updates the resolution manager.
static const char * GetBuildHash()
returns engine hash generated during build process.
virtual void update(float deltaTime)
Internal function for handling updates; called every frame before rendering.
static int GetVersionPatch()
Returns the patch version of the engine.
environment getEnvironmentSettings()
Returns the current environment settings.
Engine(const char *title, int width, int height, GameInfo gInfo)
Constructor for the Engine class.
void setVSync(bool enabled)
Toggles VSync (Vertical Synchronization).
std::shared_ptr< VexUI > createVexUI()
Creates and returns a shared pointer to a VexUI instance.
vex::Entity getCamera()
Function returning the entity of the camera.
void setEnvironmentSettings(environment settings)
Applies new global environment settings (lighting, shading) to the interface.
virtual void processEvent(const SDL_Event &event, float deltaTime)
Internal virtual function for handling window/keyboard events.
static int GetVersionMajor()
Returns the major version of the engine.
bool getVSync() const
Returns the current state of VSync.
Interface Class for vulkan backend.
MeshManager & getMeshManager()
Getter for MeshManager.
void init(Engine *engine)
Initialize the MeshManager.
SceneManager class implements scene management functionality like loading and unloading scenes,...
std::vector< std::string > GetAllSceneNames() const
Retrieves the names of all currently loaded scenes.
Provides iteration over entities that have all specified component types.
void each(Func func)
Iterates over all entities with the specified component types.
This file defines struct holding all vulkan data, like device, surface, swapchain,...
uint32_t Entity
Type alias representing a unique entity identifier in the ECS system.
std::filesystem::path VEX_EXPORT GetExecutableDir()
Retrieves the directory containing the current executable.
ResolutionMode
Enumeration representing different resolution modes.
void VEX_EXPORT log(const char *fmt,...)
Logs a formatted message.
void VEX_EXPORT handle_exception(const std::exception &e)
Handles an exception based on build configuration.
constexpr Entity NULL_ENTITY
Special entity value indicating an invalid or null entity.
void VEX_EXPORT SetUserDataDir(const std::string &userDataDir)
Sets the current user data directory.
void ProcessParticles(vex::Registry ®istry, float deltaTime)
Processes CPU-side particle simulation and prepares GPU data.
this struct contains information about the game. like project name and version.
Data structure to pass state between render stages.
Struct containing VexUI class.
This struct is used to hold all environment settings. eg. shading details or lighting setup.