JNGL
Easy to use cross-platform 2D game library
Loading...
Searching...
No Matches
FrameBuffer.hpp
Go to the documentation of this file.
1// Copyright 2012-2026 Jan Niklas Hasse <jhasse@bixense.com>
2// For conditions of distribution and use, see copyright notice in LICENSE.txt
5#pragma once
6
7#include "Mat3.hpp"
8#include "Pixels.hpp"
9#include "Rgba.hpp"
10#include "ShaderProgram.hpp"
11#include "Vec2.hpp"
12#include "Vertex.hpp"
13
14#include <functional>
15#include <memory>
16#include <vector>
17
18namespace jngl {
19
20enum class TextureFilter : uint8_t {
24
28};
29
30class ShaderProgram;
31
47public:
53 FrameBuffer(Pixels width, Pixels height, bool hdr = false);
54
56 FrameBuffer(ScaleablePixels width, ScaleablePixels height, bool hdr = false);
57
59 explicit FrameBuffer(std::array<Pixels, 2> size, bool hdr = false);
60
61 FrameBuffer(const FrameBuffer&) = delete;
62 FrameBuffer& operator=(const FrameBuffer&) = delete;
63 FrameBuffer(FrameBuffer&&) noexcept;
64 FrameBuffer& operator=(FrameBuffer&&) noexcept;
65 ~FrameBuffer();
66
68 struct Context {
69 explicit Context(std::function<void()> resetCallback);
70 Context(const Context&) = delete;
71 Context& operator=(const Context&) = delete;
72 Context(Context&&) noexcept;
73 Context& operator=(Context&&) noexcept;
74 ~Context();
75
77 void clear();
78
80 void clear(Rgb color);
81
84 void clear(Rgba color);
85
86 private:
87 std::function<void()> resetCallback;
88 };
89
95#if __cplusplus >= 201703L
96 [[nodiscard]]
97#endif
98 Context use() const;
99
101 void draw(Vec2 position, const ShaderProgram* = nullptr) const;
102 void draw(double x, double y) const;
103
107 void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
108
115 void draw(Mat3 modelview, TextureFilter textureFilter, const ShaderProgram* = nullptr) const;
116
118 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
119
122 [[deprecated("use jngl::FrameBuffer::Context::clear(Color) instead")]]
123 static void clear();
124
126 Vec2 getSize() const;
127
128 Pixels getPixelWidth() const;
129 Pixels getPixelHeight() const;
130
136 uint32_t getTextureID() const;
137
138private:
139 struct Impl;
141};
142
143} // namespace jngl
Contains jngl::Mat3 class.
Contains jngl::Pixels class.
Contains jngl::Rgba class.
Contains jngl::ShaderProgram class.
Contains jngl::Vec2 class.
Image framebuffer object which can be rendered on.
FrameBuffer(ScaleablePixels width, ScaleablePixels height, bool hdr=false)
Creates a framebuffer object with width times height scalable pixels.
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.
void draw(Mat3 modelview, TextureFilter textureFilter, const ShaderProgram *=nullptr) const
Draws the framebuffer image to the screen with a specific texture filter.
void drawMesh(const std::vector< Vertex > &vertexes, const ShaderProgram *=nullptr) const
Draws a list of triangles with the framebuffer's texture on it.
FrameBuffer(std::array< Pixels, 2 > size, bool hdr=false)
Creates a framebuffer object with size[0] times size[1] pixels.
FrameBuffer(Pixels width, Pixels height, bool hdr=false)
Creates a framebuffer object with width times height actual pixels.
Context use() const
Starts drawing on the FrameBuffer as long as Context is alive.
void draw(Mat3 modelview, const ShaderProgram *=nullptr) const
Draws the framebuffer image to the screen.
uint32_t getTextureID() const
Returns the OpenGL texture ID of the associated image buffer.
Vec2 getSize() const
Returns the size in screen pixels.
3x3 matrix
Definition Mat3.hpp:18
Scale-dependent pixels, corresponds to actual pixels on the screen.
Definition Pixels.hpp:14
Object representing a RGB color, new version of jngl::Color (which will be deprecated in the future)
Definition Rgb.hpp:21
Object representing a RGBA color.
Definition Rgba.hpp:22
Scale-independent pixels, also called "screen pixels".
Linked vertex and fragment shaders.
Two-dimensional vector.
Definition Vec2.hpp:36
JNGL's main namespace.
@ NearestNeighbor
Nearest Neighbor Filtering is used when jngl::AppParameters::pixelArt is true.
@ Bilinear
Bilinear Filtering is used when jngl::AppParameters::pixelArt is false.
jngl::Mat3 modelview()
Returns a copy of the global ModelView matrix.
Lifetime object when the FrameBuffer is in use.
void clear(Rgb color)
Clear the framebuffer with color.
void clear()
Clear the framebuffer with a transparent background.
void clear(Rgba color)
Clear the framebuffer with color, including its alpha, e.g.