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:
1 parent
393b59ad5d
commit
1b5cc7a79c
9 files changed
+43
-91
No files matched your search
@@ -1,7 +1,6 @@
|
||||
find_package(VulkanHeaders REQUIRED)
|
||||
find_package(VulkanLoader REQUIRED)
|
||||
# find_package(vulkan-validationlayers)
|
||||
find_package(glfw3 REQUIRED)
|
||||
find_package(freetype REQUIRED)
|
||||
find_package(stb REQUIRED)
|
||||
find_package(SDL3 REQUIRED)
|
||||
@@ -42,7 +41,6 @@ target_link_libraries(graphics PUBLIC
|
||||
vulkan-headers::vulkan-headers
|
||||
Vulkan::Loader
|
||||
# vulkan-validationlayers::vulkan-validationlayers
|
||||
glfw
|
||||
Freetype::Freetype
|
||||
stb::stb
|
||||
sdl::sdl
|
||||
|
||||
@@ -12,7 +12,7 @@
|
||||
#ifndef GRAPHICS_CONTEXT_H
|
||||
#define GRAPHICS_CONTEXT_H
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "SDL3/SDL.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
@@ -93,7 +93,7 @@ struct pipeline_internal_t
|
||||
|
||||
struct graphics_context_t
|
||||
{
|
||||
GLFWwindow* window_handle;
|
||||
SDL_Window* window_handle;
|
||||
VkDevice device;
|
||||
VkSurfaceKHR surface;
|
||||
VkInstance instance;
|
||||
|
||||
@@ -11,8 +11,9 @@
|
||||
|
||||
#include "vulkan_base.h"
|
||||
|
||||
#include <GLFW/glfw3.h>
|
||||
#include "utilities/localization.h"
|
||||
#include <SDL3/SDL_error.h>
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <SDL3/SDL_vulkan.h>
|
||||
#include <vma/vk_mem_alloc.h>
|
||||
|
||||
#include "utilities/commons.h"
|
||||
@@ -171,7 +172,7 @@ static rse_err_t create_instance(VkInstance* instance)
|
||||
#ifdef WAYLAND
|
||||
"VK_KHR_wayland_surface",
|
||||
#else
|
||||
"VK_KHR_xcb_surface",
|
||||
"VK_KHR_xlib_surface",
|
||||
#endif /* WAYLAND */
|
||||
#elif defined(_WIN32) || defined(WIN32)
|
||||
"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:
|
||||
*/
|
||||
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)) {
|
||||
LOGF(_("Failed to create window surface\n"));
|
||||
if (true != SDL_Vulkan_CreateSurface(window_handle, instance, NULL, surface)) {
|
||||
LOGF(_("Failed to create window surface. Error: %s\n"), SDL_GetError());
|
||||
return VULKAN_ERROR_SURFACE_CREATION_FAILED;
|
||||
}
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <SDL3/SDL_events.h>
|
||||
#include <stdlib.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)
|
||||
{
|
||||
SDL_Event event;
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
|
||||
glfwGetFramebufferSize(context->window_handle, &width, &height);
|
||||
SDL_GetWindowSizeInPixels(context->window_handle, &width, &height);
|
||||
while (width == 0 || height == 0) {
|
||||
glfwGetFramebufferSize(context->window_handle, &width, &height);
|
||||
glfwWaitEvents();
|
||||
SDL_GetWindowSizeInPixels(context->window_handle, &width, &height);
|
||||
SDL_WaitEvent(&event);
|
||||
}
|
||||
|
||||
vkDeviceWaitIdle(context->device);
|
||||
|
||||
+25
-73
@@ -10,8 +10,15 @@
|
||||
*/
|
||||
|
||||
#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 "window_errors.h"
|
||||
#include "utilities/logger.h"
|
||||
@@ -19,88 +26,32 @@
|
||||
#include "vulkan/vulkan_core.h"
|
||||
#include "vulkan_base.h"
|
||||
|
||||
/**
|
||||
* @brief GLFW error callback
|
||||
*
|
||||
* @param error Error code
|
||||
* @param description Error description
|
||||
*/
|
||||
static void error_callback(int error, const char* description)
|
||||
rse_err_t window_init(SDL_Window **window_handle, bool *is_framebuffer_resized)
|
||||
{
|
||||
(void)error;
|
||||
LOGE("Error: %s", description);
|
||||
}
|
||||
SDL_Init(SDL_INIT_VIDEO);
|
||||
|
||||
/**
|
||||
* @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);
|
||||
*window_handle = SDL_CreateWindow("RedScarfEngine", 640, 480, SDL_WINDOW_VULKAN);
|
||||
|
||||
if (NULL == *window_handle) {
|
||||
LOGF(_("Window context creation failed\n"));
|
||||
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;
|
||||
}
|
||||
|
||||
rse_err_t window_loop(struct graphics_context_t* context)
|
||||
{
|
||||
while (!glfwWindowShouldClose(context->window_handle)) {
|
||||
glfwPollEvents();
|
||||
uint8_t done = 0;
|
||||
SDL_Event event;
|
||||
|
||||
while (0 == done) {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (SDL_EVENT_QUIT == event.type) {
|
||||
done = 1;
|
||||
}
|
||||
}
|
||||
|
||||
draw_frame(context);
|
||||
}
|
||||
|
||||
@@ -108,8 +59,9 @@ rse_err_t window_loop(struct graphics_context_t* context)
|
||||
return RSE_ERROR_NO_ERROR;
|
||||
}
|
||||
|
||||
void window_terminate(GLFWwindow* window_handle)
|
||||
void window_terminate(SDL_Window* window_handle)
|
||||
{
|
||||
glfwDestroyWindow(window_handle);
|
||||
glfwTerminate();
|
||||
SDL_DestroyWindow(window_handle);
|
||||
|
||||
SDL_Quit();
|
||||
}
|
||||
@@ -12,10 +12,10 @@
|
||||
#ifndef WINDOW_H
|
||||
#define WINDOW_H
|
||||
|
||||
#include <SDL3/SDL_main.h>
|
||||
#include <vulkan/vulkan.h>
|
||||
|
||||
/* Vulkan header MUST be included before glfw */
|
||||
#include <GLFW/glfw3.h>
|
||||
|
||||
#include "utilities/commons.h"
|
||||
#include "graphics_context.h"
|
||||
@@ -30,7 +30,7 @@
|
||||
* 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
|
||||
@@ -45,6 +45,6 @@ rse_err_t window_loop(struct graphics_context_t* context);
|
||||
*
|
||||
* @param window_handle Pointer to window window_handle
|
||||
*/
|
||||
void window_terminate(GLFWwindow* window_handle);
|
||||
void window_terminate(SDL_Window* window_handle);
|
||||
|
||||
#endif /* WINDOW_H */
|
||||
Reference in new issue
Block a user