diff --git a/.clang-format b/.clang-format
new file mode 100644
index 0000000..06f8ea8
--- /dev/null
+++ b/.clang-format
@@ -0,0 +1,7 @@
+BasedOnStyle: Microsoft
+UseTab: Never
+IndentWidth: 4
+AllowShortFunctionsOnASingleLine: InlineOnly
+ColumnLimit: 100
+BreakBeforeBraces: Linux
+PointerAlignment: Left
\ No newline at end of file
diff --git a/.gitattributes b/.gitattributes
new file mode 100644
index 0000000..e4c28ea
--- /dev/null
+++ b/.gitattributes
@@ -0,0 +1 @@
+*.ttf filter=lfs diff=lfs merge=lfs -text
diff --git a/.idea/.gitignore b/.idea/.gitignore
new file mode 100644
index 0000000..13566b8
--- /dev/null
+++ b/.idea/.gitignore
@@ -0,0 +1,8 @@
+# Default ignored files
+/shelf/
+/workspace.xml
+# Editor-based HTTP Client requests
+/httpRequests/
+# Datasource local storage ignored files
+/dataSources/
+/dataSources.local.xml
diff --git a/.idea/SDL_DrawText.iml b/.idea/SDL_DrawText.iml
new file mode 100644
index 0000000..f08604b
--- /dev/null
+++ b/.idea/SDL_DrawText.iml
@@ -0,0 +1,2 @@
+
+
\ No newline at end of file
diff --git a/.idea/misc.xml b/.idea/misc.xml
new file mode 100644
index 0000000..79b3c94
--- /dev/null
+++ b/.idea/misc.xml
@@ -0,0 +1,4 @@
+
+
+
+
\ No newline at end of file
diff --git a/.idea/modules.xml b/.idea/modules.xml
new file mode 100644
index 0000000..a310a14
--- /dev/null
+++ b/.idea/modules.xml
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/.idea/vcs.xml b/.idea/vcs.xml
new file mode 100644
index 0000000..94a25f7
--- /dev/null
+++ b/.idea/vcs.xml
@@ -0,0 +1,6 @@
+
+
+
+
+
+
\ No newline at end of file
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 8f8c222..846d61e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,7 +7,7 @@ set(BIN_DIR ${test_SOURCE_DIR}/bin)
# Bump up warning levels appropriately for clang, gcc & msvc and build in debug mode
if (${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU" OR ${CMAKE_CXX_COMPILER_ID} STREQUAL "Clang")
- set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++11")
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -pedantic -std=c++14")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O2")
elseif (${CMAKE_CXX_COMPILER_ID} STREQUAL "MSVC")
diff --git a/build/fonts/OfenbacherSchwabCAT.ttf b/build/fonts/OfenbacherSchwabCAT.ttf
index 7596111..25ce53b 100644
Binary files a/build/fonts/OfenbacherSchwabCAT.ttf and b/build/fonts/OfenbacherSchwabCAT.ttf differ
diff --git a/src/drawtext.cpp b/src/drawtext.cpp
index 2a813a4..6a41f4c 100644
--- a/src/drawtext.cpp
+++ b/src/drawtext.cpp
@@ -4,132 +4,93 @@
#include
#include
#include
+#include
-DrawText::DrawText(const char* fontPath, const int fontSize, const SDL_Color& fontColor)
+DrawText::DrawText(const char* fontPath, int fontSize, const SDL_Color& fontColor,
+ uint16_t initialCharacter, uint16_t finalCharacter, bool throwExceptions)
+ : throwExceptions_{throwExceptions}
{
+ if (finalCharacter - initialCharacter <= 0) {
+ throw std::logic_error("The final character must be bigger than the initial character.");
+ }
+
TTF_Font* font = TTF_OpenFont(fontPath, fontSize);
if (!font) {
std::stringstream message;
message << "DrawText::DrawText: " << TTF_GetError();
throw std::runtime_error(message.str());
}
- createAlphabet(font, fontColor);
+ createAlphabet(font, fontColor, initialCharacter, finalCharacter);
TTF_CloseFont(font);
}
DrawText::~DrawText()
{
- for (auto& i : alphabet) {
- SDL_FreeSurface(i);
+ for (std::map::iterator it = alphabet_.begin(); it != alphabet_.end();
+ it++) {
+ SDL_FreeSurface(it->second);
}
}
-void DrawText::createAlphabet(TTF_Font* font, const SDL_Color& fontColor)
+void DrawText::createAlphabet(TTF_Font* font, const SDL_Color& fontColor, uint16_t initialCharacter,
+ uint16_t finalCharacter)
{
- for (Uint16 c = INITIAL_CHARACTER; c <= FINAL_CHARACTER; c++) {
- alphabet[c - INITIAL_CHARACTER] = TTF_RenderGlyph_Blended(font, c, fontColor);
+ for (uint16_t c = initialCharacter; c <= finalCharacter; c++) {
+ auto* glyph = TTF_RenderGlyph_Blended(font, c, fontColor);
+ if (glyph == nullptr) {
+ throw std::runtime_error("Error creating alphabet");
+ }
+ alphabet_[c] = glyph;
}
}
-void DrawText::drawGlyph(SDL_Surface* destinationSurface, const unsigned char character, int& x,
- int& y)
+static int calculateIncrement(int x0, int w0, int x1, int w1)
{
- if (character == '\n') {
- SDL_Surface* glyph = alphabet[0];
- y = y + glyph->h;
- newLine = true;
- return;
+ int dx = 0;
+ if (w0 == 0) {
+ dx = w1;
+ } else {
+ dx = (x0 + w0) > (x1 + w1) ? w1 : w1 - (x1 + w1 - x0 - w0);
}
-
- unsigned char i = character - INITIAL_CHARACTER;
- SDL_Surface* glyph = alphabet[i];
-
- SDL_Rect dst_rect;
- dst_rect.x = x;
- dst_rect.y = y;
- dst_rect.w = glyph->w;
- dst_rect.h = glyph->h;
-
- x = x + glyph->w;
- SDL_BlitSurface(glyph, NULL, destinationSurface, &dst_rect);
+ return dx;
}
-void DrawText::print(SDL_Surface* destinationSurface, const std::string& text, int x, int y)
+void DrawText::drawGlyph(SDL_Surface* destinationSurface, uint16_t character, int& x, int& y,
+ Constrain constrain)
{
- auto tmp_x = x;
- auto tmp_y = y;
-
- for (const char* c = text.c_str(); *c != '\0'; c++) {
- if (newLine) {
- tmp_x = x;
- newLine = false;
- }
- drawGlyph(destinationSurface, *c, tmp_x, tmp_y);
+ if (y > (constrain.x0 + constrain.height) && constrain.height != 0) {
+ return;
}
-}
+ auto get_glyph_of_character = [&](uint16_t c) -> SDL_Surface* {
+ try {
+ auto* glyph = alphabet_.at(c);
+ return glyph;
+ } catch (std::out_of_range& e) {
+ if (throwExceptions_) {
+ throw std::logic_error("Trying to print character not defined in alphabet.");
+ }
+ }
+ return nullptr;
+ };
-void DrawText::drawGlyphW(SDL_Surface* destinationSurface, const wchar_t character, int& x, int& y)
-{
- if (character == '\n') {
- SDL_Surface* glyph = alphabet[0];
+ // New line
+ if (character == static_cast('\n')) {
+ auto* glyph = get_glyph_of_character(alphabet_.begin()->first);
y = y + glyph->h;
- newLine = true;
+ newLine_ = true;
return;
}
- unsigned char i = (unsigned char)character - INITIAL_CHARACTER;
- if (i >= FINAL_CHARACTER) {
+
+ // Normal character
+ auto* glyph = get_glyph_of_character(character);
+ if (glyph == nullptr) {
return;
}
- SDL_Surface* glyph = alphabet[i];
-
- SDL_Rect dst_rect;
- dst_rect.x = x;
- dst_rect.y = y;
- dst_rect.w = glyph->w;
- dst_rect.h = glyph->h;
+ const int dx = calculateIncrement(constrain.x0, constrain.width, x, glyph->w);
+ const int dy = calculateIncrement(constrain.y0, constrain.height, y, glyph->h);
+ SDL_Rect src_rect = {0, 0, dx, dy};
+ SDL_Rect dst_rect = {x, y, dx, dy};
x = x + glyph->w;
- SDL_BlitSurface(glyph, NULL, destinationSurface, &dst_rect);
-}
-
-void DrawText::print(SDL_Surface* destinationSurface, const std::wstring& text, int x, int y)
-{
- auto tmp_x = x;
- auto tmp_y = y;
-
- for (auto character = text.c_str(); *character != '\0'; character++) {
- if (newLine) {
- tmp_x = x;
- newLine = false;
- }
- drawGlyphW(destinationSurface, *character, tmp_x, tmp_y);
- }
-}
-std::string DrawText::format(const std::string text, ...)
-{
- std::string a;
-
- char buffer[BUFFER_SIZE] = {};
- {
- va_list list;
- va_start(list, text);
- vsnprintf(buffer, BUFFER_SIZE, text.c_str(), list);
- va_end(list);
- };
- a = buffer;
- return a;
-}
-
-std::wstring DrawText::format(const std::wstring text, ...)
-{
- std::wstring out;
- wchar_t buffer[BUFFER_SIZE] = {};
- {
- va_list list;
- va_start(list, text);
- vswprintf(buffer, BUFFER_SIZE, text.c_str(), list);
- va_end(list);
- };
- out = buffer;
- return out;
+ SDL_BlitSurface(glyph, &src_rect, destinationSurface, &dst_rect);
}
\ No newline at end of file
diff --git a/src/drawtext.h b/src/drawtext.h
index cb10028..69c3e69 100644
--- a/src/drawtext.h
+++ b/src/drawtext.h
@@ -3,98 +3,128 @@
#include
#include
-#include
-
-class DrawText {
-public:
- /**
- * @brief Defines the properties of the text to draw on the screen.
- * @param fontPath Path to the TTF file to be used.
- * @param fontSize Size of the font to write on screen.
- * @param fontColor Color of the text to write.
- */
- DrawText(const char *fontPath, int fontSize, const SDL_Color &fontColor);
-
- /**
- * @brief Destroys the alphabet and free memory.
- */
- ~DrawText();
- /**
- * @brief Print text on a given surface
- * @param destinationSurface Surface where the text will be written.
- * @param text Text to write on the screen.
- * @param x Horizontal position of the text.
- * @param y Vertical position of the text.
- * @param ... Optional variables for the special characters in the string.
- */
- void print(SDL_Surface *destinationSurface, const std::string& text, int x, int y);
-
- /**
- * @brief Print text on a given surface
- * @param destinationSurface Surface where the text will be written.
- * @param text Unicode text to write on the screen.
- * @param x Horizontal position of the text.
- * @param y Vertical position of the text.
- */
- void print(SDL_Surface *destinationSurface, const std::wstring& text, int x, int y);
+#include
+#include
+#include