Skip to content

Commit d05569e

Browse files
authored
Merge pull request #150 from ntut-xuan/149-Missing-Font-Texture
Completed "Add missing texture when font is not found"
2 parents 29fcdc6 + a9749db commit d05569e

File tree

7 files changed

+373
-30
lines changed

7 files changed

+373
-30
lines changed

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ set(INCLUDE_FILES
7373
${INCLUDE_DIR}/Core/Texture.hpp
7474
${INCLUDE_DIR}/Core/TextureUtils.hpp
7575
${INCLUDE_DIR}/Core/Drawable.hpp
76+
${INCLUDE_DIR}/Core/MissingFontTextureBase64.hpp
77+
${INCLUDE_DIR}/Core/MissingImageTextureBase64.hpp
7678

7779
${INCLUDE_DIR}/Util/LoadTextFile.hpp
7880
${INCLUDE_DIR}/Util/Logger.hpp

include/Core/MissingFontTextureBase64.hpp

Lines changed: 302 additions & 0 deletions
Large diffs are not rendered by default.

include/Core/MissingTextureBase64.hpp renamed to include/Core/MissingImageTextureBase64.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#ifndef CORE_MISSING_TEXTURE_BASE64_HPP
2-
#define CORE_MISSING_TEXTURE_BASE64_HPP
1+
#ifndef CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP
2+
#define CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP
33

44
// A transparent image base64 string.
55
// Since we want to hardcode the image, we have such a long string here.
66
// The original image should find in here: https://i.imgur.com/zS4sPCN.png
7-
static constexpr const char *MISSING_TEXTURE =
7+
static constexpr const char *MISSING_IMAGE_TEXTURE =
88
"iVBORw0KGgoAAAANSUhEUgAAAQAAAAEABAMAAACuXLVVAAAAIGNIUk0AAHomAACAhAAA+"
99
"gAAAIDoAAB1MAAA6mAAADqYAAAXcJy6UTwAAAAhUExURf8A3P8A3uEAwh4AGgAAAP8A3+"
1010
"IAwx0AGcoArjUALv///"
@@ -21,4 +21,4 @@ static constexpr const char *MISSING_TEXTURE =
2121
"IgAAACh0RVh0ZGF0ZTp0aW1lc3RhbXAAMjAyMy0xMi0yNVQxODozMjo0NCswMDowMIYEjHkAAA"
2222
"AASUVORK5CYII=";
2323

24-
#endif // CORE_MISSING_TEXTURE_BASE64_HPP
24+
#endif // CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP

include/Util/MissingTexture.hpp

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,24 @@
44
#include <SDL.h>
55
#include <SDL_image.h>
66
#include <Util/Logger.hpp>
7-
#include <iostream>
8-
#include <string>
97

108
#include "Base64.hpp"
11-
#include "Core/MissingTextureBase64.hpp"
9+
#include "Core/MissingFontTextureBase64.hpp"
10+
#include "Core/MissingImageTextureBase64.hpp"
1211

13-
static constexpr auto MISSING_TEXTURE_BASE64_DECODE_LENGTH =
14-
DecodeBase64Length(MISSING_TEXTURE);
15-
static constexpr auto MISSING_TEXTURE_BASE64_DECODE =
16-
DecodeBase64<MISSING_TEXTURE_BASE64_DECODE_LENGTH>(MISSING_TEXTURE);
12+
static constexpr auto MISSING_FONT_TEXTURE_BASE64_DECODE_LENGTH =
13+
DecodeBase64Length(MISSING_FONT_TEXTURE);
14+
static constexpr auto MISSING_FONT_TEXTURE_BASE64_DECODE =
15+
DecodeBase64<MISSING_FONT_TEXTURE_BASE64_DECODE_LENGTH>(
16+
MISSING_FONT_TEXTURE);
1717

18-
SDL_Surface *GetMissingTextureSDLSurface();
18+
static constexpr auto MISSING_IMAGE_TEXTURE_BASE64_DECODE_LENGTH =
19+
DecodeBase64Length(MISSING_IMAGE_TEXTURE);
20+
static constexpr auto MISSING_IMAGE_TEXTURE_BASE64_DECODE =
21+
DecodeBase64<MISSING_IMAGE_TEXTURE_BASE64_DECODE_LENGTH>(
22+
MISSING_IMAGE_TEXTURE);
23+
24+
SDL_Surface *GetMissingFontTextureSDLSurface();
25+
SDL_Surface *GetMissingImageTextureSDLSurface();
1926

2027
#endif

src/Util/Image.cpp

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

66
#include "Core/Texture.hpp"
77
#include "Core/TextureUtils.hpp"
8-
98
#include "Util/MissingTexture.hpp"
109
#include "Util/TransformUtils.hpp"
1110

@@ -17,7 +16,7 @@ std::shared_ptr<SDL_Surface> LoadSurface(const std::string &filepath) {
1716
SDL_FreeSurface);
1817

1918
if (surface == nullptr) {
20-
surface = {GetMissingTextureSDLSurface(), SDL_FreeSurface};
19+
surface = {GetMissingImageTextureSDLSurface(), SDL_FreeSurface};
2120
LOG_ERROR("Failed to load image: '{}'", filepath);
2221
LOG_ERROR("{}", IMG_GetError());
2322
}
@@ -40,6 +39,12 @@ Image::Image(const std::string &filepath)
4039

4140
auto surface = s_Store.Get(filepath);
4241

42+
if (surface == nullptr) {
43+
LOG_ERROR("Failed to load image: '{}'", filepath);
44+
LOG_ERROR("{}", IMG_GetError());
45+
surface = {GetMissingImageTextureSDLSurface(), SDL_FreeSurface};
46+
}
47+
4348
m_Texture = std::make_unique<Core::Texture>(
4449
Core::SdlFormatToGlFormat(surface->format->format), surface->w,
4550
surface->h, surface->pixels);

src/Util/MissingTexture.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
#include "Util/MissingTexture.hpp"
66

7-
SDL_Surface *GetMissingTextureSDLSurface() {
8-
SDL_RWops *rwop = SDL_RWFromConstMem(MISSING_TEXTURE_BASE64_DECODE.data(),
9-
MISSING_TEXTURE_BASE64_DECODE.size());
7+
namespace Util {
8+
SDL_Surface *GetMissingImageTextureSDLSurface() {
9+
SDL_RWops *rwop =
10+
SDL_RWFromConstMem(MISSING_IMAGE_TEXTURE_BASE64_DECODE.data(),
11+
MISSING_IMAGE_TEXTURE_BASE64_DECODE.size());
1012
SDL_Surface *aSurface = IMG_LoadTyped_RW(rwop, 1, "PNG");
1113

1214
if (aSurface == nullptr) {
@@ -15,3 +17,17 @@ SDL_Surface *GetMissingTextureSDLSurface() {
1517

1618
return aSurface;
1719
}
20+
21+
SDL_Surface *GetMissingFontTextureSDLSurface() {
22+
SDL_RWops *rwop =
23+
SDL_RWFromConstMem(MISSING_FONT_TEXTURE_BASE64_DECODE.data(),
24+
MISSING_FONT_TEXTURE_BASE64_DECODE.size());
25+
SDL_Surface *aSurface = IMG_LoadTyped_RW(rwop, 1, "JPG");
26+
27+
if (aSurface == nullptr) {
28+
LOG_ERROR("base64ToSurface");
29+
}
30+
31+
return aSurface;
32+
}
33+
} // namespace Util

src/Util/Text.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "Core/TextureUtils.hpp"
55

66
#include "Util/Logger.hpp"
7+
#include "Util/MissingTexture.hpp"
78
#include "Util/Text.hpp"
89
#include "Util/TransformUtils.hpp"
910

@@ -22,17 +23,21 @@ Text::Text(const std::string &font, int fontSize, const std::string &text,
2223
m_UniformBuffer = std::make_unique<Core::UniformBuffer<Core::Matrices>>(
2324
*s_Program, "Matrices", 0);
2425

26+
auto surface =
27+
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>();
2528
m_Font = {TTF_OpenFont(font.c_str(), fontSize), TTF_CloseFont};
2629

27-
auto surface =
28-
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
29-
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
30-
m_Color.ToSdlColor(), 0),
31-
SDL_FreeSurface,
32-
};
33-
if (surface == nullptr) {
34-
LOG_ERROR("Failed to create text");
30+
if (m_Font == nullptr) {
31+
LOG_ERROR("Failed to load font: '{}'", font.c_str());
3532
LOG_ERROR("{}", TTF_GetError());
33+
surface = {GetMissingFontTextureSDLSurface(), SDL_FreeSurface};
34+
} else {
35+
surface =
36+
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
37+
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
38+
m_Color.ToSdlColor(), 0),
39+
SDL_FreeSurface,
40+
};
3641
}
3742

3843
m_Texture = std::make_unique<Core::Texture>(
@@ -102,11 +107,17 @@ void Text::InitVertexArray() {
102107

103108
void Text::ApplyTexture() {
104109
auto surface =
105-
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
106-
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
107-
m_Color.ToSdlColor(), 0),
108-
SDL_FreeSurface,
109-
};
110+
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>();
111+
if (m_Font == nullptr) {
112+
surface = {GetMissingFontTextureSDLSurface(), SDL_FreeSurface};
113+
} else {
114+
surface =
115+
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
116+
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
117+
m_Color.ToSdlColor(), 0),
118+
SDL_FreeSurface,
119+
};
120+
}
110121
if (surface == nullptr) {
111122
LOG_ERROR("Failed to create text: {}", TTF_GetError());
112123
}

0 commit comments

Comments
 (0)