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 "TextureFilter.hpp"
16
17#include <memory>
18#include <vector>
19
20namespace jngl {
21
22class ShaderProgram;
23
39public:
53 FrameBuffer(Pixels width, Pixels height, bool hdr = false, bool antiAliased = false);
54
56 FrameBuffer(ScaleablePixels width, ScaleablePixels height, bool hdr = false,
57 bool antiAliased = false);
58
60 explicit FrameBuffer(std::array<Pixels, 2> size, bool hdr = false, bool antiAliased = false);
61
62 FrameBuffer(const FrameBuffer&) = delete;
63 FrameBuffer& operator=(const FrameBuffer&) = delete;
64 FrameBuffer(FrameBuffer&&) noexcept;
65 FrameBuffer& operator=(FrameBuffer&&) noexcept;
66 ~FrameBuffer();
67
69 struct Context {
70 explicit Context(std::function<void()> resetCallback);
71 Context(const Context&) = delete;
72 Context& operator=(const Context&) = delete;
73 Context(Context&&) noexcept;
74 Context& operator=(Context&&) noexcept;
75 ~Context();
76
78 void clear();
79
81 void clear(Rgb color);
82
85 void clear(Rgba color);
86
87 private:
88 std::function<void()> resetCallback;
89 };
90
96#if __cplusplus >= 201703L
97 [[nodiscard]]
98#endif
99 Context use() const;
100
102 void draw(Vec2 position, const ShaderProgram* = nullptr) const;
103 void draw(double x, double y) const;
104
108 void draw(Mat3 modelview, const ShaderProgram* = nullptr) const;
109
116 void draw(Mat3 modelview, TextureFilter textureFilter, const ShaderProgram* = nullptr) const;
117
121 void drawMesh(const std::vector<Vertex>& vertexes, const ShaderProgram* = nullptr) const;
122
127 void drawMesh(const Mat3& modelview, const std::vector<Vertex>& vertexes,
128 const ShaderProgram* shaderProgram = nullptr) const;
129 void drawMesh(const Mat3& modelview, const std::vector<Vertex>& vertexes, Rgba color,
130 const ShaderProgram* = nullptr) const;
131
134 [[deprecated("use jngl::FrameBuffer::Context::clear(Color) instead")]]
135 static void clear();
136
138 Vec2 getSize() const;
139
140 Pixels getPixelWidth() const;
141 Pixels getPixelHeight() const;
142
151 [[deprecated("There's no OpenGL texture with other rendering backends, draw the FrameBuffer "
152 "with draw() instead")]]
153 uint32_t getTextureID() const;
154
155private:
156 struct Impl;
158};
159
160} // namespace jngl
Contains jngl::Mat3 class.
Contains jngl::Pixels class.
Contains jngl::Rgba class.
Contains jngl::ShaderProgram class.
Contains jngl::TextureFilter.
Contains jngl::Vec2 class.
Image framebuffer object which can be rendered on.
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, bool hdr=false, bool antiAliased=false)
Creates a framebuffer object with width times height actual pixels.
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(ScaleablePixels width, ScaleablePixels height, bool hdr=false, bool antiAliased=false)
Creates a framebuffer object with width times height scalable 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.
FrameBuffer(std::array< Pixels, 2 > size, bool hdr=false, bool antiAliased=false)
Creates a framebuffer object with size[0] times size[1] pixels.
void drawMesh(const Mat3 &modelview, const std::vector< Vertex > &vertexes, const ShaderProgram *shaderProgram=nullptr) const
Draws a list of triangles with the framebuffer's texture on it, with modelview instead of the global ...
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.
TextureFilter
How a texture is sampled when it's drawn bigger or smaller than its pixel size.
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.