Skip to content
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

Font support with freetype #177

Closed
wants to merge 13 commits into from
Closed
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile.base
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ IRXEMBEDDEDEXT := irx-em
CFLAGS += -D_EE -Wall -O3
LINKFLAGS += -O3 -Wl,-zmax-page-size=128
ASFLAGS += -O3
LIB += -lstdc++ -ldma -lpacket2 -lgraph -ldraw -lmath3d -lpng -ldebug -lz -lpad -laudsrv -lpatches -lcdvd
INC += -I$(PS2DEV)/ps2sdk/ee/include -I$(PS2DEV)/ps2sdk/common/include -I$(PS2DEV)/ps2sdk/ports/include
LIB += -lstdc++ -ldma -lpacket2 -lgraph -ldraw -lmath3d -lpng -ldebug -lz -lpad -laudsrv -lpatches -lcdvd -lfreetype
INC += -I$(PS2DEV)/ps2sdk/ee/include -I$(PS2DEV)/ps2sdk/common/include -I$(PS2DEV)/ps2sdk/ports/include -I$(PS2DEV)/ps2sdk/ports/include/freetype2

#---------------------------------------------------------------------------------
#DO NOT EDIT BELOW THIS LINE
Expand Down
2 changes: 1 addition & 1 deletion demo/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ CFLAGS :=
LIB := -ltyra
LIBDIRS := -L$(ENGINEDIR)/bin
INC := -I$(INCDIR) -I$(ENGINEDIR)/inc
INCDEP := -I$(INCDIR) -I$(ENGINEDIR)/inc
INCDEP := -I$(INCDIR) -I$(ENGINEDIR)/inc -I$(PS2DEV)/ps2sdk/ports/include/freetype2

include ../Makefile.base

Expand Down
4 changes: 2 additions & 2 deletions engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ OBJEXT := o

#Flags, Libraries and Includes
CFLAGS :=
LIB :=
LIB :=
LIBDIRS :=
INC := -I$(INCDIR)
INCDEP := -I$(INCDIR)
INCDEP := -I$(INCDIR) -I$(PS2SDK)/ports/include/freetype2

include ../Makefile.base

Expand Down
2 changes: 2 additions & 0 deletions engine/inc/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#pragma once

#include "./renderer/renderer.hpp"
#include "./font/font.hpp"
#include "./pad/pad.hpp"
#include "./audio/audio.hpp"
#include "./irx/irx_loader.hpp"
Expand Down Expand Up @@ -39,6 +40,7 @@ class Engine {
Renderer renderer;
Pad pad;
Audio audio;
Font font;
Info info;

void run(Game* t_game);
Expand Down
101 changes: 101 additions & 0 deletions engine/inc/font/font.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#pragma once

#include <loaders/texture/builder/texture_builder_data.hpp>
#include <renderer/core/texture/models/texture.hpp>
#include <renderer/core/2d/sprite/sprite.hpp>
#include <renderer/renderer.hpp>
#include <vector>
#include <ft2build.h>
#include FT_FREETYPE_H

namespace Tyra {

struct Glyph {
int value = 0; // codepoint value
Vec2 advance;
Vec2 bitmap; // bearing
Sprite sprite;
};

class FontData {
public:
int id;
float sizeInMB = 0;
bool hasKerning = false;
bool hasColor = false;
std::vector<int> size;
std::vector<std::vector<Glyph>> glyph;
std::vector<std::vector<Texture*>> texture;
FT_Face face; // handle to face object
FT_Byte* data;
};

class Font {
public:
Font();
~Font();
void init(Renderer* renderer);

/**
* Changes the maximum memory size for all fonts.
* @param maxMB Allowed value between 1 and 10.
* When the MB is greater than this value, it deletes all cached textures from
* all font. By default is 2MB.
*/
void setLimitFontTexture(float maxMB);

/**
* @return Only 1 codepoint
*/
int getCodepoint(const unsigned char* text, int* bytes);

/**
* @return Only 1 codepoint
*/
int getCodepoint(const char* text, int* bytes);
/**
* Loads a range of codepoint values.
* @param codePoints Use getCodepoint() to get the value.
*/
void loadFontGlyphs(FontData* font, const int fontSize,
std::vector<int>& codePoints,
const std::string& filePath);
/**
* Load the size you require.
* Loads chars values with 32-127
*/
void loadFont(FontData* font, const int fontSize,
const std::string& filePath);

/**
* Loads the font with the size of 32 by default.
* It is recommended to put the size you require
* so that it loads faster.
* Loads chars values with 32-127
*/
void loadFont(FontData* font, const std::string& filePath);

void drawText(FontData* font, std::string text, float x, float y,
int fontSize, Color color);
void unloadFont(FontData* font);
void unloadFontTexture(FontData* font);

bool textureLimits = true;

private:
Renderer* renderer;
FT_Library library; /* handle to library */
FT_UInt defaultFontSize = 32;
FT_Error error;
int glyphNotFound = -1;
float maxSizeInMB = 2; // Max size in MB for textures fonts.
float sizeInMBused = 0;
std::vector<int> deletedIDs;
std::vector<FontData*> fontData;

int getFontSizeIndex(FontData* font, const int fontSize);
int getGlyphIndex(std::vector<Tyra::Glyph>* glyph, const int codepoint);
int getGlyphTexture(FontData* font, const int fontIndex, int* index,
const int codepoint, const int fontSize);
};
} // namespace Tyra
1 change: 1 addition & 0 deletions engine/src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void Engine::initAll(const bool& loadUsbDriver) {
banner.show(&renderer);
audio.init();
pad.init();
font.init(&renderer);
}

} // namespace Tyra
Loading
Loading