Skip to content

New CryFont #86

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
16 changes: 15 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ endif()

################################################################################

set(FT_DISABLE_ZLIB ON CACHE BOOL "Disable zlib")
set(FT_DISABLE_BZIP2 TRUE CACHE BOOL "Disable bzip2")
set(FT_DISABLE_PNG TRUE CACHE BOOL "Disable libpng")
set(FT_DISABLE_HARFBUZZ TRUE CACHE BOOL "Disable HarfBuzz")
set(FT_DISABLE_BROTLI TRUE CACHE BOOL "Disable Brotli")

add_subdirectory(ThirdParty/FreeType)

################################################################################

add_executable(${CRYMP_CLIENT_EXE} WIN32
Code/Cry3DEngine/TimeOfDay.cpp
Code/Cry3DEngine/TimeOfDay.h
Expand Down Expand Up @@ -210,6 +220,10 @@ add_executable(${CRYMP_CLIENT_EXE} WIN32
Code/CryCommon/CrySystem/IValidator.h
Code/CryCommon/CrySystem/IXml.h
Code/CryCommon/CrySystem/TimeValue.h
Code/CryFont/CryFont.cpp
Code/CryFont/CryFont.h
Code/CryFont/Font.cpp
Code/CryFont/Font.h
Code/CryGame/Actors/Actor.cpp
Code/CryGame/Actors/Actor.h
Code/CryGame/Actors/Alien/Alien.cpp
Expand Down Expand Up @@ -758,7 +772,7 @@ target_compile_options(${CRYMP_CLIENT_EXE} PRIVATE /W3)
target_include_directories(${CRYMP_CLIENT_EXE} PRIVATE Code ${PROJECT_BINARY_DIR})
target_include_directories(${CRYMP_CLIENT_EXE} SYSTEM PRIVATE ThirdParty ThirdParty/Lua/src ThirdParty/miniz)

target_link_libraries(${CRYMP_CLIENT_EXE} PRIVATE dbghelp ws2_32 winhttp)
target_link_libraries(${CRYMP_CLIENT_EXE} PRIVATE dbghelp ws2_32 winhttp freetype)

# prevent modern MSVC from enabling ASLR and unlock memory above 2 GB
target_link_options(${CRYMP_CLIENT_EXE} PRIVATE /DYNAMICBASE:NO /LARGEADDRESSAWARE)
Expand Down
81 changes: 23 additions & 58 deletions Code/CryCommon/CryFont/IFont.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,10 @@
//
//////////////////////////////////////////////////////////////////////

#ifndef CRYFONT_ICRYFONT_H
#define CRYFONT_ICRYFONT_H
#pragma once

#include "CryCommon/CryMath/Cry_Color.h"
#include "CryCommon/CryMath/Cry_Math.h"

#ifdef GetCharWidth
#undef GetCharWidth
#undef GetUserName
#endif

struct ISystem;

//////////////////////////////////////////////////////////////////////////////////////////////
// THE Only exported function of the DLL

// export for the dll, very clear ;=)
extern "C"
#ifdef CRYFONT_EXPORTS
DLL_EXPORT
#else
DLL_IMPORT
#endif
struct ICryFont* CreateCryFontInterface(ISystem *pSystem);

typedef ICryFont *(*PFNCREATECRYFONTINTERFACE)(ISystem *pSystem);

//////////////////////////////////////////////////////////////////////////////////////////////
// Rendering interfaces
enum CRYFONT_RENDERINGINTERFACE
{
CRYFONT_RI_OPENGL = 0, // pRIData is ignored
CRYFONT_RI_LAST
};
#include "CryCommon/CryMath/Cry_Color.h"

//////////////////////////////////////////////////////////////////////////////////////////////
struct ICryFont
Expand All @@ -63,30 +33,28 @@ struct ICryFont
};

//////////////////////////////////////////////////////////////////////////////////////////////
#define TTFFLAG_SMOOTH_NONE 0x00000000 // no smooth
#define TTFFLAG_SMOOTH_BLUR 0x00000001 // smooth by bluring it
#define TTFFLAG_SMOOTH_SUPERSAMPLE 0x00000002 // smooth by rendering the characters into a bigger texture,
// and then resize it to the normal size using bilinear filtering
#define TTFFLAG_SMOOTH_NONE 0x00000000 // no smooth
#define TTFFLAG_SMOOTH_BLUR 0x00000001 // smooth by bluring it
#define TTFFLAG_SMOOTH_SUPERSAMPLE 0x00000002 // smooth by rendering the characters into a bigger texture,
// and then resize it to the normal size using bilinear filtering

#define TTFFLAG_SMOOTH_MASK 0x0000000f // mask for retrieving
#define TTFFLAG_SMOOTH_SHIFT 0 // shift amount for retrieving
#define TTFFLAG_SMOOTH_MASK 0x0000000f // mask for retrieving
#define TTFFLAG_SMOOTH_SHIFT 0 // shift amount for retrieving

#define TTFLAG_SMOOTH_AMOUNT_2X 0x00010000 // blur / supersample [2x]
#define TTFLAG_SMOOTH_AMOUNT_4X 0x00020000 // blur / supersample [4x]

#define TTFFLAG_SMOOTH_AMOUNT_MASK 0x000f0000 // mask for retrieving
#define TTFFLAG_SMOOTH_AMOUNT_SHIFT 16 // shift amount for retrieving
#define TTFLAG_SMOOTH_AMOUNT_2X 0x00010000 // blur / supersample [2x]
#define TTFLAG_SMOOTH_AMOUNT_4X 0x00020000 // blur / supersample [4x]

#define TTFFLAG_SMOOTH_AMOUNT_MASK 0x000f0000 // mask for retrieving
#define TTFFLAG_SMOOTH_AMOUNT_SHIFT 16 // shift amount for retrieving

// create a ttflag
#define TTFFLAG_CREATE(smooth, amount) ((((smooth) << TTFFLAG_SMOOTH_SHIFT) & TTFFLAG_SMOOTH_MASK) | (((amount) << TTFFLAG_SMOOTH_AMOUNT_SHIFT) & TTFFLAG_SMOOTH_AMOUNT_MASK))
#define TTFFLAG_GET_SMOOTH(flag) (((flag) & TTFLAG_SMOOTH_MASK) >> TTFFLAG_SMOOTH_SHIFT)
#define TTFFLAG_GET_SMOOTH_AMOUNT(flag) (((flag) & TTFLAG_SMOOTH_SMOUNT_MASK) >> TTFFLAG_SMOOTH_AMOUNT_SHIFT)

#define TTFFLAG_CREATE(smooth, amount) ((((smooth) << TTFFLAG_SMOOTH_SHIFT) & TTFFLAG_SMOOTH_MASK) | (((amount) << TTFFLAG_SMOOTH_AMOUNT_SHIFT) & TTFFLAG_SMOOTH_AMOUNT_MASK))
#define TTFFLAG_GET_SMOOTH(flag) (((flag) & TTFLAG_SMOOTH_MASK) >> TTFFLAG_SMOOTH_SHIFT)
#define TTFFLAG_GET_SMOOTH_AMOUNT(flag) (((flag) & TTFLAG_SMOOTH_SMOUNT_MASK) >> TTFFLAG_SMOOTH_AMOUNT_SHIFT)

#define FONTRF_HCENTERED 0x80000000 // The font will be centered horizontaly around the x coo
#define FONTRF_VCENTERED 0x40000000 // The font will be centered verticaly around the y coo
#define FONTRF_FILTERED 0x20000000 // The font will be drawn with bilinear filtering
#define FONTRF_HCENTERED 0x80000000 // The font will be centered horizontaly around the x coo
#define FONTRF_VCENTERED 0x40000000 // The font will be centered verticaly around the y coo
#define FONTRF_FILTERED 0x20000000 // The font will be drawn with bilinear filtering

//////////////////////////////////////////////////////////////////////////////////////////////
struct IFFont
Expand Down Expand Up @@ -120,10 +88,10 @@ struct IFFont
virtual bool UsingRealPixels()=0;

//! Set the characters base size
virtual void SetSize(const vector2f &size) = 0;
virtual void SetSize(const Vec2& size) = 0;

//! Return the seted size
virtual vector2f &GetSize() = 0;
virtual Vec2& GetSize() = 0;

//! Return the char width
virtual float GetCharWidth() = 0;
Expand Down Expand Up @@ -153,7 +121,7 @@ struct IFFont

//! Compute the text size
//! \param bASCIIMultiLine true='\','n' is a valid return, false=it's not
virtual vector2f GetTextSize(const char *szMsg, const bool bASCIIMultiLine=true ) = 0;
virtual Vec2 GetTextSize(const char *szMsg, const bool bASCIIMultiLine=true ) = 0;

//! Draw a formated string
//! \param bASCIIMultiLine true='\','n' is a valid return, false=it's not
Expand All @@ -168,10 +136,10 @@ struct IFFont

//! Compute the text size
//! \param bASCIIMultiLine true='\','n' is a valid return, false=it's not
virtual vector2f GetTextSizeW(const wchar_t *swStr, const bool bASCIIMultiLine=true ) = 0;
virtual Vec2 GetTextSizeW(const wchar_t *swStr, const bool bASCIIMultiLine=true ) = 0;

// Compute the text size
virtual vector2f GetWrappedTextSizeW(const wchar_t *swStr, float w, const bool bASCIIMultiLine=true ) = 0;
virtual Vec2 GetWrappedTextSizeW(const wchar_t *swStr, float w, const bool bASCIIMultiLine=true ) = 0;

//! Compute virtual text-length (because of special chars...)
virtual int GetTextLength(const char *szMsg, const bool bASCIIMultiLine=true) = 0;
Expand All @@ -182,6 +150,3 @@ struct IFFont
//! Puts the memory used by this font into the given sizer
virtual void GetMemoryUsage (class ICrySizer* pSizer) = 0;
};


#endif // CRYFONT_ICRYFONT_H
60 changes: 60 additions & 0 deletions Code/CryFont/CryFont.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <algorithm>

#include "CrySystem/CryLog.h"

#include "CryFont.h"
#include "Font.h"

CryFont::CryFont()
{
FT_Error error = FT_Init_FreeType(&m_freetype);
if (error)
{
CryLogErrorAlways("[CryFont] FreeType initialization failed with error %d", error);
}
}

CryFont::~CryFont()
{
FT_Done_FreeType(m_freetype);
}

void CryFont::Release()
{
}

IFFont* CryFont::NewFont(const char* name)
{
if (const auto it = std::ranges::find(m_fonts, name, [](auto& f) { return f->name; }); it != m_fonts.end())
{
return it->get();
}
else
{
auto& font = m_fonts.emplace_back(std::make_unique<Font>());
font->name = name;
font->freetype = m_freetype;

CryLogAlways("[CryFont] Created font %s", name);

return font.get();
}
}

IFFont* CryFont::GetFont(const char* name)
{
if (const auto it = std::ranges::find(m_fonts, name, [](auto& f) { return f->name; }); it != m_fonts.end())
{
return it->get();
}
else
{
CryLogWarningAlways("[CryFont] Cannot find font %s", name);

return nullptr;
}
}

void CryFont::GetMemoryUsage(ICrySizer*)
{
}
35 changes: 35 additions & 0 deletions Code/CryFont/CryFont.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#pragma once

#include <memory>
#include <vector>

#include <ft2build.h>
#include FT_FREETYPE_H

#include "CryCommon/CryFont/IFont.h"

struct Font;

class CryFont final : public ICryFont
{
FT_Library m_freetype = nullptr;

std::vector<std::unique_ptr<Font>> m_fonts;

public:
CryFont();
~CryFont();

////////////////////////////////////////////////////////////////////////////////
// ICryFont
////////////////////////////////////////////////////////////////////////////////

void Release() override;

IFFont* NewFont(const char* name) override;
IFFont* GetFont(const char* name) override;

void GetMemoryUsage(ICrySizer*) override;

////////////////////////////////////////////////////////////////////////////////
};
Loading