Replace GLFW with SDL3

Since I plan to use SDL3 API, I had to replace GLFW
window and surface creation with SDL3 variant. For now
it's working fine.
This commit is contained in:
everyonecancode authored and Piotr Krygier committed 2025-08-29 10:02:14 +02:00
1 parent 393b59ad5d
commit 1b5cc7a79c
9 files changed
+43 -91

No files matched your search

-1
View File
@@ -2,7 +2,6 @@
vulkan-headers/1.4.309.0 vulkan-headers/1.4.309.0
vulkan-loader/1.4.309.0 vulkan-loader/1.4.309.0
libgettext/0.22 libgettext/0.22
glfw/3.4
freetype/2.13.3 freetype/2.13.3
stb/cci.20240531 stb/cci.20240531
sdl/3.2.14 sdl/3.2.14
-2
View File
@@ -1,7 +1,6 @@
find_package(VulkanHeaders REQUIRED) find_package(VulkanHeaders REQUIRED)
find_package(VulkanLoader REQUIRED) find_package(VulkanLoader REQUIRED)
# find_package(vulkan-validationlayers) # find_package(vulkan-validationlayers)
find_package(glfw3 REQUIRED)
find_package(freetype REQUIRED) find_package(freetype REQUIRED)
find_package(stb REQUIRED) find_package(stb REQUIRED)
find_package(SDL3 REQUIRED) find_package(SDL3 REQUIRED)
@@ -42,7 +41,6 @@ target_link_libraries(graphics PUBLIC
vulkan-headers::vulkan-headers vulkan-headers::vulkan-headers
Vulkan::Loader Vulkan::Loader
# vulkan-validationlayers::vulkan-validationlayers # vulkan-validationlayers::vulkan-validationlayers
glfw
Freetype::Freetype Freetype::Freetype
stb::stb stb::stb
sdl::sdl sdl::sdl
+2 -2
View File
@@ -12,7 +12,7 @@
#ifndef GRAPHICS_CONTEXT_H #ifndef GRAPHICS_CONTEXT_H
#define GRAPHICS_CONTEXT_H #define GRAPHICS_CONTEXT_H
#include <GLFW/glfw3.h> #include "SDL3/SDL.h"
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <vma/vk_mem_alloc.h> #include <vma/vk_mem_alloc.h>
@@ -93,7 +93,7 @@ struct pipeline_internal_t
struct graphics_context_t struct graphics_context_t
{ {
GLFWwindow* window_handle; SDL_Window* window_handle;
VkDevice device; VkDevice device;
VkSurfaceKHR surface; VkSurfaceKHR surface;
VkInstance instance; VkInstance instance;
+7 -6
View File
@@ -11,8 +11,9 @@
#include "vulkan_base.h" #include "vulkan_base.h"
#include <GLFW/glfw3.h> #include <SDL3/SDL_error.h>
#include "utilities/localization.h" #include <SDL3/SDL_main.h>
#include <SDL3/SDL_vulkan.h>
#include <vma/vk_mem_alloc.h> #include <vma/vk_mem_alloc.h>
#include "utilities/commons.h" #include "utilities/commons.h"
@@ -171,7 +172,7 @@ static rse_err_t create_instance(VkInstance* instance)
#ifdef WAYLAND #ifdef WAYLAND
"VK_KHR_wayland_surface", "VK_KHR_wayland_surface",
#else #else
"VK_KHR_xcb_surface", "VK_KHR_xlib_surface",
#endif /* WAYLAND */ #endif /* WAYLAND */
#elif defined(_WIN32) || defined(WIN32) #elif defined(_WIN32) || defined(WIN32)
"VK_KHR_win32_surface", "VK_KHR_win32_surface",
@@ -274,10 +275,10 @@ static rse_err_t create_instance(VkInstance* instance)
* *
* @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors: * @return rse_err_t RSE_ERROR_NO_ERROR on success. Possible errors:
*/ */
static rse_err_t create_surface(VkInstance instance, GLFWwindow* window_handle, VkSurfaceKHR* surface) static rse_err_t create_surface(VkInstance instance, SDL_Window* window_handle, VkSurfaceKHR* surface)
{ {
if (VK_SUCCESS != glfwCreateWindowSurface(instance, window_handle, NULL, surface)) { if (true != SDL_Vulkan_CreateSurface(window_handle, instance, NULL, surface)) {
LOGF(_("Failed to create window surface\n")); LOGF(_("Failed to create window surface. Error: %s\n"), SDL_GetError());
return VULKAN_ERROR_SURFACE_CREATION_FAILED; return VULKAN_ERROR_SURFACE_CREATION_FAILED;
} }
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
+5 -3
View File
@@ -1,3 +1,4 @@
#include <SDL3/SDL_events.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -157,13 +158,14 @@ static rse_err_t create_framebuffers(struct graphics_context_t* context)
*/ */
void recreate_swapchain(struct graphics_context_t* context) void recreate_swapchain(struct graphics_context_t* context)
{ {
SDL_Event event;
int width = 0; int width = 0;
int height = 0; int height = 0;
glfwGetFramebufferSize(context->window_handle, &width, &height); SDL_GetWindowSizeInPixels(context->window_handle, &width, &height);
while (width == 0 || height == 0) { while (width == 0 || height == 0) {
glfwGetFramebufferSize(context->window_handle, &width, &height); SDL_GetWindowSizeInPixels(context->window_handle, &width, &height);
glfwWaitEvents(); SDL_WaitEvent(&event);
} }
vkDeviceWaitIdle(context->device); vkDeviceWaitIdle(context->device);
+25 -73
View File
@@ -10,8 +10,15 @@
*/ */
#include "window.h" #include "window.h"
#include <SDL3/SDL_events.h>
#include <SDL3/SDL_init.h>
#include <SDL3/SDL_video.h>
#include <stdint.h>
#include "GLFW/glfw3.h" #include "SDL3/SDL.h"
#include "SDL3/SDL_main.h"
#include "utilities/commons.h"
#include "utilities/localization.h" #include "utilities/localization.h"
#include "window_errors.h" #include "window_errors.h"
#include "utilities/logger.h" #include "utilities/logger.h"
@@ -19,88 +26,32 @@
#include "vulkan/vulkan_core.h" #include "vulkan/vulkan_core.h"
#include "vulkan_base.h" #include "vulkan_base.h"
/** rse_err_t window_init(SDL_Window **window_handle, bool *is_framebuffer_resized)
* @brief GLFW error callback
*
* @param error Error code
* @param description Error description
*/
static void error_callback(int error, const char* description)
{ {
(void)error; SDL_Init(SDL_INIT_VIDEO);
LOGE("Error: %s", description);
}
/** *window_handle = SDL_CreateWindow("RedScarfEngine", 640, 480, SDL_WINDOW_VULKAN);
* @brief Pressed key callback
*
* @param window Window in which contex the key has been pressed
* @param key The keyboard key that was pressed or released
* @param scancode The system-specific scancode of the key.
* @param action GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT
* @param mods Bit field describing which modifier keys were held down
*/
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
(void)scancode;
(void)action;
(void)mods;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
}
static void framebuffer_resized_callback(GLFWwindow* window, int width, int height)
{
bool* is_frambuffer_resized = (bool*)glfwGetWindowUserPointer(window);
(void)window;
(void)width;
(void)height;
*is_frambuffer_resized = 1;
}
rse_err_t window_init(GLFWwindow** window_handle, bool* is_framebuffer_resized)
{
/* Initiliaze GLFW AP */
if (GLFW_TRUE != glfwInit())
{
LOGF(_("GLFW init failed!\n"));
return WINDOW_ERROR_GLFW_INIT_FAILED;
}
/* Check for Vulkan support */
if (GLFW_FALSE == glfwVulkanSupported()) {
LOGF(_("Unable to load Vulkan\n"));
return WINDOW_ERROR_VULKAN_NOT_LOADED;
}
/* Set error callback */
glfwSetErrorCallback(error_callback);
/* Create window */
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
*window_handle = glfwCreateWindow(640, 480, "RedScarfEngine", NULL, NULL);
if (NULL == *window_handle) { if (NULL == *window_handle) {
LOGF(_("Window context creation failed\n")); LOGF(_("Window context creation failed\n"));
return WINDOW_ERROR_WINDOW_NOT_CREATED; return WINDOW_ERROR_WINDOW_NOT_CREATED;
} }
glfwSetKeyCallback(*window_handle, key_callback);
glfwSetFramebufferSizeCallback(*window_handle, framebuffer_resized_callback);
glfwSetWindowUserPointer(*window_handle, is_framebuffer_resized);
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
rse_err_t window_loop(struct graphics_context_t* context) rse_err_t window_loop(struct graphics_context_t* context)
{ {
while (!glfwWindowShouldClose(context->window_handle)) { uint8_t done = 0;
glfwPollEvents(); SDL_Event event;
while (0 == done) {
while (SDL_PollEvent(&event)) {
if (SDL_EVENT_QUIT == event.type) {
done = 1;
}
}
draw_frame(context); draw_frame(context);
} }
@@ -108,8 +59,9 @@ rse_err_t window_loop(struct graphics_context_t* context)
return RSE_ERROR_NO_ERROR; return RSE_ERROR_NO_ERROR;
} }
void window_terminate(GLFWwindow* window_handle) void window_terminate(SDL_Window* window_handle)
{ {
glfwDestroyWindow(window_handle); SDL_DestroyWindow(window_handle);
glfwTerminate();
SDL_Quit();
} }
+3 -3
View File
@@ -12,10 +12,10 @@
#ifndef WINDOW_H #ifndef WINDOW_H
#define WINDOW_H #define WINDOW_H
#include <SDL3/SDL_main.h>
#include <vulkan/vulkan.h> #include <vulkan/vulkan.h>
/* Vulkan header MUST be included before glfw */ /* Vulkan header MUST be included before glfw */
#include <GLFW/glfw3.h>
#include "utilities/commons.h" #include "utilities/commons.h"
#include "graphics_context.h" #include "graphics_context.h"
@@ -30,7 +30,7 @@
* WINDOW_WINDOW_NOT_CREATED * WINDOW_WINDOW_NOT_CREATED
* *
*/ */
rse_err_t window_init(GLFWwindow** window_handle, bool* is_framebuffer_resized); rse_err_t window_init(SDL_Window **window_handle, bool *is_framebuffer_resized);
/** /**
* @brief Main window loop * @brief Main window loop
@@ -45,6 +45,6 @@ rse_err_t window_loop(struct graphics_context_t* context);
* *
* @param window_handle Pointer to window window_handle * @param window_handle Pointer to window window_handle
*/ */
void window_terminate(GLFWwindow* window_handle); void window_terminate(SDL_Window* window_handle);
#endif /* WINDOW_H */ #endif /* WINDOW_H */
-1
View File
@@ -1,6 +1,5 @@
#include "graphics/rse_graphics.h" #include "graphics/rse_graphics.h"
#include "utilities/errors_common.h" #include "utilities/errors_common.h"
#include "utilities/localization.h"
#include "utilities/logger.h" #include "utilities/logger.h"
#include "SDL3/SDL_thread.h" #include "SDL3/SDL_thread.h"
+1
View File
@@ -12,6 +12,7 @@
#ifndef LOGGER_H #ifndef LOGGER_H
#define LOGGER_H #define LOGGER_H
#include "utilities/localization.h"
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>