Skip to content

Commit

Permalink
Merge pull request #150 from ntut-xuan/149-Missing-Font-Texture
Browse files Browse the repository at this point in the history
Completed "Add missing texture when font is not found"
  • Loading branch information
ntut-xuan authored Jun 11, 2024
2 parents 29fcdc6 + a9749db commit d05569e
Show file tree
Hide file tree
Showing 7 changed files with 373 additions and 30 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ set(INCLUDE_FILES
${INCLUDE_DIR}/Core/Texture.hpp
${INCLUDE_DIR}/Core/TextureUtils.hpp
${INCLUDE_DIR}/Core/Drawable.hpp
${INCLUDE_DIR}/Core/MissingFontTextureBase64.hpp
${INCLUDE_DIR}/Core/MissingImageTextureBase64.hpp

${INCLUDE_DIR}/Util/LoadTextFile.hpp
${INCLUDE_DIR}/Util/Logger.hpp
Expand Down
302 changes: 302 additions & 0 deletions include/Core/MissingFontTextureBase64.hpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#ifndef CORE_MISSING_TEXTURE_BASE64_HPP
#define CORE_MISSING_TEXTURE_BASE64_HPP
#ifndef CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP
#define CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP

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

#endif // CORE_MISSING_TEXTURE_BASE64_HPP
#endif // CORE_MISSING_IMAGE_TEXTURE_BASE64_HPP
23 changes: 15 additions & 8 deletions include/Util/MissingTexture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@
#include <SDL.h>
#include <SDL_image.h>
#include <Util/Logger.hpp>
#include <iostream>
#include <string>

#include "Base64.hpp"
#include "Core/MissingTextureBase64.hpp"
#include "Core/MissingFontTextureBase64.hpp"
#include "Core/MissingImageTextureBase64.hpp"

static constexpr auto MISSING_TEXTURE_BASE64_DECODE_LENGTH =
DecodeBase64Length(MISSING_TEXTURE);
static constexpr auto MISSING_TEXTURE_BASE64_DECODE =
DecodeBase64<MISSING_TEXTURE_BASE64_DECODE_LENGTH>(MISSING_TEXTURE);
static constexpr auto MISSING_FONT_TEXTURE_BASE64_DECODE_LENGTH =
DecodeBase64Length(MISSING_FONT_TEXTURE);
static constexpr auto MISSING_FONT_TEXTURE_BASE64_DECODE =
DecodeBase64<MISSING_FONT_TEXTURE_BASE64_DECODE_LENGTH>(
MISSING_FONT_TEXTURE);

SDL_Surface *GetMissingTextureSDLSurface();
static constexpr auto MISSING_IMAGE_TEXTURE_BASE64_DECODE_LENGTH =
DecodeBase64Length(MISSING_IMAGE_TEXTURE);
static constexpr auto MISSING_IMAGE_TEXTURE_BASE64_DECODE =
DecodeBase64<MISSING_IMAGE_TEXTURE_BASE64_DECODE_LENGTH>(
MISSING_IMAGE_TEXTURE);

SDL_Surface *GetMissingFontTextureSDLSurface();
SDL_Surface *GetMissingImageTextureSDLSurface();

#endif
9 changes: 7 additions & 2 deletions src/Util/Image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "Core/Texture.hpp"
#include "Core/TextureUtils.hpp"

#include "Util/MissingTexture.hpp"
#include "Util/TransformUtils.hpp"

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

if (surface == nullptr) {
surface = {GetMissingTextureSDLSurface(), SDL_FreeSurface};
surface = {GetMissingImageTextureSDLSurface(), SDL_FreeSurface};
LOG_ERROR("Failed to load image: '{}'", filepath);
LOG_ERROR("{}", IMG_GetError());
}
Expand All @@ -40,6 +39,12 @@ Image::Image(const std::string &filepath)

auto surface = s_Store.Get(filepath);

if (surface == nullptr) {
LOG_ERROR("Failed to load image: '{}'", filepath);
LOG_ERROR("{}", IMG_GetError());
surface = {GetMissingImageTextureSDLSurface(), SDL_FreeSurface};
}

m_Texture = std::make_unique<Core::Texture>(
Core::SdlFormatToGlFormat(surface->format->format), surface->w,
surface->h, surface->pixels);
Expand Down
22 changes: 19 additions & 3 deletions src/Util/MissingTexture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@

#include "Util/MissingTexture.hpp"

SDL_Surface *GetMissingTextureSDLSurface() {
SDL_RWops *rwop = SDL_RWFromConstMem(MISSING_TEXTURE_BASE64_DECODE.data(),
MISSING_TEXTURE_BASE64_DECODE.size());
namespace Util {
SDL_Surface *GetMissingImageTextureSDLSurface() {
SDL_RWops *rwop =
SDL_RWFromConstMem(MISSING_IMAGE_TEXTURE_BASE64_DECODE.data(),
MISSING_IMAGE_TEXTURE_BASE64_DECODE.size());
SDL_Surface *aSurface = IMG_LoadTyped_RW(rwop, 1, "PNG");

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

return aSurface;
}

SDL_Surface *GetMissingFontTextureSDLSurface() {
SDL_RWops *rwop =
SDL_RWFromConstMem(MISSING_FONT_TEXTURE_BASE64_DECODE.data(),
MISSING_FONT_TEXTURE_BASE64_DECODE.size());
SDL_Surface *aSurface = IMG_LoadTyped_RW(rwop, 1, "JPG");

if (aSurface == nullptr) {
LOG_ERROR("base64ToSurface");
}

return aSurface;
}
} // namespace Util
37 changes: 24 additions & 13 deletions src/Util/Text.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "Core/TextureUtils.hpp"

#include "Util/Logger.hpp"
#include "Util/MissingTexture.hpp"
#include "Util/Text.hpp"
#include "Util/TransformUtils.hpp"

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

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

auto surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
SDL_FreeSurface,
};
if (surface == nullptr) {
LOG_ERROR("Failed to create text");
if (m_Font == nullptr) {
LOG_ERROR("Failed to load font: '{}'", font.c_str());
LOG_ERROR("{}", TTF_GetError());
surface = {GetMissingFontTextureSDLSurface(), SDL_FreeSurface};
} else {
surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
SDL_FreeSurface,
};
}

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

void Text::ApplyTexture() {
auto surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
SDL_FreeSurface,
};
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>();
if (m_Font == nullptr) {
surface = {GetMissingFontTextureSDLSurface(), SDL_FreeSurface};
} else {
surface =
std::unique_ptr<SDL_Surface, std::function<void(SDL_Surface *)>>{
TTF_RenderUTF8_Blended_Wrapped(m_Font.get(), m_Text.c_str(),
m_Color.ToSdlColor(), 0),
SDL_FreeSurface,
};
}
if (surface == nullptr) {
LOG_ERROR("Failed to create text: {}", TTF_GetError());
}
Expand Down

0 comments on commit d05569e

Please sign in to comment.