Skip to content

Commit

Permalink
Add vulkan window class
Browse files Browse the repository at this point in the history
  • Loading branch information
albin-johansson committed Apr 24, 2024
1 parent 60e7850 commit 8cdbed4
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
57 changes: 57 additions & 0 deletions source/vulkan/inc/tactile/vulkan/vulkan_window.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#pragma once

#include "tactile/base/container/expected.hpp"
#include "tactile/base/container/smart_ptr.hpp"
#include "tactile/base/prelude.hpp"
#include "tactile/render/window.hpp"
#include "tactile/vulkan/api.hpp"

struct SDL_Window;

namespace tactile {

/// \addtogroup Vulkan
/// \{

/**
* Represents a window usable in Vulkan contexts.
*/
class TACTILE_VULKAN_API VulkanWindow final : public IWindow
{
public:
TACTILE_DELETE_COPY(VulkanWindow);
TACTILE_DEFAULT_MOVE(VulkanWindow);

~VulkanWindow() noexcept override = default;

/**
* Creates a Vulkan window.
*
* \return
* A Vulkan window if successful; an error code otherwise.
*/
[[nodiscard]]
static auto make() -> Result<VulkanWindow>;

void show() override;

void hide() override;

void maximize() override;

private:
struct WindowDeleter final
{
void operator()(SDL_Window* window) noexcept;
};

Unique<SDL_Window, WindowDeleter> mWindow;

explicit VulkanWindow(SDL_Window* window);
};

/// \}

} // namespace tactile
53 changes: 53 additions & 0 deletions source/vulkan/src/tactile/vulkan/vulkan_window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright (C) 2023 Albin Johansson (GNU General Public License v3.0)

#include "tactile/vulkan/vulkan_window.hpp"

#include <SDL2/SDL.h>

#include "tactile/vulkan/vulkan_error.hpp"

namespace tactile {

void VulkanWindow::WindowDeleter::operator()(SDL_Window* window) noexcept
{
SDL_DestroyWindow(window);
}

auto VulkanWindow::make() -> Result<VulkanWindow>
{
const auto window_flags =
SDL_WINDOW_HIDDEN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_VULKAN;
auto* window = SDL_CreateWindow("Tactile " TACTILE_VERSION_STRING,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
800,
600,
window_flags);

if (!window) {
return unexpected(make_error(VulkanError::kInitFailed));
}

return VulkanWindow {window};
}

VulkanWindow::VulkanWindow(SDL_Window* window)
: mWindow {window}
{}

void VulkanWindow::show()
{
SDL_ShowWindow(mWindow.get());
}

void VulkanWindow::hide()
{
SDL_HideWindow(mWindow.get());
}

void VulkanWindow::maximize()
{
SDL_MaximizeWindow(mWindow.get());
}

} // namespace tactile

0 comments on commit 8cdbed4

Please sign in to comment.