SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
SwapChain.h
1#pragma once
2
3#include "spark/render/Export.h"
4#include "spark/render/FrameBuffer.h"
5#include "spark/render/Image.h"
6#include "spark/render/Format.h"
7
8#include "spark/math/Vector2.h"
9
10namespace spark::render
11{
15 class SPARK_RENDER_EXPORT ISwapChain
16 {
17 public:
18 virtual ~ISwapChain() noexcept = default;
19
24 [[nodiscard]] virtual Format surfaceFormat() const noexcept = 0;
25
30 [[nodiscard]] virtual unsigned int buffers() const noexcept = 0;
31
36 [[nodiscard]] virtual math::Vector2<unsigned int> renderArea() const noexcept = 0;
37
45 [[nodiscard]] virtual const IImage* image(unsigned int back_buffer) const = 0;
46
51 [[nodiscard]] std::vector<const IImage*> images() const noexcept { return genericImages(); }
52
57 void present(const IFrameBuffer& frame_buffer) const noexcept { genericPresent(frame_buffer); }
58
63 [[nodiscard]] virtual std::vector<Format> surfaceFormats() const noexcept = 0;
64
78 virtual void reset(Format surface_format, math::Vector2<unsigned int> render_area, unsigned int buffers) noexcept = 0;
79
84 [[nodiscard]] virtual unsigned int swapBackBuffer() const noexcept = 0;
85
86 private:
89 [[nodiscard]] virtual std::vector<const IImage*> genericImages() const noexcept = 0;
90 virtual void genericPresent(const IFrameBuffer& frame_buffer) const noexcept = 0;
92 };
93
99 template <typename ImageType, typename FrameBufferType>
100 class SwapChain : public ISwapChain
101 {
102 public:
103 using image_type = ImageType;
104 using frame_buffer_type = FrameBufferType;
105
106 public:
108 [[nodiscard]] virtual std::vector<const image_type*> images() const noexcept = 0;
109
111 virtual void present(const frame_buffer_type& frame_buffer) const noexcept = 0;
112
113 private:
114 [[nodiscard]] std::vector<const IImage*> genericImages() const noexcept final
115 {
116 auto tmp = images();
117 std::vector<const IImage*> images_vector;
118 images_vector.reserve(tmp.size());
119 std::ranges::transform(tmp, std::back_inserter(images_vector), [](const auto& image) { return static_cast<const IImage*>(image); });
120 return images_vector;
121 }
122
123 void genericPresent(const IFrameBuffer& frame_buffer) const noexcept final { present(static_cast<const frame_buffer_type&>(frame_buffer)); }
124 };
125}
Interface for a frame buffer.
Definition FrameBuffer.h:17
Definition Image.h:131
Interface for a swap chain, a chain of multiple IImage instances that can be presented using a ISurfa...
Definition SwapChain.h:16
virtual Format surfaceFormat() const noexcept=0
Gets the swap chain's surface format.
void present(const IFrameBuffer &frame_buffer) const noexcept
Queues a present that gets executed after frame_buffer signals that it's done rendering.
Definition SwapChain.h:57
virtual std::vector< Format > surfaceFormats() const noexcept=0
Gets all the swap chain's supported surface formats.
Represents a swap chain, i.e. a chain of multiple IImage instances, that can be presented to a ISurfa...
Definition SwapChain.h:101
virtual std::vector< const image_type * > images() const noexcept=0
Gets all the swap chain's images.