Skip to content

Commit 197879b

Browse files
committed
Fix all warnings
- Add `static_cast`s where necessary - Simplify the interface of the Text class (only deal with utf-8 encoded `std::strings` and let the user manage the conversions) - Use c++20
1 parent f46d3e7 commit 197879b

22 files changed

+181
-259
lines changed

examples/magnifying_glass/magnifying_glass.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,14 @@ int main() {
5656
window.setCamera(camera);
5757
const glm::vec2 win_size = window.getSize();
5858
window.setClipPlanes(0, win_size.x, win_size.y, 0);
59-
window.setViewport(0, 0, win_size.x, win_size.y);
59+
window.setViewport(0, 0, static_cast<int>(win_size.x), static_cast<int>(win_size.y));
6060

6161
window.draw(rect);
6262
window.draw(frame);
6363

6464
window.setCamera(frameCam);
65-
window.setClipPlanes(zoomValue, frameSize - zoomValue, frameSize - zoomValue, zoomValue);
66-
window.setViewport(framePos.x, win_size.y - framePos.y - frameSize, frameSize, frameSize);
65+
window.setClipPlanes(static_cast<float>(zoomValue), static_cast<float>(frameSize - zoomValue), static_cast<float>(frameSize - zoomValue), static_cast<float>(zoomValue));
66+
window.setViewport(framePos.x, static_cast<int>(win_size.y) - framePos.y - frameSize, frameSize, frameSize);
6767

6868
window.draw(rect);
6969

examples/text/text.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ int main() {
2525
for (wchar_t c = L'\u03B1'; c < L'\u03CA'; c++) {
2626
greek_lowercase += c;
2727
}
28-
my::Text greek_alphabet(greek_lowercase, open_sans, 30);
28+
my::Text greek_alphabet(my::util::toUtf8(greek_lowercase), open_sans, 30);
2929
greek_alphabet.setColor(my::Color::red);
3030
greek_alphabet.setPosition(50, 500);
3131

32-
my::Text math_formula(U"\u2200\U0001D465 \u2208 \u211D\u2217, 1/\U0001D465 is defined", math_font, 30);
32+
my::Text math_formula(my::util::toUtf8(U"\u2200\U0001D465 \u2208 \u211D\u2217, 1/\U0001D465 is defined"), math_font, 30);
3333
math_formula.setColor(my::Color::green);
3434
math_formula.rotate(-20);
3535
math_formula.setPosition(600, 450, true);
@@ -41,7 +41,7 @@ int main() {
4141
}
4242
hiraganas += c;
4343
}
44-
my::Text some_hiraganas(hiraganas, jp_font, 30);
44+
my::Text some_hiraganas(my::util::toUtf8(hiraganas), jp_font, 30);
4545
some_hiraganas.setColor(my::Color::white);
4646
some_hiraganas.setPosition(window.getSize().x / 2, 75, true);
4747
some_hiraganas.setOutlineThickness(2);

include/MyGL/Color.hpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <glm/glm.hpp>
77

88
#include <string>
9+
#include <cstdint>
910

1011
namespace my
1112
{
@@ -112,4 +113,4 @@ namespace my
112113
MYGL_EXPORT bool operator==(const my::Color& color1, const my::Color& color2) noexcept;
113114
MYGL_EXPORT bool operator!=(const my::Color& color1, const my::Color& color2) noexcept;
114115

115-
#endif // MYGL_COLOR
116+
#endif // MYGL_COLOR

include/MyGL/Drawable/AbstractShape.hpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <string>
1212
#include <vector>
13+
#include <numbers>
1314

1415
namespace my
1516
{
@@ -22,7 +23,7 @@ namespace my
2223
static void initShaders();
2324

2425
protected:
25-
static const float pi;
26+
static constexpr float pi = std::numbers::pi_v<float>;
2627
static my::ShaderProgram shader;
2728
static my::ShaderProgram texShader;
2829

@@ -174,7 +175,7 @@ namespace my
174175
* @param b The blue component
175176
* @param alpha The alpha (transparency) value, by default 255
176177
*/
177-
MYGL_EXPORT virtual void setColor(int r, int g, int b, int alpha = 255) noexcept;
178+
MYGL_EXPORT virtual void setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) noexcept;
178179

179180
/**
180181
* @brief Sets the shape's color from an existing color
@@ -207,7 +208,7 @@ namespace my
207208
* @param b The blue component
208209
* @param alpha The alpha (transparency) value, by default 255
209210
*/
210-
MYGL_EXPORT void setOutlineColor(int r, int g, int b, int alpha = 255) noexcept;
211+
MYGL_EXPORT void setOutlineColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255) noexcept;
211212

212213
/**
213214
* @brief Tells wether 2 shapes are overlapping using the separating

include/MyGL/Drawable/Font.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ namespace my
2929
* @param glyph A glyph slot containing the bitmap we want to copy (it
3030
* should be loaded with the FT_LOAD_RENDER flag)
3131
* @param texture The vector we want to twrite to
32-
* @param x The x coord of thetop left hand corner of the zone we wil copy the bitmap to
33-
* @param y The y coord of thetop left hand corner of the zone we wil copy the bitmap to
32+
* @param x The x coord of the top left hand corner of the zone we wil copy the bitmap to
33+
* @param y The y coord of the top left hand corner of the zone we wil copy the bitmap to
3434
* @param width The final texture's width
3535
*/
36-
static void addGlyph(FT_GlyphSlot& glyph, std::vector<uint8_t>& texture, size_t x, size_t y, size_t width);
36+
static void addGlyph(FT_GlyphSlot& glyph, std::vector<uint8_t>& texture, FT_Pos x, FT_Pos y, FT_Pos width);
3737

3838
/**
3939
* @brief Creates a texture containing a string

include/MyGL/Drawable/Text.hpp

+33-23
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,38 @@
55

66
#include "Font.hpp"
77
#include "Rectangle.hpp"
8+
#include <iterator>
9+
#include <string_view>
10+
#include <utf8.h>
811

912
namespace my
1013
{
14+
namespace util
15+
{
16+
template<typename StringType>
17+
inline std::string toUtf8(const StringType& text) {
18+
std::basic_string_view view{std::begin(text), std::end(text)};
19+
20+
using CharType = typename decltype(view)::value_type;
21+
22+
if constexpr (sizeof(CharType) == 1) {
23+
return view;
24+
} else if constexpr (sizeof(CharType) == 2) {
25+
std::string result;
26+
result.reserve(2 * view.size());
27+
utf8::utf16to8(view.begin(), view.end(), std::back_inserter(result));
28+
return result;
29+
} else if constexpr (sizeof(CharType) == 4) {
30+
std::string result;
31+
result.reserve(4 * view.size());
32+
utf8::utf32to8(view.begin(), view.end(), std::back_inserter(result));
33+
return result;
34+
} else {
35+
static_assert(false, "Unknown character encoding");
36+
}
37+
}
38+
} // namespace util
39+
1140
/**
1241
* @brief Class for drawing strings of text
1342
*/
@@ -42,47 +71,28 @@ namespace my
4271
*/
4372
MYGL_EXPORT Text();
4473

45-
/** @name Constructors
46-
@{ */
4774
/**
4875
* @brief Creates a text object
49-
* @param text A std::string (or wstring, u16string, u32string) with the text to
50-
* be displayed
76+
* @param text A utf-8 encoded std::string with the text to be displayed
5177
* @param font A my::Font object defining the text's font
5278
* @param size The character size in pixels
5379
*/
5480
MYGL_EXPORT Text(const std::string& text, my::Font& font, unsigned int size = 30u);
55-
MYGL_EXPORT Text(const std::wstring& text, my::Font& font, unsigned int size = 30u);
56-
MYGL_EXPORT Text(const std::u16string& text, my::Font& font, unsigned int size = 30u);
57-
MYGL_EXPORT Text(const std::u32string& text, my::Font& font, unsigned int size = 30u);
58-
/** @} */
5981

6082
/** @brief Default destructor */
6183
MYGL_EXPORT ~Text() override;
6284

63-
/** @name Setters
64-
@{ */
6585
/**
6686
* @brief Sets the displayed text's content
67-
* @param text The new text to display
87+
* @param text A utf-8 encoded std::string
6888
*/
6989
MYGL_EXPORT void setContent(const std::string& text);
70-
MYGL_EXPORT void setContent(const std::wstring& text);
71-
MYGL_EXPORT void setContent(const std::u16string& text);
72-
MYGL_EXPORT void setContent(const std::u32string& text);
73-
/** @} */
7490

75-
/** @name Getters
76-
@{ */
7791
/**
7892
* @brief Returns the text that is currently displayed
79-
* @return The content of the string that is currently displayed
93+
* @return The text that is currently displayed as a utf-8 encoded std::string
8094
*/
81-
MYGL_EXPORT std::string getString() const;
82-
MYGL_EXPORT std::wstring getWString() const;
83-
MYGL_EXPORT std::u16string getU16String() const;
84-
MYGL_EXPORT std::u32string getU32String() const noexcept;
85-
/** @} */
95+
MYGL_EXPORT const std::string& getContent() const;
8696

8797
/**
8898
* @brief Changes the font

include/MyGL/Image.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "mygl_export.h"
55
#include <memory>
66
#include <string>
7+
#include <cstdint>
78

89
namespace my
910
{
@@ -72,10 +73,9 @@ namespace my
7273
MYGL_EXPORT bool isUsable() const noexcept;
7374

7475
/**
75-
* @brief Returns a read only pointer to the pixel array
76-
* @return
76+
* @brief Returns a pointer to the pixel array
7777
*/
78-
MYGL_EXPORT const uint8_t* data() const noexcept;
78+
MYGL_EXPORT uint8_t* data() const noexcept;
7979

8080
/**
8181
* @brief Indicates the image's width
@@ -97,4 +97,4 @@ namespace my
9797
};
9898
} // namespace my
9999

100-
#endif // MYGL_IMAGE
100+
#endif // MYGL_IMAGE

include/MyGL/Texture.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ namespace my
113113
* @param b The blue component
114114
* @param alpha The alpha component
115115
*/
116-
MYGL_EXPORT void setBorderColor(int r, int g, int b, int alpha = 255);
116+
MYGL_EXPORT void setBorderColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha = 255);
117117

118118
/**
119119
* @brief Sets the border color used with GL_CLAMP_TO_BORDER wrapping method

include/MyGL/Window.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ namespace my
158158
* @param bottom The frustum's bottom coordinate
159159
* @param top The frustum's top coordinate
160160
*/
161-
MYGL_EXPORT void setClipPlanes(int left, int right, int bottom, int top);
161+
MYGL_EXPORT void setClipPlanes(float left, float right, float bottom, float top);
162162

163163
/**
164164
* @brief Sets the viewport's position and size

include/MyGL/common.hpp

+3-10
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ namespace my
2323
* @brief Helper function for static variables relying on glfw functions
2424
*
2525
* This function should be used to call glfw functions when it is unsure
26-
* whether glfw was already initialized (for example in static variables'
26+
* whether glfw was already initialized (for example in static variables
2727
* definitions), it ensures that glfw is initialized before calling the
28-
* function
28+
* function.
2929
*
3030
* @tparam GLFWfunction
3131
* @tparam ...Args
@@ -38,13 +38,6 @@ namespace my
3838
initGLFW();
3939
return function(args...);
4040
}
41-
42-
/**
43-
* @brief Sleeps for a certain amount of time
44-
* @param nanoseconds The sleep time in nanoseconds
45-
*/
46-
MYGL_EXPORT void sleep(unsigned long long nanoseconds); // TODO
47-
4841
} // namespace my
4942

50-
#endif // MYGL_COMMON
43+
#endif // MYGL_COMMON

meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
project('MyGL', 'c', 'cpp',
22
license: 'zlib',
33
version: '0.1.0',
4-
default_options: ['cpp_std=c++17'])
4+
default_options: ['cpp_std=c++20'])
55

66
subdir('include')
77

src/Cursor.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ namespace my
3131
bool Cursor::load(const Image& image, int xhot, int yhot) {
3232
if (image.isUsable()) {
3333
GLFWimage cursor;
34-
cursor.width = image.getWidth();
35-
cursor.height = image.getHeight();
36-
cursor.pixels = const_cast<uint8_t*>(image.data());
34+
cursor.width = static_cast<int>(image.getWidth());
35+
cursor.height = static_cast<int>(image.getHeight());
36+
cursor.pixels = image.data();
3737
p_cursor.reset(glfwCreateCursor(&cursor, xhot, cursor.height - 1 - yhot), CursorDeleter());
3838
} else {
3939
p_cursor.reset();
@@ -49,4 +49,4 @@ namespace my
4949
return p_cursor != nullptr;
5050
}
5151

52-
} // namespace my
52+
} // namespace my

src/Drawable/AbstractShape.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ constexpr const char* texFragmentSource =
4444

4545
namespace my
4646
{
47-
const float AbstractShape::pi = 3.1415926535f;
4847
ShaderProgram AbstractShape::shader;
4948
ShaderProgram AbstractShape::texShader;
5049
const ShaderProgram& AbstractShape::defaultShader = AbstractShape::shader;
@@ -70,8 +69,9 @@ namespace my
7069
}
7170

7271
AbstractShape::AbstractShape(int width, int height) :
73-
m_position(0.0f, 0.0f), m_originalScale(width / 2.0f, height / 2.0f), m_scaleFactor(1.0f, 1.0f), m_rotationAngle(0.0f), m_updateMatrix(true),
74-
m_model(1.0f), m_color(100, 100, 100), m_outlineThickness(0.0f), m_outlineColor(255, 255, 255), m_outlineModel(1.0f), m_isTextured(false) {
72+
m_position(0.0f, 0.0f), m_originalScale(static_cast<float>(width) / 2.0f, static_cast<float>(height) / 2.0f), m_scaleFactor(1.0f, 1.0f),
73+
m_rotationAngle(0.0f), m_updateMatrix(true), m_model(1.0f), m_color(100, 100, 100), m_outlineThickness(0.0f), m_outlineColor(255, 255, 255),
74+
m_outlineModel(1.0f), m_isTextured(false) {
7575
initShaders();
7676
m_shader = shader;
7777
m_outlineShader = shader;
@@ -154,8 +154,8 @@ namespace my
154154
return m_rotationAngle;
155155
}
156156

157-
void AbstractShape::setColor(int r, int g, int b, int alpha) noexcept {
158-
m_color = my::Color(glm::clamp(r, 0, 255), glm::clamp(g, 0, 255), glm::clamp(b, 0, 255), glm::clamp(alpha, 0, 255));
157+
void AbstractShape::setColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha) noexcept {
158+
m_color = my::Color(r, g, b, alpha);
159159
}
160160

161161
void AbstractShape::setColor(const my::Color& color) noexcept {
@@ -180,8 +180,8 @@ namespace my
180180
m_outlineColor = color;
181181
}
182182

183-
void AbstractShape::setOutlineColor(int r, int g, int b, int alpha) noexcept {
184-
m_outlineColor = my::Color(glm::clamp(r, 0, 255), glm::clamp(g, 0, 255), glm::clamp(b, 0, 255), glm::clamp(alpha, 0, 255));
183+
void AbstractShape::setOutlineColor(uint8_t r, uint8_t g, uint8_t b, uint8_t alpha) noexcept {
184+
m_outlineColor = my::Color(r, g, b, alpha);
185185
}
186186

187187
bool AbstractShape::SATCollides(const AbstractShape& shape) const {

0 commit comments

Comments
 (0)