Skip to content

Commit d76c488

Browse files
authored
Merge pull request #191 from NOOBDY/aa
added anti-aliasing support and some documentation
2 parents 579691c + aa31479 commit d76c488

File tree

6 files changed

+83
-13
lines changed

6 files changed

+83
-13
lines changed

include/Core/Texture.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
namespace Core {
77
class Texture {
88
public:
9-
Texture(GLint format, int width, int height, const void *data);
9+
Texture(GLint format, int width, int height, const void *data, bool useAA);
1010
Texture(const Texture &) = delete;
1111
Texture(Texture &&texture);
1212

@@ -21,9 +21,13 @@ class Texture {
2121
void Unbind() const;
2222

2323
void UpdateData(GLint format, int width, int height, const void *data);
24+
void UseAntiAliasing(bool useAA);
2425

2526
private:
2627
GLuint m_TextureId;
28+
29+
GLenum m_MinFilter;
30+
GLenum m_MagFilter;
2731
};
2832
} // namespace Core
2933

include/Util/Image.hpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Core/VertexArray.hpp"
1313

1414
#include "Util/AssetStore.hpp"
15-
#include "Util/Transform.hpp"
1615

1716
namespace Util {
1817
/**
@@ -29,8 +28,10 @@ class Image : public Core::Drawable {
2928
* @brief Constructor that takes a file path to the image.
3029
*
3130
* @param filepath The file path to the image.
31+
* @param useAA Flag indicating whether anti-aliasing should be enabled
32+
* (default is true).
3233
*/
33-
explicit Image(const std::string &filepath);
34+
Image(const std::string &filepath, bool useAA = true);
3435

3536
/**
3637
* @brief Retrieves the size of the image.
@@ -50,6 +51,20 @@ class Image : public Core::Drawable {
5051
*/
5152
void SetImage(const std::string &filepath);
5253

54+
/**
55+
* @brief Sets whether anti-aliasing (AA) should be enabled or disabled.
56+
*
57+
* @param useAA A boolean value indicating whether anti-aliasing should be
58+
* enabled (true) or disabled (false).
59+
*
60+
* @note This function only sets the internal flag for anti-aliasing and
61+
* does not directly affect rendering. The actual effect of anti-aliasing
62+
* depends on the rendering pipeline and the graphics hardware capabilities.
63+
*
64+
* @sa https://en.wikipedia.org/wiki/Spatial_anti-aliasing
65+
*/
66+
void UseAntiAliasing(bool useAA);
67+
5368
/**
5469
* @brief Draws the image with a given transform and z-index.
5570
*

include/Util/Text.hpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include "Core/VertexArray.hpp"
1313

1414
#include "Util/Color.hpp"
15-
#include "Util/Transform.hpp"
1615

1716
namespace Util {
1817
/**
@@ -25,8 +24,18 @@ namespace Util {
2524
*/
2625
class Text : public Core::Drawable {
2726
public:
27+
/**
28+
* @brief Constructor for the Text class.
29+
*
30+
* @param font The font file path or name.
31+
* @param size The font size.
32+
* @param text The text content to render.
33+
* @param color The color of the text (default is gray).
34+
* @param useAA Flag indicating whether anti-aliasing should be enabled
35+
* (default is true).
36+
*/
2837
Text(const std::string &font, int size, const std::string &text,
29-
const Util::Color &color = Color(127, 127, 127));
38+
const Util::Color &color = Color(127, 127, 127), bool useAA = true);
3039

3140
glm::vec2 GetSize() const override { return m_Size; };
3241

@@ -50,9 +59,26 @@ class Text : public Core::Drawable {
5059
ApplyTexture();
5160
};
5261

62+
/**
63+
* @brief Sets whether anti-aliasing (AA) should be enabled or disabled.
64+
*
65+
* @param useAA A boolean value indicating whether anti-aliasing should be
66+
* enabled (true) or disabled (false).
67+
*
68+
* @note This function only sets the internal flag for anti-aliasing and
69+
* does not directly affect rendering. The actual effect of anti-aliasing
70+
* depends on the rendering pipeline and the graphics hardware capabilities.
71+
*
72+
* @sa https://en.wikipedia.org/wiki/Spatial_anti-aliasing
73+
*/
74+
void UseAntiAliasing(bool useAA);
75+
5376
/**
5477
* @brief Draws the text with a given transform and z-index.
5578
*
79+
* This function draws the image at the specified z-index and applies the
80+
* given transform.
81+
*
5682
* @param transform The transform to apply to the text.
5783
* @param zIndex The z-index at which to draw the text.
5884
*/

src/Core/Texture.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
#include "Util/Logger.hpp"
66

77
namespace Core {
8-
Texture::Texture(GLint format, int width, int height, const void *data) {
8+
Texture::Texture(GLint format, int width, int height, const void *data,
9+
bool useAA) {
910
glGenTextures(1, &m_TextureId);
11+
UseAntiAliasing(useAA);
1012
UpdateData(format, width, height, data);
1113
}
1214

@@ -56,7 +58,23 @@ void Texture::UpdateData(GLint format, int width, int height,
5658
glTexImage2D(GL_TEXTURE_2D, 0, GlFormatToGlInternalFormat(format), width,
5759
height, 0, format, GL_UNSIGNED_BYTE, data);
5860

59-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60-
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
61+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_MinFilter);
62+
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_MagFilter);
63+
glGenerateMipmap(GL_TEXTURE_2D);
64+
}
65+
66+
void Texture::UseAntiAliasing(bool useAA) {
67+
/**
68+
* additional docs
69+
* https://www.khronos.org/opengl/wiki/Texture
70+
* https://www.khronos.org/opengl/wiki/Sampler_Object#Sampling_parameters
71+
*/
72+
if (useAA) {
73+
m_MinFilter = GL_LINEAR_MIPMAP_LINEAR;
74+
m_MagFilter = GL_LINEAR;
75+
} else {
76+
m_MinFilter = GL_NEAREST_MIPMAP_NEAREST;
77+
m_MagFilter = GL_NEAREST;
78+
}
6179
}
6280
} // namespace Core

src/Util/Image.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include "Core/Texture.hpp"
77
#include "Core/TextureUtils.hpp"
88
#include "Util/MissingTexture.hpp"
9-
#include "Util/TransformUtils.hpp"
109

1110
#include "config.hpp"
1211
#include <glm/fwd.hpp>
@@ -25,7 +24,7 @@ std::shared_ptr<SDL_Surface> LoadSurface(const std::string &filepath) {
2524
}
2625

2726
namespace Util {
28-
Image::Image(const std::string &filepath)
27+
Image::Image(const std::string &filepath, bool useAA)
2928
: m_Path(filepath) {
3029
if (s_Program == nullptr) {
3130
InitProgram();
@@ -47,7 +46,7 @@ Image::Image(const std::string &filepath)
4746

4847
m_Texture = std::make_unique<Core::Texture>(
4948
Core::SdlFormatToGlFormat(surface->format->format), surface->w,
50-
surface->h, surface->pixels);
49+
surface->h, surface->pixels, useAA);
5150
m_Size = {surface->w, surface->h};
5251
}
5352

@@ -59,6 +58,10 @@ void Image::SetImage(const std::string &filepath) {
5958
m_Size = {surface->w, surface->h};
6059
}
6160

61+
void Image::UseAntiAliasing(bool useAA) {
62+
m_Texture->UseAntiAliasing(useAA);
63+
}
64+
6265
void Image::Draw(const Core::Matrices &data) {
6366
m_UniformBuffer->SetData(0, data);
6467

src/Util/Text.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Util {
1212
Text::Text(const std::string &font, int fontSize, const std::string &text,
13-
const Util::Color &color)
13+
const Util::Color &color, bool useAA)
1414
: m_Text(text),
1515
m_Color(color) {
1616
if (s_Program == nullptr) {
@@ -43,10 +43,14 @@ Text::Text(const std::string &font, int fontSize, const std::string &text,
4343
m_Texture = std::make_unique<Core::Texture>(
4444
Core::SdlFormatToGlFormat(surface->format->format),
4545
surface->pitch / surface->format->BytesPerPixel, surface->h,
46-
surface->pixels);
46+
surface->pixels, useAA);
4747
m_Size = {surface->pitch / surface->format->BytesPerPixel, surface->h};
4848
}
4949

50+
void Text::UseAntiAliasing(bool useAA) {
51+
m_Texture->UseAntiAliasing(useAA);
52+
}
53+
5054
void Text::Draw(const Core::Matrices &data) {
5155
m_UniformBuffer->SetData(0, data);
5256

0 commit comments

Comments
 (0)