JNGL
framebuffer.hpp
Go to the documentation of this file.
1 // Copyright 2012-2021 Jan Niklas Hasse <[email protected]>
2 // For conditions of distribution and use, see copyright notice in LICENSE.txt
5 #pragma once
6 
7 #include "Color.hpp"
8 #include "Finally.hpp"
9 #include "Mat3.hpp"
10 #include "Pixels.hpp"
11 #include "Vec2.hpp"
12 #include "Vertex.hpp"
13 
14 #include <memory>
15 #include <vector>
16 
17 namespace jngl {
18 
19 class ShaderProgram;
20 
22 class FrameBuffer {
23 public:
25  FrameBuffer(Pixels width, Pixels height);
26 
29 
32 
33  FrameBuffer(const FrameBuffer&) = delete;
34  FrameBuffer& operator=(const FrameBuffer&) = delete;
35  FrameBuffer(FrameBuffer&&) = default;
36  FrameBuffer& operator=(FrameBuffer&&) = default;
37  ~FrameBuffer();
38 
40  struct Context {
41  explicit Context(std::function<void()> resetCallback);
42  Context(const Context&) = delete;
43  Context& operator=(const Context&) = delete;
44  Context(Context&&) noexcept;
45  Context& operator=(Context&&) noexcept;
46  ~Context();
47 
49  void clear();
50 
52  void clear(Color color);
53 
54  private:
55  std::function<void()> resetCallback;
56  };
57 
61 #if __cplusplus >= 201703L
62  [[nodiscard]]
63 #endif
64  Context use() const;
65 
67  void draw(Vec2 position, const ShaderProgram* = nullptr) const;
68  void draw(double x, double y) const;
69 
70  void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
71 
73  void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
74 
77  [[deprecated("use jngl::FrameBuffer::Context::clear(Color) instead")]]
78  static void clear();
79 
81  Vec2 getSize() const;
82 
88  uint32_t getTextureID() const;
89 
90 private:
91  struct Impl;
93 };
94 
95 } // namespace jngl
Contains jngl::Color class.
Contains jngl::Finally class.
Contains jngl::Mat3 class.
Contains jngl::Pixels class.
Contains jngl::Vec2 class.
Object representing a RGB color.
Definition: Color.hpp:25
Image framebuffer object which can be rendered on.
Definition: framebuffer.hpp:22
void draw(Vec2 position, const ShaderProgram *=nullptr) const
Draws the framebuffer image to the screen.
static void clear()
Clear the framebuffer with the color set by jngl::setBackgroundColor.
FrameBuffer(Pixels width, Pixels height)
Creates a framebuffer object with width times height actual pixels.
void drawMesh(const std::vector< Vertex > &vertexes, const ShaderProgram *=nullptr) const
Draws a list of triangles with the framebuffer's texture on it.
Context use() const
Starts drawing on the FrameBuffer as long as Context is alive.
FrameBuffer(ScaleablePixels width, ScaleablePixels height)
Creates a framebuffer object with width times height scalable pixels.
uint32_t getTextureID() const
Returns the OpenGL texture ID of the associated image buffer.
Vec2 getSize() const
Returns the size in screen pixels.
FrameBuffer(std::array< Pixels, 2 > size)
Creates a framebuffer object with size[0] times size[1] pixels.
3x3 matrix
Definition: Mat3.hpp:20
Scale-dependent pixels, corresponds to actual pixels on the screen.
Definition: Pixels.hpp:14
Scale-independent pixels, see jngl::getScaleFactor()
Linked vertex and fragment shaders.
Two-dimensional vector.
Definition: Vec2.hpp:35
JNGL's main namespace.
Definition: App.hpp:10
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
Lifetime object when the FrameBuffer is in use.
Definition: framebuffer.hpp:40
void clear(Color color)
Clear the framebuffer with color.
void clear()
Clear the framebuffer with a transparent background.