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 "ShaderProgram.hpp"
10#include "Vec2.hpp"
11#include "Vertex.hpp"
12
13#include <functional>
14#include <memory>
15#include <vector>
16
17namespace jngl {
18
19enum class TextureFilter : uint8_t {
23
27};
28
29class ShaderProgram;
30
46public:
52 FrameBuffer(Pixels width, Pixels height, bool hdr = false);
53
55 FrameBuffer(ScaleablePixels width, ScaleablePixels height, bool hdr = false);
56
58 explicit FrameBuffer(std::array<Pixels, 2> size, bool hdr = false);
59
60 FrameBuffer(const FrameBuffer&) = delete;
61 FrameBuffer& operator=(const FrameBuffer&) = delete;
62 FrameBuffer(FrameBuffer&&) noexcept;
63 FrameBuffer& operator=(FrameBuffer&&) noexcept;
64 ~FrameBuffer();
65
67 struct Context {
68 explicit Context(std::function<void()> resetCallback);
69 Context(const Context&) = delete;
70 Context& operator=(const Context&) = delete;
71 Context(Context&&) noexcept;
72 Context& operator=(Context&&) noexcept;
73 ~Context();
74
76 void clear();
77
79 void clear(Rgb color);
80
81 private:
82 std::function<void()> resetCallback;
83 };
84
90#if __cplusplus >= 201703L
91 [[nodiscard]]
92#endif
93 Context use() const;
94
96 void draw(Vec2 position, const ShaderProgram* = nullptr) const;
97 void draw(double x, double y) const;
98
102 void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
103
110 void draw(Mat3 modelview, TextureFilter textureFilter, const ShaderProgram* = nullptr) const;
111
113 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
114
117 [[deprecated("use jngl::FrameBuffer::Context::clear(Color) instead")]]
118 static void clear();
119
121 Vec2 getSize() const;
122
123 Pixels getPixelWidth() const;
124 Pixels getPixelHeight() const;
125
131 uint32_t getTextureID() const;
132
133private:
134 struct Impl;
136};
137
138} // namespace jngl
Contains jngl::Mat3 class.
Contains jngl::Pixels 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
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.