GLM works only for C++ and I plan on moving to back to C. There is a C port, but it would be good for me to figure out vertex math myself. Hence, my own library has been created.
119 lines
2.6 KiB
C++
119 lines
2.6 KiB
C++
/**
|
|
* @file window.cxx
|
|
* @author Piotr Krygier (everyonencancode@gmail.com)
|
|
* @brief
|
|
* @version 0.1
|
|
* @date 2022-02-15
|
|
*
|
|
* @copyright Copyright (c) 2022
|
|
*
|
|
*/
|
|
|
|
#include "window.hpp"
|
|
|
|
#include "locale_window.h"
|
|
#include "utilities/logger.hpp"
|
|
#include "vulkan_base.hpp"
|
|
|
|
namespace rse::graphics::window
|
|
{
|
|
|
|
/* Error codes */
|
|
typedef enum {
|
|
WINDOW_ERROR_NO_ERROR = 0U,
|
|
WINDOW_ERROR_GLFW_INIT_FAILED,
|
|
WINDOW_ERROR_VULKAN_NOT_LOADED,
|
|
WINDOW_ERROR_WINDOW_NOT_CREATED
|
|
} WINDOW_ERROR;
|
|
|
|
GLFWwindow *gpWindowHandle = NULL;
|
|
|
|
/**
|
|
* @brief GLFW erro callback
|
|
*
|
|
* @param error Error code
|
|
* @param description Error description
|
|
*/
|
|
static void errrorCallback(int error, const char* description)
|
|
{
|
|
(void)error;
|
|
LOGE << "Error: " << description;
|
|
}
|
|
|
|
/**
|
|
* @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 keyCallback(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);
|
|
}
|
|
}
|
|
|
|
|
|
uint16_t windowInit()
|
|
{
|
|
/* Initiliaze GLFW AP */
|
|
if (GLFW_TRUE != glfwInit())
|
|
{
|
|
LOGF << windowErrorMesssages[WINDOW_GLFW_INIT_FAILED];
|
|
return WINDOW_ERROR_GLFW_INIT_FAILED;
|
|
}
|
|
|
|
|
|
/* Check for Vulkan support */
|
|
if (GLFW_FALSE == glfwVulkanSupported()) {
|
|
LOGF << windowErrorMesssages[WINDOW_VULKAN_NOT_SUPPORTED];
|
|
return WINDOW_ERROR_VULKAN_NOT_LOADED;
|
|
}
|
|
|
|
/* Set error callback */
|
|
glfwSetErrorCallback(errrorCallback);
|
|
|
|
/* Create window */
|
|
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
|
|
|
|
gpWindowHandle = glfwCreateWindow(640, 480, "RedScarfEngine", NULL, NULL);
|
|
|
|
if (NULL == gpWindowHandle) {
|
|
LOGF << windowErrorMesssages[WINDOW_NOT_CREATED];
|
|
return WINDOW_ERROR_WINDOW_NOT_CREATED;
|
|
}
|
|
|
|
glfwSetKeyCallback(gpWindowHandle, keyCallback);
|
|
|
|
return WINDOW_ERROR_NO_ERROR;
|
|
}
|
|
|
|
uint16_t windowLoop()
|
|
{
|
|
while (!glfwWindowShouldClose(gpWindowHandle)) {
|
|
glfwPollEvents();
|
|
rse::graphics::vulkanbase::drawFrame();
|
|
}
|
|
|
|
return WINDOW_ERROR_NO_ERROR;
|
|
}
|
|
|
|
void windowTerminate()
|
|
{
|
|
glfwDestroyWindow(gpWindowHandle);
|
|
glfwTerminate();
|
|
}
|
|
|
|
|
|
GLFWwindow* getWindowHandle()
|
|
{
|
|
return gpWindowHandle;
|
|
}
|
|
|
|
} /* namespace rse::graphics::window */ |